File: aesgcmkw.c

package info (click to toggle)
jose 14-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,884 kB
  • sloc: ansic: 10,235; javascript: 987; sh: 586; makefile: 9
file content (299 lines) | stat: -rw-r--r-- 7,487 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
/*
 * Copyright 2016 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "misc.h"
#include <jose/b64.h>
#include <jose/jwk.h>
#include "../hooks.h"
#include <string.h>

#define NAMES "A128GCMKW", "A192GCMKW", "A256GCMKW"

static inline const char *
kw2enc(const jose_hook_alg_t *alg)
{
    switch (str2enum(alg->name, NAMES, NULL)) {
    case 0: return "A128GCM";
    case 1: return "A192GCM";
    case 2: return "A256GCM";
    default: return NULL;
    }
}

static inline const jose_hook_alg_t *
kw2alg(const jose_hook_alg_t *alg)
{
    const char *enc = NULL;

    enc = kw2enc(alg);
    if (!enc)
        return NULL;

    return jose_hook_alg_find(JOSE_HOOK_ALG_KIND_ENCR, enc);
}

static json_int_t
alg2len(const char *alg)
{
    switch (str2enum(alg, NAMES, NULL)) {
    case 0: return 16;
    case 1: return 24;
    case 2: return 32;
    default: return 0;
    }
}

static bool
jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
{
    const char *alg = NULL;

    if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
        return false;

    return alg2len(alg) != 0;
}

static bool
jwk_prep_execute(jose_cfg_t *cfg, json_t *jwk)
{
    const char *alg = NULL;
    const char *kty = NULL;
    json_int_t byt = 0;
    json_int_t len = 0;

    if (json_unpack(jwk, "{s:s,s?s,s?I}",
                    "alg", &alg, "kty", &kty, "bytes", &byt) == -1)
        return false;

    len = alg2len(alg);
    if (len == 0)
        return false;

    if (byt != 0 && len != byt)
        return false;

    if (kty && strcmp(kty, "oct") != 0)
        return false;

    if (json_object_set_new(jwk, "kty", json_string("oct")) < 0)
        return false;

    if (json_object_set_new(jwk, "bytes", json_integer(len)) < 0)
        return false;

    return true;
}

static const char *
alg_wrap_alg(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
{
    const char *name = NULL;
    const char *type = NULL;

    if (json_unpack((json_t *) jwk, "{s?s,s?s}",
                    "alg", &name, "kty", &type) < 0)
        return NULL;

    if (name)
        return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;

    if (!type || strcmp(type, "oct") != 0)
        return NULL;

    switch (jose_b64_dec(json_object_get(jwk, "k"), NULL, 0)) {
    case 16: return "A128GCMKW";
    case 24: return "A192GCMKW";
    case 32: return "A256GCMKW";
    default: break;
    }

    return NULL;
}

static const char *
alg_wrap_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
{
    return kw2enc(alg);
}

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)
{
    const jose_hook_alg_t *enc = NULL;
    jose_io_auto_t *e = NULL;
    jose_io_auto_t *d = NULL;
    jose_io_auto_t *c = NULL;
    jose_io_auto_t *p = NULL;
    json_auto_t *tmp = NULL;
    const char *k = NULL;
    json_t *h = NULL;
    void *ct = NULL;
    void *pt = NULL;
    size_t ptl = 0;
    size_t ctl = 0;
    size_t kl = 0;

    if (!json_object_get(cek, "k") && !jose_jwk_gen(cfg, cek))
        return false;

    /* Obtain the plaintext to wrap. */
    if (json_unpack(cek, "{s:s%}", "k", &k, &kl) < 0)
        return false;

    p = jose_io_malloc(cfg, &pt, &ptl);
    if (!p)
        return false;

    d = jose_b64_dec_io(p);
    if (!d || !d->feed(d, k, kl) || !d->done(d))
        return false;

    /* Perform the wrapping. */
    enc = kw2alg(alg);
    if (!enc)
        return false;

    tmp = json_object();
    if (!tmp)
        return false;

    c = jose_io_malloc(cfg, &ct, &ctl);
    if (!c)
        return false;

    e = enc->encr.enc(enc, cfg, tmp, jwk, c);
    if (!e || !e->feed(e, pt, ptl) || !e->done(e))
        return false;

    /* Save the output. */
    h = json_object_get(rcp, "header");
    if (!h) {
        if (json_object_set_new(rcp, "header", h = json_object()) < 0)
            return false;
    }
    if (!json_is_object(h))
        return false;

    if (json_object_update(h, tmp) < 0)
        return false;

    if (json_object_set_new(rcp, "encrypted_key", jose_b64_enc(ct, ctl)) < 0)
        return false;

    return add_entity(jwe, rcp, "recipients", "header", "encrypted_key", NULL);
}

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)
{
    const jose_hook_alg_t *enc = NULL;
    jose_io_auto_t *c = NULL;
    jose_io_auto_t *d = NULL;
    jose_io_auto_t *p = NULL;
    json_auto_t *hdr = NULL;
    json_auto_t *tmp = NULL;
    const char *ct = NULL;
    void *pt = NULL;
    size_t ptl = 0;
    size_t ctl = 0;

    /* Prepare synthetic JWE */
    hdr = jose_jwe_hdr(jwe, rcp);
    if (!hdr)
        return false;

    tmp = json_object();
    if (!tmp)
        return false;

    if (json_object_set(tmp, "iv", json_object_get(hdr, "iv")) < 0)
        return false;

    if (json_object_set(tmp, "tag", json_object_get(hdr, "tag")) < 0)
        return false;

    /* Perform the unwrap. */
    if (json_unpack((json_t *) rcp, "{s:s%}", "encrypted_key", &ct, &ctl) < 0)
        return false;

    enc = kw2alg(alg);
    if (!enc)
        return false;

    p = jose_io_malloc(cfg, &pt, &ptl);
    if (!p)
        return false;

    c = enc->encr.dec(enc, cfg, tmp, jwk, p);
    if (!c)
        return false;

    d = jose_b64_dec_io(c);
    if (!d || !d->feed(d, ct, ctl) || !d->done(d))
        return false;

    /* Set the output value */
    if (json_object_set_new(cek, "k", jose_b64_enc(pt, ptl)) < 0)
        return false;

    return true;
}

static void __attribute__((constructor))
constructor(void)
{
    static jose_hook_jwk_t jwk = {
        .kind = JOSE_HOOK_JWK_KIND_PREP,
        .prep.handles = jwk_prep_handles,
        .prep.execute = jwk_prep_execute,
    };

    static jose_hook_alg_t algs[] = {
        { .kind = JOSE_HOOK_ALG_KIND_WRAP,
          .name = "A128GCMKW",
          .wrap.eprm = "wrapKey",
          .wrap.dprm = "unwrapKey",
          .wrap.alg = alg_wrap_alg,
          .wrap.enc = alg_wrap_enc,
          .wrap.wrp = alg_wrap_wrp,
          .wrap.unw = alg_wrap_unw },
        { .kind = JOSE_HOOK_ALG_KIND_WRAP,
          .name = "A192GCMKW",
          .wrap.eprm = "wrapKey",
          .wrap.dprm = "unwrapKey",
          .wrap.alg = alg_wrap_alg,
          .wrap.enc = alg_wrap_enc,
          .wrap.wrp = alg_wrap_wrp,
          .wrap.unw = alg_wrap_unw },
        { .kind = JOSE_HOOK_ALG_KIND_WRAP,
          .name = "A256GCMKW",
          .wrap.eprm = "wrapKey",
          .wrap.dprm = "unwrapKey",
          .wrap.alg = alg_wrap_alg,
          .wrap.enc = alg_wrap_enc,
          .wrap.wrp = alg_wrap_wrp,
          .wrap.unw = alg_wrap_unw },
        {}
    };

    jose_hook_jwk_push(&jwk);
    for (size_t i = 0; algs[i].name; i++)
        jose_hook_alg_push(&algs[i]);
}