1 /* crypto/ecdsa/ecs_asn1.c */
2 /* ====================================================================
3 * Copyright (c) 2000-2002 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
58 #include <openssl/asn1.h>
59 #include <openssl/asn1t.h>
61 typedef struct ecdsa_priv_key_st {
63 ECPKPARAMETERS *parameters;
64 ASN1_OBJECT *named_curve;
65 ASN1_OCTET_STRING *pub_key;
69 ASN1_SEQUENCE(ECDSA_SIG) = {
70 ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
71 ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
72 } ASN1_SEQUENCE_END(ECDSA_SIG)
74 DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
75 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
76 IMPLEMENT_ASN1_FUNCTIONS_const(ECDSA_SIG)
78 ASN1_SEQUENCE(ECDSAPrivateKey) = {
79 ASN1_SIMPLE(ECDSAPrivateKey, version, LONG),
80 ASN1_SIMPLE(ECDSAPrivateKey, parameters, ECPKPARAMETERS),
81 ASN1_SIMPLE(ECDSAPrivateKey, pub_key, ASN1_OCTET_STRING),
82 ASN1_SIMPLE(ECDSAPrivateKey, priv_key, BIGNUM)
83 } ASN1_SEQUENCE_END(ECDSAPrivateKey)
85 DECLARE_ASN1_FUNCTIONS_const(ECDSAPrivateKey)
86 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSAPrivateKey, ecdsaPrivateKey)
87 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(ECDSAPrivateKey, ECDSAPrivateKey, ECDSAPrivateKey)
88 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSAPrivateKey, ECDSAPrivateKey, ecdsaPrivateKey)
90 int i2d_ECDSAParameters(ECDSA *a, unsigned char **out)
94 ECDSAerr(ECDSA_F_I2D_ECDSAPARAMETERS,
95 ERR_R_PASSED_NULL_PARAMETER);
98 return i2d_ECPKParameters(a->group, out);
101 ECDSA *d2i_ECDSAParameters(ECDSA **a, const unsigned char **in, long len)
106 if (in == NULL || *in == NULL)
108 ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS,
109 ERR_R_PASSED_NULL_PARAMETER);
113 group = d2i_ECPKParameters(NULL, in, len);
117 ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS,
122 if (a == NULL || *a == NULL)
124 if ((ret = ECDSA_new()) == NULL)
126 ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS,
127 ERR_R_MALLOC_FAILURE);
137 EC_GROUP_clear_free(ret->group);
144 ECDSA *d2i_ECDSAPrivateKey(ECDSA **a, const unsigned char **in, long len)
148 ECDSAPrivateKey *priv_key=NULL;
150 if ((priv_key = ECDSAPrivateKey_new()) == NULL)
152 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
156 if ((priv_key = d2i_ecdsaPrivateKey(&priv_key, in, len)) == NULL)
158 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY,
159 ECDSA_R_D2I_ECDSA_PRIVATEKEY_FAILURE);
160 ECDSAPrivateKey_free(priv_key);
164 if (a == NULL || *a == NULL)
166 if ((ret = ECDSA_new()) == NULL)
168 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY,
169 ERR_R_MALLOC_FAILURE);
179 EC_GROUP_clear_free(ret->group);
181 ret->group = EC_ASN1_pkparameters2group(priv_key->parameters);
182 if (ret->group == NULL)
184 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
188 ret->version = priv_key->version;
189 if (priv_key->priv_key)
191 if ((ret->priv_key = BN_dup(priv_key->priv_key)) == NULL)
193 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY,
200 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY,
201 ECDSA_R_MISSING_PRIVATE_KEY);
205 if ((ret->pub_key = EC_POINT_new(ret->group)) == NULL)
207 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
211 if (!EC_POINT_oct2point(ret->group, ret->pub_key,
212 priv_key->pub_key->data, priv_key->pub_key->length, NULL))
214 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
222 if (ret) ECDSA_free(ret);
226 ECDSAPrivateKey_free(priv_key);
230 int i2d_ECDSAPrivateKey(ECDSA *a, unsigned char **out)
233 unsigned char *buffer=NULL;
235 ECDSAPrivateKey *priv_key=NULL;
237 if (a == NULL || a->group == NULL)
239 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
240 ERR_R_PASSED_NULL_PARAMETER);
244 if ((priv_key = ECDSAPrivateKey_new()) == NULL)
246 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
247 ERR_R_MALLOC_FAILURE);
251 if ((priv_key->parameters = EC_ASN1_group2pkparameters(a->group,
252 priv_key->parameters)) == NULL)
254 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
258 priv_key->version = a->version;
260 if (BN_copy(priv_key->priv_key, a->priv_key) == NULL)
262 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_BN_LIB);
266 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
267 ECDSA_get_conversion_form(a), NULL, 0, NULL);
268 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
270 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
273 if (!EC_POINT_point2oct(a->group, a->pub_key,
274 ECDSA_get_conversion_form(a), buffer, buf_len, NULL))
276 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
279 if (!M_ASN1_OCTET_STRING_set(priv_key->pub_key, buffer, buf_len))
281 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_ASN1_LIB);
284 if ((ret = i2d_ecdsaPrivateKey(priv_key, out)) == 0)
286 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
287 ECDSA_R_I2D_ECDSA_PRIVATEKEY);
294 OPENSSL_free(buffer);
296 ECDSAPrivateKey_free(priv_key);
301 ECDSA *ECDSAPublicKey_set_octet_string(ECDSA **a, const unsigned char **in, long len)
305 if (a == NULL || (*a) == NULL || (*a)->group == NULL)
307 /* sorry, but a EC_GROUP-structur is necessary
308 * to set the public key */
309 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ECDSA_R_MISSING_PARAMETERS);
313 if (ret->pub_key == NULL && (ret->pub_key = EC_POINT_new(ret->group)) == NULL)
315 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
318 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL))
320 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
323 ECDSA_set_conversion_form(ret, (point_conversion_form_t)(*in[0] & ~0x01));
327 int ECDSAPublicKey_get_octet_string(ECDSA *a, unsigned char **out)
333 ECDSAerr(ECDSA_F_I2D_ECDSAPUBLICKEY, ECDSA_R_MISSING_PARAMETERS);
336 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
337 ECDSA_get_conversion_form(a), NULL, 0, NULL);
338 if (out == NULL || buf_len == 0)
339 /* out == NULL => just return the length of the octet string */
342 if ((*out = OPENSSL_malloc(buf_len)) == NULL)
344 ECDSAerr(ECDSA_F_I2D_ECDSAPUBLICKEY, ERR_R_MALLOC_FAILURE);
347 if (!EC_POINT_point2oct(a->group, a->pub_key, ECDSA_get_conversion_form(a),
348 *out, buf_len, NULL))
350 ECDSAerr(ECDSA_F_I2D_ECDSAPUBLICKEY, ERR_R_EC_LIB);