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).
57 #include <openssl/err.h>
58 #include <openssl/asn1t.h>
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)
65 DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
66 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
67 IMPLEMENT_ASN1_FUNCTIONS_const(ECDSA_SIG)
69 int i2d_ECDSAParameters(ECDSA *a, unsigned char **out)
73 ECDSAerr(ECDSA_F_I2D_ECDSAPARAMETERS,
74 ERR_R_PASSED_NULL_PARAMETER);
77 return i2d_ECPKParameters(a->group, out);
80 ECDSA *d2i_ECDSAParameters(ECDSA **a, const unsigned char **in, long len)
85 if (in == NULL || *in == NULL)
87 ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS,
88 ERR_R_PASSED_NULL_PARAMETER);
92 group = d2i_ECPKParameters(NULL, in, len);
96 ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS,
101 if (a == NULL || *a == NULL)
103 if ((ret = ECDSA_new()) == NULL)
105 ECDSAerr(ECDSA_F_D2I_ECDSAPARAMETERS,
106 ERR_R_MALLOC_FAILURE);
116 EC_GROUP_clear_free(ret->group);
123 ECDSA *d2i_ECDSAPrivateKey(ECDSA **a, const unsigned char **in, long len)
127 EC_PRIVATEKEY *priv_key=NULL;
129 if ((priv_key = EC_PRIVATEKEY_new()) == NULL)
131 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
135 if ((priv_key = d2i_EC_PRIVATEKEY(&priv_key, in, len)) == NULL)
137 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
138 EC_PRIVATEKEY_free(priv_key);
142 if (a == NULL || *a == NULL)
144 if ((ret = ECDSA_new()) == NULL)
146 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY,
147 ERR_R_MALLOC_FAILURE);
156 if (priv_key->parameters)
159 EC_GROUP_clear_free(ret->group);
160 ret->group = EC_ASN1_pkparameters2group(priv_key->parameters);
163 if (ret->group == NULL)
165 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
169 ret->version = priv_key->version;
171 if (priv_key->privateKey)
173 ret->priv_key = BN_bin2bn(
174 M_ASN1_STRING_data(priv_key->privateKey),
175 M_ASN1_STRING_length(priv_key->privateKey),
177 if (ret->priv_key == NULL)
179 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY,
186 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY,
187 ECDSA_R_MISSING_PRIVATE_KEY);
191 if (priv_key->publicKey)
194 EC_POINT_clear_free(ret->pub_key);
195 ret->pub_key = EC_POINT_new(ret->group);
196 if (ret->pub_key == NULL)
198 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
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))
205 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
220 EC_PRIVATEKEY_free(priv_key);
225 int i2d_ECDSAPrivateKey(ECDSA *a, unsigned char **out)
228 unsigned char *buffer=NULL;
229 size_t buf_len=0, tmp_len;
230 EC_PRIVATEKEY *priv_key=NULL;
232 if (a == NULL || a->group == NULL || a->priv_key == NULL)
234 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
235 ERR_R_PASSED_NULL_PARAMETER);
239 if ((priv_key = EC_PRIVATEKEY_new()) == NULL)
241 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
242 ERR_R_MALLOC_FAILURE);
246 priv_key->version = a->version;
248 buf_len = (size_t)BN_num_bytes(a->priv_key);
249 buffer = OPENSSL_malloc(buf_len);
252 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
253 ERR_R_MALLOC_FAILURE);
257 if (!BN_bn2bin(a->priv_key, buffer))
259 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_BN_LIB);
263 if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len))
265 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_ASN1_LIB);
269 if (!(ECDSA_get_enc_flag(a) & ECDSA_PKEY_NO_PARAMETERS))
271 if ((priv_key->parameters = EC_ASN1_group2pkparameters(
272 a->group, priv_key->parameters)) == NULL)
274 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
279 if (!(ECDSA_get_enc_flag(a) & ECDSA_PKEY_NO_PUBKEY))
281 priv_key->publicKey = M_ASN1_BIT_STRING_new();
282 if (priv_key->publicKey == NULL)
284 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
285 ERR_R_MALLOC_FAILURE);
289 tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
290 ECDSA_get_conversion_form(a), NULL, 0, NULL);
292 if (tmp_len > buf_len)
293 buffer = OPENSSL_realloc(buffer, tmp_len);
296 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY,
297 ERR_R_MALLOC_FAILURE);
303 if (!EC_POINT_point2oct(a->group, a->pub_key,
304 ECDSA_get_conversion_form(a), buffer, buf_len, NULL))
306 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
310 if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer,
313 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_ASN1_LIB);
318 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0)
320 ECDSAerr(ECDSA_F_I2D_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
326 OPENSSL_free(buffer);
328 EC_PRIVATEKEY_free(priv_key);
333 ECDSA *ECDSAPublicKey_set_octet_string(ECDSA **a, const unsigned char **in, long len)
337 if (a == NULL || (*a) == NULL || (*a)->group == NULL)
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);
345 if (ret->pub_key == NULL && (ret->pub_key = EC_POINT_new(ret->group)) == NULL)
347 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
350 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL))
352 ECDSAerr(ECDSA_F_D2I_ECDSAPRIVATEKEY, ERR_R_EC_LIB);
355 ECDSA_set_conversion_form(ret, (point_conversion_form_t)(*in[0] & ~0x01));
359 int ECDSAPublicKey_get_octet_string(ECDSA *a, unsigned char **out)
365 ECDSAerr(ECDSA_F_I2D_ECDSAPUBLICKEY, ECDSA_R_MISSING_PARAMETERS);
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 */
374 if ((*out = OPENSSL_malloc(buf_len)) == NULL)
376 ECDSAerr(ECDSA_F_I2D_ECDSAPUBLICKEY, ERR_R_MALLOC_FAILURE);
379 if (!EC_POINT_point2oct(a->group, a->pub_key, ECDSA_get_conversion_form(a),
380 *out, buf_len, NULL))
382 ECDSAerr(ECDSA_F_I2D_ECDSAPUBLICKEY, ERR_R_EC_LIB);