1 /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
2 /*
3 * Copyright 2016 Red Hat, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "misc.h"
19 #include <jose/b64.h>
20 #include <jose/jwk.h>
21 #include "../hooks.h"
22 #include <string.h>
23
24 #define NAMES "A128GCMKW", "A192GCMKW", "A256GCMKW"
25
26 static inline const char *
kw2enc(const jose_hook_alg_t * alg)27 kw2enc(const jose_hook_alg_t *alg)
28 {
29 switch (str2enum(alg->name, NAMES, NULL)) {
30 case 0: return "A128GCM";
31 case 1: return "A192GCM";
32 case 2: return "A256GCM";
33 default: return NULL;
34 }
35 }
36
37 static inline const jose_hook_alg_t *
kw2alg(const jose_hook_alg_t * alg)38 kw2alg(const jose_hook_alg_t *alg)
39 {
40 const char *enc = NULL;
41
42 enc = kw2enc(alg);
43 if (!enc)
44 return NULL;
45
46 return jose_hook_alg_find(JOSE_HOOK_ALG_KIND_ENCR, enc);
47 }
48
49 static json_int_t
alg2len(const char * alg)50 alg2len(const char *alg)
51 {
52 switch (str2enum(alg, NAMES, NULL)) {
53 case 0: return 16;
54 case 1: return 24;
55 case 2: return 32;
56 default: return 0;
57 }
58 }
59
60 static bool
jwk_prep_handles(jose_cfg_t * cfg,const json_t * jwk)61 jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
62 {
63 const char *alg = NULL;
64
65 if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
66 return false;
67
68 return alg2len(alg) != 0;
69 }
70
71 static bool
jwk_prep_execute(jose_cfg_t * cfg,json_t * jwk)72 jwk_prep_execute(jose_cfg_t *cfg, json_t *jwk)
73 {
74 const char *alg = NULL;
75 const char *kty = NULL;
76 json_int_t byt = 0;
77 json_int_t len = 0;
78
79 if (json_unpack(jwk, "{s:s,s?s,s?I}",
80 "alg", &alg, "kty", &kty, "bytes", &byt) == -1)
81 return false;
82
83 len = alg2len(alg);
84 if (len == 0)
85 return false;
86
87 if (byt != 0 && len != byt)
88 return false;
89
90 if (kty && strcmp(kty, "oct") != 0)
91 return false;
92
93 if (json_object_set_new(jwk, "kty", json_string("oct")) < 0)
94 return false;
95
96 if (json_object_set_new(jwk, "bytes", json_integer(len)) < 0)
97 return false;
98
99 return true;
100 }
101
102 static const char *
alg_wrap_alg(const jose_hook_alg_t * alg,jose_cfg_t * cfg,const json_t * jwk)103 alg_wrap_alg(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
104 {
105 const char *name = NULL;
106 const char *type = NULL;
107
108 if (json_unpack((json_t *) jwk, "{s?s,s?s}",
109 "alg", &name, "kty", &type) < 0)
110 return NULL;
111
112 if (name)
113 return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
114
115 if (!type || strcmp(type, "oct") != 0)
116 return NULL;
117
118 switch (jose_b64_dec(json_object_get(jwk, "k"), NULL, 0)) {
119 case 16: return "A128GCMKW";
120 case 24: return "A192GCMKW";
121 case 32: return "A256GCMKW";
122 default: break;
123 }
124
125 return NULL;
126 }
127
128 static const char *
alg_wrap_enc(const jose_hook_alg_t * alg,jose_cfg_t * cfg,const json_t * jwk)129 alg_wrap_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
130 {
131 return kw2enc(alg);
132 }
133
134 static bool
alg_wrap_wrp(const jose_hook_alg_t * alg,jose_cfg_t * cfg,json_t * jwe,json_t * rcp,const json_t * jwk,json_t * cek)135 alg_wrap_wrp(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
136 json_t *rcp, const json_t *jwk, json_t *cek)
137 {
138 const jose_hook_alg_t *enc = NULL;
139 jose_io_auto_t *e = NULL;
140 jose_io_auto_t *d = NULL;
141 jose_io_auto_t *c = NULL;
142 jose_io_auto_t *p = NULL;
143 json_auto_t *tmp = NULL;
144 const char *k = NULL;
145 json_t *h = NULL;
146 void *ct = NULL;
147 void *pt = NULL;
148 size_t ptl = 0;
149 size_t ctl = 0;
150 size_t kl = 0;
151
152 if (!json_object_get(cek, "k") && !jose_jwk_gen(cfg, cek))
153 return false;
154
155 /* Obtain the plaintext to wrap. */
156 if (json_unpack(cek, "{s:s%}", "k", &k, &kl) < 0)
157 return false;
158
159 p = jose_io_malloc(cfg, &pt, &ptl);
160 if (!p)
161 return false;
162
163 d = jose_b64_dec_io(p);
164 if (!d || !d->feed(d, k, kl) || !d->done(d))
165 return false;
166
167 /* Perform the wrapping. */
168 enc = kw2alg(alg);
169 if (!enc)
170 return false;
171
172 tmp = json_object();
173 if (!tmp)
174 return false;
175
176 c = jose_io_malloc(cfg, &ct, &ctl);
177 if (!c)
178 return false;
179
180 e = enc->encr.enc(enc, cfg, tmp, jwk, c);
181 if (!e || !e->feed(e, pt, ptl) || !e->done(e))
182 return false;
183
184 /* Save the output. */
185 h = json_object_get(rcp, "header");
186 if (!h) {
187 if (json_object_set_new(rcp, "header", h = json_object()) < 0)
188 return false;
189 }
190 if (!json_is_object(h))
191 return false;
192
193 if (json_object_update(h, tmp) < 0)
194 return false;
195
196 if (json_object_set_new(rcp, "encrypted_key", jose_b64_enc(ct, ctl)) < 0)
197 return false;
198
199 return add_entity(jwe, rcp, "recipients", "header", "encrypted_key", NULL);
200 }
201
202 static bool
alg_wrap_unw(const jose_hook_alg_t * alg,jose_cfg_t * cfg,const json_t * jwe,const json_t * rcp,const json_t * jwk,json_t * cek)203 alg_wrap_unw(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
204 const json_t *rcp, const json_t *jwk, json_t *cek)
205 {
206 const jose_hook_alg_t *enc = NULL;
207 jose_io_auto_t *c = NULL;
208 jose_io_auto_t *d = NULL;
209 jose_io_auto_t *p = NULL;
210 json_auto_t *hdr = NULL;
211 json_auto_t *tmp = NULL;
212 const char *ct = NULL;
213 void *pt = NULL;
214 size_t ptl = 0;
215 size_t ctl = 0;
216
217 /* Prepare synthetic JWE */
218 hdr = jose_jwe_hdr(jwe, rcp);
219 if (!hdr)
220 return false;
221
222 tmp = json_object();
223 if (!tmp)
224 return false;
225
226 if (json_object_set(tmp, "iv", json_object_get(hdr, "iv")) < 0)
227 return false;
228
229 if (json_object_set(tmp, "tag", json_object_get(hdr, "tag")) < 0)
230 return false;
231
232 /* Perform the unwrap. */
233 if (json_unpack((json_t *) rcp, "{s:s%}", "encrypted_key", &ct, &ctl) < 0)
234 return false;
235
236 enc = kw2alg(alg);
237 if (!enc)
238 return false;
239
240 p = jose_io_malloc(cfg, &pt, &ptl);
241 if (!p)
242 return false;
243
244 c = enc->encr.dec(enc, cfg, tmp, jwk, p);
245 if (!c)
246 return false;
247
248 d = jose_b64_dec_io(c);
249 if (!d || !d->feed(d, ct, ctl) || !d->done(d))
250 return false;
251
252 /* Set the output value */
253 if (json_object_set_new(cek, "k", jose_b64_enc(pt, ptl)) < 0)
254 return false;
255
256 return true;
257 }
258
259 static void __attribute__((constructor))
constructor(void)260 constructor(void)
261 {
262 static jose_hook_jwk_t jwk = {
263 .kind = JOSE_HOOK_JWK_KIND_PREP,
264 .prep.handles = jwk_prep_handles,
265 .prep.execute = jwk_prep_execute,
266 };
267
268 static jose_hook_alg_t algs[] = {
269 { .kind = JOSE_HOOK_ALG_KIND_WRAP,
270 .name = "A128GCMKW",
271 .wrap.eprm = "wrapKey",
272 .wrap.dprm = "unwrapKey",
273 .wrap.alg = alg_wrap_alg,
274 .wrap.enc = alg_wrap_enc,
275 .wrap.wrp = alg_wrap_wrp,
276 .wrap.unw = alg_wrap_unw },
277 { .kind = JOSE_HOOK_ALG_KIND_WRAP,
278 .name = "A192GCMKW",
279 .wrap.eprm = "wrapKey",
280 .wrap.dprm = "unwrapKey",
281 .wrap.alg = alg_wrap_alg,
282 .wrap.enc = alg_wrap_enc,
283 .wrap.wrp = alg_wrap_wrp,
284 .wrap.unw = alg_wrap_unw },
285 { .kind = JOSE_HOOK_ALG_KIND_WRAP,
286 .name = "A256GCMKW",
287 .wrap.eprm = "wrapKey",
288 .wrap.dprm = "unwrapKey",
289 .wrap.alg = alg_wrap_alg,
290 .wrap.enc = alg_wrap_enc,
291 .wrap.wrp = alg_wrap_wrp,
292 .wrap.unw = alg_wrap_unw },
293 {}
294 };
295
296 jose_hook_jwk_push(&jwk);
297 for (size_t i = 0; algs[i].name; i++)
298 jose_hook_alg_push(&algs[i]);
299 }
300