Constify various mostly X509-related parameter types in crypto/ and apps/
[openssl.git] / crypto / evp / pkey_mac.c
1 /*
2  * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11 #include <openssl/err.h>
12 #include <openssl/evp.h>
13 #include <openssl/engine.h>
14 #include <openssl/params.h>
15 #include <openssl/core_names.h>
16 #include "crypto/evp.h"
17 #include "evp_local.h"
18
19 /* MAC PKEY context structure */
20
21 typedef struct {
22     EVP_MAC_CTX *ctx;
23
24     /*
25      * We know of two MAC types:
26      *
27      * 1. those who take a secret in raw form, i.e. raw data as a
28      *    ASN1_OCTET_STRING embedded in a EVP_PKEY.  So far, that's
29      *    all of them but CMAC.
30      * 2. those who take a secret with associated cipher in very generic
31      *    form, i.e. a complete EVP_MAC_CTX embedded in a PKEY.  So far,
32      *    only CMAC does this.
33      *
34      * (one might wonder why the second form isn't used for all)
35      */
36 #define MAC_TYPE_RAW    1   /* HMAC like MAC type (all but CMAC so far) */
37 #define MAC_TYPE_MAC    2   /* CMAC like MAC type (only CMAC known so far) */
38     int type;
39
40     /* The following is only used for MAC_TYPE_RAW implementations */
41     struct {
42         const EVP_MD *md;           /* temp storage of MD */
43         ASN1_OCTET_STRING ktmp;     /* temp storage for key */
44     } raw_data;
45 } MAC_PKEY_CTX;
46
47 static void pkey_mac_cleanup(EVP_PKEY_CTX *ctx);
48
49 static int pkey_mac_init(EVP_PKEY_CTX *ctx)
50 {
51     MAC_PKEY_CTX *hctx;
52     /* We're being smart and using the same base NIDs for PKEY and for MAC */
53     int nid = ctx->pmeth->pkey_id;
54     EVP_MAC *mac = EVP_MAC_fetch(ctx->libctx, OBJ_nid2sn(nid), ctx->propquery);
55
56     if (mac == NULL) {
57         EVPerr(EVP_F_PKEY_MAC_INIT, EVP_R_FETCH_FAILED);
58         return 0;
59     }
60     if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) {
61         EVPerr(EVP_F_PKEY_MAC_INIT, ERR_R_MALLOC_FAILURE);
62         return 0;
63     }
64
65     hctx->ctx = EVP_MAC_CTX_new(mac);
66     if (hctx->ctx == NULL) {
67         OPENSSL_free(hctx);
68         return 0;
69     }
70
71     if (nid == EVP_PKEY_CMAC) {
72         hctx->type = MAC_TYPE_MAC;
73     } else {
74         hctx->type = MAC_TYPE_RAW;
75         hctx->raw_data.ktmp.type = V_ASN1_OCTET_STRING;
76     }
77
78     pkey_mac_cleanup(ctx);
79     EVP_PKEY_CTX_set_data(ctx, hctx);
80     ctx->keygen_info_count = 0;
81
82     return 1;
83 }
84
85 static int pkey_mac_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
86 {
87     MAC_PKEY_CTX *sctx, *dctx;
88
89     sctx = EVP_PKEY_CTX_get_data(src);
90     if (sctx->ctx->data == NULL)
91         return 0;
92
93     dctx = OPENSSL_zalloc(sizeof(*dctx));
94     if (dctx == NULL) {
95         EVPerr(EVP_F_PKEY_MAC_COPY, ERR_R_MALLOC_FAILURE);
96         return 0;
97     }
98
99     EVP_PKEY_CTX_set_data(dst, dctx);
100     dst->keygen_info_count = 0;
101
102     dctx->ctx = EVP_MAC_CTX_dup(sctx->ctx);
103     if (dctx->ctx == NULL)
104         goto err;
105
106     /*
107      * Normally, nothing special would be done with the MAC method.  In
108      * this particular case, though, the MAC method was fetched internally
109      * by pkey_mac_init() above or by EVP_PKEY_new_CMAC_key() and passed
110      * via the EVP_MAC_CTX, so it is effectively like every new EVP_MAC_CTX
111      * fetches the MAC method anew in this case.  Therefore, its reference
112      * count must be adjusted here.
113      */
114     if (!EVP_MAC_up_ref(EVP_MAC_CTX_mac(dctx->ctx)))
115         goto err;
116
117     dctx->type = sctx->type;
118
119     switch (dctx->type) {
120     case MAC_TYPE_RAW:
121         dctx->raw_data.md = sctx->raw_data.md;
122         if (ASN1_STRING_get0_data(&sctx->raw_data.ktmp) != NULL &&
123             !ASN1_STRING_copy(&dctx->raw_data.ktmp, &sctx->raw_data.ktmp))
124             goto err;
125         break;
126     case MAC_TYPE_MAC:
127         /* Nothing more to do */
128         break;
129     default:
130         /* This should be dead code */
131         return 0;
132     }
133     return 1;
134  err:
135     pkey_mac_cleanup(dst);
136     return 0;
137 }
138
139 static void pkey_mac_cleanup(EVP_PKEY_CTX *ctx)
140 {
141     /*
142      * For the exact same reasons the MAC reference count is incremented
143      * in pkey_mac_copy() above, it must be explicitly freed here.
144      */
145
146     MAC_PKEY_CTX *hctx = ctx == NULL ? NULL : EVP_PKEY_CTX_get_data(ctx);
147
148     if (hctx != NULL) {
149         EVP_MAC *mac = EVP_MAC_CTX_mac(hctx->ctx);
150
151         switch (hctx->type) {
152         case MAC_TYPE_RAW:
153             OPENSSL_clear_free(hctx->raw_data.ktmp.data,
154                                hctx->raw_data.ktmp.length);
155             break;
156         }
157         EVP_MAC_CTX_free(hctx->ctx);
158         EVP_MAC_free(mac);
159         OPENSSL_free(hctx);
160         EVP_PKEY_CTX_set_data(ctx, NULL);
161     }
162 }
163
164 static int pkey_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
165 {
166     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
167     int nid = ctx->pmeth->pkey_id;
168
169     switch (hctx->type) {
170     case MAC_TYPE_RAW:
171         {
172             ASN1_OCTET_STRING *hkey = NULL;
173
174             if (!hctx->raw_data.ktmp.data)
175                 return 0;
176             hkey = ASN1_OCTET_STRING_dup(&hctx->raw_data.ktmp);
177             if (!hkey)
178                 return 0;
179             EVP_PKEY_assign(pkey, nid, hkey);
180         }
181         break;
182     case MAC_TYPE_MAC:
183         {
184             EVP_MAC_CTX *cmkey = EVP_MAC_CTX_dup(hctx->ctx);
185
186             if (cmkey == NULL)
187                 return 0;
188             if (!EVP_MAC_up_ref(EVP_MAC_CTX_mac(hctx->ctx)))
189                 return 0;
190             EVP_PKEY_assign(pkey, nid, cmkey);
191         }
192         break;
193     default:
194         /* This should be dead code */
195         return 0;
196     }
197
198     return 1;
199 }
200
201 static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
202 {
203     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
204
205     if (!EVP_MAC_update(hctx->ctx, data, count))
206         return 0;
207     return 1;
208 }
209
210 static int pkey_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
211 {
212     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
213     ASN1_OCTET_STRING *key = NULL;
214     int rv = 1;
215     /*
216      * For MACs with the EVP_PKEY_FLAG_SIGCTX_CUSTOM flag set and that
217      * gets the key passed as an ASN.1 OCTET STRING, we set the key here,
218      * as this may be only time it's set during a DigestSign.
219      *
220      * MACs that pass around the key in form of EVP_MAC_CTX are setting
221      * the key through other mechanisms.  (this is only CMAC for now)
222      */
223     int set_key =
224         hctx->type == MAC_TYPE_RAW
225         && (ctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) != 0;
226
227     if (set_key) {
228         if (!EVP_MAC_is_a(EVP_MAC_CTX_mac(hctx->ctx),
229                           OBJ_nid2sn(EVP_PKEY_id(EVP_PKEY_CTX_get0_pkey(ctx)))))
230             return 0;
231         key = EVP_PKEY_get0(EVP_PKEY_CTX_get0_pkey(ctx));
232         if (key == NULL)
233             return 0;
234     }
235
236     EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
237     EVP_MD_CTX_set_update_fn(mctx, int_update);
238
239     /* Some MACs don't support this control...  that's fine */
240     {
241         OSSL_PARAM params[3];
242         size_t params_n = 0;
243         int flags = EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT);
244
245         /* TODO(3.0) "flags" isn't quite right, i.e. a quick hack for now */
246         params[params_n++] =
247             OSSL_PARAM_construct_int(OSSL_MAC_PARAM_FLAGS, &flags);
248         if (set_key)
249             params[params_n++] =
250                 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
251                                                   key->data, key->length);
252         params[params_n++] = OSSL_PARAM_construct_end();
253         rv = EVP_MAC_CTX_set_params(hctx->ctx, params);
254     }
255     return rv;
256 }
257
258 static int pkey_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
259                              size_t *siglen, EVP_MD_CTX *mctx)
260 {
261     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
262
263     return EVP_MAC_final(hctx->ctx, sig, siglen, EVP_MAC_size(hctx->ctx));
264 }
265
266 static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
267 {
268     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
269
270     switch (type) {
271
272     case EVP_PKEY_CTRL_CIPHER:
273         switch (hctx->type) {
274         case MAC_TYPE_RAW:
275             return -2;       /* The raw types don't support ciphers */
276         case MAC_TYPE_MAC:
277             {
278                 OSSL_PARAM params[3];
279                 size_t params_n = 0;
280                 char *ciphname = (char *)OBJ_nid2sn(EVP_CIPHER_nid(p2));
281 #ifndef OPENSSL_NO_ENGINE
282                 char *engineid = (char *)ENGINE_get_id(ctx->engine);
283
284                 params[params_n++] =
285                     OSSL_PARAM_construct_utf8_string("engine", engineid, 0);
286 #endif
287                 params[params_n++] =
288                     OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
289                                                      ciphname, 0);
290                 params[params_n] = OSSL_PARAM_construct_end();
291
292                 if (!EVP_MAC_CTX_set_params(hctx->ctx, params)
293                     || !EVP_MAC_init(hctx->ctx))
294                     return 0;
295             }
296             break;
297         default:
298             /* This should be dead code */
299             return 0;
300         }
301         break;
302
303     case EVP_PKEY_CTRL_MD:
304         switch (hctx->type) {
305         case MAC_TYPE_RAW:
306             hctx->raw_data.md = p2;
307             break;
308         case MAC_TYPE_MAC: {
309                 EVP_MAC_CTX *new_mac_ctx;
310
311                 if (ctx->pkey == NULL)
312                     return 0;
313                 new_mac_ctx = EVP_MAC_CTX_dup(ctx->pkey->pkey.ptr);
314                 if (new_mac_ctx == NULL)
315                     return 0;
316                 EVP_MAC_CTX_free(hctx->ctx);
317                 hctx->ctx = new_mac_ctx;
318             }
319             break;
320         default:
321             /* This should be dead code */
322             return 0;
323         }
324         break;
325
326     case EVP_PKEY_CTRL_SET_DIGEST_SIZE:
327         {
328             OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
329             size_t size = (size_t)p1;
330             size_t verify = 0;
331
332             /*
333              * We verify that the length is actually set by getting back
334              * the same parameter and checking that it matches what we
335              * tried to set.
336              * TODO(3.0) when we have a more direct mechanism to check if
337              * a parameter was used, we must refactor this to use that.
338              */
339
340             params[0] =
341                 OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &size);
342
343             if (!EVP_MAC_CTX_set_params(hctx->ctx, params))
344                 return 0;
345
346             params[0] =
347                 OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &verify);
348
349             if (!EVP_MAC_CTX_get_params(hctx->ctx, params))
350                 return 0;
351
352             /*
353              * Since EVP_MAC_CTX_{get,set}_params() returned successfully,
354              * we can only assume that the size was ignored, i.e. this
355              * control is unsupported.
356              */
357             if (verify != size)
358                 return -2;
359         }
360         break;
361     case EVP_PKEY_CTRL_SET_MAC_KEY:
362         switch (hctx->type) {
363         case MAC_TYPE_RAW:
364             if ((!p2 && p1 > 0) || (p1 < -1))
365                 return 0;
366             if (!ASN1_OCTET_STRING_set(&hctx->raw_data.ktmp, p2, p1))
367                 return 0;
368             break;
369         case MAC_TYPE_MAC:
370             {
371                 OSSL_PARAM params[2];
372                 size_t params_n = 0;
373
374                 params[params_n++] =
375                     OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
376                                                       p2, p1);
377                 params[params_n] = OSSL_PARAM_construct_end();
378
379                 return EVP_MAC_CTX_set_params(hctx->ctx, params);
380             }
381             break;
382         default:
383             /* This should be dead code */
384             return 0;
385         }
386         break;
387
388     case EVP_PKEY_CTRL_DIGESTINIT:
389         switch (hctx->type) {
390         case MAC_TYPE_RAW:
391             /* Ensure that we have attached the implementation */
392             if (!EVP_MAC_init(hctx->ctx))
393                 return 0;
394             {
395                 ASN1_OCTET_STRING *key =
396                     (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
397                 OSSL_PARAM params[4];
398                 size_t params_n = 0;
399                 char *mdname =
400                     (char *)OBJ_nid2sn(EVP_MD_nid(hctx->raw_data.md));
401 #ifndef OPENSSL_NO_ENGINE
402                 char *engineid = ctx->engine == NULL
403                     ? NULL : (char *)ENGINE_get_id(ctx->engine);
404
405                 if (engineid != NULL)
406                     params[params_n++] =
407                         OSSL_PARAM_construct_utf8_string("engine", engineid, 0);
408 #endif
409                 params[params_n++] =
410                     OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
411                                                      mdname, 0);
412                 params[params_n++] =
413                     OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
414                                                       key->data, key->length);
415                 params[params_n] = OSSL_PARAM_construct_end();
416
417                 return EVP_MAC_CTX_set_params(hctx->ctx, params);
418             }
419             break;
420         case MAC_TYPE_MAC:
421             return -2;       /* The mac types don't support ciphers */
422         default:
423             /* This should be dead code */
424             return 0;
425         }
426         break;
427
428     default:
429         return -2;
430
431     }
432     return 1;
433 }
434
435 static int pkey_mac_ctrl_str(EVP_PKEY_CTX *ctx,
436                               const char *type, const char *value)
437 {
438     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
439     const EVP_MAC *mac = EVP_MAC_CTX_mac(hctx->ctx);
440     OSSL_PARAM params[2];
441     int ok = 0;
442
443     /*
444      * Translation of some control names that are equivalent to a single
445      * parameter name.
446      *
447      * "md" and "digest" are the same thing, we use the single "digest"
448      *
449      * "digestsize" was a setting control in siphash, but naming wise,
450      * it's really the same as "size".
451      */
452     if (strcmp(type, "md") == 0)
453         type = OSSL_MAC_PARAM_DIGEST;
454     else if (strcmp(type, "digestsize") == 0)
455         type = OSSL_MAC_PARAM_SIZE;
456
457     if (!OSSL_PARAM_allocate_from_text(&params[0],
458                                        EVP_MAC_settable_ctx_params(mac),
459                                        type, value, strlen(value) + 1, NULL))
460         return 0;
461     params[1] = OSSL_PARAM_construct_end();
462     ok = EVP_MAC_CTX_set_params(hctx->ctx, params);
463     OPENSSL_free(params[0].data);
464     return ok;
465 }
466
467 static const EVP_PKEY_METHOD cmac_pkey_meth = {
468     EVP_PKEY_CMAC,
469     EVP_PKEY_FLAG_SIGCTX_CUSTOM,
470     pkey_mac_init,
471     pkey_mac_copy,
472     pkey_mac_cleanup,
473
474     0, 0,
475
476     0,
477     pkey_mac_keygen,
478
479     0, 0,
480
481     0, 0,
482
483     0, 0,
484
485     pkey_mac_signctx_init,
486     pkey_mac_signctx,
487
488     0, 0,
489
490     0, 0,
491
492     0, 0,
493
494     0, 0,
495
496     pkey_mac_ctrl,
497     pkey_mac_ctrl_str
498 };
499
500 const EVP_PKEY_METHOD *cmac_pkey_method(void)
501 {
502     return &cmac_pkey_meth;
503 }
504
505 static const EVP_PKEY_METHOD hmac_pkey_meth = {
506     EVP_PKEY_HMAC,
507     0,
508     pkey_mac_init,
509     pkey_mac_copy,
510     pkey_mac_cleanup,
511
512     0, 0,
513
514     0,
515     pkey_mac_keygen,
516
517     0, 0,
518
519     0, 0,
520
521     0, 0,
522
523     pkey_mac_signctx_init,
524     pkey_mac_signctx,
525
526     0, 0,
527
528     0, 0,
529
530     0, 0,
531
532     0, 0,
533
534     pkey_mac_ctrl,
535     pkey_mac_ctrl_str
536 };
537
538 const EVP_PKEY_METHOD *hmac_pkey_method(void)
539 {
540     return &hmac_pkey_meth;
541 }
542
543 static const EVP_PKEY_METHOD siphash_pkey_meth = {
544     EVP_PKEY_SIPHASH,
545     EVP_PKEY_FLAG_SIGCTX_CUSTOM,
546     pkey_mac_init,
547     pkey_mac_copy,
548     pkey_mac_cleanup,
549
550     0, 0,
551
552     0,
553     pkey_mac_keygen,
554
555     0, 0,
556
557     0, 0,
558
559     0, 0,
560
561     pkey_mac_signctx_init,
562     pkey_mac_signctx,
563
564     0, 0,
565
566     0, 0,
567
568     0, 0,
569
570     0, 0,
571
572     pkey_mac_ctrl,
573     pkey_mac_ctrl_str
574 };
575
576 const EVP_PKEY_METHOD *siphash_pkey_method(void)
577 {
578     return &siphash_pkey_meth;
579 }
580
581 static const EVP_PKEY_METHOD poly1305_pkey_meth = {
582     EVP_PKEY_POLY1305,
583     EVP_PKEY_FLAG_SIGCTX_CUSTOM,
584     pkey_mac_init,
585     pkey_mac_copy,
586     pkey_mac_cleanup,
587
588     0, 0,
589
590     0,
591     pkey_mac_keygen,
592
593     0, 0,
594
595     0, 0,
596
597     0, 0,
598
599     pkey_mac_signctx_init,
600     pkey_mac_signctx,
601
602     0, 0,
603
604     0, 0,
605
606     0, 0,
607
608     0, 0,
609
610     pkey_mac_ctrl,
611     pkey_mac_ctrl_str
612 };
613
614 const EVP_PKEY_METHOD *poly1305_pkey_method(void)
615 {
616     return &poly1305_pkey_meth;
617 }