Skip to content

Commit

Permalink
WasmThemis utilities (#489)
Browse files Browse the repository at this point in the history
* Correctly report error codes

Classical off-by-one mistake. We incorrectly reported error codes as
'undefined' when doing "new ThemisError(subsystem, errorCode)". This
affects only human-readable description, not the 'errorCode' property,
but it's very annoying for developers.

Use ThemisError.describe only when there are 3 (three) or more arguments
with human-readable error causes.

* Move 'coerceToBytes' into utility module

This function is going to be useful in multiple places for type checking
of user byte arrays. Let's move it into a common utility module. While
we're here, let it throw 'TypeError' instead of 'ThemisError' which
should be more recognizable (and does not expect Themis "subsystem").
  • Loading branch information
ilammy committed Jul 5, 2019
1 parent 2d86ea4 commit 019d5e0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
16 changes: 3 additions & 13 deletions src/wrappers/themis/wasm/src/secure_keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

const libthemis = require('./libthemis.js')
const errors = require('./themis_error.js')
const utils = require('./utils.js')

const subsystem = 'KeyPair'

Expand All @@ -40,31 +41,20 @@ const keyKinds = {

class PrivateKey extends Uint8Array {
constructor(array) {
array = coerceToBytes(array)
array = utils.coerceToBytes(array)
super(array)
validateKeyBuffer(this, [keyKinds.EC_PRIVATE, keyKinds.RSA_PRIVATE])
}
}

class PublicKey extends Uint8Array {
constructor(array) {
array = coerceToBytes(array)
array = utils.coerceToBytes(array)
super(array)
validateKeyBuffer(this, [keyKinds.EC_PUBLIC, keyKinds.RSA_PUBLIC])
}
}

function coerceToBytes(key) {
if (key instanceof Uint8Array) {
return key
}
if (key instanceof ArrayBuffer) {
return new Uint8Array(key)
}
throw new ThemisError(subsystem, ThemisErrorCode.INVALID_PARAMETER,
'key type mismatch, expect "Uint8Array" or "ArrayBuffer"')
}

function validateKeyBuffer(buffer, expectedKinds) {
if (buffer.length == 0) {
throw new ThemisError(subsystem, ThemisErrorCode.INVALID_PARAMETER,
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/themis/wasm/src/themis_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const errorCodes = {
class ThemisError extends Error {
constructor(subsystem, errorCode) {
var message = 'Themis: ' + subsystem + ': '
if (arguments.length >= 2) {
if (arguments.length > 2) {
message += arguments[2]
for (var i = 3; i < arguments.length; i++) {
message += ', '
Expand Down
33 changes: 33 additions & 0 deletions src/wrappers/themis/wasm/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2019 Cossack Labs Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @file
* Miscellaneous utilities.
*/

/**
* Convert an object into a byte buffer.
*
* @throws TypeError if input type is not supported
*/
module.exports.coerceToBytes = function(buffer) {
if (buffer instanceof Uint8Array) {
return buffer
}
if (buffer instanceof ArrayBuffer) {
return new Uint8Array(buffer)
}
throw new TypeError('type mismatch, expect "Uint8Array" or "ArrayBuffer"')
}

0 comments on commit 019d5e0

Please sign in to comment.