@friggframework/core 2.0.0--canary.744.e18e27f.0 → 2.0.0--canary.745.0209482d.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +13 -6
- package/README.md +4 -1
- package/application/commands/encryption-commands.js +135 -0
- package/database/encryption/README.md +158 -13
- package/database/encryption/ciphertext-format.js +11 -16
- package/database/encryption/documentdb-encryption-service.md +14 -15
- package/database/repositories/encrypted-field-store-factory.js +27 -0
- package/database/repositories/encrypted-field-store-interface.js +53 -0
- package/database/repositories/encrypted-field-store-mongo.js +69 -0
- package/database/repositories/encrypted-field-store-postgres.js +119 -0
- package/database/use-cases/reencrypt-legacy-fields-use-case.js +114 -0
- package/encrypt/Cryptor.js +302 -54
- package/encrypt/aes.js +111 -10
- package/encrypt/envelope.js +200 -0
- package/encrypt/key-material.js +73 -0
- package/handlers/routers/HEALTHCHECK.md +30 -0
- package/handlers/routers/health.js +22 -0
- package/handlers/use-cases/check-transport-security-health-use-case.js +137 -0
- package/integrations/repositories/integration-repository-mongo.js +15 -34
- package/modules/requester/oauth-2.js +0 -56
- package/modules/requester/requester.js +0 -125
- package/package.json +6 -6
- package/types/encrypt/index.d.ts +9 -0
- package/types/module-plugin/index.d.ts +0 -13
package/encrypt/aes.js
CHANGED
|
@@ -1,27 +1,128 @@
|
|
|
1
1
|
const crypto = require('crypto');
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* AES primitives for Cryptor.
|
|
5
|
+
*
|
|
6
|
+
* - AES-256-GCM (envelope v2): authenticated. Every new value uses it.
|
|
7
|
+
* - AES-256-CTR (envelope v1): NOT authenticated. Kept only to read values
|
|
8
|
+
* written before v2; Cryptor never writes it.
|
|
9
|
+
*/
|
|
4
10
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
const GCM = 'aes-256-gcm';
|
|
12
|
+
const GCM_IV_BYTES = 12;
|
|
13
|
+
const GCM_TAG_BYTES = 16;
|
|
14
|
+
const CTR = 'aes-256-ctr';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {Buffer|string} plaintext strings are UTF-8
|
|
18
|
+
* @param {Buffer} key 32 bytes
|
|
19
|
+
* @param {Buffer} [aad] associated data to authenticate (not encrypted)
|
|
20
|
+
* @returns {{ iv: Buffer, ciphertext: Buffer, tag: Buffer }}
|
|
21
|
+
*/
|
|
22
|
+
function encryptGcm(plaintext, key, aad) {
|
|
23
|
+
const iv = crypto.randomBytes(GCM_IV_BYTES);
|
|
24
|
+
const cipher = crypto.createCipheriv(GCM, key, iv, {
|
|
25
|
+
authTagLength: GCM_TAG_BYTES,
|
|
26
|
+
});
|
|
27
|
+
if (aad) {
|
|
28
|
+
cipher.setAAD(aad);
|
|
29
|
+
}
|
|
30
|
+
const ciphertext = Buffer.concat([
|
|
31
|
+
cipher.update(
|
|
32
|
+
Buffer.isBuffer(plaintext)
|
|
33
|
+
? plaintext
|
|
34
|
+
: Buffer.from(plaintext, 'utf8')
|
|
35
|
+
),
|
|
36
|
+
cipher.final(),
|
|
37
|
+
]);
|
|
38
|
+
return { iv, ciphertext, tag: cipher.getAuthTag() };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {{ iv: Buffer, ciphertext: Buffer, tag: Buffer }} sealed
|
|
43
|
+
* @param {Buffer} key 32 bytes
|
|
44
|
+
* @param {Buffer} [aad] the associated data given to encryptGcm
|
|
45
|
+
* @returns {Buffer} plaintext
|
|
46
|
+
* @throws {Error} when the tag does not match: the ciphertext, IV, tag or
|
|
47
|
+
* associated data were changed, or the key is wrong.
|
|
48
|
+
*/
|
|
49
|
+
function decryptGcm({ iv, ciphertext, tag }, key, aad) {
|
|
50
|
+
// authTagLength makes setAuthTag refuse a truncated tag.
|
|
51
|
+
const decipher = crypto.createDecipheriv(GCM, key, iv, {
|
|
52
|
+
authTagLength: GCM_TAG_BYTES,
|
|
53
|
+
});
|
|
54
|
+
if (aad) {
|
|
55
|
+
decipher.setAAD(aad);
|
|
56
|
+
}
|
|
57
|
+
decipher.setAuthTag(tag);
|
|
58
|
+
try {
|
|
59
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
'Ciphertext failed authentication: it was changed or the key is wrong',
|
|
63
|
+
{ cause: error }
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
8
67
|
|
|
9
|
-
|
|
68
|
+
/** AES-256-GCM over a data key: iv || ciphertext || tag, as one buffer. */
|
|
69
|
+
function wrapKey(dataKey, masterKey, aad) {
|
|
70
|
+
const { iv, ciphertext, tag } = encryptGcm(dataKey, masterKey, aad);
|
|
71
|
+
return Buffer.concat([iv, ciphertext, tag]);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function unwrapKey(wrapped, masterKey, aad) {
|
|
75
|
+
if (wrapped.length <= GCM_IV_BYTES + GCM_TAG_BYTES) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
'Ciphertext failed authentication: the wrapped data key is too short'
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return decryptGcm(
|
|
81
|
+
{
|
|
82
|
+
iv: wrapped.subarray(0, GCM_IV_BYTES),
|
|
83
|
+
ciphertext: wrapped.subarray(
|
|
84
|
+
GCM_IV_BYTES,
|
|
85
|
+
wrapped.length - GCM_TAG_BYTES
|
|
86
|
+
),
|
|
87
|
+
tag: wrapped.subarray(wrapped.length - GCM_TAG_BYTES),
|
|
88
|
+
},
|
|
89
|
+
masterKey,
|
|
90
|
+
aad
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Legacy aes-256-ctr encryption: "ivHex:ciphertextHex". No integrity
|
|
96
|
+
* protection.
|
|
97
|
+
* @deprecated Only for tests that build v1 fixtures. Use encryptGcm.
|
|
98
|
+
*/
|
|
99
|
+
function encryptCtr(text, key) {
|
|
100
|
+
const iv = crypto.randomBytes(16);
|
|
101
|
+
const cipher = crypto.createCipheriv(CTR, key, iv);
|
|
10
102
|
let crypted = cipher.update(text, 'utf8', 'hex');
|
|
11
103
|
crypted += cipher.final('hex');
|
|
12
|
-
return `${
|
|
104
|
+
return `${iv.toString('hex')}:${crypted}`;
|
|
13
105
|
}
|
|
14
106
|
|
|
15
|
-
|
|
107
|
+
/** Reads a v1 "ivHex:ciphertextHex" value. */
|
|
108
|
+
function decryptCtr(text, key) {
|
|
16
109
|
const parts = text.toString().split(':');
|
|
17
110
|
const iv = Buffer.from(parts[0], 'hex');
|
|
18
|
-
const decipher = crypto.createDecipheriv(
|
|
111
|
+
const decipher = crypto.createDecipheriv(CTR, key, iv);
|
|
19
112
|
let dec = decipher.update(parts[1], 'hex', 'utf8');
|
|
20
113
|
dec += decipher.final('utf8');
|
|
21
114
|
return dec;
|
|
22
115
|
}
|
|
23
116
|
|
|
24
117
|
module.exports = {
|
|
25
|
-
|
|
26
|
-
|
|
118
|
+
encryptGcm,
|
|
119
|
+
decryptGcm,
|
|
120
|
+
wrapKey,
|
|
121
|
+
unwrapKey,
|
|
122
|
+
encryptCtr,
|
|
123
|
+
decryptCtr,
|
|
124
|
+
/** @deprecated aes-256-ctr without authentication; use encryptGcm. */
|
|
125
|
+
encrypt: encryptCtr,
|
|
126
|
+
/** @deprecated reads legacy aes-256-ctr values; use decryptGcm. */
|
|
127
|
+
decrypt: decryptCtr,
|
|
27
128
|
};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ciphertext envelope formats written by Cryptor.encrypt().
|
|
3
|
+
*
|
|
4
|
+
* v1 (legacy, read only):
|
|
5
|
+
* keyId:iv:ciphertext:encryptedKey
|
|
6
|
+
* keyId and encryptedKey are base64, iv and ciphertext are hex.
|
|
7
|
+
* aes-256-ctr, no authentication tag.
|
|
8
|
+
*
|
|
9
|
+
* v2 (current):
|
|
10
|
+
* v2:keyId:iv:ciphertext:tag:encryptedKey
|
|
11
|
+
* Every part after the marker is base64. AES-256-GCM with a 12-byte IV and
|
|
12
|
+
* a 16-byte tag, under a 32-byte data key. The header `v2:keyId` is
|
|
13
|
+
* authenticated as associated data.
|
|
14
|
+
*
|
|
15
|
+
* The version marker cannot be confused with a v1 key id: Buffer#toString
|
|
16
|
+
* ('base64') always pads to a multiple of 4 characters ("v2" is 2), and a
|
|
17
|
+
* 4-character "v" + digits string would decode to a UTF-8 continuation byte
|
|
18
|
+
* first, which no encoded string starts with.
|
|
19
|
+
*
|
|
20
|
+
* Markers v3 and later are reserved. A value that carries one is reported as
|
|
21
|
+
* an envelope (so it is never treated as plaintext) and parseEnvelope()
|
|
22
|
+
* refuses it with an "upgrade" error instead of misreading it.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const CURRENT_ENVELOPE_VERSION = 'v2';
|
|
26
|
+
|
|
27
|
+
const IV_BYTES = 12;
|
|
28
|
+
const TAG_BYTES = 16;
|
|
29
|
+
const MIN_ENVELOPE_LENGTH = 50;
|
|
30
|
+
|
|
31
|
+
const VERSION_MARKER = /^v([1-9][0-9]*)$/;
|
|
32
|
+
const LOOSE_BASE64 = /^[A-Za-z0-9+/=]+$/;
|
|
33
|
+
const STRICT_BASE64 =
|
|
34
|
+
/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
35
|
+
|
|
36
|
+
const isBase64 = (part, { allowEmpty = false } = {}) =>
|
|
37
|
+
part === '' ? allowEmpty : STRICT_BASE64.test(part);
|
|
38
|
+
|
|
39
|
+
const decodeKeyId = (keyIdB64) => Buffer.from(keyIdB64, 'base64').toString();
|
|
40
|
+
|
|
41
|
+
/** The authenticated header of a v2 envelope. */
|
|
42
|
+
const v2Header = (keyIdB64) => `v2:${keyIdB64}`;
|
|
43
|
+
|
|
44
|
+
function splitV2(value) {
|
|
45
|
+
const parts = value.split(':');
|
|
46
|
+
if (parts.length !== 6 || parts[0] !== 'v2') {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const [, keyIdB64, ivB64, ciphertextB64, tagB64, encryptedKey] = parts;
|
|
50
|
+
if (
|
|
51
|
+
!isBase64(keyIdB64) ||
|
|
52
|
+
!isBase64(ivB64) ||
|
|
53
|
+
!isBase64(ciphertextB64, { allowEmpty: true }) ||
|
|
54
|
+
!isBase64(tagB64) ||
|
|
55
|
+
!isBase64(encryptedKey)
|
|
56
|
+
) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
const iv = Buffer.from(ivB64, 'base64');
|
|
60
|
+
const tag = Buffer.from(tagB64, 'base64');
|
|
61
|
+
if (iv.length !== IV_BYTES || tag.length !== TAG_BYTES) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
version: 'v2',
|
|
66
|
+
keyIdB64,
|
|
67
|
+
keyId: decodeKeyId(keyIdB64),
|
|
68
|
+
iv,
|
|
69
|
+
ciphertext: Buffer.from(ciphertextB64, 'base64'),
|
|
70
|
+
tag,
|
|
71
|
+
encryptedKey,
|
|
72
|
+
header: v2Header(keyIdB64),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A version marker after v2: reserved for formats this code cannot read. */
|
|
77
|
+
function laterVersion(marker) {
|
|
78
|
+
const match = VERSION_MARKER.exec(marker);
|
|
79
|
+
return match && Number(match[1]) > 2 ? marker : null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {*} value
|
|
84
|
+
* @returns {string|null} 'v1', 'v2', a later marker such as 'v3', or null
|
|
85
|
+
* when the value is not a ciphertext envelope.
|
|
86
|
+
*/
|
|
87
|
+
function getEnvelopeVersion(value) {
|
|
88
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
const parts = value.split(':');
|
|
92
|
+
if (parts[0] === 'v2') {
|
|
93
|
+
// Structural check only: a damaged v2 value must still count as
|
|
94
|
+
// ciphertext, so that decrypting it fails instead of the damaged
|
|
95
|
+
// value being returned as data. parseEnvelope() checks the sizes.
|
|
96
|
+
const plausible =
|
|
97
|
+
value.length >= MIN_ENVELOPE_LENGTH &&
|
|
98
|
+
parts.length === 6 &&
|
|
99
|
+
parts.every(
|
|
100
|
+
(part, index) =>
|
|
101
|
+
LOOSE_BASE64.test(part) || (part === '' && index === 3)
|
|
102
|
+
);
|
|
103
|
+
return plausible ? 'v2' : null;
|
|
104
|
+
}
|
|
105
|
+
if (VERSION_MARKER.test(parts[0])) {
|
|
106
|
+
const marker = laterVersion(parts[0]);
|
|
107
|
+
const plausible =
|
|
108
|
+
marker &&
|
|
109
|
+
value.length >= MIN_ENVELOPE_LENGTH &&
|
|
110
|
+
parts.length >= 3 &&
|
|
111
|
+
parts.slice(1).every((part) => LOOSE_BASE64.test(part));
|
|
112
|
+
return plausible ? marker : null;
|
|
113
|
+
}
|
|
114
|
+
// v1: real envelopes are well over 50 characters (the encrypted data key
|
|
115
|
+
// alone is longer), so shorter colon-separated plaintext is not taken
|
|
116
|
+
// for ciphertext.
|
|
117
|
+
if (
|
|
118
|
+
value.length >= MIN_ENVELOPE_LENGTH &&
|
|
119
|
+
parts.length === 4 &&
|
|
120
|
+
parts.every((part) => LOOSE_BASE64.test(part))
|
|
121
|
+
) {
|
|
122
|
+
return 'v1';
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function isEncryptedEnvelope(value) {
|
|
128
|
+
return getEnvelopeVersion(value) !== null;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Split an envelope into its parts. Lenient for v1 (any four parts, as
|
|
133
|
+
* Cryptor.decrypt always was); strict for v2.
|
|
134
|
+
* @throws {Error} for a value that is not an envelope, a malformed v2
|
|
135
|
+
* envelope, or a later version.
|
|
136
|
+
*/
|
|
137
|
+
function parseEnvelope(value) {
|
|
138
|
+
if (typeof value !== 'string') {
|
|
139
|
+
throw new Error('Value is not a Frigg ciphertext envelope');
|
|
140
|
+
}
|
|
141
|
+
const parts = value.split(':');
|
|
142
|
+
if (parts[0] === 'v2') {
|
|
143
|
+
const v2 = splitV2(value);
|
|
144
|
+
if (!v2) {
|
|
145
|
+
throw new Error(
|
|
146
|
+
'Malformed v2 ciphertext envelope: expected v2:keyId:iv:ciphertext:tag:encryptedKey with a 12-byte IV and a 16-byte tag'
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
return v2;
|
|
150
|
+
}
|
|
151
|
+
const later = laterVersion(parts[0]);
|
|
152
|
+
if (later) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`Unsupported ciphertext envelope version ${later}: this version of @friggframework/core reads v1 and v2. Upgrade @friggframework/core to read it.`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
if (parts.length === 4) {
|
|
158
|
+
const [keyIdB64, iv, ciphertext, encryptedKey] = parts;
|
|
159
|
+
return {
|
|
160
|
+
version: 'v1',
|
|
161
|
+
keyIdB64,
|
|
162
|
+
keyId: decodeKeyId(keyIdB64),
|
|
163
|
+
encryptedText: `${iv}:${ciphertext}`,
|
|
164
|
+
encryptedKey,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
throw new Error('Value is not a Frigg ciphertext envelope');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Write a v2 envelope.
|
|
172
|
+
* @param {{ keyIdB64: string, iv: Buffer, ciphertext: Buffer, tag: Buffer, encryptedKey: string }} parts
|
|
173
|
+
*/
|
|
174
|
+
function formatEnvelope({ keyIdB64, iv, ciphertext, tag, encryptedKey }) {
|
|
175
|
+
if (!Buffer.isBuffer(iv) || iv.length !== IV_BYTES) {
|
|
176
|
+
throw new Error('A v2 envelope needs a 12-byte IV');
|
|
177
|
+
}
|
|
178
|
+
if (!Buffer.isBuffer(tag) || tag.length !== TAG_BYTES) {
|
|
179
|
+
throw new Error('A v2 envelope needs a 16-byte authentication tag');
|
|
180
|
+
}
|
|
181
|
+
return [
|
|
182
|
+
'v2',
|
|
183
|
+
keyIdB64,
|
|
184
|
+
iv.toString('base64'),
|
|
185
|
+
ciphertext.toString('base64'),
|
|
186
|
+
tag.toString('base64'),
|
|
187
|
+
encryptedKey,
|
|
188
|
+
].join(':');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
module.exports = {
|
|
192
|
+
CURRENT_ENVELOPE_VERSION,
|
|
193
|
+
IV_BYTES,
|
|
194
|
+
TAG_BYTES,
|
|
195
|
+
getEnvelopeVersion,
|
|
196
|
+
isEncryptedEnvelope,
|
|
197
|
+
parseEnvelope,
|
|
198
|
+
formatEnvelope,
|
|
199
|
+
v2Header,
|
|
200
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AES master key material (AES_KEY, DEPRECATED_AES_KEY).
|
|
3
|
+
*
|
|
4
|
+
* AES-256 needs 32 bytes of key. Accepted encodings:
|
|
5
|
+
* - 64 hex characters: openssl rand -hex 32 (full 256 bits)
|
|
6
|
+
* - base64 of 32 bytes: openssl rand -base64 32 (full 256 bits)
|
|
7
|
+
* - legacy 32 characters: the UTF-8 bytes of the string, which is what
|
|
8
|
+
* aes-256-ctr used before envelope v2. Frigg's docs used to say
|
|
9
|
+
* `openssl rand -hex 16`, which gives 32 hex characters: 32 bytes of key
|
|
10
|
+
* but only 128 bits of entropy. Still accepted so existing data reads.
|
|
11
|
+
*
|
|
12
|
+
* The encodings cannot overlap: a legacy key is 32 bytes long, a hex key is
|
|
13
|
+
* 64 characters and a base64 key is 44 characters.
|
|
14
|
+
*
|
|
15
|
+
* Errors never contain the key value.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const KEY_BYTES = 32;
|
|
19
|
+
const HEX_KEY = /^[0-9a-fA-F]{64}$/;
|
|
20
|
+
const BASE64_KEY = /^[A-Za-z0-9+/]{43}=$/;
|
|
21
|
+
|
|
22
|
+
const GENERATE_HINT =
|
|
23
|
+
'Generate one with `openssl rand -hex 32` (64 hex characters) or `openssl rand -base64 32`.';
|
|
24
|
+
|
|
25
|
+
function describeAesKey(value) {
|
|
26
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
27
|
+
return { valid: false };
|
|
28
|
+
}
|
|
29
|
+
if (HEX_KEY.test(value)) {
|
|
30
|
+
return { valid: true, format: 'hex', strength: 'full' };
|
|
31
|
+
}
|
|
32
|
+
if (
|
|
33
|
+
BASE64_KEY.test(value) &&
|
|
34
|
+
Buffer.from(value, 'base64').length === KEY_BYTES
|
|
35
|
+
) {
|
|
36
|
+
return { valid: true, format: 'base64', strength: 'full' };
|
|
37
|
+
}
|
|
38
|
+
if (Buffer.byteLength(value, 'utf8') === KEY_BYTES) {
|
|
39
|
+
return { valid: true, format: 'legacy', strength: 'legacy' };
|
|
40
|
+
}
|
|
41
|
+
return { valid: false };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {string|null|undefined} value
|
|
46
|
+
* @param {{ name?: string }} [options] variable name for error messages
|
|
47
|
+
* @returns {{ key: Buffer, format: 'hex'|'base64'|'legacy', strength: 'full'|'legacy' }}
|
|
48
|
+
*/
|
|
49
|
+
function parseAesKey(value, { name = 'AES_KEY' } = {}) {
|
|
50
|
+
if (value === undefined || value === null || value === '') {
|
|
51
|
+
throw new Error(`${name} is not set. ${GENERATE_HINT}`);
|
|
52
|
+
}
|
|
53
|
+
const description = describeAesKey(value);
|
|
54
|
+
if (!description.valid) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`${name} must be 32 random bytes as 64 hex characters or base64 ` +
|
|
57
|
+
`(a legacy 32-character key is also accepted); the value has ` +
|
|
58
|
+
`${
|
|
59
|
+
typeof value === 'string' ? value.length : 0
|
|
60
|
+
} characters. ${GENERATE_HINT}`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
const encoding = { hex: 'hex', base64: 'base64', legacy: 'utf8' }[
|
|
64
|
+
description.format
|
|
65
|
+
];
|
|
66
|
+
return {
|
|
67
|
+
key: Buffer.from(value, encoding),
|
|
68
|
+
format: description.format,
|
|
69
|
+
strength: description.strength,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = { parseAesKey, describeAesKey, KEY_BYTES };
|
|
@@ -68,6 +68,18 @@ Comprehensive health check that tests all service components and dependencies.
|
|
|
68
68
|
"count": 5,
|
|
69
69
|
"available": ["integration1", "integration2", "..."]
|
|
70
70
|
}
|
|
71
|
+
},
|
|
72
|
+
"transportSecurity": {
|
|
73
|
+
"status": "healthy", // "healthy" or "warning"; never changes the overall status
|
|
74
|
+
"nodeVersion": "v22.23.5",
|
|
75
|
+
"opensslVersion": "3.5.9",
|
|
76
|
+
"defaultEcdhCurve": "auto",
|
|
77
|
+
"postQuantumKeyExchange": {
|
|
78
|
+
"group": "X25519MLKEM768",
|
|
79
|
+
"supported": true,
|
|
80
|
+
"offeredByDefault": true
|
|
81
|
+
}
|
|
82
|
+
// "recommendation": "..." is added when offeredByDefault is false
|
|
71
83
|
}
|
|
72
84
|
},
|
|
73
85
|
"responseTime": 250 // total endpoint response time in milliseconds
|
|
@@ -145,6 +157,24 @@ Kubernetes-style readiness probe. Returns whether the service is ready to receiv
|
|
|
145
157
|
- Verifies available modules and integrations are loaded
|
|
146
158
|
- Reports counts and lists of available components
|
|
147
159
|
|
|
160
|
+
### Transport Security (post-quantum TLS)
|
|
161
|
+
- Tells you if outbound TLS from this runtime offers hybrid post-quantum key
|
|
162
|
+
exchange (`X25519MLKEM768`, ML-KEM-768 with X25519). This covers every
|
|
163
|
+
outbound call: AWS SDK calls (KMS, S3, SQS) and API module calls.
|
|
164
|
+
- `supported`: OpenSSL in this runtime knows the group.
|
|
165
|
+
- `offeredByDefault`: the default TLS settings offer it. This is `true` with
|
|
166
|
+
OpenSSL 3.5 or later (Node.js 22.20 or later, Node.js 24.5 or later) when
|
|
167
|
+
`tls.DEFAULT_ECDH_CURVE` is `auto`, or when an override lists the group.
|
|
168
|
+
- The peer still chooses the group. AWS KMS, Secrets Manager and S3 accept
|
|
169
|
+
it. A partner API that does not accept it gets classical key exchange.
|
|
170
|
+
- The check is local. It makes no network call.
|
|
171
|
+
- `warning` means outbound TLS uses classical key exchange only. Recorded
|
|
172
|
+
traffic is then open to "harvest now, decrypt later". The
|
|
173
|
+
`recommendation` field tells you what to change. A warning never changes
|
|
174
|
+
the overall status or the status code.
|
|
175
|
+
- Inbound TLS (clients to Frigg) is set by API Gateway or CloudFront, not by
|
|
176
|
+
this runtime. See `docs/reference/post-quantum-readiness.md`.
|
|
177
|
+
|
|
148
178
|
## Usage Examples
|
|
149
179
|
|
|
150
180
|
### Monitoring Systems
|
|
@@ -27,6 +27,9 @@ const {
|
|
|
27
27
|
const {
|
|
28
28
|
CheckIntegrationsHealthUseCase,
|
|
29
29
|
} = require('../use-cases/check-integrations-health-use-case');
|
|
30
|
+
const {
|
|
31
|
+
CheckTransportSecurityHealthUseCase,
|
|
32
|
+
} = require('../use-cases/check-transport-security-health-use-case');
|
|
30
33
|
|
|
31
34
|
const router = Router();
|
|
32
35
|
const healthCheckRepository = createHealthCheckRepository({ prismaClient: prisma });
|
|
@@ -66,6 +69,8 @@ const checkIntegrationsHealthUseCase = new CheckIntegrationsHealthUseCase({
|
|
|
66
69
|
moduleFactory,
|
|
67
70
|
integrationClasses,
|
|
68
71
|
});
|
|
72
|
+
const checkTransportSecurityHealthUseCase =
|
|
73
|
+
new CheckTransportSecurityHealthUseCase();
|
|
69
74
|
|
|
70
75
|
const validateApiKey = (req, res, next) => {
|
|
71
76
|
const apiKey = req.headers['x-frigg-health-api-key'];
|
|
@@ -472,6 +477,23 @@ router.get('/health/detailed', async (_req, res) => {
|
|
|
472
477
|
console.log('Integrations check error:', error.message);
|
|
473
478
|
}
|
|
474
479
|
|
|
480
|
+
// Informational: a runtime without hybrid post-quantum key exchange still
|
|
481
|
+
// works, so this check never changes the overall status.
|
|
482
|
+
try {
|
|
483
|
+
response.checks.transportSecurity =
|
|
484
|
+
checkTransportSecurityHealthUseCase.execute();
|
|
485
|
+
console.log(
|
|
486
|
+
'Transport security check completed:',
|
|
487
|
+
response.checks.transportSecurity
|
|
488
|
+
);
|
|
489
|
+
} catch (error) {
|
|
490
|
+
response.checks.transportSecurity = {
|
|
491
|
+
status: 'error',
|
|
492
|
+
error: error.message,
|
|
493
|
+
};
|
|
494
|
+
console.log('Transport security check error:', error.message);
|
|
495
|
+
}
|
|
496
|
+
|
|
475
497
|
response.responseTime = Date.now() - startTime;
|
|
476
498
|
|
|
477
499
|
const statusCode = response.status === 'healthy' ? 200 : 503;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
const nodeTls = require('tls');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The hybrid key exchange group (X25519 + ML-KEM-768, FIPS 203) that TLS 1.3
|
|
5
|
+
* clients and servers use for post-quantum key agreement. AWS endpoints such
|
|
6
|
+
* as KMS, Secrets Manager and S3 accept it.
|
|
7
|
+
*/
|
|
8
|
+
const HYBRID_POST_QUANTUM_GROUP = 'X25519MLKEM768';
|
|
9
|
+
|
|
10
|
+
/** OpenSSL 3.5 is the first release that offers the group by default. */
|
|
11
|
+
const FIRST_DEFAULT_OPENSSL = { major: 3, minor: 5 };
|
|
12
|
+
|
|
13
|
+
const UPGRADE_RECOMMENDATION =
|
|
14
|
+
'Outbound TLS from this runtime uses classical key exchange only. Use a ' +
|
|
15
|
+
'runtime with OpenSSL 3.5 or later (Node.js 22.20 or later, or Node.js 24.5 or later) ' +
|
|
16
|
+
`to get hybrid post-quantum key exchange (${HYBRID_POST_QUANTUM_GROUP}) ` +
|
|
17
|
+
'on outbound TLS.';
|
|
18
|
+
|
|
19
|
+
function parseOpensslVersion(version) {
|
|
20
|
+
const match = /^(\d+)\.(\d+)/.exec(String(version || ''));
|
|
21
|
+
if (!match) return null;
|
|
22
|
+
return { major: Number(match[1]), minor: Number(match[2]) };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isAtLeast(version, minimum) {
|
|
26
|
+
if (!version) return false;
|
|
27
|
+
if (version.major !== minimum.major) return version.major > minimum.major;
|
|
28
|
+
return version.minor >= minimum.minor;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Reports whether outbound TLS from this runtime offers hybrid post-quantum
|
|
33
|
+
* key exchange with Node's default TLS settings. AWS SDK calls (KMS, S3, SQS)
|
|
34
|
+
* and API module calls through node-fetch use those defaults. A connection
|
|
35
|
+
* that sets its own `ecdhCurve`, or an OpenSSL config file, can still change
|
|
36
|
+
* the groups; this check cannot see that. The peer decides which group it
|
|
37
|
+
* uses.
|
|
38
|
+
*
|
|
39
|
+
* The check is local and cheap: it asks OpenSSL to accept the group and reads
|
|
40
|
+
* the default curve list. It makes no network call.
|
|
41
|
+
*
|
|
42
|
+
* A missing group is a warning, not a failure: the app still works, and the
|
|
43
|
+
* fix is a runtime upgrade.
|
|
44
|
+
*/
|
|
45
|
+
class CheckTransportSecurityHealthUseCase {
|
|
46
|
+
/**
|
|
47
|
+
* @param {Object} [params]
|
|
48
|
+
* @param {Object} [params.tlsModule] - Node's `tls` module (injectable for tests)
|
|
49
|
+
* @param {Object} [params.versions] - `process.versions`
|
|
50
|
+
* @param {string} [params.nodeVersion] - `process.version`
|
|
51
|
+
*/
|
|
52
|
+
constructor({
|
|
53
|
+
tlsModule = nodeTls,
|
|
54
|
+
versions = process.versions,
|
|
55
|
+
nodeVersion = process.version,
|
|
56
|
+
} = {}) {
|
|
57
|
+
this.tls = tlsModule;
|
|
58
|
+
this.versions = versions || {};
|
|
59
|
+
this.nodeVersion = nodeVersion;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
execute() {
|
|
63
|
+
const opensslVersion = this.versions.openssl || null;
|
|
64
|
+
const defaultEcdhCurve = this.tls.DEFAULT_ECDH_CURVE;
|
|
65
|
+
const supported = this._isGroupSupported(HYBRID_POST_QUANTUM_GROUP);
|
|
66
|
+
// The hybrid group exists only in TLS 1.3.
|
|
67
|
+
const tls13Allowed = !/^TLSv1(\.[012])?$/.test(
|
|
68
|
+
String(this.tls.DEFAULT_MAX_VERSION || 'TLSv1.3')
|
|
69
|
+
);
|
|
70
|
+
const offeredByDefault =
|
|
71
|
+
supported &&
|
|
72
|
+
tls13Allowed &&
|
|
73
|
+
this._isOfferedByDefault(defaultEcdhCurve, opensslVersion);
|
|
74
|
+
|
|
75
|
+
const result = {
|
|
76
|
+
status: offeredByDefault ? 'healthy' : 'warning',
|
|
77
|
+
nodeVersion: this.nodeVersion,
|
|
78
|
+
opensslVersion,
|
|
79
|
+
defaultEcdhCurve,
|
|
80
|
+
postQuantumKeyExchange: {
|
|
81
|
+
group: HYBRID_POST_QUANTUM_GROUP,
|
|
82
|
+
supported,
|
|
83
|
+
offeredByDefault,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
if (!offeredByDefault) {
|
|
88
|
+
result.recommendation = tls13Allowed
|
|
89
|
+
? this._recommendation(supported, defaultEcdhCurve)
|
|
90
|
+
: `tls.DEFAULT_MAX_VERSION is set to "${this.tls.DEFAULT_MAX_VERSION}". ` +
|
|
91
|
+
`${HYBRID_POST_QUANTUM_GROUP} needs TLS 1.3: remove the override.`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** OpenSSL refuses a group it does not know ("Failed to set ECDH curve"). */
|
|
98
|
+
_isGroupSupported(group) {
|
|
99
|
+
try {
|
|
100
|
+
this.tls.createSecureContext({ ecdhCurve: group });
|
|
101
|
+
return true;
|
|
102
|
+
} catch {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
_isOfferedByDefault(defaultEcdhCurve, opensslVersion) {
|
|
108
|
+
if (!defaultEcdhCurve || defaultEcdhCurve === 'auto') {
|
|
109
|
+
return isAtLeast(
|
|
110
|
+
parseOpensslVersion(opensslVersion),
|
|
111
|
+
FIRST_DEFAULT_OPENSSL
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return String(defaultEcdhCurve)
|
|
115
|
+
.split(':')
|
|
116
|
+
.map((name) => name.trim().toLowerCase())
|
|
117
|
+
.includes(HYBRID_POST_QUANTUM_GROUP.toLowerCase());
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
_recommendation(supported, defaultEcdhCurve) {
|
|
121
|
+
const isOverridden = defaultEcdhCurve && defaultEcdhCurve !== 'auto';
|
|
122
|
+
if (supported && isOverridden) {
|
|
123
|
+
return (
|
|
124
|
+
`tls.DEFAULT_ECDH_CURVE is set to "${defaultEcdhCurve}", which ` +
|
|
125
|
+
`does not include ${HYBRID_POST_QUANTUM_GROUP}. Put ` +
|
|
126
|
+
`${HYBRID_POST_QUANTUM_GROUP} first in the list, or remove the ` +
|
|
127
|
+
'override.'
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return UPGRADE_RECOMMENDATION;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = {
|
|
135
|
+
CheckTransportSecurityHealthUseCase,
|
|
136
|
+
HYBRID_POST_QUANTUM_GROUP,
|
|
137
|
+
};
|