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