|
| 1 | +/* |
| 2 | + * Copyright 2020-2022 Hyperledger Cactus Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import fs from "fs"; |
| 7 | +import jwt from "jsonwebtoken"; |
| 8 | +import crypto from "crypto"; |
| 9 | +import { configRead } from "../util/config"; |
| 10 | + |
| 11 | +type PayloadType = Parameters<typeof jwt.sign>[0]; |
| 12 | + |
| 13 | +const DEFAULT_EXPIRATION_TIME = 60 * 15; // 15 minutes |
| 14 | + |
| 15 | +const supportedJwtAlgos: jwt.Algorithm[] = [ |
| 16 | + "ES256", |
| 17 | + "ES384", |
| 18 | + "ES512", |
| 19 | + "RS256", |
| 20 | + "RS384", |
| 21 | + "RS512", |
| 22 | +]; |
| 23 | + |
| 24 | +// Will keep the private key once it's succesfully read |
| 25 | +let privateKey: string; |
| 26 | + |
| 27 | +/** |
| 28 | + * Sign a message to be sent from socketio connector (validator) to a client. |
| 29 | + * |
| 30 | + * @param privateKey - Validator private key. Only ECDSA and RSA keys are supported. |
| 31 | + * @param payload - Message to be encoded. |
| 32 | + * @param jwtAlgo - JWT algorithm to use. Must match key used (ES*** or RS***) |
| 33 | + * @param expirationTime - JWT expiration time |
| 34 | + * @returns JWT signed message that can be sent over the wire. |
| 35 | + */ |
| 36 | + export function signValidatorMessageJwt( |
| 37 | + privateKey: jwt.Secret, |
| 38 | + payload: PayloadType, |
| 39 | + jwtAlgo: jwt.Algorithm = "ES256", |
| 40 | + expirationTime: number = DEFAULT_EXPIRATION_TIME, |
| 41 | +): string { |
| 42 | + if (!supportedJwtAlgos.includes(jwtAlgo)) { |
| 43 | + throw new Error( |
| 44 | + `Wrong JWT Algorithm. Supported algos: ${supportedJwtAlgos.toString()}`, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + // Check if key supported and JWT algorithm matches the provided key type |
| 49 | + const keyType = crypto.createPrivateKey(privateKey).asymmetricKeyType; |
| 50 | + if ( |
| 51 | + !( |
| 52 | + (keyType === "rsa" && jwtAlgo.startsWith("RS")) || |
| 53 | + (keyType === "ec" && jwtAlgo.startsWith("ES")) |
| 54 | + ) |
| 55 | + ) { |
| 56 | + throw new Error(`Not supported combination ${keyType}/${jwtAlgo}.`); |
| 57 | + } |
| 58 | + |
| 59 | + const option: jwt.SignOptions = { |
| 60 | + algorithm: jwtAlgo, |
| 61 | + expiresIn: expirationTime, |
| 62 | + }; |
| 63 | + |
| 64 | + return jwt.sign(payload, privateKey, option); |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Validator-side function to sign message to be sent to the client. |
| 69 | + * Will read the private key either as value in validator config `sslParam.keyValue`, |
| 70 | + * or read from filesystem under path `sslParam.key`. |
| 71 | + * |
| 72 | + * @param payload - Message to sign |
| 73 | + * @returns Signed message |
| 74 | + */ |
| 75 | +export function signMessageJwt(payload: object): string { |
| 76 | + if (!privateKey) { |
| 77 | + try { |
| 78 | + privateKey = configRead<string>('sslParam.keyValue'); |
| 79 | + } catch { |
| 80 | + privateKey = fs.readFileSync(configRead('sslParam.key'), "ascii"); |
| 81 | + } |
| 82 | + } |
| 83 | + const jwtAlgo = configRead<jwt.Algorithm>('sslParam.jwtAlgo', 'ES256'); |
| 84 | + return signValidatorMessageJwt(privateKey, payload, jwtAlgo); |
| 85 | +} |
0 commit comments