Skip to content

Commit fcad028

Browse files
committed
If publicKeyFingerprintType not defined defaults to pulling the publicKeyFingerprint out as a publicKey
1 parent 5185a10 commit fcad028

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ const config = {
290290
],
291291
mode: "JWE",
292292
encryptedValueFieldName: "encryptedData",
293-
publicKeyFingerprintType: "publicKey",
294293
encryptionCertificate: "./path/to/public.cert",
295294
privateKey: "./path/to/your/private.key",
296295
};
@@ -394,7 +393,6 @@ const config = {
394393
],
395394
mode: "JWE",
396395
encryptedValueFieldName: "encryptedData",
397-
publicKeyFingerprintType: "publicKey",
398396
encryptionCertificate: "./path/to/public.cert",
399397
privateKey: "./path/to/your/private.key",
400398
};
@@ -443,7 +441,6 @@ const config = {
443441
],
444442
mode: "JWE",
445443
encryptedValueFieldName: "encryptedData",
446-
publicKeyFingerprintType: "publicKey",
447444
encryptionCertificate: "./path/to/public.cert",
448445
privateKey: "./path/to/your/private.key",
449446
};

lib/mcapi/crypto/jwe-crypto.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,17 @@ function getPrivateKey(config) {
202202
*/
203203
function computePublicFingerprint(config, encryptionCertificate) {
204204
if (config && encryptionCertificate) {
205-
return utils.computePublicFingerprint(
206-
config,
207-
forge.pki.certificateFromPem(encryptionCertificate),
208-
config.dataEncoding
209-
);
205+
if(config.publicKeyFingerprintType) {
206+
return utils.computePublicFingerprint(
207+
config,
208+
forge.pki.certificateFromPem(encryptionCertificate),
209+
config.dataEncoding
210+
);
211+
} else {
212+
return utils.publicKeyFingerprint(
213+
forge.pki.certificateFromPem(encryptionCertificate)
214+
);
215+
}
210216
} else {
211217
return null;
212218
}
@@ -235,7 +241,7 @@ function validateFingerprint(config, contains) {
235241
const propertiesOptionalDataEncoding = ["dataEncoding"];
236242
const propertiesOptionalFingerprint = ["publicKeyFingerprint"];
237243
if (
238-
!contains(propertiesOptionalFingerprint) &&
244+
contains(propertiesFingerprint) &&
239245
config[propertiesFingerprint[0]] !== "certificate" &&
240246
config[propertiesFingerprint[0]] !== "publicKey"
241247
) {

lib/mcapi/utils/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ module.exports.computePublicFingerprint = function (
273273
);
274274
break;
275275
case "publicKey":
276-
fingerprint = publicKeyFingerprint(encryptionCertificate);
276+
fingerprint = this.publicKeyFingerprint(encryptionCertificate);
277277
break;
278278
}
279279
}
@@ -289,13 +289,13 @@ function publicCertificateFingerprint(publicCertificate, encoding) {
289289
return bytesToString(md.digest().getBytes(), encoding);
290290
}
291291

292-
function publicKeyFingerprint(publicCertificate) {
292+
module.exports.publicKeyFingerprint = function(publicCertificate) {
293293
return forge.pki.getPublicKeyFingerprint(publicCertificate.publicKey, {
294294
type: "SubjectPublicKeyInfo",
295295
md: createMessageDigest("SHA-256"),
296296
encoding: c.HEX,
297297
});
298-
}
298+
};
299299

300300
function createMessageDigest(digest) {
301301
switch (digest.toUpperCase()) {

test/jwe-crypto.test.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,6 @@ describe("JWE Crypto", () => {
8787
});
8888
});
8989

90-
it("without publicKeyFingerprintType", () => {
91-
const config = JSON.parse(JSON.stringify(testConfig));
92-
delete config["publicKeyFingerprintType"];
93-
assert.throws(
94-
() => new Crypto(config),
95-
/Config not valid: publicKeyFingerprintType should be: 'certificate' or 'publicKey'/
96-
);
97-
});
98-
9990
it("without publicKeyFingerprintType, but providing the publicKeyFingerprint", () => {
10091
const config = JSON.parse(JSON.stringify(testConfig));
10192
delete config["publicKeyFingerprintType"];
@@ -267,5 +258,18 @@ describe("JWE Crypto", () => {
267258
})
268259
);
269260
});
261+
262+
it("compute public fingerprint: defaults to publicKey with publicKeyFingerprintType set", () => {
263+
const strippedConfig = JSON.parse(JSON.stringify(testConfig));
264+
delete strippedConfig["publicKeyFingerprintType"];
265+
delete strippedConfig["dataEncoding"];
266+
267+
assert.ok(
268+
"80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279",
269+
computePublicFingerprint.call(crypto, {
270+
strippedConfig
271+
})
272+
);
273+
});
270274
});
271275
});

0 commit comments

Comments
 (0)