Skip to content

Commit a4c5994

Browse files
committed
Adding ability to chose dataEncoding for publicKeyFingerPrint when doing JWE encryption
Removing duplicate code
1 parent c9cca0a commit a4c5994

File tree

7 files changed

+52
-24
lines changed

7 files changed

+52
-24
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ const config = {
290290
],
291291
mode: "JWE",
292292
encryptedValueFieldName: "encryptedData",
293-
publicKeyFingerprintType: "certificate",
293+
publicKeyFingerprintType: "publicKey",
294294
encryptionCertificate: "./path/to/public.cert",
295295
privateKey: "./path/to/your/private.key",
296296
};
@@ -394,7 +394,7 @@ const config = {
394394
],
395395
mode: "JWE",
396396
encryptedValueFieldName: "encryptedData",
397-
publicKeyFingerprintType: "certificate",
397+
publicKeyFingerprintType: "publicKey",
398398
encryptionCertificate: "./path/to/public.cert",
399399
privateKey: "./path/to/your/private.key",
400400
};
@@ -443,7 +443,7 @@ const config = {
443443
],
444444
mode: "JWE",
445445
encryptedValueFieldName: "encryptedData",
446-
publicKeyFingerprintType: "certificate",
446+
publicKeyFingerprintType: "publicKey",
447447
encryptionCertificate: "./path/to/public.cert",
448448
privateKey: "./path/to/your/private.key",
449449
};

lib/mcapi/crypto/jwe-crypto.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function computePublicFingerprint(config, encryptionCertificate) {
205205
return utils.computePublicFingerprint(
206206
config,
207207
forge.pki.certificateFromPem(encryptionCertificate),
208-
c.BASE64
208+
config.dataEncoding
209209
);
210210
} else {
211211
return null;
@@ -232,14 +232,25 @@ function isValidConfig(config) {
232232
*/
233233
function validateFingerprint(config, contains) {
234234
const propertiesFingerprint = ["publicKeyFingerprintType"];
235+
const propertiesOptionalDataEncoding = ["dataEncoding"];
235236
const propertiesOptionalFingerprint = ["publicKeyFingerprint"];
236237
if (
237238
!contains(propertiesOptionalFingerprint) &&
238239
config[propertiesFingerprint[0]] !== "certificate" &&
239240
config[propertiesFingerprint[0]] !== "publicKey"
240241
) {
241242
throw Error(
242-
"Config not valid: propertiesFingerprint should be: 'certificate' or 'publicKey'"
243+
"Config not valid: publicKeyFingerprintType should be: 'certificate' or 'publicKey'"
244+
);
245+
}
246+
if (
247+
!contains(propertiesOptionalFingerprint) &&
248+
config[propertiesFingerprint[0]] === "certificate" &&
249+
!(config[propertiesOptionalDataEncoding[0]] === c.BASE64 ||
250+
config[propertiesOptionalDataEncoding[0]] === c.HEX)
251+
) {
252+
throw Error(
253+
"Config not valid: if publicKeyFingerprintType is 'certificate' dataEncoding must be either 'base64' or 'hex'"
243254
);
244255
}
245256
}

lib/mcapi/encryption/field-level-encryption.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,7 @@ function encryptBody(path, body) {
103103
const elem = utils.elemFromPath(path.element, body);
104104
if (elem && elem.node) {
105105
const encryptedData = this.crypto.encryptData({ data: elem.node });
106-
body = utils.mutateObjectProperty(path.obj, encryptedData, body);
107-
// delete encrypted field if not overridden
108-
if (
109-
!utils.isJsonRoot(path.obj) &&
110-
path.element !== path.obj + "." + this.config.encryptedValueFieldName
111-
) {
112-
utils.deleteNode(path.element, body);
113-
}
106+
body = utils.addEncryptedDataToBody(encryptedData, path, this.config.encryptedValueFieldName, body);
114107
}
115108
return body;
116109
}

lib/mcapi/encryption/jwe-encryption.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,7 @@ function encryptBody(path, body) {
6767
const elem = utils.elemFromPath(path.element, body);
6868
if (elem && elem.node) {
6969
const encryptedData = this.crypto.encryptData({ data: elem.node });
70-
body = utils.mutateObjectProperty(path.obj, encryptedData, body);
71-
// delete encrypted field if not overridden
72-
if (
73-
!utils.isJsonRoot(path.obj) &&
74-
path.element !== path.obj + "." + this.config.encryptedValueFieldName
75-
) {
76-
utils.deleteNode(path.element, body);
77-
}
70+
body = utils.addEncryptedDataToBody(encryptedData, path, this.config.encryptedValueFieldName, body);
7871
}
7972
return body;
8073
}

lib/mcapi/utils/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,5 +415,17 @@ function hasEncryptionParam(encParams, bodyMap) {
415415
return encParams && encParams.length === 1 && bodyMap && bodyMap[0];
416416
}
417417

418+
module.exports.addEncryptedDataToBody = function(encryptedData, path, encryptedValueFieldName, body) {
419+
body = this.mutateObjectProperty(path.obj, encryptedData, body);
420+
if (
421+
!isJsonRoot(path.obj) &&
422+
path.element !== path.obj + "." + encryptedValueFieldName
423+
) {
424+
this.deleteNode(path.element, body);
425+
}
426+
return body;
427+
};
428+
429+
418430

419431

test/jwe-crypto.test.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe("JWE Crypto", () => {
9292
delete config["publicKeyFingerprintType"];
9393
assert.throws(
9494
() => new Crypto(config),
95-
/Config not valid: propertiesFingerprint should be: 'certificate' or 'publicKey'/
95+
/Config not valid: publicKeyFingerprintType should be: 'certificate' or 'publicKey'/
9696
);
9797
});
9898

@@ -108,16 +108,34 @@ describe("JWE Crypto", () => {
108108
config.publicKeyFingerprintType = "foobar";
109109
assert.throws(
110110
() => new Crypto(config),
111-
/Config not valid: propertiesFingerprint should be: 'certificate' or 'publicKey'/
111+
/Config not valid: publicKeyFingerprintType should be: 'certificate' or 'publicKey'/
112112
);
113113
});
114114

115-
it("with right publicKeyFingerprintType: certificate", () => {
115+
it("with right publicKeyFingerprintType: certificate and dataEncoding: base64", () => {
116116
const config = JSON.parse(JSON.stringify(testConfig));
117117
config.publicKeyFingerprintType = "certificate";
118+
config.dataEncoding = "base64";
118119
assert.doesNotThrow(() => new Crypto(config));
119120
});
120121

122+
it("with right publicKeyFingerprintType: certificate and dataEncoding: hex", () => {
123+
const config = JSON.parse(JSON.stringify(testConfig));
124+
config.publicKeyFingerprintType = "certificate";
125+
config.dataEncoding = "hex";
126+
assert.doesNotThrow(() => new Crypto(config));
127+
});
128+
129+
it("with right publicKeyFingerprintType: certificate and dataEncoding: null", () => {
130+
const config = JSON.parse(JSON.stringify(testConfig));
131+
config.publicKeyFingerprintType = "certificate";
132+
delete config["dataEncoding"];
133+
assert.throws(
134+
() => new Crypto(config),
135+
/Config not valid: if publicKeyFingerprintType is 'certificate' dataEncoding must be either 'base64' or 'hex'/
136+
);
137+
});
138+
121139
it("with right publicKeyFingerprintType: publicKey", () => {
122140
const config = JSON.parse(JSON.stringify(testConfig));
123141
config.publicKeyFingerprintType = "publicKey";

test/mock/jwe-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ module.exports = {
6464
mode: "JWE",
6565
encryptedValueFieldName: "encryptedData",
6666
publicKeyFingerprintType: "certificate",
67+
dataEncoding: "base64",
6768
encryptionCertificate: "./test/res/test_certificate.cert",
6869
privateKey: "./test/res/test_key.der",
6970
};

0 commit comments

Comments
 (0)