@confluentinc/schemaregistry
Version:
Node.js client for Confluent Schema Registry
77 lines (76 loc) • 2.72 kB
JavaScript
;
/**
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateAesKeySize = validateAesKeySize;
exports.requireUint8Array = requireUint8Array;
exports.validateVersion = validateVersion;
exports.validateEcdsaParams = validateEcdsaParams;
const invalid_arguments_exception_1 = require("./exception/invalid_arguments_exception");
const security_exception_1 = require("./exception/security_exception");
const SUPPORTED_AES_KEY_SIZES = [16, 32];
/**
* Validates AES key sizes, at the moment only 128-bit and 256-bit keys are
* supported.
*
* @param n - the key size in bytes
* @throws {@link InvalidArgumentsException}
*/
function validateAesKeySize(n) {
if (!SUPPORTED_AES_KEY_SIZES.includes(n)) {
throw new invalid_arguments_exception_1.InvalidArgumentsException('unsupported AES key size: ' + n);
}
}
/**
* Validates that the input is a non null Uint8Array.
*
* @throws {@link InvalidArgumentsException}
*/
function requireUint8Array(input) {
if (input == null || !(input instanceof Uint8Array)) {
throw new invalid_arguments_exception_1.InvalidArgumentsException('input must be a non null Uint8Array');
}
}
/**
* Validates version, throws exception if candidate version is negative or
* bigger than expected.
*
* @param candidate - version to be validated
* @param maxVersion - upper bound on version
* @throws {@link SecurityException}
*/
function validateVersion(candidate, maxVersion) {
if (candidate < 0 || candidate > maxVersion) {
throw new security_exception_1.SecurityException('Version is out of bound, must be ' +
'between 0 and ' + maxVersion + '.');
}
}
/**
* Validates ECDSA parameters.
*
* @throws {@link SecurityException}
*/
function validateEcdsaParams(curve, hash) {
switch (curve) {
case 'P-256':
if (hash != 'SHA-256') {
throw new security_exception_1.SecurityException('expected SHA-256 (because curve is P-256) but got ' + hash);
}
break;
case 'P-384':
if (hash != 'SHA-384' && hash != 'SHA-512') {
throw new security_exception_1.SecurityException('expected SHA-384 or SHA-512 (because curve is P-384) but got ' +
hash);
}
break;
case 'P-521':
if (hash != 'SHA-512') {
throw new security_exception_1.SecurityException('expected SHA-512 (because curve is P-521) but got ' + hash);
}
break;
default:
throw new security_exception_1.SecurityException('unsupported curve: ' + curve);
}
}