summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp/p_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/evp/p_lib.c')
-rw-r--r--src/lib/libcrypto/evp/p_lib.c177
1 files changed, 6 insertions, 171 deletions
diff --git a/src/lib/libcrypto/evp/p_lib.c b/src/lib/libcrypto/evp/p_lib.c
index 22155ecf62..215b94292a 100644
--- a/src/lib/libcrypto/evp/p_lib.c
+++ b/src/lib/libcrypto/evp/p_lib.c
@@ -58,60 +58,24 @@
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/err.h>
63#include <openssl/objects.h> 61#include <openssl/objects.h>
64#include <openssl/evp.h> 62#include <openssl/evp.h>
65#include <openssl/asn1_mac.h> 63#include <openssl/asn1_mac.h>
66#include <openssl/x509.h> 64#include <openssl/x509.h>
67#ifndef OPENSSL_NO_RSA
68#include <openssl/rsa.h>
69#endif
70#ifndef OPENSSL_NO_DSA
71#include <openssl/dsa.h>
72#endif
73#ifndef OPENSSL_NO_DH
74#include <openssl/dh.h>
75#endif
76 65
77static void EVP_PKEY_free_it(EVP_PKEY *x); 66static void EVP_PKEY_free_it(EVP_PKEY *x);
78 67
79int EVP_PKEY_bits(EVP_PKEY *pkey) 68int EVP_PKEY_bits(EVP_PKEY *pkey)
80 { 69 {
81 if (0)
82 return 0;
83#ifndef OPENSSL_NO_RSA 70#ifndef OPENSSL_NO_RSA
84 else if (pkey->type == EVP_PKEY_RSA) 71 if (pkey->type == EVP_PKEY_RSA)
85 return(BN_num_bits(pkey->pkey.rsa->n)); 72 return(BN_num_bits(pkey->pkey.rsa->n));
73 else
86#endif 74#endif
87#ifndef OPENSSL_NO_DSA 75#ifndef OPENSSL_NO_DSA
88 else if (pkey->type == EVP_PKEY_DSA) 76 if (pkey->type == EVP_PKEY_DSA)
89 return(BN_num_bits(pkey->pkey.dsa->p)); 77 return(BN_num_bits(pkey->pkey.dsa->p));
90#endif 78#endif
91#ifndef OPENSSL_NO_EC
92 else if (pkey->type == EVP_PKEY_EC)
93 {
94 BIGNUM *order = BN_new();
95 const EC_GROUP *group;
96 int ret;
97
98 if (!order)
99 {
100 ERR_clear_error();
101 return 0;
102 }
103 group = EC_KEY_get0_group(pkey->pkey.ec);
104 if (!EC_GROUP_get_order(group, order, NULL))
105 {
106 ERR_clear_error();
107 return 0;
108 }
109
110 ret = BN_num_bits(order);
111 BN_free(order);
112 return ret;
113 }
114#endif
115 return(0); 79 return(0);
116 } 80 }
117 81
@@ -128,11 +92,6 @@ int EVP_PKEY_size(EVP_PKEY *pkey)
128 if (pkey->type == EVP_PKEY_DSA) 92 if (pkey->type == EVP_PKEY_DSA)
129 return(DSA_size(pkey->pkey.dsa)); 93 return(DSA_size(pkey->pkey.dsa));
130#endif 94#endif
131#ifndef OPENSSL_NO_ECDSA
132 if (pkey->type == EVP_PKEY_EC)
133 return(ECDSA_size(pkey->pkey.ec));
134#endif
135
136 return(0); 95 return(0);
137 } 96 }
138 97
@@ -148,20 +107,10 @@ int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
148 return(ret); 107 return(ret);
149 } 108 }
150#endif 109#endif
151#ifndef OPENSSL_NO_EC
152 if (pkey->type == EVP_PKEY_EC)
153 {
154 int ret = pkey->save_parameters;
155
156 if (mode >= 0)
157 pkey->save_parameters = mode;
158 return(ret);
159 }
160#endif
161 return(0); 110 return(0);
162 } 111 }
163 112
164int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) 113int EVP_PKEY_copy_parameters(EVP_PKEY *to, EVP_PKEY *from)
165 { 114 {
166 if (to->type != from->type) 115 if (to->type != from->type)
167 { 116 {
@@ -192,23 +141,12 @@ int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
192 to->pkey.dsa->g=a; 141 to->pkey.dsa->g=a;
193 } 142 }
194#endif 143#endif
195#ifndef OPENSSL_NO_EC
196 if (to->type == EVP_PKEY_EC)
197 {
198 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
199 if (group == NULL)
200 goto err;
201 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
202 goto err;
203 EC_GROUP_free(group);
204 }
205#endif
206 return(1); 144 return(1);
207err: 145err:
208 return(0); 146 return(0);
209 } 147 }
210 148
211int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) 149int EVP_PKEY_missing_parameters(EVP_PKEY *pkey)
212 { 150 {
213#ifndef OPENSSL_NO_DSA 151#ifndef OPENSSL_NO_DSA
214 if (pkey->type == EVP_PKEY_DSA) 152 if (pkey->type == EVP_PKEY_DSA)
@@ -220,18 +158,10 @@ int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
220 return(1); 158 return(1);
221 } 159 }
222#endif 160#endif
223#ifndef OPENSSL_NO_EC
224 if (pkey->type == EVP_PKEY_EC)
225 {
226 if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
227 return(1);
228 }
229#endif
230
231 return(0); 161 return(0);
232 } 162 }
233 163
234int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) 164int EVP_PKEY_cmp_parameters(EVP_PKEY *a, EVP_PKEY *b)
235 { 165 {
236#ifndef OPENSSL_NO_DSA 166#ifndef OPENSSL_NO_DSA
237 if ((a->type == EVP_PKEY_DSA) && (b->type == EVP_PKEY_DSA)) 167 if ((a->type == EVP_PKEY_DSA) && (b->type == EVP_PKEY_DSA))
@@ -244,72 +174,9 @@ int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
244 return(1); 174 return(1);
245 } 175 }
246#endif 176#endif
247#ifndef OPENSSL_NO_EC
248 if (a->type == EVP_PKEY_EC && b->type == EVP_PKEY_EC)
249 {
250 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
251 *group_b = EC_KEY_get0_group(b->pkey.ec);
252 if (EC_GROUP_cmp(group_a, group_b, NULL))
253 return 0;
254 else
255 return 1;
256 }
257#endif
258 return(-1); 177 return(-1);
259 } 178 }
260 179
261int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
262 {
263 if (a->type != b->type)
264 return -1;
265
266 if (EVP_PKEY_cmp_parameters(a, b) == 0)
267 return 0;
268
269 switch (a->type)
270 {
271#ifndef OPENSSL_NO_RSA
272 case EVP_PKEY_RSA:
273 if (BN_cmp(b->pkey.rsa->n,a->pkey.rsa->n) != 0
274 || BN_cmp(b->pkey.rsa->e,a->pkey.rsa->e) != 0)
275 return 0;
276 break;
277#endif
278#ifndef OPENSSL_NO_DSA
279 case EVP_PKEY_DSA:
280 if (BN_cmp(b->pkey.dsa->pub_key,a->pkey.dsa->pub_key) != 0)
281 return 0;
282 break;
283#endif
284#ifndef OPENSSL_NO_EC
285 case EVP_PKEY_EC:
286 {
287 int r;
288 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
289 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
290 *pb = EC_KEY_get0_public_key(b->pkey.ec);
291 r = EC_POINT_cmp(group, pa, pb, NULL);
292 if (r != 0)
293 {
294 if (r == 1)
295 return 0;
296 else
297 return -2;
298 }
299 }
300 break;
301#endif
302#ifndef OPENSSL_NO_DH
303 case EVP_PKEY_DH:
304 return -2;
305#endif
306 default:
307 return -2;
308 }
309
310 return 1;
311 }
312
313EVP_PKEY *EVP_PKEY_new(void) 180EVP_PKEY *EVP_PKEY_new(void)
314 { 181 {
315 EVP_PKEY *ret; 182 EVP_PKEY *ret;
@@ -379,29 +246,6 @@ DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
379} 246}
380#endif 247#endif
381 248
382#ifndef OPENSSL_NO_EC
383
384int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
385{
386 int ret = EVP_PKEY_assign_EC_KEY(pkey,key);
387 if (ret)
388 EC_KEY_up_ref(key);
389 return ret;
390}
391
392EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
393{
394 if (pkey->type != EVP_PKEY_EC)
395 {
396 EVPerr(EVP_F_EVP_PKEY_GET1_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
397 return NULL;
398 }
399 EC_KEY_up_ref(pkey->pkey.ec);
400 return pkey->pkey.ec;
401}
402#endif
403
404
405#ifndef OPENSSL_NO_DH 249#ifndef OPENSSL_NO_DH
406 250
407int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) 251int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
@@ -438,8 +282,6 @@ int EVP_PKEY_type(int type)
438 return(EVP_PKEY_DSA); 282 return(EVP_PKEY_DSA);
439 case EVP_PKEY_DH: 283 case EVP_PKEY_DH:
440 return(EVP_PKEY_DH); 284 return(EVP_PKEY_DH);
441 case EVP_PKEY_EC:
442 return(EVP_PKEY_EC);
443 default: 285 default:
444 return(NID_undef); 286 return(NID_undef);
445 } 287 }
@@ -464,8 +306,6 @@ void EVP_PKEY_free(EVP_PKEY *x)
464 } 306 }
465#endif 307#endif
466 EVP_PKEY_free_it(x); 308 EVP_PKEY_free_it(x);
467 if (x->attributes)
468 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
469 OPENSSL_free(x); 309 OPENSSL_free(x);
470 } 310 }
471 311
@@ -487,11 +327,6 @@ static void EVP_PKEY_free_it(EVP_PKEY *x)
487 DSA_free(x->pkey.dsa); 327 DSA_free(x->pkey.dsa);
488 break; 328 break;
489#endif 329#endif
490#ifndef OPENSSL_NO_EC
491 case EVP_PKEY_EC:
492 EC_KEY_free(x->pkey.ec);
493 break;
494#endif
495#ifndef OPENSSL_NO_DH 330#ifndef OPENSSL_NO_DH
496 case EVP_PKEY_DH: 331 case EVP_PKEY_DH:
497 DH_free(x->pkey.dh); 332 DH_free(x->pkey.dh);