summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/x_pubkey.c
diff options
context:
space:
mode:
authortedu <>2014-04-18 00:10:08 +0000
committertedu <>2014-04-18 00:10:08 +0000
commit07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d (patch)
tree6327d50d69a1982f840dc68fe928ea459e2c41e0 /src/lib/libcrypto/asn1/x_pubkey.c
parent288a9e368d9d4a72792b12a00ad69e3592d94073 (diff)
downloadopenbsd-07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d.tar.gz
openbsd-07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d.tar.bz2
openbsd-07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d.zip
putting most of the braces in the right column is the very least we can do.
Diffstat (limited to 'src/lib/libcrypto/asn1/x_pubkey.c')
-rw-r--r--src/lib/libcrypto/asn1/x_pubkey.c138
1 files changed, 69 insertions, 69 deletions
diff --git a/src/lib/libcrypto/asn1/x_pubkey.c b/src/lib/libcrypto/asn1/x_pubkey.c
index 684f40899f..454d50f24c 100644
--- a/src/lib/libcrypto/asn1/x_pubkey.c
+++ b/src/lib/libcrypto/asn1/x_pubkey.c
@@ -71,14 +71,14 @@
71/* Minor tweak to operation: free up EVP_PKEY */ 71/* Minor tweak to operation: free up EVP_PKEY */
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 { 76 {
77 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; 77 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
78 EVP_PKEY_free(pubkey->pkey); 78 EVP_PKEY_free(pubkey->pkey);
79 }
80 return 1;
81 } 79 }
80 return 1;
81}
82 82
83ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = { 83ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
84 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR), 84 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
@@ -88,7 +88,7 @@ ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
88IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY) 88IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
89 89
90int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) 90int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
91 { 91{
92 X509_PUBKEY *pk=NULL; 92 X509_PUBKEY *pk=NULL;
93 93
94 if (x == NULL) return(0); 94 if (x == NULL) return(0);
@@ -96,28 +96,28 @@ int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
96 if ((pk=X509_PUBKEY_new()) == NULL) goto error; 96 if ((pk=X509_PUBKEY_new()) == NULL) goto error;
97 97
98 if (pkey->ameth) 98 if (pkey->ameth)
99 { 99 {
100 if (pkey->ameth->pub_encode) 100 if (pkey->ameth->pub_encode)
101 { 101 {
102 if (!pkey->ameth->pub_encode(pk, pkey)) 102 if (!pkey->ameth->pub_encode(pk, pkey))
103 { 103 {
104 X509err(X509_F_X509_PUBKEY_SET, 104 X509err(X509_F_X509_PUBKEY_SET,
105 X509_R_PUBLIC_KEY_ENCODE_ERROR); 105 X509_R_PUBLIC_KEY_ENCODE_ERROR);
106 goto error; 106 goto error;
107 }
108 } 107 }
108 }
109 else 109 else
110 { 110 {
111 X509err(X509_F_X509_PUBKEY_SET, 111 X509err(X509_F_X509_PUBKEY_SET,
112 X509_R_METHOD_NOT_SUPPORTED); 112 X509_R_METHOD_NOT_SUPPORTED);
113 goto error; 113 goto error;
114 }
115 } 114 }
115 }
116 else 116 else
117 { 117 {
118 X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM); 118 X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM);
119 goto error; 119 goto error;
120 } 120 }
121 121
122 if (*x != NULL) 122 if (*x != NULL)
123 X509_PUBKEY_free(*x); 123 X509_PUBKEY_free(*x);
@@ -128,62 +128,62 @@ int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
128error: 128error:
129 if (pk != NULL) X509_PUBKEY_free(pk); 129 if (pk != NULL) X509_PUBKEY_free(pk);
130 return 0; 130 return 0;
131 } 131}
132 132
133EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key) 133EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
134 { 134{
135 EVP_PKEY *ret=NULL; 135 EVP_PKEY *ret=NULL;
136 136
137 if (key == NULL) goto error; 137 if (key == NULL) goto error;
138 138
139 if (key->pkey != NULL) 139 if (key->pkey != NULL)
140 { 140 {
141 CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); 141 CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
142 return key->pkey; 142 return key->pkey;
143 } 143 }
144 144
145 if (key->public_key == NULL) goto error; 145 if (key->public_key == NULL) goto error;
146 146
147 if ((ret = EVP_PKEY_new()) == NULL) 147 if ((ret = EVP_PKEY_new()) == NULL)
148 { 148 {
149 X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE); 149 X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);
150 goto error; 150 goto error;
151 } 151 }
152 152
153 if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) 153 if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm)))
154 { 154 {
155 X509err(X509_F_X509_PUBKEY_GET,X509_R_UNSUPPORTED_ALGORITHM); 155 X509err(X509_F_X509_PUBKEY_GET,X509_R_UNSUPPORTED_ALGORITHM);
156 goto error; 156 goto error;
157 } 157 }
158 158
159 if (ret->ameth->pub_decode) 159 if (ret->ameth->pub_decode)
160 { 160 {
161 if (!ret->ameth->pub_decode(ret, key)) 161 if (!ret->ameth->pub_decode(ret, key))
162 { 162 {
163 X509err(X509_F_X509_PUBKEY_GET, 163 X509err(X509_F_X509_PUBKEY_GET,
164 X509_R_PUBLIC_KEY_DECODE_ERROR); 164 X509_R_PUBLIC_KEY_DECODE_ERROR);
165 goto error; 165 goto error;
166 }
167 } 166 }
167 }
168 else 168 else
169 { 169 {
170 X509err(X509_F_X509_PUBKEY_GET, X509_R_METHOD_NOT_SUPPORTED); 170 X509err(X509_F_X509_PUBKEY_GET, X509_R_METHOD_NOT_SUPPORTED);
171 goto error; 171 goto error;
172 } 172 }
173 173
174 /* Check to see if another thread set key->pkey first */ 174 /* Check to see if another thread set key->pkey first */
175 CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY); 175 CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
176 if (key->pkey) 176 if (key->pkey)
177 { 177 {
178 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY); 178 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
179 EVP_PKEY_free(ret); 179 EVP_PKEY_free(ret);
180 ret = key->pkey; 180 ret = key->pkey;
181 } 181 }
182 else 182 else
183 { 183 {
184 key->pkey = ret; 184 key->pkey = ret;
185 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY); 185 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
186 } 186 }
187 CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY); 187 CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);
188 188
189 return ret; 189 return ret;
@@ -192,7 +192,7 @@ EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
192 if (ret != NULL) 192 if (ret != NULL)
193 EVP_PKEY_free(ret); 193 EVP_PKEY_free(ret);
194 return(NULL); 194 return(NULL);
195 } 195}
196 196
197/* Now two pseudo ASN1 routines that take an EVP_PKEY structure 197/* Now two pseudo ASN1 routines that take an EVP_PKEY structure
198 * and encode or decode as X509_PUBKEY 198 * and encode or decode as X509_PUBKEY
@@ -200,7 +200,7 @@ EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
200 200
201EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, 201EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp,
202 long length) 202 long length)
203 { 203{
204 X509_PUBKEY *xpk; 204 X509_PUBKEY *xpk;
205 EVP_PKEY *pktmp; 205 EVP_PKEY *pktmp;
206 xpk = d2i_X509_PUBKEY(NULL, pp, length); 206 xpk = d2i_X509_PUBKEY(NULL, pp, length);
@@ -209,15 +209,15 @@ EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp,
209 X509_PUBKEY_free(xpk); 209 X509_PUBKEY_free(xpk);
210 if(!pktmp) return NULL; 210 if(!pktmp) return NULL;
211 if(a) 211 if(a)
212 { 212 {
213 EVP_PKEY_free(*a); 213 EVP_PKEY_free(*a);
214 *a = pktmp; 214 *a = pktmp;
215 }
216 return pktmp;
217 } 215 }
216 return pktmp;
217}
218 218
219int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp) 219int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
220 { 220{
221 X509_PUBKEY *xpk=NULL; 221 X509_PUBKEY *xpk=NULL;
222 int ret; 222 int ret;
223 if(!a) return 0; 223 if(!a) return 0;
@@ -225,7 +225,7 @@ int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
225 ret = i2d_X509_PUBKEY(xpk, pp); 225 ret = i2d_X509_PUBKEY(xpk, pp);
226 X509_PUBKEY_free(xpk); 226 X509_PUBKEY_free(xpk);
227 return ret; 227 return ret;
228 } 228}
229 229
230/* The following are equivalents but which return RSA and DSA 230/* The following are equivalents but which return RSA and DSA
231 * keys 231 * keys
@@ -233,7 +233,7 @@ int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
233#ifndef OPENSSL_NO_RSA 233#ifndef OPENSSL_NO_RSA
234RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, 234RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp,
235 long length) 235 long length)
236 { 236{
237 EVP_PKEY *pkey; 237 EVP_PKEY *pkey;
238 RSA *key; 238 RSA *key;
239 const unsigned char *q; 239 const unsigned char *q;
@@ -245,35 +245,35 @@ RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp,
245 if (!key) return NULL; 245 if (!key) return NULL;
246 *pp = q; 246 *pp = q;
247 if (a) 247 if (a)
248 { 248 {
249 RSA_free(*a); 249 RSA_free(*a);
250 *a = key; 250 *a = key;
251 }
252 return key;
253 } 251 }
252 return key;
253}
254 254
255int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp) 255int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
256 { 256{
257 EVP_PKEY *pktmp; 257 EVP_PKEY *pktmp;
258 int ret; 258 int ret;
259 if (!a) return 0; 259 if (!a) return 0;
260 pktmp = EVP_PKEY_new(); 260 pktmp = EVP_PKEY_new();
261 if (!pktmp) 261 if (!pktmp)
262 { 262 {
263 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE); 263 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
264 return 0; 264 return 0;
265 } 265 }
266 EVP_PKEY_set1_RSA(pktmp, a); 266 EVP_PKEY_set1_RSA(pktmp, a);
267 ret = i2d_PUBKEY(pktmp, pp); 267 ret = i2d_PUBKEY(pktmp, pp);
268 EVP_PKEY_free(pktmp); 268 EVP_PKEY_free(pktmp);
269 return ret; 269 return ret;
270 } 270}
271#endif 271#endif
272 272
273#ifndef OPENSSL_NO_DSA 273#ifndef OPENSSL_NO_DSA
274DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, 274DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp,
275 long length) 275 long length)
276 { 276{
277 EVP_PKEY *pkey; 277 EVP_PKEY *pkey;
278 DSA *key; 278 DSA *key;
279 const unsigned char *q; 279 const unsigned char *q;
@@ -285,34 +285,34 @@ DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp,
285 if (!key) return NULL; 285 if (!key) return NULL;
286 *pp = q; 286 *pp = q;
287 if (a) 287 if (a)
288 { 288 {
289 DSA_free(*a); 289 DSA_free(*a);
290 *a = key; 290 *a = key;
291 }
292 return key;
293 } 291 }
292 return key;
293}
294 294
295int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp) 295int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
296 { 296{
297 EVP_PKEY *pktmp; 297 EVP_PKEY *pktmp;
298 int ret; 298 int ret;
299 if(!a) return 0; 299 if(!a) return 0;
300 pktmp = EVP_PKEY_new(); 300 pktmp = EVP_PKEY_new();
301 if(!pktmp) 301 if(!pktmp)
302 { 302 {
303 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE); 303 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
304 return 0; 304 return 0;
305 } 305 }
306 EVP_PKEY_set1_DSA(pktmp, a); 306 EVP_PKEY_set1_DSA(pktmp, a);
307 ret = i2d_PUBKEY(pktmp, pp); 307 ret = i2d_PUBKEY(pktmp, pp);
308 EVP_PKEY_free(pktmp); 308 EVP_PKEY_free(pktmp);
309 return ret; 309 return ret;
310 } 310}
311#endif 311#endif
312 312
313#ifndef OPENSSL_NO_EC 313#ifndef OPENSSL_NO_EC
314EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length) 314EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
315 { 315{
316 EVP_PKEY *pkey; 316 EVP_PKEY *pkey;
317 EC_KEY *key; 317 EC_KEY *key;
318 const unsigned char *q; 318 const unsigned char *q;
@@ -324,38 +324,38 @@ EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
324 if (!key) return(NULL); 324 if (!key) return(NULL);
325 *pp = q; 325 *pp = q;
326 if (a) 326 if (a)
327 { 327 {
328 EC_KEY_free(*a); 328 EC_KEY_free(*a);
329 *a = key; 329 *a = key;
330 }
331 return(key);
332 } 330 }
331 return(key);
332}
333 333
334int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp) 334int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
335 { 335{
336 EVP_PKEY *pktmp; 336 EVP_PKEY *pktmp;
337 int ret; 337 int ret;
338 if (!a) return(0); 338 if (!a) return(0);
339 if ((pktmp = EVP_PKEY_new()) == NULL) 339 if ((pktmp = EVP_PKEY_new()) == NULL)
340 { 340 {
341 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE); 341 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
342 return(0); 342 return(0);
343 } 343 }
344 EVP_PKEY_set1_EC_KEY(pktmp, a); 344 EVP_PKEY_set1_EC_KEY(pktmp, a);
345 ret = i2d_PUBKEY(pktmp, pp); 345 ret = i2d_PUBKEY(pktmp, pp);
346 EVP_PKEY_free(pktmp); 346 EVP_PKEY_free(pktmp);
347 return(ret); 347 return(ret);
348 } 348}
349#endif 349#endif
350 350
351int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, 351int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
352 int ptype, void *pval, 352 int ptype, void *pval,
353 unsigned char *penc, int penclen) 353 unsigned char *penc, int penclen)
354 { 354{
355 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval)) 355 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
356 return 0; 356 return 0;
357 if (penc) 357 if (penc)
358 { 358 {
359 if (pub->public_key->data) 359 if (pub->public_key->data)
360 free(pub->public_key->data); 360 free(pub->public_key->data);
361 pub->public_key->data = penc; 361 pub->public_key->data = penc;
@@ -363,23 +363,23 @@ int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
363 /* Set number of unused bits to zero */ 363 /* Set number of unused bits to zero */
364 pub->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); 364 pub->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
365 pub->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT; 365 pub->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT;
366 }
367 return 1;
368 } 366 }
367 return 1;
368}
369 369
370int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, 370int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
371 const unsigned char **pk, int *ppklen, 371 const unsigned char **pk, int *ppklen,
372 X509_ALGOR **pa, 372 X509_ALGOR **pa,
373 X509_PUBKEY *pub) 373 X509_PUBKEY *pub)
374 { 374{
375 if (ppkalg) 375 if (ppkalg)
376 *ppkalg = pub->algor->algorithm; 376 *ppkalg = pub->algor->algorithm;
377 if (pk) 377 if (pk)
378 { 378 {
379 *pk = pub->public_key->data; 379 *pk = pub->public_key->data;
380 *ppklen = pub->public_key->length; 380 *ppklen = pub->public_key->length;
381 } 381 }
382 if (pa) 382 if (pa)
383 *pa = pub->algor; 383 *pa = pub->algor;
384 return 1; 384 return 1;
385 } 385}