7ef6aaa8d40bf86b23ad1b91a696dc52d0032b1d
[openssl.git] / crypto / dsa / dsa_ameth.c
1 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
2  * project 2006.
3  */
4 /* ====================================================================
5  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer. 
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    licensing@OpenSSL.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57
58 #include <stdio.h>
59 #include "cryptlib.h"
60 #include <openssl/x509.h>
61 #include <openssl/asn1.h>
62 #include <openssl/dsa.h>
63 #include "asn1_locl.h"
64
65 static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
66         {
67         const unsigned char *p, *pm;
68         int pklen, pmlen;
69         int ptype;
70         void *pval;
71         ASN1_STRING *pstr;
72         X509_ALGOR *palg;
73         ASN1_INTEGER *public_key = NULL;
74
75         DSA *dsa = NULL;
76
77         if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
78                 return 0;
79         X509_ALGOR_get0(NULL, &ptype, &pval, palg);
80
81         if (ptype != V_ASN1_SEQUENCE)
82                 {
83                 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
84                 goto err;
85                 }
86
87         pstr = pval;    
88         pm = pstr->data;
89         pmlen = pstr->length;
90
91         if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen)))
92                 {
93                 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
94                 goto err;
95                 }
96
97         if (!(public_key=d2i_ASN1_INTEGER(NULL, &p, pklen)))
98                 {
99                 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
100                 goto err;
101                 }
102
103         /* We have parameters now set public key */
104         if (!(dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)))
105                 {
106                 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
107                 goto err;
108                 }
109
110         ASN1_INTEGER_free(public_key);
111
112         return 1;
113
114         err:
115         if (pubkey)
116                 ASN1_INTEGER_free(public_key);
117         if (dsa)
118                 DSA_free(dsa);
119         return 0;
120
121         }
122
123 static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
124         {
125         DSA *dsa;
126         void *pval;
127         int ptype;
128         unsigned char *penc = NULL;
129         int penclen;
130
131         dsa=pkey->pkey.dsa;
132         if (pkey->save_parameters)
133                 {
134                 ASN1_STRING *str;
135                 str = ASN1_STRING_new();
136                 str->length = i2d_DSAparams(dsa, &str->data);
137                 if (str->length <= 0)
138                         {
139                         DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
140                         goto err;
141                         }
142                 ptype = V_ASN1_SEQUENCE;
143                 }
144         else
145                 {
146                 ptype = V_ASN1_UNDEF;
147                 pval = NULL;
148                 }
149         dsa->write_params=0;
150
151         penclen = i2d_DSAPublicKey(dsa, &penc);
152
153         if (penclen <= 0)
154                 {
155                 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
156                 goto err;
157                 }
158
159         if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA),
160                                 ptype, pval, penc, penclen))
161                 return 1;
162
163         err:
164         if (penc)
165                 OPENSSL_free(penc);
166         if (pval)
167                 ASN1_STRING_free(pval);
168
169         return 0;
170         }
171
172 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
173         {
174         if (BN_cmp(b->pkey.dsa->pub_key,a->pkey.dsa->pub_key) != 0)
175                 return 0;
176         else
177                 return 1;
178         }
179
180 /* In PKCS#8 DSA: you just get a private key integer and parameters in the
181  * AlgorithmIdentifier the pubkey must be recalculated.
182  */
183         
184 static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
185         {
186         const unsigned char *p, *pm;
187         int pklen, pmlen;
188         int ptype;
189         void *pval;
190         ASN1_STRING *pstr;
191         X509_ALGOR *palg;
192         ASN1_INTEGER *privkey = NULL;
193         BN_CTX *ctx = NULL;
194
195         STACK_OF(ASN1_TYPE) *ndsa = NULL;
196         DSA *dsa = NULL;
197
198         if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
199                 return 0;
200         X509_ALGOR_get0(NULL, &ptype, &pval, palg);
201
202         /* Check for broken DSA PKCS#8, UGH! */
203         if (*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED))
204                 {
205                 ASN1_TYPE *t1, *t2;
206                 if(!(ndsa = ASN1_seq_unpack_ASN1_TYPE(p, pklen, 
207                                                           d2i_ASN1_TYPE,
208                                                           ASN1_TYPE_free)))
209                         goto decerr;
210                 if (sk_ASN1_TYPE_num(ndsa) != 2)
211                         goto decerr;
212                 /* Handle Two broken types:
213                  * SEQUENCE {parameters, priv_key}
214                  * SEQUENCE {pub_key, priv_key}
215                  */
216
217                 t1 = sk_ASN1_TYPE_value(ndsa, 0);
218                 t2 = sk_ASN1_TYPE_value(ndsa, 1);
219                 if (t1->type == V_ASN1_SEQUENCE)
220                         {
221                         p8->broken = PKCS8_EMBEDDED_PARAM;
222                         pval = t1->value.ptr;
223                         }
224                 else if (ptype == V_ASN1_SEQUENCE)
225                         p8->broken = PKCS8_NS_DB;
226                 else
227                         goto decerr;
228
229                 if (t2->type != V_ASN1_INTEGER)
230                         goto decerr;
231
232                 privkey = t2->value.integer;
233                 }
234         else
235                 {
236                 if (!(privkey=d2i_ASN1_INTEGER(NULL, &p, pklen)))
237                         goto decerr;
238                 if (ptype != V_ASN1_SEQUENCE)
239                         goto decerr;
240                 }
241
242         pstr = pval;    
243         pm = pstr->data;
244         pmlen = pstr->length;
245         if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen)))
246                 goto decerr;
247         /* We have parameters now set private key */
248         if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL)))
249                 {
250                 DSAerr(DSA_F_DSA_PRIV_DECODE,DSA_R_BN_ERROR);
251                 goto dsaerr;
252                 }
253         /* Calculate public key */
254         if (!(dsa->pub_key = BN_new()))
255                 {
256                 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
257                 goto dsaerr;
258                 }
259         if (!(ctx = BN_CTX_new()))
260                 {
261                 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
262                 goto dsaerr;
263                 }
264                         
265         if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx))
266                 {
267                 DSAerr(DSA_F_DSA_PRIV_DECODE,DSA_R_BN_ERROR);
268                 goto dsaerr;
269                 }
270
271         EVP_PKEY_assign_DSA(pkey, dsa);
272         BN_CTX_free (ctx);
273         if(ndsa)
274                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
275         else
276                 ASN1_INTEGER_free(privkey);
277
278         return 1;
279
280         decerr:
281         DSAerr(DSA_F_DSA_PRIV_DECODE, EVP_R_DECODE_ERROR);
282         dsaerr:
283         BN_CTX_free (ctx);
284         sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
285         DSA_free(dsa);
286         EVP_PKEY_free(pkey);
287         return 0;
288         }
289
290 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
291 {
292         ASN1_STRING *params = NULL;
293         ASN1_INTEGER *prkey = NULL;
294         unsigned char *dp = NULL;
295         int dplen;
296
297         params = ASN1_STRING_new();
298
299         if (!params)
300                 {
301                 DSAerr(DSA_F_DSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
302                 goto err;
303                 }
304
305         params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
306         if (params->length <= 0)
307                 {
308                 DSAerr(DSA_F_DSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
309                 goto err;
310                 }
311         params->type = V_ASN1_SEQUENCE;
312
313         /* Get private key into integer */
314         prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
315
316         if (!prkey)
317                 {
318                 DSAerr(DSA_F_DSA_PRIV_ENCODE,DSA_R_BN_ERROR);
319                 goto err;
320                 }
321
322         dplen = i2d_ASN1_INTEGER(prkey, &dp);
323
324         ASN1_INTEGER_free(prkey);
325
326         if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
327                                 V_ASN1_SEQUENCE, params, dp, dplen))
328                 goto err;
329
330         return 1;
331
332 err:
333         if (dp != NULL)
334                 OPENSSL_free(dp);
335         if (params != NULL)
336                 ASN1_STRING_free(params);
337         if (prkey != NULL)
338                 ASN1_INTEGER_free(prkey);
339         return 0;
340 }
341
342 static int int_dsa_size(const EVP_PKEY *pkey)
343         {
344         return(DSA_size(pkey->pkey.dsa));
345         }
346
347 static int dsa_bits(const EVP_PKEY *pkey)
348         {
349         return BN_num_bits(pkey->pkey.dsa->p);
350         }
351
352 static int dsa_missing_parameters(const EVP_PKEY *pkey)
353         {
354         DSA *dsa;
355         dsa=pkey->pkey.dsa;
356         if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
357                         return 1;
358         return 0;
359         }
360
361 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
362         {
363         BIGNUM *a;
364
365         if ((a=BN_dup(from->pkey.dsa->p)) == NULL)
366                 return 0;
367         if (to->pkey.dsa->p != NULL)
368                 BN_free(to->pkey.dsa->p);
369         to->pkey.dsa->p=a;
370
371         if ((a=BN_dup(from->pkey.dsa->q)) == NULL)
372                 return 0;
373         if (to->pkey.dsa->q != NULL)
374                 BN_free(to->pkey.dsa->q);
375         to->pkey.dsa->q=a;
376
377         if ((a=BN_dup(from->pkey.dsa->g)) == NULL)
378                 return 0;
379         if (to->pkey.dsa->g != NULL)
380                 BN_free(to->pkey.dsa->g);
381         to->pkey.dsa->g=a;
382         return 1;
383         }
384
385 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
386         {
387         if (    BN_cmp(a->pkey.dsa->p,b->pkey.dsa->p) ||
388                 BN_cmp(a->pkey.dsa->q,b->pkey.dsa->q) ||
389                 BN_cmp(a->pkey.dsa->g,b->pkey.dsa->g))
390                 return 0;
391         else
392                 return 1;
393         }
394
395 static void int_dsa_free(EVP_PKEY *pkey)
396         {
397         DSA_free(pkey->pkey.dsa);
398         }
399
400 static void update_buflen(const BIGNUM *b, size_t *pbuflen)
401         {
402         int i;
403         if (!b)
404                 return;
405         if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
406                         *pbuflen = i;
407         }
408
409 int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
410         {
411         unsigned char *m=NULL;
412         int ret=0;
413         size_t buf_len=0;
414         const char *ktype = NULL;
415
416         const BIGNUM *priv_key, *pub_key;
417
418         if (ptype == 2)
419                 priv_key = x->priv_key;
420         else
421                 priv_key = NULL;
422
423         if (ptype > 0)
424                 pub_key = x->pub_key;
425         else
426                 pub_key = NULL;
427
428         if (ptype == 2)
429                 ktype = "Private-Key";
430         else if (ptype == 1)
431                 ktype = "Public-Key";
432         else
433                 ktype = "DSA-Parameters";
434
435         if (x->p == NULL)
436                 {
437                 DSAerr(DSA_F_DSA_PRINT,DSA_R_MISSING_PARAMETERS);
438                 goto err;
439                 }
440
441         update_buflen(x->p, &buf_len);
442         update_buflen(x->q, &buf_len);
443         update_buflen(x->g, &buf_len);
444         update_buflen(priv_key, &buf_len);
445         update_buflen(pub_key, &buf_len);
446
447         m=(unsigned char *)OPENSSL_malloc(buf_len+10);
448         if (m == NULL)
449                 {
450                 DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);
451                 goto err;
452                 }
453
454         if (priv_key)
455                 {
456                 if(!BIO_indent(bp,off,128))
457                    goto err;
458                 if (BIO_printf(bp,"%s: (%d bit)\n",ktype, BN_num_bits(x->p))
459                         <= 0) goto err;
460                 }
461
462         if (!ASN1_bn_print(bp,"priv:",priv_key,m,off))
463                 goto err;
464         if (!ASN1_bn_print(bp,"pub: ",pub_key,m,off))
465                 goto err;
466         if (!ASN1_bn_print(bp,"P:   ",x->p,m,off)) goto err;
467         if (!ASN1_bn_print(bp,"Q:   ",x->q,m,off)) goto err;
468         if (!ASN1_bn_print(bp,"G:   ",x->g,m,off)) goto err;
469         ret=1;
470 err:
471         if (m != NULL) OPENSSL_free(m);
472         return(ret);
473         }
474
475
476 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
477                                                         ASN1_PCTX *ctx)
478         {
479         return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
480         }
481
482 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
483                                                         ASN1_PCTX *ctx)
484         {
485         return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
486         }
487
488
489 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
490                                                         ASN1_PCTX *ctx)
491         {
492         return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
493         }
494
495 /* NB these are sorted in pkey_id order, lowest first */
496
497 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = 
498         {
499
500                 {
501                 EVP_PKEY_DSA2,
502                 EVP_PKEY_DSA,
503                 ASN1_PKEY_ALIAS
504                 },
505
506                 {
507                 EVP_PKEY_DSA1,
508                 EVP_PKEY_DSA,
509                 ASN1_PKEY_ALIAS
510                 },
511
512                 {
513                 EVP_PKEY_DSA4,
514                 EVP_PKEY_DSA,
515                 ASN1_PKEY_ALIAS
516                 },
517
518                 {
519                 EVP_PKEY_DSA3,
520                 EVP_PKEY_DSA,
521                 ASN1_PKEY_ALIAS
522                 },
523
524                 {
525                 EVP_PKEY_DSA,
526                 EVP_PKEY_DSA,
527                 0,
528
529                 dsa_pub_decode,
530                 dsa_pub_encode,
531                 dsa_pub_cmp,
532                 dsa_pub_print,
533
534                 dsa_priv_decode,
535                 dsa_priv_encode,
536                 dsa_priv_print,
537
538                 int_dsa_size,
539                 dsa_bits,
540
541                 0,0,
542                 dsa_missing_parameters,
543                 dsa_copy_parameters,
544                 dsa_cmp_parameters,
545                 dsa_param_print,
546
547                 int_dsa_free,
548                 0
549                 }
550         };
551