@sphereon/ssi-sdk-ext.x509-utils
Version:
Sphereon SSI-SDK plugin functions for X.509 Certificate handling.
105 lines • 4.84 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RSASigner = void 0;
const u8a = __importStar(require("uint8arrays"));
const crypto_1 = require("./crypto");
const rsa_key_1 = require("./rsa-key");
const x509_utils_1 = require("./x509-utils");
class RSASigner {
/**
*
* @param key Either in PEM or JWK format (no raw hex keys here!)
* @param opts The algorithm and signature/encryption schemes
*/
constructor(key, opts) {
var _a, _b;
if (typeof key === 'string') {
this.jwk = (0, x509_utils_1.PEMToJwk)(key, opts === null || opts === void 0 ? void 0 : opts.visibility);
}
else {
this.jwk = key;
}
this.hashAlgorithm = (_a = opts === null || opts === void 0 ? void 0 : opts.hashAlgorithm) !== null && _a !== void 0 ? _a : 'SHA-256';
this.scheme = (_b = opts === null || opts === void 0 ? void 0 : opts.scheme) !== null && _b !== void 0 ? _b : 'RSA-PSS';
}
getImportParams() {
if (this.scheme === 'RSA-PSS') {
return { name: this.scheme, saltLength: 32 };
}
return { name: this.scheme /*, hash: this.hashAlgorithm*/ };
}
getKey() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.key) {
this.key = yield (0, rsa_key_1.cryptoSubtleImportRSAKey)(this.jwk, this.scheme, this.hashAlgorithm);
}
return this.key;
});
}
bufferToString(buf) {
const uint8Array = new Uint8Array(buf);
return u8a.toString(uint8Array, 'base64url'); // Needs to be base64url for JsonWebSignature2020. Don't change!
}
sign(data) {
return __awaiter(this, void 0, void 0, function* () {
const input = data;
const key = yield this.getKey();
const signature = this.bufferToString(yield (0, crypto_1.globalCrypto)(false).subtle.sign(this.getImportParams(), key, input));
if (!signature) {
throw Error('Could not sign input data');
}
// base64url signature
return signature;
});
}
verify(data, signature) {
return __awaiter(this, void 0, void 0, function* () {
const jws = signature.includes('.') ? signature.split('.')[2] : signature;
const input = typeof data == 'string' ? u8a.fromString(data, 'utf-8') : data;
let key = yield this.getKey();
if (!key.usages.includes('verify')) {
const verifyJwk = Object.assign({}, this.jwk);
delete verifyJwk.d;
delete verifyJwk.use;
delete verifyJwk.key_ops;
key = yield (0, rsa_key_1.cryptoSubtleImportRSAKey)(verifyJwk, this.scheme, this.hashAlgorithm);
}
const verificationResult = yield (0, crypto_1.globalCrypto)(false).subtle.verify(this.getImportParams(), key, u8a.fromString(jws, 'base64url'), input);
return verificationResult;
});
}
}
exports.RSASigner = RSASigner;
//# sourceMappingURL=rsa-signer.js.map