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