summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp/p_lib.c
diff options
context:
space:
mode:
authorjsing <>2014-05-07 17:42:51 +0000
committerjsing <>2014-05-07 17:42:51 +0000
commitdd9b16d5489a1196ae0fcbe4315fe0122b042d7d (patch)
treeb341b2cdc0818410f55d0a4662a40677e4899326 /src/lib/libcrypto/evp/p_lib.c
parent1b7393e13080fa69e951cc4f482e3d5d85639c70 (diff)
downloadopenbsd-dd9b16d5489a1196ae0fcbe4315fe0122b042d7d.tar.gz
openbsd-dd9b16d5489a1196ae0fcbe4315fe0122b042d7d.tar.bz2
openbsd-dd9b16d5489a1196ae0fcbe4315fe0122b042d7d.zip
KNF.
Diffstat (limited to 'src/lib/libcrypto/evp/p_lib.c')
-rw-r--r--src/lib/libcrypto/evp/p_lib.c351
1 files changed, 186 insertions, 165 deletions
diff --git a/src/lib/libcrypto/evp/p_lib.c b/src/lib/libcrypto/evp/p_lib.c
index 5f5f5e328d..97d922d1ba 100644
--- a/src/lib/libcrypto/evp/p_lib.c
+++ b/src/lib/libcrypto/evp/p_lib.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used. 19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or 20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -82,134 +82,138 @@
82 82
83static void EVP_PKEY_free_it(EVP_PKEY *x); 83static void EVP_PKEY_free_it(EVP_PKEY *x);
84 84
85int EVP_PKEY_bits(EVP_PKEY *pkey) 85int
86 { 86EVP_PKEY_bits(EVP_PKEY *pkey)
87{
87 if (pkey && pkey->ameth && pkey->ameth->pkey_bits) 88 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
88 return pkey->ameth->pkey_bits(pkey); 89 return pkey->ameth->pkey_bits(pkey);
89 return 0; 90 return 0;
90 } 91}
91 92
92int EVP_PKEY_size(EVP_PKEY *pkey) 93int
93 { 94EVP_PKEY_size(EVP_PKEY *pkey)
95{
94 if (pkey && pkey->ameth && pkey->ameth->pkey_size) 96 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
95 return pkey->ameth->pkey_size(pkey); 97 return pkey->ameth->pkey_size(pkey);
96 return 0; 98 return 0;
97 } 99}
98 100
99int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) 101int
100 { 102EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
103{
101#ifndef OPENSSL_NO_DSA 104#ifndef OPENSSL_NO_DSA
102 if (pkey->type == EVP_PKEY_DSA) 105 if (pkey->type == EVP_PKEY_DSA) {
103 { 106 int ret = pkey->save_parameters;
104 int ret=pkey->save_parameters;
105 107
106 if (mode >= 0) 108 if (mode >= 0)
107 pkey->save_parameters=mode; 109 pkey->save_parameters = mode;
108 return(ret); 110 return (ret);
109 } 111 }
110#endif 112#endif
111#ifndef OPENSSL_NO_EC 113#ifndef OPENSSL_NO_EC
112 if (pkey->type == EVP_PKEY_EC) 114 if (pkey->type == EVP_PKEY_EC) {
113 {
114 int ret = pkey->save_parameters; 115 int ret = pkey->save_parameters;
115 116
116 if (mode >= 0) 117 if (mode >= 0)
117 pkey->save_parameters = mode; 118 pkey->save_parameters = mode;
118 return(ret); 119 return (ret);
119 }
120#endif
121 return(0);
122 } 120 }
121#endif
122 return (0);
123}
123 124
124int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) 125int
125 { 126EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
126 if (to->type != from->type) 127{
127 { 128 if (to->type != from->type) {
128 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES); 129 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,
130 EVP_R_DIFFERENT_KEY_TYPES);
129 goto err; 131 goto err;
130 } 132 }
131 133
132 if (EVP_PKEY_missing_parameters(from)) 134 if (EVP_PKEY_missing_parameters(from)) {
133 { 135 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,
134 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARAMETERS); 136 EVP_R_MISSING_PARAMETERS);
135 goto err; 137 goto err;
136 } 138 }
137 if (from->ameth && from->ameth->param_copy) 139 if (from->ameth && from->ameth->param_copy)
138 return from->ameth->param_copy(to, from); 140 return from->ameth->param_copy(to, from);
141
139err: 142err:
140 return 0; 143 return 0;
141 } 144}
142 145
143int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) 146int
144 { 147EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
148{
145 if (pkey->ameth && pkey->ameth->param_missing) 149 if (pkey->ameth && pkey->ameth->param_missing)
146 return pkey->ameth->param_missing(pkey); 150 return pkey->ameth->param_missing(pkey);
147 return 0; 151 return 0;
148 } 152}
149 153
150int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) 154int
151 { 155EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
156{
152 if (a->type != b->type) 157 if (a->type != b->type)
153 return -1; 158 return -1;
154 if (a->ameth && a->ameth->param_cmp) 159 if (a->ameth && a->ameth->param_cmp)
155 return a->ameth->param_cmp(a, b); 160 return a->ameth->param_cmp(a, b);
156 return -2; 161 return -2;
157 } 162}
158 163
159int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) 164int
160 { 165EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
166{
161 if (a->type != b->type) 167 if (a->type != b->type)
162 return -1; 168 return -1;
163 169
164 if (a->ameth) 170 if (a->ameth) {
165 {
166 int ret; 171 int ret;
167 /* Compare parameters if the algorithm has them */ 172 /* Compare parameters if the algorithm has them */
168 if (a->ameth->param_cmp) 173 if (a->ameth->param_cmp) {
169 {
170 ret = a->ameth->param_cmp(a, b); 174 ret = a->ameth->param_cmp(a, b);
171 if (ret <= 0) 175 if (ret <= 0)
172 return ret; 176 return ret;
173 } 177 }
174 178
175 if (a->ameth->pub_cmp) 179 if (a->ameth->pub_cmp)
176 return a->ameth->pub_cmp(a, b); 180 return a->ameth->pub_cmp(a, b);
177 } 181 }
178 182
179 return -2; 183 return -2;
180 } 184}
181 185
182EVP_PKEY *EVP_PKEY_new(void) 186EVP_PKEY *
183 { 187EVP_PKEY_new(void)
188{
184 EVP_PKEY *ret; 189 EVP_PKEY *ret;
185 190
186 ret=(EVP_PKEY *)malloc(sizeof(EVP_PKEY)); 191 ret = (EVP_PKEY *)malloc(sizeof(EVP_PKEY));
187 if (ret == NULL) 192 if (ret == NULL) {
188 { 193 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
189 EVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE); 194 return (NULL);
190 return(NULL);
191 }
192 ret->type=EVP_PKEY_NONE;
193 ret->save_type=EVP_PKEY_NONE;
194 ret->references=1;
195 ret->ameth=NULL;
196 ret->engine=NULL;
197 ret->pkey.ptr=NULL;
198 ret->attributes=NULL;
199 ret->save_parameters=1;
200 return(ret);
201 } 195 }
196 ret->type = EVP_PKEY_NONE;
197 ret->save_type = EVP_PKEY_NONE;
198 ret->references = 1;
199 ret->ameth = NULL;
200 ret->engine = NULL;
201 ret->pkey.ptr = NULL;
202 ret->attributes = NULL;
203 ret->save_parameters = 1;
204 return (ret);
205}
202 206
203/* Setup a public key ASN1 method and ENGINE from a NID or a string. 207/* Setup a public key ASN1 method and ENGINE from a NID or a string.
204 * If pkey is NULL just return 1 or 0 if the algorithm exists. 208 * If pkey is NULL just return 1 or 0 if the algorithm exists.
205 */ 209 */
206 210
207static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len) 211static int
208 { 212pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
213{
209 const EVP_PKEY_ASN1_METHOD *ameth; 214 const EVP_PKEY_ASN1_METHOD *ameth;
210 ENGINE *e = NULL; 215 ENGINE *e = NULL;
211 if (pkey) 216 if (pkey) {
212 {
213 if (pkey->pkey.ptr) 217 if (pkey->pkey.ptr)
214 EVP_PKEY_free_it(pkey); 218 EVP_PKEY_free_it(pkey);
215 /* If key type matches and a method exists then this 219 /* If key type matches and a method exists then this
@@ -219,13 +223,12 @@ static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
219 return 1; 223 return 1;
220#ifndef OPENSSL_NO_ENGINE 224#ifndef OPENSSL_NO_ENGINE
221 /* If we have an ENGINE release it */ 225 /* If we have an ENGINE release it */
222 if (pkey->engine) 226 if (pkey->engine) {
223 {
224 ENGINE_finish(pkey->engine); 227 ENGINE_finish(pkey->engine);
225 pkey->engine = NULL; 228 pkey->engine = NULL;
226 }
227#endif
228 } 229 }
230#endif
231 }
229 if (str) 232 if (str)
230 ameth = EVP_PKEY_asn1_find_str(&e, str, len); 233 ameth = EVP_PKEY_asn1_find_str(&e, str, len);
231 else 234 else
@@ -234,57 +237,61 @@ static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
234 if (!pkey && e) 237 if (!pkey && e)
235 ENGINE_finish(e); 238 ENGINE_finish(e);
236#endif 239#endif
237 if (!ameth) 240 if (!ameth) {
238 {
239 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); 241 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
240 return 0; 242 return 0;
241 } 243 }
242 if (pkey) 244 if (pkey) {
243 {
244 pkey->ameth = ameth; 245 pkey->ameth = ameth;
245 pkey->engine = e; 246 pkey->engine = e;
246 247
247 pkey->type = pkey->ameth->pkey_id; 248 pkey->type = pkey->ameth->pkey_id;
248 pkey->save_type=type; 249 pkey->save_type = type;
249 }
250 return 1;
251 } 250 }
251 return 1;
252}
252 253
253int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) 254int
254 { 255EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
256{
255 return pkey_set_type(pkey, type, NULL, -1); 257 return pkey_set_type(pkey, type, NULL, -1);
256 } 258}
257 259
258int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) 260int
259 { 261EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
262{
260 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len); 263 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
261 } 264}
262 265
263int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) 266int
264 { 267EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
268{
265 if (!EVP_PKEY_set_type(pkey, type)) 269 if (!EVP_PKEY_set_type(pkey, type))
266 return 0; 270 return 0;
267 pkey->pkey.ptr=key; 271 pkey->pkey.ptr = key;
268 return (key != NULL); 272 return (key != NULL);
269 } 273}
270 274
271void *EVP_PKEY_get0(EVP_PKEY *pkey) 275void *
272 { 276EVP_PKEY_get0(EVP_PKEY *pkey)
277{
273 return pkey->pkey.ptr; 278 return pkey->pkey.ptr;
274 } 279}
275 280
276#ifndef OPENSSL_NO_RSA 281#ifndef OPENSSL_NO_RSA
277int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) 282int
283EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
278{ 284{
279 int ret = EVP_PKEY_assign_RSA(pkey, key); 285 int ret = EVP_PKEY_assign_RSA(pkey, key);
280 if(ret) 286 if (ret)
281 RSA_up_ref(key); 287 RSA_up_ref(key);
282 return ret; 288 return ret;
283} 289}
284 290
285RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) 291RSA *
286 { 292EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
287 if(pkey->type != EVP_PKEY_RSA) { 293{
294 if (pkey->type != EVP_PKEY_RSA) {
288 EVPerr(EVP_F_EVP_PKEY_GET1_RSA, EVP_R_EXPECTING_AN_RSA_KEY); 295 EVPerr(EVP_F_EVP_PKEY_GET1_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
289 return NULL; 296 return NULL;
290 } 297 }
@@ -294,17 +301,19 @@ RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
294#endif 301#endif
295 302
296#ifndef OPENSSL_NO_DSA 303#ifndef OPENSSL_NO_DSA
297int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) 304int
305EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
298{ 306{
299 int ret = EVP_PKEY_assign_DSA(pkey, key); 307 int ret = EVP_PKEY_assign_DSA(pkey, key);
300 if(ret) 308 if (ret)
301 DSA_up_ref(key); 309 DSA_up_ref(key);
302 return ret; 310 return ret;
303} 311}
304 312
305DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) 313DSA *
306 { 314EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
307 if(pkey->type != EVP_PKEY_DSA) { 315{
316 if (pkey->type != EVP_PKEY_DSA) {
308 EVPerr(EVP_F_EVP_PKEY_GET1_DSA, EVP_R_EXPECTING_A_DSA_KEY); 317 EVPerr(EVP_F_EVP_PKEY_GET1_DSA, EVP_R_EXPECTING_A_DSA_KEY);
309 return NULL; 318 return NULL;
310 } 319 }
@@ -315,18 +324,19 @@ DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
315 324
316#ifndef OPENSSL_NO_EC 325#ifndef OPENSSL_NO_EC
317 326
318int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) 327int
328EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
319{ 329{
320 int ret = EVP_PKEY_assign_EC_KEY(pkey,key); 330 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
321 if (ret) 331 if (ret)
322 EC_KEY_up_ref(key); 332 EC_KEY_up_ref(key);
323 return ret; 333 return ret;
324} 334}
325 335
326EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) 336EC_KEY *
337EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
327{ 338{
328 if (pkey->type != EVP_PKEY_EC) 339 if (pkey->type != EVP_PKEY_EC) {
329 {
330 EVPerr(EVP_F_EVP_PKEY_GET1_EC_KEY, EVP_R_EXPECTING_A_EC_KEY); 340 EVPerr(EVP_F_EVP_PKEY_GET1_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
331 return NULL; 341 return NULL;
332 } 342 }
@@ -338,17 +348,19 @@ EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
338 348
339#ifndef OPENSSL_NO_DH 349#ifndef OPENSSL_NO_DH
340 350
341int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) 351int
352EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
342{ 353{
343 int ret = EVP_PKEY_assign_DH(pkey, key); 354 int ret = EVP_PKEY_assign_DH(pkey, key);
344 if(ret) 355 if (ret)
345 DH_up_ref(key); 356 DH_up_ref(key);
346 return ret; 357 return ret;
347} 358}
348 359
349DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) 360DH *
350 { 361EVP_PKEY_get1_DH(EVP_PKEY *pkey)
351 if(pkey->type != EVP_PKEY_DH) { 362{
363 if (pkey->type != EVP_PKEY_DH) {
352 EVPerr(EVP_F_EVP_PKEY_GET1_DH, EVP_R_EXPECTING_A_DH_KEY); 364 EVPerr(EVP_F_EVP_PKEY_GET1_DH, EVP_R_EXPECTING_A_DH_KEY);
353 return NULL; 365 return NULL;
354 } 366 }
@@ -357,8 +369,9 @@ DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
357} 369}
358#endif 370#endif
359 371
360int EVP_PKEY_type(int type) 372int
361 { 373EVP_PKEY_type(int type)
374{
362 int ret; 375 int ret;
363 const EVP_PKEY_ASN1_METHOD *ameth; 376 const EVP_PKEY_ASN1_METHOD *ameth;
364 ENGINE *e; 377 ENGINE *e;
@@ -372,89 +385,97 @@ int EVP_PKEY_type(int type)
372 ENGINE_finish(e); 385 ENGINE_finish(e);
373#endif 386#endif
374 return ret; 387 return ret;
375 } 388}
376 389
377int EVP_PKEY_id(const EVP_PKEY *pkey) 390int
378 { 391EVP_PKEY_id(const EVP_PKEY *pkey)
392{
379 return pkey->type; 393 return pkey->type;
380 } 394}
381 395
382int EVP_PKEY_base_id(const EVP_PKEY *pkey) 396int
383 { 397EVP_PKEY_base_id(const EVP_PKEY *pkey)
398{
384 return EVP_PKEY_type(pkey->type); 399 return EVP_PKEY_type(pkey->type);
385 } 400}
386 401
387void EVP_PKEY_free(EVP_PKEY *x) 402void
388 { 403EVP_PKEY_free(EVP_PKEY *x)
404{
389 int i; 405 int i;
390 406
391 if (x == NULL) return; 407 if (x == NULL)
408 return;
392 409
393 i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_EVP_PKEY); 410 i = CRYPTO_add(&x->references, -1, CRYPTO_LOCK_EVP_PKEY);
394 if (i > 0) return; 411 if (i > 0)
412 return;
395 413
396 EVP_PKEY_free_it(x); 414 EVP_PKEY_free_it(x);
397 if (x->attributes) 415 if (x->attributes)
398 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); 416 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
399 free(x); 417 free(x);
400 } 418}
401 419
402static void EVP_PKEY_free_it(EVP_PKEY *x) 420static void
403 { 421EVP_PKEY_free_it(EVP_PKEY *x)
404 if (x->ameth && x->ameth->pkey_free) 422{
405 { 423 if (x->ameth && x->ameth->pkey_free) {
406 x->ameth->pkey_free(x); 424 x->ameth->pkey_free(x);
407 x->pkey.ptr = NULL; 425 x->pkey.ptr = NULL;
408 } 426 }
409#ifndef OPENSSL_NO_ENGINE 427#ifndef OPENSSL_NO_ENGINE
410 if (x->engine) 428 if (x->engine) {
411 {
412 ENGINE_finish(x->engine); 429 ENGINE_finish(x->engine);
413 x->engine = NULL; 430 x->engine = NULL;
414 }
415#endif
416 } 431 }
432#endif
433}
417 434
418static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, 435static int
419 const char *kstr) 436unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, const char *kstr)
420 { 437{
421 BIO_indent(out, indent, 128); 438 BIO_indent(out, indent, 128);
422 BIO_printf(out, "%s algorithm \"%s\" unsupported\n", 439 BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
423 kstr, OBJ_nid2ln(pkey->type)); 440 kstr, OBJ_nid2ln(pkey->type));
424 return 1; 441 return 1;
425 } 442}
426 443
427int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, 444int
428 int indent, ASN1_PCTX *pctx) 445EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
429 { 446 ASN1_PCTX *pctx)
447{
430 if (pkey->ameth && pkey->ameth->pub_print) 448 if (pkey->ameth && pkey->ameth->pub_print)
431 return pkey->ameth->pub_print(out, pkey, indent, pctx); 449 return pkey->ameth->pub_print(out, pkey, indent, pctx);
432 450
433 return unsup_alg(out, pkey, indent, "Public Key"); 451 return unsup_alg(out, pkey, indent, "Public Key");
434 } 452}
435 453
436int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, 454int
437 int indent, ASN1_PCTX *pctx) 455EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
438 { 456 ASN1_PCTX *pctx)
457{
439 if (pkey->ameth && pkey->ameth->priv_print) 458 if (pkey->ameth && pkey->ameth->priv_print)
440 return pkey->ameth->priv_print(out, pkey, indent, pctx); 459 return pkey->ameth->priv_print(out, pkey, indent, pctx);
441 460
442 return unsup_alg(out, pkey, indent, "Private Key"); 461 return unsup_alg(out, pkey, indent, "Private Key");
443 } 462}
444 463
445int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, 464int
446 int indent, ASN1_PCTX *pctx) 465EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
447 { 466 ASN1_PCTX *pctx)
467{
448 if (pkey->ameth && pkey->ameth->param_print) 468 if (pkey->ameth && pkey->ameth->param_print)
449 return pkey->ameth->param_print(out, pkey, indent, pctx); 469 return pkey->ameth->param_print(out, pkey, indent, pctx);
450 return unsup_alg(out, pkey, indent, "Parameters"); 470 return unsup_alg(out, pkey, indent, "Parameters");
451 } 471}
452 472
453int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid) 473int
454 { 474EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
475{
455 if (!pkey->ameth || !pkey->ameth->pkey_ctrl) 476 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
456 return -2; 477 return -2;
457 return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 478 return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
458 0, pnid); 479 0, pnid);
459 } 480}
460 481