sl@0
|
1 |
/* crypto/evp/p_lib.c */
|
sl@0
|
2 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This package is an SSL implementation written
|
sl@0
|
6 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
7 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
10 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
11 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
13 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
14 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
17 |
* the code are not to be removed.
|
sl@0
|
18 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
19 |
* as the author of the parts of the library used.
|
sl@0
|
20 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
21 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
24 |
* modification, are permitted provided that the following conditions
|
sl@0
|
25 |
* are met:
|
sl@0
|
26 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
27 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
30 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
31 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
32 |
* must display the following acknowledgement:
|
sl@0
|
33 |
* "This product includes cryptographic software written by
|
sl@0
|
34 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
35 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
36 |
* being used are not cryptographic related :-).
|
sl@0
|
37 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
38 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
39 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
40 |
*
|
sl@0
|
41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
51 |
* SUCH DAMAGE.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
55 |
* copied and put under another distribution licence
|
sl@0
|
56 |
* [including the GNU Public Licence.]
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
|
sl@0
|
59 |
#include <stdio.h>
|
sl@0
|
60 |
#include "cryptlib.h"
|
sl@0
|
61 |
#include <openssl/bn.h>
|
sl@0
|
62 |
#include <openssl/err.h>
|
sl@0
|
63 |
#include <openssl/objects.h>
|
sl@0
|
64 |
#include <openssl/evp.h>
|
sl@0
|
65 |
#include <openssl/asn1_mac.h>
|
sl@0
|
66 |
#include <openssl/x509.h>
|
sl@0
|
67 |
#ifndef OPENSSL_NO_RSA
|
sl@0
|
68 |
#include <openssl/rsa.h>
|
sl@0
|
69 |
#endif
|
sl@0
|
70 |
#ifndef OPENSSL_NO_DSA
|
sl@0
|
71 |
#include <openssl/dsa.h>
|
sl@0
|
72 |
#endif
|
sl@0
|
73 |
#ifndef OPENSSL_NO_DH
|
sl@0
|
74 |
#include <openssl/dh.h>
|
sl@0
|
75 |
#endif
|
sl@0
|
76 |
|
sl@0
|
77 |
static void EVP_PKEY_free_it(EVP_PKEY *x);
|
sl@0
|
78 |
|
sl@0
|
79 |
EXPORT_C int EVP_PKEY_bits(EVP_PKEY *pkey)
|
sl@0
|
80 |
{
|
sl@0
|
81 |
if (0)
|
sl@0
|
82 |
return 0;
|
sl@0
|
83 |
#ifndef OPENSSL_NO_RSA
|
sl@0
|
84 |
else if (pkey->type == EVP_PKEY_RSA)
|
sl@0
|
85 |
return(BN_num_bits(pkey->pkey.rsa->n));
|
sl@0
|
86 |
#endif
|
sl@0
|
87 |
#ifndef OPENSSL_NO_DSA
|
sl@0
|
88 |
else if (pkey->type == EVP_PKEY_DSA)
|
sl@0
|
89 |
return(BN_num_bits(pkey->pkey.dsa->p));
|
sl@0
|
90 |
#endif
|
sl@0
|
91 |
#ifndef OPENSSL_NO_EC
|
sl@0
|
92 |
else if (pkey->type == EVP_PKEY_EC)
|
sl@0
|
93 |
{
|
sl@0
|
94 |
BIGNUM *order = BN_new();
|
sl@0
|
95 |
const EC_GROUP *group;
|
sl@0
|
96 |
int ret;
|
sl@0
|
97 |
|
sl@0
|
98 |
if (!order)
|
sl@0
|
99 |
{
|
sl@0
|
100 |
ERR_clear_error();
|
sl@0
|
101 |
return 0;
|
sl@0
|
102 |
}
|
sl@0
|
103 |
group = EC_KEY_get0_group(pkey->pkey.ec);
|
sl@0
|
104 |
if (!EC_GROUP_get_order(group, order, NULL))
|
sl@0
|
105 |
{
|
sl@0
|
106 |
ERR_clear_error();
|
sl@0
|
107 |
return 0;
|
sl@0
|
108 |
}
|
sl@0
|
109 |
|
sl@0
|
110 |
ret = BN_num_bits(order);
|
sl@0
|
111 |
BN_free(order);
|
sl@0
|
112 |
return ret;
|
sl@0
|
113 |
}
|
sl@0
|
114 |
#endif
|
sl@0
|
115 |
return(0);
|
sl@0
|
116 |
}
|
sl@0
|
117 |
|
sl@0
|
118 |
EXPORT_C int EVP_PKEY_size(EVP_PKEY *pkey)
|
sl@0
|
119 |
{
|
sl@0
|
120 |
if (pkey == NULL)
|
sl@0
|
121 |
return(0);
|
sl@0
|
122 |
#ifndef OPENSSL_NO_RSA
|
sl@0
|
123 |
if (pkey->type == EVP_PKEY_RSA)
|
sl@0
|
124 |
return(RSA_size(pkey->pkey.rsa));
|
sl@0
|
125 |
else
|
sl@0
|
126 |
#endif
|
sl@0
|
127 |
#ifndef OPENSSL_NO_DSA
|
sl@0
|
128 |
if (pkey->type == EVP_PKEY_DSA)
|
sl@0
|
129 |
return(DSA_size(pkey->pkey.dsa));
|
sl@0
|
130 |
#endif
|
sl@0
|
131 |
#ifndef OPENSSL_NO_ECDSA
|
sl@0
|
132 |
if (pkey->type == EVP_PKEY_EC)
|
sl@0
|
133 |
return(ECDSA_size(pkey->pkey.ec));
|
sl@0
|
134 |
#endif
|
sl@0
|
135 |
|
sl@0
|
136 |
return(0);
|
sl@0
|
137 |
}
|
sl@0
|
138 |
|
sl@0
|
139 |
EXPORT_C int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
|
sl@0
|
140 |
{
|
sl@0
|
141 |
#ifndef OPENSSL_NO_DSA
|
sl@0
|
142 |
if (pkey->type == EVP_PKEY_DSA)
|
sl@0
|
143 |
{
|
sl@0
|
144 |
int ret=pkey->save_parameters;
|
sl@0
|
145 |
|
sl@0
|
146 |
if (mode >= 0)
|
sl@0
|
147 |
pkey->save_parameters=mode;
|
sl@0
|
148 |
return(ret);
|
sl@0
|
149 |
}
|
sl@0
|
150 |
#endif
|
sl@0
|
151 |
#ifndef OPENSSL_NO_EC
|
sl@0
|
152 |
if (pkey->type == EVP_PKEY_EC)
|
sl@0
|
153 |
{
|
sl@0
|
154 |
int ret = pkey->save_parameters;
|
sl@0
|
155 |
|
sl@0
|
156 |
if (mode >= 0)
|
sl@0
|
157 |
pkey->save_parameters = mode;
|
sl@0
|
158 |
return(ret);
|
sl@0
|
159 |
}
|
sl@0
|
160 |
#endif
|
sl@0
|
161 |
return(0);
|
sl@0
|
162 |
}
|
sl@0
|
163 |
|
sl@0
|
164 |
EXPORT_C int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
|
sl@0
|
165 |
{
|
sl@0
|
166 |
if (to->type != from->type)
|
sl@0
|
167 |
{
|
sl@0
|
168 |
EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES);
|
sl@0
|
169 |
goto err;
|
sl@0
|
170 |
}
|
sl@0
|
171 |
|
sl@0
|
172 |
if (EVP_PKEY_missing_parameters(from))
|
sl@0
|
173 |
{
|
sl@0
|
174 |
EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARAMETERS);
|
sl@0
|
175 |
goto err;
|
sl@0
|
176 |
}
|
sl@0
|
177 |
#ifndef OPENSSL_NO_DSA
|
sl@0
|
178 |
if (to->type == EVP_PKEY_DSA)
|
sl@0
|
179 |
{
|
sl@0
|
180 |
BIGNUM *a;
|
sl@0
|
181 |
|
sl@0
|
182 |
if ((a=BN_dup(from->pkey.dsa->p)) == NULL) goto err;
|
sl@0
|
183 |
if (to->pkey.dsa->p != NULL) BN_free(to->pkey.dsa->p);
|
sl@0
|
184 |
to->pkey.dsa->p=a;
|
sl@0
|
185 |
|
sl@0
|
186 |
if ((a=BN_dup(from->pkey.dsa->q)) == NULL) goto err;
|
sl@0
|
187 |
if (to->pkey.dsa->q != NULL) BN_free(to->pkey.dsa->q);
|
sl@0
|
188 |
to->pkey.dsa->q=a;
|
sl@0
|
189 |
|
sl@0
|
190 |
if ((a=BN_dup(from->pkey.dsa->g)) == NULL) goto err;
|
sl@0
|
191 |
if (to->pkey.dsa->g != NULL) BN_free(to->pkey.dsa->g);
|
sl@0
|
192 |
to->pkey.dsa->g=a;
|
sl@0
|
193 |
}
|
sl@0
|
194 |
#endif
|
sl@0
|
195 |
#ifndef OPENSSL_NO_EC
|
sl@0
|
196 |
if (to->type == EVP_PKEY_EC)
|
sl@0
|
197 |
{
|
sl@0
|
198 |
EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
|
sl@0
|
199 |
if (group == NULL)
|
sl@0
|
200 |
goto err;
|
sl@0
|
201 |
if (EC_KEY_set_group(to->pkey.ec, group) == 0)
|
sl@0
|
202 |
goto err;
|
sl@0
|
203 |
EC_GROUP_free(group);
|
sl@0
|
204 |
}
|
sl@0
|
205 |
#endif
|
sl@0
|
206 |
return(1);
|
sl@0
|
207 |
err:
|
sl@0
|
208 |
return(0);
|
sl@0
|
209 |
}
|
sl@0
|
210 |
|
sl@0
|
211 |
EXPORT_C int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
|
sl@0
|
212 |
{
|
sl@0
|
213 |
#ifndef OPENSSL_NO_DSA
|
sl@0
|
214 |
if (pkey->type == EVP_PKEY_DSA)
|
sl@0
|
215 |
{
|
sl@0
|
216 |
DSA *dsa;
|
sl@0
|
217 |
|
sl@0
|
218 |
dsa=pkey->pkey.dsa;
|
sl@0
|
219 |
if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
|
sl@0
|
220 |
return(1);
|
sl@0
|
221 |
}
|
sl@0
|
222 |
#endif
|
sl@0
|
223 |
#ifndef OPENSSL_NO_EC
|
sl@0
|
224 |
if (pkey->type == EVP_PKEY_EC)
|
sl@0
|
225 |
{
|
sl@0
|
226 |
if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
|
sl@0
|
227 |
return(1);
|
sl@0
|
228 |
}
|
sl@0
|
229 |
#endif
|
sl@0
|
230 |
|
sl@0
|
231 |
return(0);
|
sl@0
|
232 |
}
|
sl@0
|
233 |
|
sl@0
|
234 |
EXPORT_C int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
|
sl@0
|
235 |
{
|
sl@0
|
236 |
#ifndef OPENSSL_NO_DSA
|
sl@0
|
237 |
if ((a->type == EVP_PKEY_DSA) && (b->type == EVP_PKEY_DSA))
|
sl@0
|
238 |
{
|
sl@0
|
239 |
if ( BN_cmp(a->pkey.dsa->p,b->pkey.dsa->p) ||
|
sl@0
|
240 |
BN_cmp(a->pkey.dsa->q,b->pkey.dsa->q) ||
|
sl@0
|
241 |
BN_cmp(a->pkey.dsa->g,b->pkey.dsa->g))
|
sl@0
|
242 |
return(0);
|
sl@0
|
243 |
else
|
sl@0
|
244 |
return(1);
|
sl@0
|
245 |
}
|
sl@0
|
246 |
#endif
|
sl@0
|
247 |
#ifndef OPENSSL_NO_EC
|
sl@0
|
248 |
if (a->type == EVP_PKEY_EC && b->type == EVP_PKEY_EC)
|
sl@0
|
249 |
{
|
sl@0
|
250 |
const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
|
sl@0
|
251 |
*group_b = EC_KEY_get0_group(b->pkey.ec);
|
sl@0
|
252 |
if (EC_GROUP_cmp(group_a, group_b, NULL))
|
sl@0
|
253 |
return 0;
|
sl@0
|
254 |
else
|
sl@0
|
255 |
return 1;
|
sl@0
|
256 |
}
|
sl@0
|
257 |
#endif
|
sl@0
|
258 |
return(-1);
|
sl@0
|
259 |
}
|
sl@0
|
260 |
|
sl@0
|
261 |
EXPORT_C int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
|
sl@0
|
262 |
{
|
sl@0
|
263 |
if (a->type != b->type)
|
sl@0
|
264 |
return -1;
|
sl@0
|
265 |
|
sl@0
|
266 |
if (EVP_PKEY_cmp_parameters(a, b) == 0)
|
sl@0
|
267 |
return 0;
|
sl@0
|
268 |
|
sl@0
|
269 |
switch (a->type)
|
sl@0
|
270 |
{
|
sl@0
|
271 |
#ifndef OPENSSL_NO_RSA
|
sl@0
|
272 |
case EVP_PKEY_RSA:
|
sl@0
|
273 |
if (BN_cmp(b->pkey.rsa->n,a->pkey.rsa->n) != 0
|
sl@0
|
274 |
|| BN_cmp(b->pkey.rsa->e,a->pkey.rsa->e) != 0)
|
sl@0
|
275 |
return 0;
|
sl@0
|
276 |
break;
|
sl@0
|
277 |
#endif
|
sl@0
|
278 |
#ifndef OPENSSL_NO_DSA
|
sl@0
|
279 |
case EVP_PKEY_DSA:
|
sl@0
|
280 |
if (BN_cmp(b->pkey.dsa->pub_key,a->pkey.dsa->pub_key) != 0)
|
sl@0
|
281 |
return 0;
|
sl@0
|
282 |
break;
|
sl@0
|
283 |
#endif
|
sl@0
|
284 |
#ifndef OPENSSL_NO_EC
|
sl@0
|
285 |
case EVP_PKEY_EC:
|
sl@0
|
286 |
{
|
sl@0
|
287 |
int r;
|
sl@0
|
288 |
const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
|
sl@0
|
289 |
const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
|
sl@0
|
290 |
*pb = EC_KEY_get0_public_key(b->pkey.ec);
|
sl@0
|
291 |
r = EC_POINT_cmp(group, pa, pb, NULL);
|
sl@0
|
292 |
if (r != 0)
|
sl@0
|
293 |
{
|
sl@0
|
294 |
if (r == 1)
|
sl@0
|
295 |
return 0;
|
sl@0
|
296 |
else
|
sl@0
|
297 |
return -2;
|
sl@0
|
298 |
}
|
sl@0
|
299 |
}
|
sl@0
|
300 |
break;
|
sl@0
|
301 |
#endif
|
sl@0
|
302 |
#ifndef OPENSSL_NO_DH
|
sl@0
|
303 |
case EVP_PKEY_DH:
|
sl@0
|
304 |
return -2;
|
sl@0
|
305 |
#endif
|
sl@0
|
306 |
default:
|
sl@0
|
307 |
return -2;
|
sl@0
|
308 |
}
|
sl@0
|
309 |
|
sl@0
|
310 |
return 1;
|
sl@0
|
311 |
}
|
sl@0
|
312 |
|
sl@0
|
313 |
EXPORT_C EVP_PKEY *EVP_PKEY_new(void)
|
sl@0
|
314 |
{
|
sl@0
|
315 |
EVP_PKEY *ret;
|
sl@0
|
316 |
|
sl@0
|
317 |
ret=(EVP_PKEY *)OPENSSL_malloc(sizeof(EVP_PKEY));
|
sl@0
|
318 |
if (ret == NULL)
|
sl@0
|
319 |
{
|
sl@0
|
320 |
EVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE);
|
sl@0
|
321 |
return(NULL);
|
sl@0
|
322 |
}
|
sl@0
|
323 |
ret->type=EVP_PKEY_NONE;
|
sl@0
|
324 |
ret->references=1;
|
sl@0
|
325 |
ret->pkey.ptr=NULL;
|
sl@0
|
326 |
ret->attributes=NULL;
|
sl@0
|
327 |
ret->save_parameters=1;
|
sl@0
|
328 |
return(ret);
|
sl@0
|
329 |
}
|
sl@0
|
330 |
|
sl@0
|
331 |
EXPORT_C int EVP_PKEY_assign(EVP_PKEY *pkey, int type, char *key)
|
sl@0
|
332 |
{
|
sl@0
|
333 |
if (pkey == NULL) return(0);
|
sl@0
|
334 |
if (pkey->pkey.ptr != NULL)
|
sl@0
|
335 |
EVP_PKEY_free_it(pkey);
|
sl@0
|
336 |
pkey->type=EVP_PKEY_type(type);
|
sl@0
|
337 |
pkey->save_type=type;
|
sl@0
|
338 |
pkey->pkey.ptr=key;
|
sl@0
|
339 |
return(key != NULL);
|
sl@0
|
340 |
}
|
sl@0
|
341 |
|
sl@0
|
342 |
#ifndef OPENSSL_NO_RSA
|
sl@0
|
343 |
EXPORT_C int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
|
sl@0
|
344 |
{
|
sl@0
|
345 |
int ret = EVP_PKEY_assign_RSA(pkey, key);
|
sl@0
|
346 |
if(ret)
|
sl@0
|
347 |
RSA_up_ref(key);
|
sl@0
|
348 |
return ret;
|
sl@0
|
349 |
}
|
sl@0
|
350 |
|
sl@0
|
351 |
EXPORT_C RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
|
sl@0
|
352 |
{
|
sl@0
|
353 |
if(pkey->type != EVP_PKEY_RSA) {
|
sl@0
|
354 |
EVPerr(EVP_F_EVP_PKEY_GET1_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
|
sl@0
|
355 |
return NULL;
|
sl@0
|
356 |
}
|
sl@0
|
357 |
RSA_up_ref(pkey->pkey.rsa);
|
sl@0
|
358 |
return pkey->pkey.rsa;
|
sl@0
|
359 |
}
|
sl@0
|
360 |
#endif
|
sl@0
|
361 |
|
sl@0
|
362 |
#ifndef OPENSSL_NO_DSA
|
sl@0
|
363 |
EXPORT_C int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
|
sl@0
|
364 |
{
|
sl@0
|
365 |
int ret = EVP_PKEY_assign_DSA(pkey, key);
|
sl@0
|
366 |
if(ret)
|
sl@0
|
367 |
DSA_up_ref(key);
|
sl@0
|
368 |
return ret;
|
sl@0
|
369 |
}
|
sl@0
|
370 |
|
sl@0
|
371 |
EXPORT_C DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
|
sl@0
|
372 |
{
|
sl@0
|
373 |
if(pkey->type != EVP_PKEY_DSA) {
|
sl@0
|
374 |
EVPerr(EVP_F_EVP_PKEY_GET1_DSA, EVP_R_EXPECTING_A_DSA_KEY);
|
sl@0
|
375 |
return NULL;
|
sl@0
|
376 |
}
|
sl@0
|
377 |
DSA_up_ref(pkey->pkey.dsa);
|
sl@0
|
378 |
return pkey->pkey.dsa;
|
sl@0
|
379 |
}
|
sl@0
|
380 |
#endif
|
sl@0
|
381 |
|
sl@0
|
382 |
#ifndef OPENSSL_NO_EC
|
sl@0
|
383 |
|
sl@0
|
384 |
EXPORT_C int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
|
sl@0
|
385 |
{
|
sl@0
|
386 |
int ret = EVP_PKEY_assign_EC_KEY(pkey,key);
|
sl@0
|
387 |
if (ret)
|
sl@0
|
388 |
EC_KEY_up_ref(key);
|
sl@0
|
389 |
return ret;
|
sl@0
|
390 |
}
|
sl@0
|
391 |
|
sl@0
|
392 |
EXPORT_C EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
|
sl@0
|
393 |
{
|
sl@0
|
394 |
if (pkey->type != EVP_PKEY_EC)
|
sl@0
|
395 |
{
|
sl@0
|
396 |
EVPerr(EVP_F_EVP_PKEY_GET1_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
|
sl@0
|
397 |
return NULL;
|
sl@0
|
398 |
}
|
sl@0
|
399 |
EC_KEY_up_ref(pkey->pkey.ec);
|
sl@0
|
400 |
return pkey->pkey.ec;
|
sl@0
|
401 |
}
|
sl@0
|
402 |
#endif
|
sl@0
|
403 |
|
sl@0
|
404 |
|
sl@0
|
405 |
#ifndef OPENSSL_NO_DH
|
sl@0
|
406 |
|
sl@0
|
407 |
EXPORT_C int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
|
sl@0
|
408 |
{
|
sl@0
|
409 |
int ret = EVP_PKEY_assign_DH(pkey, key);
|
sl@0
|
410 |
if(ret)
|
sl@0
|
411 |
DH_up_ref(key);
|
sl@0
|
412 |
return ret;
|
sl@0
|
413 |
}
|
sl@0
|
414 |
|
sl@0
|
415 |
EXPORT_C DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
|
sl@0
|
416 |
{
|
sl@0
|
417 |
if(pkey->type != EVP_PKEY_DH) {
|
sl@0
|
418 |
EVPerr(EVP_F_EVP_PKEY_GET1_DH, EVP_R_EXPECTING_A_DH_KEY);
|
sl@0
|
419 |
return NULL;
|
sl@0
|
420 |
}
|
sl@0
|
421 |
DH_up_ref(pkey->pkey.dh);
|
sl@0
|
422 |
return pkey->pkey.dh;
|
sl@0
|
423 |
}
|
sl@0
|
424 |
#endif
|
sl@0
|
425 |
|
sl@0
|
426 |
EXPORT_C int EVP_PKEY_type(int type)
|
sl@0
|
427 |
{
|
sl@0
|
428 |
switch (type)
|
sl@0
|
429 |
{
|
sl@0
|
430 |
case EVP_PKEY_RSA:
|
sl@0
|
431 |
case EVP_PKEY_RSA2:
|
sl@0
|
432 |
return(EVP_PKEY_RSA);
|
sl@0
|
433 |
case EVP_PKEY_DSA:
|
sl@0
|
434 |
case EVP_PKEY_DSA1:
|
sl@0
|
435 |
case EVP_PKEY_DSA2:
|
sl@0
|
436 |
case EVP_PKEY_DSA3:
|
sl@0
|
437 |
case EVP_PKEY_DSA4:
|
sl@0
|
438 |
return(EVP_PKEY_DSA);
|
sl@0
|
439 |
case EVP_PKEY_DH:
|
sl@0
|
440 |
return(EVP_PKEY_DH);
|
sl@0
|
441 |
case EVP_PKEY_EC:
|
sl@0
|
442 |
return(EVP_PKEY_EC);
|
sl@0
|
443 |
default:
|
sl@0
|
444 |
return(NID_undef);
|
sl@0
|
445 |
}
|
sl@0
|
446 |
}
|
sl@0
|
447 |
|
sl@0
|
448 |
EXPORT_C void EVP_PKEY_free(EVP_PKEY *x)
|
sl@0
|
449 |
{
|
sl@0
|
450 |
int i;
|
sl@0
|
451 |
|
sl@0
|
452 |
if (x == NULL) return;
|
sl@0
|
453 |
|
sl@0
|
454 |
i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_EVP_PKEY);
|
sl@0
|
455 |
#ifdef REF_PRINT
|
sl@0
|
456 |
REF_PRINT("EVP_PKEY",x);
|
sl@0
|
457 |
#endif
|
sl@0
|
458 |
if (i > 0) return;
|
sl@0
|
459 |
#ifdef REF_CHECK
|
sl@0
|
460 |
if (i < 0)
|
sl@0
|
461 |
{
|
sl@0
|
462 |
fprintf(stderr,"EVP_PKEY_free, bad reference count\n");
|
sl@0
|
463 |
abort();
|
sl@0
|
464 |
}
|
sl@0
|
465 |
#endif
|
sl@0
|
466 |
EVP_PKEY_free_it(x);
|
sl@0
|
467 |
if (x->attributes)
|
sl@0
|
468 |
sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
|
sl@0
|
469 |
OPENSSL_free(x);
|
sl@0
|
470 |
}
|
sl@0
|
471 |
|
sl@0
|
472 |
static void EVP_PKEY_free_it(EVP_PKEY *x)
|
sl@0
|
473 |
{
|
sl@0
|
474 |
switch (x->type)
|
sl@0
|
475 |
{
|
sl@0
|
476 |
#ifndef OPENSSL_NO_RSA
|
sl@0
|
477 |
case EVP_PKEY_RSA:
|
sl@0
|
478 |
case EVP_PKEY_RSA2:
|
sl@0
|
479 |
RSA_free(x->pkey.rsa);
|
sl@0
|
480 |
break;
|
sl@0
|
481 |
#endif
|
sl@0
|
482 |
#ifndef OPENSSL_NO_DSA
|
sl@0
|
483 |
case EVP_PKEY_DSA:
|
sl@0
|
484 |
case EVP_PKEY_DSA2:
|
sl@0
|
485 |
case EVP_PKEY_DSA3:
|
sl@0
|
486 |
case EVP_PKEY_DSA4:
|
sl@0
|
487 |
DSA_free(x->pkey.dsa);
|
sl@0
|
488 |
break;
|
sl@0
|
489 |
#endif
|
sl@0
|
490 |
#ifndef OPENSSL_NO_EC
|
sl@0
|
491 |
case EVP_PKEY_EC:
|
sl@0
|
492 |
EC_KEY_free(x->pkey.ec);
|
sl@0
|
493 |
break;
|
sl@0
|
494 |
#endif
|
sl@0
|
495 |
#ifndef OPENSSL_NO_DH
|
sl@0
|
496 |
case EVP_PKEY_DH:
|
sl@0
|
497 |
DH_free(x->pkey.dh);
|
sl@0
|
498 |
break;
|
sl@0
|
499 |
#endif
|
sl@0
|
500 |
}
|
sl@0
|
501 |
}
|
sl@0
|
502 |
|