Skip to content

Commit 959a61d

Browse files
authored
Update: configure if embedded keys can be used for signature verification (#147)
1 parent 3e5e8be commit 959a61d

File tree

9 files changed

+3240
-449
lines changed

9 files changed

+3240
-449
lines changed

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,26 @@ To verify using a key embedded in the JWS:
422422

423423
```javascript
424424
jose.JWS.createVerify().
425-
verify(input).
425+
verify(input, { allowEmbeddedKey: true }).
426426
then(function(result) {
427427
// ...
428428
});
429429
```
430430

431+
Alternatively, a cached `createVerify()` can be configured to allow an embedded key:
432+
433+
```javascript
434+
var verifier = jose.JWS.createVerify({ allowEmbeddedKey: true });
435+
436+
verifier.verify(input).
437+
then(function(result) {
438+
// ...
439+
});
440+
```
441+
431442
The key can be embedded using either 'jwk' or 'x5c', and can be located in either the JWS Unprotected Header or JWS Protected Header.
432443

433-
**NOTE:** `verify()` will use the embedded key (if found) instead of any other key.
444+
**NOTE:** `verify()` will use the embedded key (if found and permitted) instead of any other key.
434445

435446
#### Handling `crit` Header Members ####
436447

lib/jws/verify.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var JWSVerifier = function(ks, globalOpts) {
3131
keystore = JWK.createKeyStore();
3232
}
3333

34-
globalOpts = merge({}, globalOpts);
34+
globalOpts = merge({ allowEmbeddedKey: false }, globalOpts);
3535

3636
Object.defineProperty(this, "defaultKey", {
3737
value: assumedKey || undefined,
@@ -128,9 +128,9 @@ var JWSVerifier = function(ks, globalOpts) {
128128
p = p.then(function(sig) {
129129
var algKey;
130130
// TODO: resolve jku, x5c, x5u
131-
if (sig.header.jwk) {
131+
if (opts.allowEmbeddedKey && sig.header.jwk) {
132132
algKey = JWK.asKey(sig.header.jwk);
133-
} else if (sig.header.x5c) {
133+
} else if (opts.allowEmbeddedKey && sig.header.x5c) {
134134
algKey = sig.header.x5c[0];
135135
algKey = new Buffer(algKey, "base64");
136136
// TODO: callback to validate chain
@@ -247,8 +247,8 @@ var JWSVerifier = function(ks, globalOpts) {
247247
* @param {JWK.Key|JWK.KeyStore} ks The Key or KeyStore to use for verification.
248248
* @returns {JWS.Verifier} The new Verifier.
249249
*/
250-
function createVerify(ks) {
251-
var vfy = new JWSVerifier(ks);
250+
function createVerify(ks, opts) {
251+
var vfy = new JWSVerifier(ks, opts);
252252

253253
return vfy;
254254
}

lib/parse/compact.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ function parseCompact(input) {
1919
if (3 === parts.length) {
2020
// JWS
2121
type = "JWS";
22-
op = function(ks) {
22+
op = function(ks, opts) {
2323
return jose.JWS.createVerify(ks).
24-
verify(input);
24+
verify(input, opts);
2525
};
2626
} else if (5 === parts.length) {
2727
// JWE
2828
type = "JWE";
29-
op = function(ks) {
29+
op = function(ks, opts) {
3030
return jose.JWE.createDecrypt(ks).
31-
decrypt(input);
31+
decrypt(input, opts);
3232
};
3333
} else {
3434
throw new TypeError("invalid jose serialization");

lib/parse/json.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ function parseJSON(input) {
2121
if ("signatures" in input || "signature" in input) {
2222
// JWS
2323
type = "JWS";
24-
op = function(ks) {
24+
op = function(ks, opts) {
2525
return jose.JWS.createVerify(ks).
26-
verify(input);
26+
verify(input, opts);
2727
};
2828
// headers can be (signatures[].protected, signatures[].header, signature.protected, signature.header)
2929
headers = input.signatures ||
@@ -51,9 +51,9 @@ function parseJSON(input) {
5151
} else if ("ciphertext" in input) {
5252
// JWE
5353
type = "JWE";
54-
op = function(ks) {
54+
op = function(ks, opts) {
5555
return jose.JWE.createDecrypt(ks).
56-
decrypt(input);
56+
decrypt(input, opts);
5757
};
5858
// headers can be (protected, unprotected, recipients[].header)
5959
var root = {};

0 commit comments

Comments
 (0)