1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/evp.h>
58 
59 #include <assert.h>
60 #include <string.h>
61 
62 #include <openssl/dsa.h>
63 #include <openssl/ec.h>
64 #include <openssl/err.h>
65 #include <openssl/mem.h>
66 #include <openssl/nid.h>
67 #include <openssl/rsa.h>
68 #include <openssl/thread.h>
69 
70 #include "internal.h"
71 #include "../internal.h"
72 
73 
EVP_PKEY_new(void)74 EVP_PKEY *EVP_PKEY_new(void) {
75   EVP_PKEY *ret;
76 
77   ret = OPENSSL_malloc(sizeof(EVP_PKEY));
78   if (ret == NULL) {
79     OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
80     return NULL;
81   }
82 
83   OPENSSL_memset(ret, 0, sizeof(EVP_PKEY));
84   ret->type = EVP_PKEY_NONE;
85   ret->references = 1;
86 
87   return ret;
88 }
89 
free_it(EVP_PKEY * pkey)90 static void free_it(EVP_PKEY *pkey) {
91   if (pkey->ameth && pkey->ameth->pkey_free) {
92     pkey->ameth->pkey_free(pkey);
93     pkey->pkey.ptr = NULL;
94     pkey->type = EVP_PKEY_NONE;
95   }
96 }
97 
EVP_PKEY_free(EVP_PKEY * pkey)98 void EVP_PKEY_free(EVP_PKEY *pkey) {
99   if (pkey == NULL) {
100     return;
101   }
102 
103   if (!CRYPTO_refcount_dec_and_test_zero(&pkey->references)) {
104     return;
105   }
106 
107   free_it(pkey);
108   OPENSSL_free(pkey);
109 }
110 
EVP_PKEY_up_ref(EVP_PKEY * pkey)111 int EVP_PKEY_up_ref(EVP_PKEY *pkey) {
112   CRYPTO_refcount_inc(&pkey->references);
113   return 1;
114 }
115 
EVP_PKEY_is_opaque(const EVP_PKEY * pkey)116 int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
117   if (pkey->ameth && pkey->ameth->pkey_opaque) {
118     return pkey->ameth->pkey_opaque(pkey);
119   }
120   return 0;
121 }
122 
EVP_PKEY_supports_digest(const EVP_PKEY * pkey,const EVP_MD * md)123 int EVP_PKEY_supports_digest(const EVP_PKEY *pkey, const EVP_MD *md) {
124   if (pkey->ameth && pkey->ameth->pkey_supports_digest) {
125     return pkey->ameth->pkey_supports_digest(pkey, md);
126   }
127   return 1;
128 }
129 
EVP_PKEY_cmp(const EVP_PKEY * a,const EVP_PKEY * b)130 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
131   if (a->type != b->type) {
132     return -1;
133   }
134 
135   if (a->ameth) {
136     int ret;
137     /* Compare parameters if the algorithm has them */
138     if (a->ameth->param_cmp) {
139       ret = a->ameth->param_cmp(a, b);
140       if (ret <= 0) {
141         return ret;
142       }
143     }
144 
145     if (a->ameth->pub_cmp) {
146       return a->ameth->pub_cmp(a, b);
147     }
148   }
149 
150   return -2;
151 }
152 
EVP_PKEY_copy_parameters(EVP_PKEY * to,const EVP_PKEY * from)153 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
154   if (to->type != from->type) {
155     OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
156     goto err;
157   }
158 
159   if (EVP_PKEY_missing_parameters(from)) {
160     OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
161     goto err;
162   }
163 
164   if (from->ameth && from->ameth->param_copy) {
165     return from->ameth->param_copy(to, from);
166   }
167 
168 err:
169   return 0;
170 }
171 
EVP_PKEY_missing_parameters(const EVP_PKEY * pkey)172 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
173   if (pkey->ameth && pkey->ameth->param_missing) {
174     return pkey->ameth->param_missing(pkey);
175   }
176   return 0;
177 }
178 
EVP_PKEY_size(const EVP_PKEY * pkey)179 int EVP_PKEY_size(const EVP_PKEY *pkey) {
180   if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
181     return pkey->ameth->pkey_size(pkey);
182   }
183   return 0;
184 }
185 
EVP_PKEY_bits(EVP_PKEY * pkey)186 int EVP_PKEY_bits(EVP_PKEY *pkey) {
187   if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
188     return pkey->ameth->pkey_bits(pkey);
189   }
190   return 0;
191 }
192 
EVP_PKEY_id(const EVP_PKEY * pkey)193 int EVP_PKEY_id(const EVP_PKEY *pkey) {
194   return pkey->type;
195 }
196 
197 /* evp_pkey_asn1_find returns the ASN.1 method table for the given |nid|, which
198  * should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is
199  * unknown. */
evp_pkey_asn1_find(int nid)200 static const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int nid) {
201   switch (nid) {
202     case EVP_PKEY_RSA:
203       return &rsa_asn1_meth;
204     case EVP_PKEY_EC:
205       return &ec_asn1_meth;
206     case EVP_PKEY_DSA:
207       return &dsa_asn1_meth;
208     default:
209       return NULL;
210   }
211 }
212 
EVP_PKEY_type(int nid)213 int EVP_PKEY_type(int nid) {
214   const EVP_PKEY_ASN1_METHOD *meth = evp_pkey_asn1_find(nid);
215   if (meth == NULL) {
216     return NID_undef;
217   }
218   return meth->pkey_id;
219 }
220 
EVP_PKEY_set1_RSA(EVP_PKEY * pkey,RSA * key)221 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
222   if (EVP_PKEY_assign_RSA(pkey, key)) {
223     RSA_up_ref(key);
224     return 1;
225   }
226   return 0;
227 }
228 
EVP_PKEY_assign_RSA(EVP_PKEY * pkey,RSA * key)229 int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
230   return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
231 }
232 
EVP_PKEY_get0_RSA(EVP_PKEY * pkey)233 RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey) {
234   if (pkey->type != EVP_PKEY_RSA) {
235     OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
236     return NULL;
237   }
238   return pkey->pkey.rsa;
239 }
240 
EVP_PKEY_get1_RSA(EVP_PKEY * pkey)241 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) {
242   RSA *rsa = EVP_PKEY_get0_RSA(pkey);
243   if (rsa != NULL) {
244     RSA_up_ref(rsa);
245   }
246   return rsa;
247 }
248 
EVP_PKEY_set1_DSA(EVP_PKEY * pkey,DSA * key)249 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
250   if (EVP_PKEY_assign_DSA(pkey, key)) {
251     DSA_up_ref(key);
252     return 1;
253   }
254   return 0;
255 }
256 
EVP_PKEY_assign_DSA(EVP_PKEY * pkey,DSA * key)257 int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
258   return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
259 }
260 
EVP_PKEY_get0_DSA(EVP_PKEY * pkey)261 DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey) {
262   if (pkey->type != EVP_PKEY_DSA) {
263     OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY);
264     return NULL;
265   }
266   return pkey->pkey.dsa;
267 }
268 
EVP_PKEY_get1_DSA(EVP_PKEY * pkey)269 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) {
270   DSA *dsa = EVP_PKEY_get0_DSA(pkey);
271   if (dsa != NULL) {
272     DSA_up_ref(dsa);
273   }
274   return dsa;
275 }
276 
EVP_PKEY_set1_EC_KEY(EVP_PKEY * pkey,EC_KEY * key)277 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
278   if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
279     EC_KEY_up_ref(key);
280     return 1;
281   }
282   return 0;
283 }
284 
EVP_PKEY_assign_EC_KEY(EVP_PKEY * pkey,EC_KEY * key)285 int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
286   return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
287 }
288 
EVP_PKEY_get0_EC_KEY(EVP_PKEY * pkey)289 EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) {
290   if (pkey->type != EVP_PKEY_EC) {
291     OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_EC_KEY_KEY);
292     return NULL;
293   }
294   return pkey->pkey.ec;
295 }
296 
EVP_PKEY_get1_EC_KEY(EVP_PKEY * pkey)297 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) {
298   EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
299   if (ec_key != NULL) {
300     EC_KEY_up_ref(ec_key);
301   }
302   return ec_key;
303 }
304 
EVP_PKEY_get0_DH(EVP_PKEY * pkey)305 DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey) { return NULL; }
306 
EVP_PKEY_assign(EVP_PKEY * pkey,int type,void * key)307 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
308   if (!EVP_PKEY_set_type(pkey, type)) {
309     return 0;
310   }
311   pkey->pkey.ptr = key;
312   return key != NULL;
313 }
314 
EVP_PKEY_set_type(EVP_PKEY * pkey,int type)315 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
316   const EVP_PKEY_ASN1_METHOD *ameth;
317 
318   if (pkey && pkey->pkey.ptr) {
319     free_it(pkey);
320   }
321 
322   ameth = evp_pkey_asn1_find(type);
323   if (ameth == NULL) {
324     OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
325     ERR_add_error_dataf("algorithm %d", type);
326     return 0;
327   }
328 
329   if (pkey) {
330     pkey->ameth = ameth;
331     pkey->type = pkey->ameth->pkey_id;
332   }
333 
334   return 1;
335 }
336 
337 
338 
EVP_PKEY_cmp_parameters(const EVP_PKEY * a,const EVP_PKEY * b)339 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
340   if (a->type != b->type) {
341     return -1;
342   }
343   if (a->ameth && a->ameth->param_cmp) {
344     return a->ameth->param_cmp(a, b);
345   }
346   return -2;
347 }
348 
EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX * ctx,const EVP_MD * md)349 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
350   return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
351                            (void *)md);
352 }
353 
EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX * ctx,const EVP_MD ** out_md)354 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
355   return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
356                            0, (void *)out_md);
357 }
358 
OpenSSL_add_all_algorithms(void)359 void OpenSSL_add_all_algorithms(void) {}
360 
OPENSSL_add_all_algorithms_conf(void)361 void OPENSSL_add_all_algorithms_conf(void) {}
362 
OpenSSL_add_all_ciphers(void)363 void OpenSSL_add_all_ciphers(void) {}
364 
OpenSSL_add_all_digests(void)365 void OpenSSL_add_all_digests(void) {}
366 
EVP_cleanup(void)367 void EVP_cleanup(void) {}
368