make update
[openssl.git] / crypto / ecdsa / ecs_asn1.c
1 /* crypto/ecdsa/ecs_asn1.c */
2 /* ====================================================================
3  * Copyright (c) 2000-2002 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer. 
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    licensing@OpenSSL.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This product includes cryptographic software written by Eric Young
51  * (eay@cryptsoft.com).  This product includes software written by Tim
52  * Hudson (tjh@cryptsoft.com).
53  *
54  */
55
56 #include "ecdsa.h"
57 #include <openssl/err.h>
58 #include <openssl/asn1t.h>
59
60 ASN1_SEQUENCE(ECDSA_SIG) = {
61         ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
62         ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
63 } ASN1_SEQUENCE_END(ECDSA_SIG)
64
65 DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
66 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
67 IMPLEMENT_ASN1_FUNCTIONS_const(ECDSA_SIG)
68
69 int i2d_ECDSAParameters(ECDSA *a, unsigned char **out)
70         {
71         if (a == NULL)
72                 {
73                 ECDSAerr(ECDSA_F_I2D_ECDSAPARAMETERS, 
74                          ERR_R_PASSED_NULL_PARAMETER);
75                 return 0;
76                 }
77         return i2d_ECPKParameters(a->group, out);
78         }
79
80 ECDSA *d2i_ECDSAParameters(ECDSA **a, const unsigned char **in, long len)
81         {
82         EC_GROUP *group;
83         ECDSA    *ret;
84
85         if (in == NULL || *in == NULL)
86                 {
87                 ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS, 
88                          ERR_R_PASSED_NULL_PARAMETER);
89                 return NULL;
90                 }
91
92         group = d2i_ECPKParameters(NULL, in, len);
93
94         if (group == NULL)
95                 {
96                 ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS, 
97                          ERR_R_EC_LIB);
98                 return NULL;
99                 }
100
101         if (a == NULL || *a == NULL)
102                 {
103                 if ((ret = ECDSA_new()) == NULL)
104                         {
105                         ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS, 
106                                  ERR_R_MALLOC_FAILURE);
107                         return NULL;
108                         }
109                 if (a)
110                         *a = ret;
111                 }
112         else
113                 ret = *a;
114
115         if (ret->group)
116                 EC_GROUP_clear_free(ret->group);
117
118         ret->group = group;
119         
120         return ret;
121         }
122
123 ECDSA *d2i_ECDSAPrivateKey(ECDSA **a, const unsigned char **in, long len)
124         {
125         int             ok=0;
126         ECDSA           *ret=NULL;
127         EC_PRIVATEKEY   *priv_key=NULL;
128
129         if ((priv_key = EC_PRIVATEKEY_new()) == NULL)
130                 {
131                 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
132                 return NULL;
133                 }
134
135         if ((priv_key = d2i_EC_PRIVATEKEY(&priv_key, in, len)) == NULL)
136                 {
137                 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
138                 EC_PRIVATEKEY_free(priv_key);
139                 return NULL;
140                 }
141
142         if (a == NULL || *a == NULL)
143                 {
144                 if ((ret = ECDSA_new()) == NULL)        
145                         {
146                         ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY,
147                                  ERR_R_MALLOC_FAILURE);
148                         goto err;
149                         }
150                 if (a)
151                         *a = ret;
152                 }
153         else
154                 ret = *a;
155
156         if (priv_key->parameters)
157                 {
158                 if (ret->group)
159                         EC_GROUP_clear_free(ret->group);
160                 ret->group = EC_ASN1_pkparameters2group(priv_key->parameters);
161                 }
162
163         if (ret->group == NULL)
164                 {
165                 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
166                 goto err;
167                 }
168
169         ret->version = priv_key->version;
170
171         if (priv_key->privateKey)
172                 {
173                 ret->priv_key = BN_bin2bn(
174                         M_ASN1_STRING_data(priv_key->privateKey),
175                         M_ASN1_STRING_length(priv_key->privateKey),
176                         ret->priv_key);
177                 if (ret->priv_key == NULL)
178                         {
179                         ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY,
180                                  ERR_R_BN_LIB);
181                         goto err;
182                         }
183                 }
184         else
185                 {
186                 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, 
187                          ECDSA_R_MISSING_PRIVATE_KEY);
188                 goto err;
189                 }
190
191         if (priv_key->publicKey)
192                 {
193                 if (ret->pub_key)
194                         EC_POINT_clear_free(ret->pub_key);
195                 ret->pub_key = EC_POINT_new(ret->group);
196                 if (ret->pub_key == NULL)
197                         {
198                         ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
199                         goto err;
200                         }
201                 if (!EC_POINT_oct2point(ret->group, ret->pub_key,
202                         M_ASN1_STRING_data(priv_key->publicKey),
203                         M_ASN1_STRING_length(priv_key->publicKey), NULL))
204                         {
205                         ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
206                         goto err;
207                         }
208                 }
209
210         ok = 1;
211 err:
212         if (!ok)
213                 {
214                 if (ret)
215                         ECDSA_free(ret);
216                 ret = NULL;
217                 }
218
219         if (priv_key)
220                 EC_PRIVATEKEY_free(priv_key);
221
222         return(ret);
223         }
224
225 int     i2d_ECDSAPrivateKey(ECDSA *a, unsigned char **out)
226         {
227         int             ret=0, ok=0;
228         unsigned char   *buffer=NULL;
229         size_t          buf_len=0, tmp_len;
230         EC_PRIVATEKEY   *priv_key=NULL;
231
232         if (a == NULL || a->group == NULL || a->priv_key == NULL)
233                 {
234                 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
235                          ERR_R_PASSED_NULL_PARAMETER);
236                 goto err;
237                 }
238
239         if ((priv_key = EC_PRIVATEKEY_new()) == NULL)
240                 {
241                 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
242                          ERR_R_MALLOC_FAILURE);
243                 goto err;
244                 }
245
246         priv_key->version = a->version;
247
248         buf_len = (size_t)BN_num_bytes(a->priv_key);
249         buffer = OPENSSL_malloc(buf_len);
250         if (buffer == NULL)
251                 {
252                 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
253                          ERR_R_MALLOC_FAILURE);
254                 goto err;
255                 }
256         
257         if (!BN_bn2bin(a->priv_key, buffer))
258                 {
259                 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_BN_LIB);
260                 goto err;
261                 }
262
263         if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len))
264                 {
265                 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_ASN1_LIB);
266                 goto err;
267                 }       
268
269         if (!(ECDSA_get_enc_flag(a) & ECDSA_PKEY_NO_PARAMETERS))
270                 {
271                 if ((priv_key->parameters = EC_ASN1_group2pkparameters(
272                         a->group, priv_key->parameters)) == NULL)
273                         {
274                         ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
275                         goto err;
276                         }
277                 }
278
279         if (!(ECDSA_get_enc_flag(a) & ECDSA_PKEY_NO_PUBKEY))
280                 {
281                 priv_key->publicKey = M_ASN1_BIT_STRING_new();
282                 if (priv_key->publicKey == NULL)
283                         {
284                         ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
285                                 ERR_R_MALLOC_FAILURE);
286                         goto err;
287                         }
288
289                 tmp_len = EC_POINT_point2oct(a->group, a->pub_key, 
290                            ECDSA_get_conversion_form(a), NULL, 0, NULL);
291
292                 if (tmp_len > buf_len)
293                         buffer = OPENSSL_realloc(buffer, tmp_len);
294                 if (buffer == NULL)
295                         {
296                         ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
297                                 ERR_R_MALLOC_FAILURE);
298                         goto err;
299                         }
300
301                 buf_len = tmp_len;
302
303                 if (!EC_POINT_point2oct(a->group, a->pub_key, 
304                         ECDSA_get_conversion_form(a), buffer, buf_len, NULL))
305                         {
306                         ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
307                         goto err;
308                         }
309
310                 if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer, 
311                                 buf_len))
312                         {
313                         ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_ASN1_LIB);
314                         goto err;
315                         }
316                 }
317
318         if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0)
319                 {
320                 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
321                 goto err;
322                 }
323         ok=1;
324 err:
325         if (buffer)
326                 OPENSSL_free(buffer);
327         if (priv_key)
328                 EC_PRIVATEKEY_free(priv_key);
329         return(ok?ret:0);
330         }
331
332
333 ECDSA   *ECDSAPublicKey_set_octet_string(ECDSA **a, const unsigned char **in, long len)
334 {
335         ECDSA *ret=NULL;
336
337         if (a == NULL || (*a) == NULL || (*a)->group == NULL)
338         {
339                 /* sorry, but a EC_GROUP-structur is necessary
340                  * to set the public key */
341                 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ECDSA_R_MISSING_PARAMETERS);
342                 return 0;
343         }
344         ret = *a;
345         if (ret->pub_key == NULL && (ret->pub_key = EC_POINT_new(ret->group)) == NULL)
346         {
347                 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
348                 return 0;
349         }
350         if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL))
351         {
352                 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
353                 return 0;
354         }
355         ECDSA_set_conversion_form(ret, (point_conversion_form_t)(*in[0] & ~0x01));
356         return ret;
357 }
358
359 int     ECDSAPublicKey_get_octet_string(ECDSA *a, unsigned char **out)
360 {
361         size_t  buf_len=0;
362
363         if (a == NULL) 
364         {
365                 ECDSAerr(ECDSA_F_I2D_ECDSAPUBLICKEY, ECDSA_R_MISSING_PARAMETERS);
366                 return 0;
367         }
368         buf_len = EC_POINT_point2oct(a->group, a->pub_key, 
369                               ECDSA_get_conversion_form(a), NULL, 0, NULL);
370         if (out == NULL || buf_len == 0)
371         /* out == NULL => just return the length of the octet string */
372                 return buf_len;
373         if (*out == NULL)
374                 if ((*out = OPENSSL_malloc(buf_len)) == NULL)
375                 {
376                         ECDSAerr(ECDSA_F_I2D_ECDSAPUBLICKEY, ERR_R_MALLOC_FAILURE);
377                         return 0;
378                 }
379         if (!EC_POINT_point2oct(a->group, a->pub_key, ECDSA_get_conversion_form(a),
380                                 *out, buf_len, NULL))
381         {
382                 ECDSAerr(ECDSA_F_I2D_ECDSAPUBLICKEY, ERR_R_EC_LIB);
383                 OPENSSL_free(*out);
384                 *out = NULL;
385                 return 0;
386         }
387         return buf_len;
388 }