ECDSA representation bugfixes
[openssl.git] / crypto / asn1 / x_pubkey.c
1 /* crypto/asn1/x_pubkey.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/asn1t.h>
62 #include <openssl/x509.h>
63
64 /* Minor tweak to operation: free up EVP_PKEY */
65 static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
66         {
67         if (operation == ASN1_OP_FREE_POST)
68                 {
69                 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
70                 EVP_PKEY_free(pubkey->pkey);
71                 }
72         return 1;
73         }
74
75 ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
76         ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
77         ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
78 } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
79
80 IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
81
82 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
83         {
84         int ok=0;
85         X509_PUBKEY *pk;
86         X509_ALGOR *a;
87         ASN1_OBJECT *o;
88         unsigned char *s,*p;
89         int i;
90
91         if (x == NULL) return(0);
92
93         if ((pk=X509_PUBKEY_new()) == NULL) goto err;
94         a=pk->algor;
95
96         /* set the algorithm id */
97         if ((o=OBJ_nid2obj(pkey->type)) == NULL) goto err;
98         ASN1_OBJECT_free(a->algorithm);
99         a->algorithm=o;
100
101         /* Set the parameter list */
102         if (!pkey->save_parameters || (pkey->type == EVP_PKEY_RSA))
103                 {
104                 if ((a->parameter == NULL) ||
105                         (a->parameter->type != V_ASN1_NULL))
106                         {
107                         ASN1_TYPE_free(a->parameter);
108                         a->parameter=ASN1_TYPE_new();
109                         a->parameter->type=V_ASN1_NULL;
110                         }
111                 }
112 #ifndef OPENSSL_NO_DSA
113         else if (pkey->type == EVP_PKEY_DSA)
114                 {
115                 unsigned char *pp;
116                 DSA *dsa;
117                 
118                 dsa=pkey->pkey.dsa;
119                 dsa->write_params=0;
120                 ASN1_TYPE_free(a->parameter);
121                 i=i2d_DSAparams(dsa,NULL);
122                 p=(unsigned char *)OPENSSL_malloc(i);
123                 pp=p;
124                 i2d_DSAparams(dsa,&pp);
125                 a->parameter=ASN1_TYPE_new();
126                 a->parameter->type=V_ASN1_SEQUENCE;
127                 a->parameter->value.sequence=ASN1_STRING_new();
128                 ASN1_STRING_set(a->parameter->value.sequence,p,i);
129                 OPENSSL_free(p);
130                 }
131 #endif
132 #ifndef OPENSSL_NO_ECDSA
133         else if (pkey->type == EVP_PKEY_ECDSA)
134                 {
135                 int nid=0;
136                 unsigned char *pp;
137                 ECDSA *ecdsa;
138                 
139                 ecdsa = pkey->pkey.ecdsa;
140                 ASN1_TYPE_free(a->parameter);
141
142                 if ((a->parameter = ASN1_TYPE_new()) == NULL)
143                         {
144                         X509err(X509_F_X509_PUBKEY_SET, ERR_R_ASN1_LIB);
145                         OPENSSL_free(p);
146                         goto err;
147                         }
148
149                 if ((ECDSA_get_parameter_flags(ecdsa) & ECDSA_FLAG_NAMED_CURVE) && (nid = EC_GROUP_get_nid(ecdsa->group)))
150                         {
151                         /* just set the OID */
152                         a->parameter->type = V_ASN1_OBJECT;
153                         a->parameter->value.object = OBJ_nid2obj(nid);
154                         }
155                 else /* explicit parameters */
156                         {
157                         if ((i = i2d_ECDSAParameters(ecdsa, NULL)) == 0)
158                                 {
159                                 X509err(X509_F_X509_PUBKEY_SET, ERR_R_ECDSA_LIB);
160                                 goto err;
161                                 }
162                         if ((p = (unsigned char *) OPENSSL_malloc(i)) == NULL)
163                                 {
164                                 X509err(X509_F_X509_PUBKEY_SET, ERR_R_MALLOC_FAILURE);
165                                 goto err;
166                                 }       
167                         pp = p;
168                         if (!i2d_ECDSAParameters(ecdsa, &pp))
169                                 {
170                                 X509err(X509_F_X509_PUBKEY_SET, ERR_R_ECDSA_LIB);
171                                 OPENSSL_free(p);
172                                 goto err;
173                                 }
174                         a->parameter->type = V_ASN1_SEQUENCE;
175                         if ((a->parameter->value.sequence = ASN1_STRING_new()) == NULL)
176                                 {
177                                 X509err(X509_F_X509_PUBKEY_SET, ERR_R_ASN1_LIB);
178                                 OPENSSL_free(p);
179                                 goto err;
180                                 }
181                         ASN1_STRING_set(a->parameter->value.sequence, p, i);
182                         OPENSSL_free(p);
183                         }
184                 }
185 #endif
186         else if (1)
187                 {
188                 X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM);
189                 goto err;
190                 }
191
192         if ((i=i2d_PublicKey(pkey,NULL)) <= 0) goto err;
193         if ((s=(unsigned char *)OPENSSL_malloc(i+1)) == NULL) goto err;
194         p=s;
195         i2d_PublicKey(pkey,&p);
196         if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err;
197         /* Set number of unused bits to zero */
198         pk->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
199         pk->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT;
200
201         OPENSSL_free(s);
202
203 #if 0
204         CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
205         pk->pkey=pkey;
206 #endif
207
208         if (*x != NULL)
209                 X509_PUBKEY_free(*x);
210
211         *x=pk;
212         pk=NULL;
213
214         ok=1;
215 err:
216         if (pk != NULL) X509_PUBKEY_free(pk);
217         return(ok);
218         }
219
220 EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
221         {
222         EVP_PKEY *ret=NULL;
223         long j;
224         int type;
225         unsigned char *p;
226 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
227         const unsigned char *cp;
228         X509_ALGOR *a;
229 #endif
230
231         if (key == NULL) goto err;
232
233         if (key->pkey != NULL)
234                 {
235                 CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
236                 return(key->pkey);
237                 }
238
239         if (key->public_key == NULL) goto err;
240
241         type=OBJ_obj2nid(key->algor->algorithm);
242         if ((ret = EVP_PKEY_new()) == NULL)
243                 {
244                 X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);
245                 goto err;
246                 }
247         ret->type = EVP_PKEY_type(type);
248
249         /* the parameters must be extracted before the public key (ECDSA!) */
250         
251 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
252         a=key->algor;
253 #endif
254
255         if (0)
256                 ;
257 #ifndef OPENSSL_NO_DSA
258         else if (ret->type == EVP_PKEY_DSA)
259                 {
260                 if (a->parameter && (a->parameter->type == V_ASN1_SEQUENCE))
261                         {
262                         if ((ret->pkey.dsa = DSA_new()) == NULL)
263                                 {
264                                 X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);
265                                 goto err;
266                                 }
267                         ret->pkey.dsa->write_params=0;
268                         cp=p=a->parameter->value.sequence->data;
269                         j=a->parameter->value.sequence->length;
270                         if (!d2i_DSAparams(&ret->pkey.dsa, &cp, (long)j))
271                                 goto err;
272                         }
273                 ret->save_parameters=1;
274                 }
275 #endif
276 #ifndef OPENSSL_NO_ECDSA
277         else if (ret->type == EVP_PKEY_ECDSA)
278                 {
279                 if (a->parameter && (a->parameter->type == V_ASN1_SEQUENCE))
280                         {
281                         /* type == V_ASN1_SEQUENCE => we have explicit parameters
282                          * (e.g. parameters in the X9_62_EC_PARAMETERS-structure )
283                          */
284                         if ((ret->pkey.ecdsa= ECDSA_new()) == NULL)
285                                 {
286                                 X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);
287                                 goto err;
288                                 }
289                         cp = p = a->parameter->value.sequence->data;
290                         j = a->parameter->value.sequence->length;
291                         if (!d2i_ECDSAParameters(&ret->pkey.ecdsa, &cp, (long)j))
292                                 {
293                                 X509err(X509_F_X509_PUBKEY_GET, ERR_R_ECDSA_LIB);
294                                 goto err;
295                                 }
296                         }
297                 else if (a->parameter && (a->parameter->type == V_ASN1_OBJECT))
298                         {
299                         /* type == V_ASN1_OBJECT => the parameters are given
300                          * by an asn1 OID
301                          */
302                         if (ret->pkey.ecdsa == NULL)
303                                 ret->pkey.ecdsa = ECDSA_new();
304                         if (ret->pkey.ecdsa->group)
305                                 EC_GROUP_free(ret->pkey.ecdsa->group);
306                         ret->pkey.ecdsa->parameter_flags |= ECDSA_FLAG_NAMED_CURVE;
307                         if ((ret->pkey.ecdsa->group = EC_GROUP_new_by_name(OBJ_obj2nid(a->parameter->value.object))) == NULL)
308                                 goto err;
309                         }
310                         /* the case implicitlyCA is currently not implemented */
311                 ret->save_parameters = 1;
312                 }
313 #endif
314
315         p=key->public_key->data;
316         j=key->public_key->length;
317         if ((ret = d2i_PublicKey(type, &ret, &p, (long)j)) == NULL)
318                 {
319                 X509err(X509_F_X509_PUBKEY_GET, X509_R_ERR_ASN1_LIB);
320                 goto err;
321                 }
322
323         key->pkey = ret;
324         CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);
325         return(ret);
326 err:
327         if (ret != NULL)
328                 EVP_PKEY_free(ret);
329         return(NULL);
330         }
331
332 /* Now two pseudo ASN1 routines that take an EVP_PKEY structure
333  * and encode or decode as X509_PUBKEY
334  */
335
336 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, unsigned char **pp,
337              long length)
338         {
339         X509_PUBKEY *xpk;
340         EVP_PKEY *pktmp;
341         xpk = d2i_X509_PUBKEY(NULL, pp, length);
342         if(!xpk) return NULL;
343         pktmp = X509_PUBKEY_get(xpk);
344         X509_PUBKEY_free(xpk);
345         if(!pktmp) return NULL;
346         if(a)
347                 {
348                 EVP_PKEY_free(*a);
349                 *a = pktmp;
350                 }
351         return pktmp;
352         }
353
354 int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
355         {
356         X509_PUBKEY *xpk=NULL;
357         int ret;
358         if(!a) return 0;
359         if(!X509_PUBKEY_set(&xpk, a)) return 0;
360         ret = i2d_X509_PUBKEY(xpk, pp);
361         X509_PUBKEY_free(xpk);
362         return ret;
363         }
364
365 /* The following are equivalents but which return RSA and DSA
366  * keys
367  */
368 #ifndef OPENSSL_NO_RSA
369 RSA *d2i_RSA_PUBKEY(RSA **a, unsigned char **pp,
370              long length)
371         {
372         EVP_PKEY *pkey;
373         RSA *key;
374         unsigned char *q;
375         q = *pp;
376         pkey = d2i_PUBKEY(NULL, &q, length);
377         if (!pkey) return NULL;
378         key = EVP_PKEY_get1_RSA(pkey);
379         EVP_PKEY_free(pkey);
380         if (!key) return NULL;
381         *pp = q;
382         if (a)
383                 {
384                 RSA_free(*a);
385                 *a = key;
386                 }
387         return key;
388         }
389
390 int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
391         {
392         EVP_PKEY *pktmp;
393         int ret;
394         if (!a) return 0;
395         pktmp = EVP_PKEY_new();
396         if (!pktmp)
397                 {
398                 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
399                 return 0;
400                 }
401         EVP_PKEY_set1_RSA(pktmp, a);
402         ret = i2d_PUBKEY(pktmp, pp);
403         EVP_PKEY_free(pktmp);
404         return ret;
405         }
406 #endif
407
408 #ifndef OPENSSL_NO_DSA
409 DSA *d2i_DSA_PUBKEY(DSA **a, unsigned char **pp,
410              long length)
411         {
412         EVP_PKEY *pkey;
413         DSA *key;
414         unsigned char *q;
415         q = *pp;
416         pkey = d2i_PUBKEY(NULL, &q, length);
417         if (!pkey) return NULL;
418         key = EVP_PKEY_get1_DSA(pkey);
419         EVP_PKEY_free(pkey);
420         if (!key) return NULL;
421         *pp = q;
422         if (a)
423                 {
424                 DSA_free(*a);
425                 *a = key;
426                 }
427         return key;
428         }
429
430 int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
431         {
432         EVP_PKEY *pktmp;
433         int ret;
434         if(!a) return 0;
435         pktmp = EVP_PKEY_new();
436         if(!pktmp)
437                 {
438                 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
439                 return 0;
440                 }
441         EVP_PKEY_set1_DSA(pktmp, a);
442         ret = i2d_PUBKEY(pktmp, pp);
443         EVP_PKEY_free(pktmp);
444         return ret;
445         }
446 #endif
447
448 #ifndef OPENSSL_NO_ECDSA
449 ECDSA *d2i_ECDSA_PUBKEY(ECDSA **a, unsigned char **pp, long length)
450         {
451         EVP_PKEY *pkey;
452         ECDSA *key;
453         unsigned char *q;
454         q = *pp;
455         pkey = d2i_PUBKEY(NULL, &q, length);
456         if (!pkey) return(NULL);
457         key = EVP_PKEY_get1_ECDSA(pkey);
458         EVP_PKEY_free(pkey);
459         if (!key)  return(NULL);
460         *pp = q;
461         if (a)
462                 {
463                 ECDSA_free(*a);
464                 *a = key;
465                 }
466         return(key);
467         }
468
469 int i2d_ECDSA_PUBKEY(ECDSA *a, unsigned char **pp)
470         {
471         EVP_PKEY *pktmp;
472         int ret;
473         if (!a) return(0);
474         if ((pktmp = EVP_PKEY_new()) == NULL)
475                 {
476                 ASN1err(ASN1_F_I2D_ECDSA_PUBKEY, ERR_R_MALLOC_FAILURE);
477                 return(0);
478                 }
479         EVP_PKEY_set1_ECDSA(pktmp, a);
480         ret = i2d_PUBKEY(pktmp, pp);
481         EVP_PKEY_free(pktmp);
482         return(ret);
483         }
484 #endif