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