summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/x_pubkey.c
diff options
context:
space:
mode:
authortedu <>2014-04-18 00:58:49 +0000
committertedu <>2014-04-18 00:58:49 +0000
commita1df5b66c7ad477a535e56a504d8da47e1998cfa (patch)
treeb216ca6fba673789f2c494699c4cf41d23337bfa /src/lib/libcrypto/asn1/x_pubkey.c
parent07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d (diff)
downloadopenbsd-a1df5b66c7ad477a535e56a504d8da47e1998cfa.tar.gz
openbsd-a1df5b66c7ad477a535e56a504d8da47e1998cfa.tar.bz2
openbsd-a1df5b66c7ad477a535e56a504d8da47e1998cfa.zip
lob a few more knf grenades in here to soften things up.
Diffstat (limited to 'src/lib/libcrypto/asn1/x_pubkey.c')
-rw-r--r--src/lib/libcrypto/asn1/x_pubkey.c73
1 files changed, 23 insertions, 50 deletions
diff --git a/src/lib/libcrypto/asn1/x_pubkey.c b/src/lib/libcrypto/asn1/x_pubkey.c
index 454d50f24c..1d6ab6580a 100644
--- a/src/lib/libcrypto/asn1/x_pubkey.c
+++ b/src/lib/libcrypto/asn1/x_pubkey.c
@@ -72,8 +72,7 @@
72static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 72static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
73 void *exarg) 73 void *exarg)
74{ 74{
75 if (operation == ASN1_OP_FREE_POST) 75 if (operation == ASN1_OP_FREE_POST) {
76 {
77 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; 76 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
78 EVP_PKEY_free(pubkey->pkey); 77 EVP_PKEY_free(pubkey->pkey);
79 } 78 }
@@ -95,26 +94,19 @@ int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
95 94
96 if ((pk=X509_PUBKEY_new()) == NULL) goto error; 95 if ((pk=X509_PUBKEY_new()) == NULL) goto error;
97 96
98 if (pkey->ameth) 97 if (pkey->ameth) {
99 { 98 if (pkey->ameth->pub_encode) {
100 if (pkey->ameth->pub_encode) 99 if (!pkey->ameth->pub_encode(pk, pkey)) {
101 {
102 if (!pkey->ameth->pub_encode(pk, pkey))
103 {
104 X509err(X509_F_X509_PUBKEY_SET, 100 X509err(X509_F_X509_PUBKEY_SET,
105 X509_R_PUBLIC_KEY_ENCODE_ERROR); 101 X509_R_PUBLIC_KEY_ENCODE_ERROR);
106 goto error; 102 goto error;
107 } 103 }
108 } 104 } else {
109 else
110 {
111 X509err(X509_F_X509_PUBKEY_SET, 105 X509err(X509_F_X509_PUBKEY_SET,
112 X509_R_METHOD_NOT_SUPPORTED); 106 X509_R_METHOD_NOT_SUPPORTED);
113 goto error; 107 goto error;
114 } 108 }
115 } 109 } else {
116 else
117 {
118 X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM); 110 X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM);
119 goto error; 111 goto error;
120 } 112 }
@@ -136,51 +128,41 @@ EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
136 128
137 if (key == NULL) goto error; 129 if (key == NULL) goto error;
138 130
139 if (key->pkey != NULL) 131 if (key->pkey != NULL) {
140 {
141 CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); 132 CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
142 return key->pkey; 133 return key->pkey;
143 } 134 }
144 135
145 if (key->public_key == NULL) goto error; 136 if (key->public_key == NULL) goto error;
146 137
147 if ((ret = EVP_PKEY_new()) == NULL) 138 if ((ret = EVP_PKEY_new()) == NULL) {
148 {
149 X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE); 139 X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);
150 goto error; 140 goto error;
151 } 141 }
152 142
153 if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) 143 if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) {
154 {
155 X509err(X509_F_X509_PUBKEY_GET,X509_R_UNSUPPORTED_ALGORITHM); 144 X509err(X509_F_X509_PUBKEY_GET,X509_R_UNSUPPORTED_ALGORITHM);
156 goto error; 145 goto error;
157 } 146 }
158 147
159 if (ret->ameth->pub_decode) 148 if (ret->ameth->pub_decode) {
160 { 149 if (!ret->ameth->pub_decode(ret, key)) {
161 if (!ret->ameth->pub_decode(ret, key))
162 {
163 X509err(X509_F_X509_PUBKEY_GET, 150 X509err(X509_F_X509_PUBKEY_GET,
164 X509_R_PUBLIC_KEY_DECODE_ERROR); 151 X509_R_PUBLIC_KEY_DECODE_ERROR);
165 goto error; 152 goto error;
166 } 153 }
167 } 154 } else {
168 else
169 {
170 X509err(X509_F_X509_PUBKEY_GET, X509_R_METHOD_NOT_SUPPORTED); 155 X509err(X509_F_X509_PUBKEY_GET, X509_R_METHOD_NOT_SUPPORTED);
171 goto error; 156 goto error;
172 } 157 }
173 158
174 /* Check to see if another thread set key->pkey first */ 159 /* Check to see if another thread set key->pkey first */
175 CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY); 160 CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
176 if (key->pkey) 161 if (key->pkey) {
177 {
178 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY); 162 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
179 EVP_PKEY_free(ret); 163 EVP_PKEY_free(ret);
180 ret = key->pkey; 164 ret = key->pkey;
181 } 165 } else {
182 else
183 {
184 key->pkey = ret; 166 key->pkey = ret;
185 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY); 167 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
186 } 168 }
@@ -208,8 +190,7 @@ EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp,
208 pktmp = X509_PUBKEY_get(xpk); 190 pktmp = X509_PUBKEY_get(xpk);
209 X509_PUBKEY_free(xpk); 191 X509_PUBKEY_free(xpk);
210 if(!pktmp) return NULL; 192 if(!pktmp) return NULL;
211 if(a) 193 if(a) {
212 {
213 EVP_PKEY_free(*a); 194 EVP_PKEY_free(*a);
214 *a = pktmp; 195 *a = pktmp;
215 } 196 }
@@ -244,8 +225,7 @@ RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp,
244 EVP_PKEY_free(pkey); 225 EVP_PKEY_free(pkey);
245 if (!key) return NULL; 226 if (!key) return NULL;
246 *pp = q; 227 *pp = q;
247 if (a) 228 if (a) {
248 {
249 RSA_free(*a); 229 RSA_free(*a);
250 *a = key; 230 *a = key;
251 } 231 }
@@ -258,8 +238,7 @@ int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
258 int ret; 238 int ret;
259 if (!a) return 0; 239 if (!a) return 0;
260 pktmp = EVP_PKEY_new(); 240 pktmp = EVP_PKEY_new();
261 if (!pktmp) 241 if (!pktmp) {
262 {
263 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE); 242 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
264 return 0; 243 return 0;
265 } 244 }
@@ -284,8 +263,7 @@ DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp,
284 EVP_PKEY_free(pkey); 263 EVP_PKEY_free(pkey);
285 if (!key) return NULL; 264 if (!key) return NULL;
286 *pp = q; 265 *pp = q;
287 if (a) 266 if (a) {
288 {
289 DSA_free(*a); 267 DSA_free(*a);
290 *a = key; 268 *a = key;
291 } 269 }
@@ -298,8 +276,7 @@ int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
298 int ret; 276 int ret;
299 if(!a) return 0; 277 if(!a) return 0;
300 pktmp = EVP_PKEY_new(); 278 pktmp = EVP_PKEY_new();
301 if(!pktmp) 279 if(!pktmp) {
302 {
303 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE); 280 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
304 return 0; 281 return 0;
305 } 282 }
@@ -323,8 +300,7 @@ EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
323 EVP_PKEY_free(pkey); 300 EVP_PKEY_free(pkey);
324 if (!key) return(NULL); 301 if (!key) return(NULL);
325 *pp = q; 302 *pp = q;
326 if (a) 303 if (a) {
327 {
328 EC_KEY_free(*a); 304 EC_KEY_free(*a);
329 *a = key; 305 *a = key;
330 } 306 }
@@ -336,8 +312,7 @@ int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
336 EVP_PKEY *pktmp; 312 EVP_PKEY *pktmp;
337 int ret; 313 int ret;
338 if (!a) return(0); 314 if (!a) return(0);
339 if ((pktmp = EVP_PKEY_new()) == NULL) 315 if ((pktmp = EVP_PKEY_new()) == NULL) {
340 {
341 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE); 316 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
342 return(0); 317 return(0);
343 } 318 }
@@ -354,8 +329,7 @@ int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
354{ 329{
355 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval)) 330 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
356 return 0; 331 return 0;
357 if (penc) 332 if (penc) {
358 {
359 if (pub->public_key->data) 333 if (pub->public_key->data)
360 free(pub->public_key->data); 334 free(pub->public_key->data);
361 pub->public_key->data = penc; 335 pub->public_key->data = penc;
@@ -374,8 +348,7 @@ int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
374{ 348{
375 if (ppkalg) 349 if (ppkalg)
376 *ppkalg = pub->algor->algorithm; 350 *ppkalg = pub->algor->algorithm;
377 if (pk) 351 if (pk) {
378 {
379 *pk = pub->public_key->data; 352 *pk = pub->public_key->data;
380 *ppklen = pub->public_key->length; 353 *ppklen = pub->public_key->length;
381 } 354 }