1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.] */
57 #include <openssl/evp.h>
61 #include <openssl/bio.h>
62 #include <openssl/dh.h>
63 #include <openssl/dsa.h>
64 #include <openssl/ec.h>
65 #include <openssl/err.h>
66 #include <openssl/mem.h>
67 #include <openssl/obj.h>
68 #include <openssl/rsa.h>
73 extern const EVP_PKEY_ASN1_METHOD ec_asn1_meth;
74 extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
75 extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth;
77 EVP_PKEY *EVP_PKEY_new(void) {
80 ret = OPENSSL_malloc(sizeof(EVP_PKEY));
82 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_new, ERR_R_MALLOC_FAILURE);
86 memset(ret, 0, sizeof(EVP_PKEY));
87 ret->type = EVP_PKEY_NONE;
93 static void free_it(EVP_PKEY *pkey) {
94 if (pkey->ameth && pkey->ameth->pkey_free) {
95 pkey->ameth->pkey_free(pkey);
96 pkey->pkey.ptr = NULL;
97 pkey->type = EVP_PKEY_NONE;
101 void EVP_PKEY_free(EVP_PKEY *pkey) {
106 if (CRYPTO_add(&pkey->references, -1, CRYPTO_LOCK_EVP_PKEY)) {
111 if (pkey->attributes) {
112 /* TODO(fork): layering: X509_ATTRIBUTE_free is an X.509 function. In
113 * practice this path isn't called but should be removed in the future. */
114 /*sk_X509_ATTRIBUTE_pop_free(pkey->attributes, X509_ATTRIBUTE_free);*/
120 int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
121 if (pkey->ameth && pkey->ameth->pkey_opaque) {
122 return pkey->ameth->pkey_opaque(pkey);
127 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
128 if (a->type != b->type) {
134 /* Compare parameters if the algorithm has them */
135 if (a->ameth->param_cmp) {
136 ret = a->ameth->param_cmp(a, b);
141 if (a->ameth->pub_cmp) {
142 return a->ameth->pub_cmp(a, b);
149 EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey) {
150 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
154 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
155 if (to->type != from->type) {
156 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_copy_parameters, EVP_R_DIFFERENT_KEY_TYPES);
160 if (EVP_PKEY_missing_parameters(from)) {
161 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_copy_parameters, EVP_R_MISSING_PARAMETERS);
165 if (from->ameth && from->ameth->param_copy) {
166 return from->ameth->param_copy(to, from);
173 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
174 if (pkey->ameth && pkey->ameth->param_missing) {
175 return pkey->ameth->param_missing(pkey);
180 int EVP_PKEY_size(const EVP_PKEY *pkey) {
181 if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
182 return pkey->ameth->pkey_size(pkey);
187 int EVP_PKEY_bits(EVP_PKEY *pkey) {
188 if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
189 return pkey->ameth->pkey_bits(pkey);
194 int EVP_PKEY_id(const EVP_PKEY *pkey) {
198 /* TODO(fork): remove the first argument. */
199 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pengine, int nid) {
203 return &rsa_asn1_meth;
205 return &hmac_asn1_meth;
207 return &ec_asn1_meth;
213 int EVP_PKEY_type(int nid) {
214 const EVP_PKEY_ASN1_METHOD *meth = EVP_PKEY_asn1_find(NULL, nid);
218 return meth->pkey_id;
221 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, const uint8_t *mac_key,
222 size_t mac_key_len) {
223 EVP_PKEY_CTX *mac_ctx = NULL;
224 EVP_PKEY *ret = NULL;
226 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
231 if (EVP_PKEY_keygen_init(mac_ctx) <= 0 ||
232 EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN,
233 EVP_PKEY_CTRL_SET_MAC_KEY, mac_key_len,
234 (uint8_t *)mac_key) <= 0 ||
235 EVP_PKEY_keygen(mac_ctx, &ret) <= 0) {
242 EVP_PKEY_CTX_free(mac_ctx);
246 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
247 if (EVP_PKEY_assign_RSA(pkey, key)) {
254 int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
255 return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
258 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) {
259 if (pkey->type != EVP_PKEY_RSA) {
260 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
263 RSA_up_ref(pkey->pkey.rsa);
264 return pkey->pkey.rsa;
267 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
268 if (EVP_PKEY_assign_DSA(pkey, key)) {
275 int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
276 return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
279 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) {
280 if (pkey->type != EVP_PKEY_DSA) {
281 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_DSA, EVP_R_EXPECTING_A_DSA_KEY);
284 DSA_up_ref(pkey->pkey.dsa);
285 return pkey->pkey.dsa;
288 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
289 if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
296 int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
297 return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
300 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) {
301 if (pkey->type != EVP_PKEY_EC) {
302 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_EC_KEY, EVP_R_EXPECTING_AN_EC_KEY_KEY);
305 EC_KEY_up_ref(pkey->pkey.ec);
306 return pkey->pkey.ec;
309 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) {
310 if (EVP_PKEY_assign_DH(pkey, key)) {
317 int EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key) {
318 return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
321 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) {
322 if (pkey->type != EVP_PKEY_DH) {
323 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_DH, EVP_R_EXPECTING_A_DH_KEY);
326 DH_up_ref(pkey->pkey.dh);
327 return pkey->pkey.dh;
330 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
331 if (!EVP_PKEY_set_type(pkey, type)) {
334 pkey->pkey.ptr = key;
338 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pengine,
341 if (len == 3 && memcmp(name, "RSA", 3) == 0) {
342 return &rsa_asn1_meth;
343 } else if (len == 4 && memcmp(name, "HMAC", 4) == 0) {
344 return &hmac_asn1_meth;
345 } if (len == 2 && memcmp(name, "EC", 2) == 0) {
346 return &ec_asn1_meth;
351 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
352 const EVP_PKEY_ASN1_METHOD *ameth;
354 if (pkey && pkey->pkey.ptr) {
358 ameth = EVP_PKEY_asn1_find(NULL, type);
360 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_set_type, EVP_R_UNSUPPORTED_ALGORITHM);
361 ERR_add_error_dataf("algorithm %d (%s)", type, OBJ_nid2sn(type));
367 pkey->type = pkey->ameth->pkey_id;
375 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
376 if (a->type != b->type) {
379 if (a->ameth && a->ameth->param_cmp) {
380 return a->ameth->param_cmp(a, b);
385 static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
387 BIO_indent(out, indent, 128);
388 BIO_printf(out, "%s algorithm \"%s\" unsupported\n", kstr,
389 OBJ_nid2ln(pkey->type));
393 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
395 if (pkey->ameth && pkey->ameth->pub_print) {
396 return pkey->ameth->pub_print(out, pkey, indent, pctx);
399 return print_unsupported(out, pkey, indent, "Public Key");
402 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
404 if (pkey->ameth && pkey->ameth->priv_print) {
405 return pkey->ameth->priv_print(out, pkey, indent, pctx);
408 return print_unsupported(out, pkey, indent, "Private Key");
411 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
413 if (pkey->ameth && pkey->ameth->param_print) {
414 return pkey->ameth->param_print(out, pkey, indent, pctx);
417 return print_unsupported(out, pkey, indent, "Parameters");
420 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
421 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
425 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
426 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
430 void OpenSSL_add_all_algorithms(void) {}
432 void EVP_cleanup(void) {}