Reorganize private crypto header files
[openssl.git] / crypto / dsa / dsa_ameth.c
1 /*
2  * Copyright 2006-2016 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 <stdio.h>
11 #include <openssl/x509.h>
12 #include <openssl/asn1.h>
13 #include <openssl/bn.h>
14 #include <openssl/cms.h>
15 #include <openssl/core_names.h>
16 #include "internal/cryptlib.h"
17 #include "crypto/asn1.h"
18 #include "crypto/evp.h"
19 #include "internal/param_build.h"
20 #include "dsa_locl.h"
21
22 static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
23 {
24     const unsigned char *p, *pm;
25     int pklen, pmlen;
26     int ptype;
27     const void *pval;
28     const ASN1_STRING *pstr;
29     X509_ALGOR *palg;
30     ASN1_INTEGER *public_key = NULL;
31
32     DSA *dsa = NULL;
33
34     if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
35         return 0;
36     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
37
38     if (ptype == V_ASN1_SEQUENCE) {
39         pstr = pval;
40         pm = pstr->data;
41         pmlen = pstr->length;
42
43         if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
44             DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
45             goto err;
46         }
47
48     } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
49         if ((dsa = DSA_new()) == NULL) {
50             DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
51             goto err;
52         }
53     } else {
54         DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
55         goto err;
56     }
57
58     if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
59         DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
60         goto err;
61     }
62
63     if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
64         DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
65         goto err;
66     }
67
68     dsa->dirty_cnt++;
69     ASN1_INTEGER_free(public_key);
70     EVP_PKEY_assign_DSA(pkey, dsa);
71     return 1;
72
73  err:
74     ASN1_INTEGER_free(public_key);
75     DSA_free(dsa);
76     return 0;
77
78 }
79
80 static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
81 {
82     DSA *dsa;
83     int ptype;
84     unsigned char *penc = NULL;
85     int penclen;
86     ASN1_STRING *str = NULL;
87     ASN1_INTEGER *pubint = NULL;
88     ASN1_OBJECT *aobj;
89
90     dsa = pkey->pkey.dsa;
91     if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) {
92         str = ASN1_STRING_new();
93         if (str == NULL) {
94             DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
95             goto err;
96         }
97         str->length = i2d_DSAparams(dsa, &str->data);
98         if (str->length <= 0) {
99             DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
100             goto err;
101         }
102         ptype = V_ASN1_SEQUENCE;
103     } else
104         ptype = V_ASN1_UNDEF;
105
106     pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
107
108     if (pubint == NULL) {
109         DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
110         goto err;
111     }
112
113     penclen = i2d_ASN1_INTEGER(pubint, &penc);
114     ASN1_INTEGER_free(pubint);
115
116     if (penclen <= 0) {
117         DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
118         goto err;
119     }
120
121     aobj = OBJ_nid2obj(EVP_PKEY_DSA);
122     if (aobj == NULL)
123         goto err;
124
125     if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
126         return 1;
127
128  err:
129     OPENSSL_free(penc);
130     ASN1_STRING_free(str);
131
132     return 0;
133 }
134
135 /*
136  * In PKCS#8 DSA: you just get a private key integer and parameters in the
137  * AlgorithmIdentifier the pubkey must be recalculated.
138  */
139
140 static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
141 {
142     const unsigned char *p, *pm;
143     int pklen, pmlen;
144     int ptype;
145     const void *pval;
146     const ASN1_STRING *pstr;
147     const X509_ALGOR *palg;
148     ASN1_INTEGER *privkey = NULL;
149     BN_CTX *ctx = NULL;
150
151     DSA *dsa = NULL;
152
153     int ret = 0;
154
155     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
156         return 0;
157     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
158
159     if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
160         goto decerr;
161     if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
162         goto decerr;
163
164     pstr = pval;
165     pm = pstr->data;
166     pmlen = pstr->length;
167     if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
168         goto decerr;
169     /* We have parameters now set private key */
170     if ((dsa->priv_key = BN_secure_new()) == NULL
171         || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
172         DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
173         goto dsaerr;
174     }
175     /* Calculate public key */
176     if ((dsa->pub_key = BN_new()) == NULL) {
177         DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
178         goto dsaerr;
179     }
180     if ((ctx = BN_CTX_new()) == NULL) {
181         DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
182         goto dsaerr;
183     }
184
185     BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);
186     if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
187         DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
188         goto dsaerr;
189     }
190
191     dsa->dirty_cnt++;
192     EVP_PKEY_assign_DSA(pkey, dsa);
193
194     ret = 1;
195     goto done;
196
197  decerr:
198     DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
199  dsaerr:
200     DSA_free(dsa);
201  done:
202     BN_CTX_free(ctx);
203     ASN1_STRING_clear_free(privkey);
204     return ret;
205 }
206
207 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
208 {
209     ASN1_STRING *params = NULL;
210     ASN1_INTEGER *prkey = NULL;
211     unsigned char *dp = NULL;
212     int dplen;
213
214     if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {
215         DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
216         goto err;
217     }
218
219     params = ASN1_STRING_new();
220
221     if (params == NULL) {
222         DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
223         goto err;
224     }
225
226     params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
227     if (params->length <= 0) {
228         DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
229         goto err;
230     }
231     params->type = V_ASN1_SEQUENCE;
232
233     /* Get private key into integer */
234     prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
235
236     if (!prkey) {
237         DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
238         goto err;
239     }
240
241     dplen = i2d_ASN1_INTEGER(prkey, &dp);
242
243     ASN1_STRING_clear_free(prkey);
244     prkey = NULL;
245
246     if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
247                          V_ASN1_SEQUENCE, params, dp, dplen))
248         goto err;
249
250     return 1;
251
252  err:
253     OPENSSL_free(dp);
254     ASN1_STRING_free(params);
255     ASN1_STRING_clear_free(prkey);
256     return 0;
257 }
258
259 static int int_dsa_size(const EVP_PKEY *pkey)
260 {
261     return DSA_size(pkey->pkey.dsa);
262 }
263
264 static int dsa_bits(const EVP_PKEY *pkey)
265 {
266     return DSA_bits(pkey->pkey.dsa);
267 }
268
269 static int dsa_security_bits(const EVP_PKEY *pkey)
270 {
271     return DSA_security_bits(pkey->pkey.dsa);
272 }
273
274 static int dsa_missing_parameters(const EVP_PKEY *pkey)
275 {
276     DSA *dsa;
277     dsa = pkey->pkey.dsa;
278     if (dsa == NULL || dsa->p == NULL || dsa->q == NULL || dsa->g == NULL)
279         return 1;
280     return 0;
281 }
282
283 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
284 {
285     BIGNUM *a;
286
287     if (to->pkey.dsa == NULL) {
288         to->pkey.dsa = DSA_new();
289         if (to->pkey.dsa == NULL)
290             return 0;
291     }
292
293     if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
294         return 0;
295     BN_free(to->pkey.dsa->p);
296     to->pkey.dsa->p = a;
297
298     if ((a = BN_dup(from->pkey.dsa->q)) == NULL)
299         return 0;
300     BN_free(to->pkey.dsa->q);
301     to->pkey.dsa->q = a;
302
303     if ((a = BN_dup(from->pkey.dsa->g)) == NULL)
304         return 0;
305     BN_free(to->pkey.dsa->g);
306     to->pkey.dsa->g = a;
307     to->pkey.dsa->dirty_cnt++;
308     return 1;
309 }
310
311 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
312 {
313     if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) ||
314         BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) ||
315         BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g))
316         return 0;
317     else
318         return 1;
319 }
320
321 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
322 {
323     if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0)
324         return 0;
325     else
326         return 1;
327 }
328
329 static void int_dsa_free(EVP_PKEY *pkey)
330 {
331     DSA_free(pkey->pkey.dsa);
332 }
333
334 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
335 {
336     int ret = 0;
337     const char *ktype = NULL;
338     const BIGNUM *priv_key, *pub_key;
339     int mod_len = 0;
340
341     if (x->p != NULL)
342         mod_len = BN_num_bits(x->p);
343
344     if (ptype == 2)
345         priv_key = x->priv_key;
346     else
347         priv_key = NULL;
348
349     if (ptype > 0)
350         pub_key = x->pub_key;
351     else
352         pub_key = NULL;
353
354     if (ptype == 2)
355         ktype = "Private-Key";
356     else if (ptype == 1)
357         ktype = "Public-Key";
358     else
359         ktype = "DSA-Parameters";
360
361     if (priv_key) {
362         if (!BIO_indent(bp, off, 128))
363             goto err;
364         if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p))
365             <= 0)
366             goto err;
367     } else {
368         if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
369             goto err;
370     }
371
372     if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
373         goto err;
374     if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
375         goto err;
376     if (!ASN1_bn_print(bp, "P:   ", x->p, NULL, off))
377         goto err;
378     if (!ASN1_bn_print(bp, "Q:   ", x->q, NULL, off))
379         goto err;
380     if (!ASN1_bn_print(bp, "G:   ", x->g, NULL, off))
381         goto err;
382     ret = 1;
383  err:
384     return ret;
385 }
386
387 static int dsa_param_decode(EVP_PKEY *pkey,
388                             const unsigned char **pder, int derlen)
389 {
390     DSA *dsa;
391
392     if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {
393         DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
394         return 0;
395     }
396     dsa->dirty_cnt++;
397     EVP_PKEY_assign_DSA(pkey, dsa);
398     return 1;
399 }
400
401 static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
402 {
403     return i2d_DSAparams(pkey->pkey.dsa, pder);
404 }
405
406 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
407                            ASN1_PCTX *ctx)
408 {
409     return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
410 }
411
412 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
413                          ASN1_PCTX *ctx)
414 {
415     return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
416 }
417
418 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
419                           ASN1_PCTX *ctx)
420 {
421     return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
422 }
423
424 static int old_dsa_priv_decode(EVP_PKEY *pkey,
425                                const unsigned char **pder, int derlen)
426 {
427     DSA *dsa;
428
429     if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
430         DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
431         return 0;
432     }
433     dsa->dirty_cnt++;
434     EVP_PKEY_assign_DSA(pkey, dsa);
435     return 1;
436 }
437
438 static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
439 {
440     return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
441 }
442
443 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
444                          const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
445 {
446     DSA_SIG *dsa_sig;
447     const unsigned char *p;
448
449     if (!sig) {
450         if (BIO_puts(bp, "\n") <= 0)
451             return 0;
452         else
453             return 1;
454     }
455     p = sig->data;
456     dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
457     if (dsa_sig) {
458         int rv = 0;
459         const BIGNUM *r, *s;
460
461         DSA_SIG_get0(dsa_sig, &r, &s);
462
463         if (BIO_write(bp, "\n", 1) != 1)
464             goto err;
465
466         if (!ASN1_bn_print(bp, "r:   ", r, NULL, indent))
467             goto err;
468         if (!ASN1_bn_print(bp, "s:   ", s, NULL, indent))
469             goto err;
470         rv = 1;
471  err:
472         DSA_SIG_free(dsa_sig);
473         return rv;
474     }
475     if (BIO_puts(bp, "\n") <= 0)
476         return 0;
477     return X509_signature_dump(bp, sig, indent);
478 }
479
480 static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
481 {
482     switch (op) {
483     case ASN1_PKEY_CTRL_PKCS7_SIGN:
484         if (arg1 == 0) {
485             int snid, hnid;
486             X509_ALGOR *alg1, *alg2;
487             PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
488             if (alg1 == NULL || alg1->algorithm == NULL)
489                 return -1;
490             hnid = OBJ_obj2nid(alg1->algorithm);
491             if (hnid == NID_undef)
492                 return -1;
493             if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
494                 return -1;
495             X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
496         }
497         return 1;
498 #ifndef OPENSSL_NO_CMS
499     case ASN1_PKEY_CTRL_CMS_SIGN:
500         if (arg1 == 0) {
501             int snid, hnid;
502             X509_ALGOR *alg1, *alg2;
503             CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
504             if (alg1 == NULL || alg1->algorithm == NULL)
505                 return -1;
506             hnid = OBJ_obj2nid(alg1->algorithm);
507             if (hnid == NID_undef)
508                 return -1;
509             if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
510                 return -1;
511             X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
512         }
513         return 1;
514
515     case ASN1_PKEY_CTRL_CMS_RI_TYPE:
516         *(int *)arg2 = CMS_RECIPINFO_NONE;
517         return 1;
518 #endif
519
520     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
521         *(int *)arg2 = NID_sha256;
522         return 1;
523
524     default:
525         return -2;
526
527     }
528
529 }
530
531 static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
532 {
533     return pkey->pkey.dsa->dirty_cnt;
534 }
535
536 static void *dsa_pkey_export_to(const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt)
537 {
538     DSA *dsa = pk->pkey.dsa;
539     OSSL_PARAM_BLD tmpl;
540     const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa);
541     const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa);
542     const BIGNUM *priv_key = DSA_get0_priv_key(dsa);
543     OSSL_PARAM *params;
544     void *provkey = NULL;
545
546     if (p == NULL || q == NULL || g == NULL)
547         return NULL;
548
549     ossl_param_bld_init(&tmpl);
550     if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_P, p)
551         || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_Q, q)
552         || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_G, g))
553         return NULL;
554
555     /*
556      * This may be used to pass domain parameters only without any key data -
557      * so "pub_key" is optional. We can never have a "priv_key" without a
558      * corresponding "pub_key" though.
559      */
560     if (pub_key != NULL) {
561         if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_DSA_PUB_KEY,
562                                     pub_key))
563             return NULL;
564
565         if (priv_key != NULL) {
566             if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_DSA_PRIV_KEY,
567                                         priv_key))
568                 return NULL;
569         }
570     }
571
572     params = ossl_param_bld_to_param(&tmpl);
573
574     /* We export, the provider imports */
575     provkey = evp_keymgmt_importkey(keymgmt, params);
576
577     ossl_param_bld_free(params);
578     return provkey;
579 }
580
581 /* NB these are sorted in pkey_id order, lowest first */
582
583 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
584
585     {
586      EVP_PKEY_DSA2,
587      EVP_PKEY_DSA,
588      ASN1_PKEY_ALIAS},
589
590     {
591      EVP_PKEY_DSA1,
592      EVP_PKEY_DSA,
593      ASN1_PKEY_ALIAS},
594
595     {
596      EVP_PKEY_DSA4,
597      EVP_PKEY_DSA,
598      ASN1_PKEY_ALIAS},
599
600     {
601      EVP_PKEY_DSA3,
602      EVP_PKEY_DSA,
603      ASN1_PKEY_ALIAS},
604
605     {
606      EVP_PKEY_DSA,
607      EVP_PKEY_DSA,
608      0,
609
610      "DSA",
611      "OpenSSL DSA method",
612
613      dsa_pub_decode,
614      dsa_pub_encode,
615      dsa_pub_cmp,
616      dsa_pub_print,
617
618      dsa_priv_decode,
619      dsa_priv_encode,
620      dsa_priv_print,
621
622      int_dsa_size,
623      dsa_bits,
624      dsa_security_bits,
625
626      dsa_param_decode,
627      dsa_param_encode,
628      dsa_missing_parameters,
629      dsa_copy_parameters,
630      dsa_cmp_parameters,
631      dsa_param_print,
632      dsa_sig_print,
633
634      int_dsa_free,
635      dsa_pkey_ctrl,
636      old_dsa_priv_decode,
637      old_dsa_priv_encode,
638
639      NULL, NULL, NULL,
640      NULL, NULL, NULL,
641      NULL, NULL, NULL, NULL,
642
643      dsa_pkey_dirty_cnt,
644      dsa_pkey_export_to
645     }
646 };