Skip to content

Commit 9e0c4dd

Browse files
authored
Update: prevent embedding 'oct' keys in JWS objects (#112)
1 parent c79f80f commit 9e0c4dd

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

lib/jwe/encrypt.js

+3
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@ function createEncrypt(opts, rcpts) {
601601
if (ref) {
602602
jwk = key.toJSON();
603603
if ("jwk" === ref) {
604+
if ("oct" === key.kty) {
605+
return Promise.reject(new Error("cannot embed key"));
606+
}
604607
header.jwk = jwk;
605608
} else if (ref in jwk) {
606609
header[ref] = jwk[ref];

lib/jws/sign.js

+3
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ function createSign(opts, signs) {
324324
if (ref) {
325325
jwk = key.toJSON();
326326
if ("jwk" === ref) {
327+
if ("oct" === key.kty) {
328+
return Promise.reject(new Error("cannot embed key"));
329+
}
327330
header.jwk = jwk;
328331
} else if (ref in jwk) {
329332
header[ref] = jwk[ref];

test/jwe/embed-test.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*!
2+
*
3+
* Copyright (c) 2016 Cisco Systems, Inc. See LICENSE file.
4+
*/
5+
"use strict";
6+
7+
var chai = require("chai");
8+
9+
var JWK = require("../../lib/jwk");
10+
var JWE = require("../../lib/jwe");
11+
12+
var assert = chai.assert;
13+
14+
describe.only("jwe/embedded", function() {
15+
var keys = {
16+
"oct": {
17+
"kty": "oct",
18+
"kid": "BBbx9f-quvmBp5gHzO1LA1r3Fm7MsXwQovuLoIq4Des",
19+
"k": "rmY1vk9qj34HAYWSc2aQJg"
20+
}
21+
// TODO: RSA and EC key tests
22+
}
23+
var payload = new Buffer("There and back again – A Hobbit's Tale, by Bilbo Baggins", "utf8");
24+
25+
before(function() {
26+
var all = Object.keys(keys);
27+
all = all.map(function(t) {
28+
return JWK.asKey(keys[t]).
29+
then(function(jwk) {
30+
keys[t] = jwk;
31+
});
32+
});
33+
return Promise.all(all);
34+
});
35+
36+
describe("oct", function() {
37+
it("failed to embed a symmetric key", function() {
38+
var badKey = keys.oct;
39+
var opts = {
40+
format: "general",
41+
protect: false
42+
};
43+
var jwe = JWE.createEncrypt(opts, {
44+
key: badKey,
45+
reference: "jwk"
46+
});
47+
jwe.update("You shall not pass!", "utf8");
48+
var p = jwe.final();
49+
p = p.then(function() {
50+
assert.ok(false, "unexpected success");
51+
}, function(err) {
52+
assert.instanceOf(err, Error);
53+
assert.equal(err.message, "cannot embed key");
54+
});
55+
return p;
56+
});
57+
});
58+
});

test/jws/embed-test.js

+35
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,39 @@ describe("jws/embedded", function() {
114114
return p;
115115
});
116116
});
117+
118+
describe("invalid", function() {
119+
var badKey = {
120+
"kty": "oct",
121+
"kid": "dNr5z3PMFZKDp7Kh7uAxmpuSiOrm0E3WZDEscsoRXeE",
122+
"alg": "HS256",
123+
"k": "UHZVSbwjVqqFCcdQUvrnX7gLXBIfMEkecVeYE7tD7fo"
124+
};
125+
before(function() {
126+
return JWK.asKey(badKey).
127+
then(function(result) {
128+
badKey = result;
129+
});
130+
});
131+
it("failed to embed a symmetric key", function() {
132+
var opts = {
133+
format: "general",
134+
protect: false
135+
};
136+
var jws = JWS.createSign(opts, {
137+
key: badKey,
138+
reference: "jwk"
139+
});
140+
jws.update("You shall not pass!", "utf8");
141+
142+
var p = jws.final();
143+
p = p.then(function() {
144+
assert.ok(false, "unexpected fail");
145+
}, function(err) {
146+
assert.instanceOf(err, Error);
147+
assert.equal(err.message, "cannot embed key");
148+
});
149+
return p;
150+
});
151+
});
117152
});

0 commit comments

Comments
 (0)