summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ec_key.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/ec/ec_key.c91
1 files changed, 70 insertions, 21 deletions
diff --git a/src/lib/libcrypto/ec/ec_key.c b/src/lib/libcrypto/ec/ec_key.c
index fcdf461d20..f57e078c7f 100644
--- a/src/lib/libcrypto/ec/ec_key.c
+++ b/src/lib/libcrypto/ec/ec_key.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_key.c,v 1.22 2018/11/09 23:39:45 tb Exp $ */ 1/* $OpenBSD: ec_key.c,v 1.23 2019/01/19 01:07:00 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -65,30 +65,18 @@
65 65
66#include <openssl/opensslconf.h> 66#include <openssl/opensslconf.h>
67 67
68#ifndef OPENSSL_NO_ENGINE
69#include <openssl/engine.h>
70#endif
71#include <openssl/err.h>
72
68#include "bn_lcl.h" 73#include "bn_lcl.h"
69#include "ec_lcl.h" 74#include "ec_lcl.h"
70#include <openssl/err.h>
71 75
72EC_KEY * 76EC_KEY *
73EC_KEY_new(void) 77EC_KEY_new(void)
74{ 78{
75 EC_KEY *ret; 79 return EC_KEY_new_method(NULL);
76
77 ret = malloc(sizeof(EC_KEY));
78 if (ret == NULL) {
79 ECerror(ERR_R_MALLOC_FAILURE);
80 return (NULL);
81 }
82 ret->version = 1;
83 ret->flags = 0;
84 ret->group = NULL;
85 ret->pub_key = NULL;
86 ret->priv_key = NULL;
87 ret->enc_flag = 0;
88 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
89 ret->references = 1;
90 ret->method_data = NULL;
91 return (ret);
92} 80}
93 81
94EC_KEY * 82EC_KEY *
@@ -102,6 +90,11 @@ EC_KEY_new_by_curve_name(int nid)
102 EC_KEY_free(ret); 90 EC_KEY_free(ret);
103 return NULL; 91 return NULL;
104 } 92 }
93 if (ret->meth->set_group != NULL &&
94 ret->meth->set_group(ret, ret->group) == 0) {
95 EC_KEY_free(ret);
96 return NULL;
97 }
105 return ret; 98 return ret;
106} 99}
107 100
@@ -117,6 +110,14 @@ EC_KEY_free(EC_KEY * r)
117 if (i > 0) 110 if (i > 0)
118 return; 111 return;
119 112
113 if (r->meth != NULL && r->meth->finish != NULL)
114 r->meth->finish(r);
115
116#ifndef OPENSSL_NO_ENGINE
117 ENGINE_finish(r->engine);
118#endif
119 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
120
120 EC_GROUP_free(r->group); 121 EC_GROUP_free(r->group);
121 EC_POINT_free(r->pub_key); 122 EC_POINT_free(r->pub_key);
122 BN_clear_free(r->priv_key); 123 BN_clear_free(r->priv_key);
@@ -135,6 +136,15 @@ EC_KEY_copy(EC_KEY * dest, const EC_KEY * src)
135 ECerror(ERR_R_PASSED_NULL_PARAMETER); 136 ECerror(ERR_R_PASSED_NULL_PARAMETER);
136 return NULL; 137 return NULL;
137 } 138 }
139 if (src->meth != dest->meth) {
140 if (dest->meth != NULL && dest->meth->finish != NULL)
141 dest->meth->finish(dest);
142#ifndef OPENSSL_NO_ENGINE
143 if (ENGINE_finish(dest->engine) == 0)
144 return 0;
145 dest->engine = NULL;
146#endif
147 }
138 /* copy the parameters */ 148 /* copy the parameters */
139 if (src->group) { 149 if (src->group) {
140 const EC_METHOD *meth = EC_GROUP_method_of(src->group); 150 const EC_METHOD *meth = EC_GROUP_method_of(src->group);
@@ -184,14 +194,32 @@ EC_KEY_copy(EC_KEY * dest, const EC_KEY * src)
184 dest->version = src->version; 194 dest->version = src->version;
185 dest->flags = src->flags; 195 dest->flags = src->flags;
186 196
197 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY, &dest->ex_data,
198 &((EC_KEY *)src)->ex_data)) /* XXX const */
199 return NULL;
200
201 if (src->meth != dest->meth) {
202#ifndef OPENSSL_NO_ENGINE
203 if (src->engine != NULL && ENGINE_init(src->engine) == 0)
204 return 0;
205 dest->engine = src->engine;
206#endif
207 dest->meth = src->meth;
208 }
209
210 if (src->meth != NULL && src->meth->copy != NULL &&
211 src->meth->copy(dest, src) == 0)
212 return 0;
213
187 return dest; 214 return dest;
188} 215}
189 216
190EC_KEY * 217EC_KEY *
191EC_KEY_dup(const EC_KEY * ec_key) 218EC_KEY_dup(const EC_KEY * ec_key)
192{ 219{
193 EC_KEY *ret = EC_KEY_new(); 220 EC_KEY *ret;
194 if (ret == NULL) 221
222 if ((ret = EC_KEY_new_method(ec_key->engine)) == NULL)
195 return NULL; 223 return NULL;
196 if (EC_KEY_copy(ret, ec_key) == NULL) { 224 if (EC_KEY_copy(ret, ec_key) == NULL) {
197 EC_KEY_free(ret); 225 EC_KEY_free(ret);
@@ -207,6 +235,18 @@ EC_KEY_up_ref(EC_KEY * r)
207 return ((i > 1) ? 1 : 0); 235 return ((i > 1) ? 1 : 0);
208} 236}
209 237
238int
239EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg)
240{
241 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
242}
243
244void *
245EC_KEY_get_ex_data(const EC_KEY *r, int idx)
246{
247 return CRYPTO_get_ex_data(&r->ex_data, idx);
248}
249
210int 250int
211EC_KEY_generate_key(EC_KEY *eckey) 251EC_KEY_generate_key(EC_KEY *eckey)
212{ 252{
@@ -407,6 +447,9 @@ EC_KEY_get0_group(const EC_KEY * key)
407int 447int
408EC_KEY_set_group(EC_KEY * key, const EC_GROUP * group) 448EC_KEY_set_group(EC_KEY * key, const EC_GROUP * group)
409{ 449{
450 if (key->meth->set_group != NULL &&
451 key->meth->set_group(key, group) == 0)
452 return 0;
410 EC_GROUP_free(key->group); 453 EC_GROUP_free(key->group);
411 key->group = EC_GROUP_dup(group); 454 key->group = EC_GROUP_dup(group);
412 return (key->group == NULL) ? 0 : 1; 455 return (key->group == NULL) ? 0 : 1;
@@ -421,6 +464,9 @@ EC_KEY_get0_private_key(const EC_KEY * key)
421int 464int
422EC_KEY_set_private_key(EC_KEY * key, const BIGNUM * priv_key) 465EC_KEY_set_private_key(EC_KEY * key, const BIGNUM * priv_key)
423{ 466{
467 if (key->meth->set_private != NULL &&
468 key->meth->set_private(key, priv_key) == 0)
469 return 0;
424 BN_clear_free(key->priv_key); 470 BN_clear_free(key->priv_key);
425 key->priv_key = BN_dup(priv_key); 471 key->priv_key = BN_dup(priv_key);
426 return (key->priv_key == NULL) ? 0 : 1; 472 return (key->priv_key == NULL) ? 0 : 1;
@@ -435,6 +481,9 @@ EC_KEY_get0_public_key(const EC_KEY * key)
435int 481int
436EC_KEY_set_public_key(EC_KEY * key, const EC_POINT * pub_key) 482EC_KEY_set_public_key(EC_KEY * key, const EC_POINT * pub_key)
437{ 483{
484 if (key->meth->set_public != NULL &&
485 key->meth->set_public(key, pub_key) == 0)
486 return 0;
438 EC_POINT_free(key->pub_key); 487 EC_POINT_free(key->pub_key);
439 key->pub_key = EC_POINT_dup(pub_key, key->group); 488 key->pub_key = EC_POINT_dup(pub_key, key->group);
440 return (key->pub_key == NULL) ? 0 : 1; 489 return (key->pub_key == NULL) ? 0 : 1;