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
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>
69
70#include "internal.h"
71
72
73extern const EVP_PKEY_ASN1_METHOD ec_asn1_meth;
74extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
75extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth;
76
77EVP_PKEY *EVP_PKEY_new(void) {
78  EVP_PKEY *ret;
79
80  ret = OPENSSL_malloc(sizeof(EVP_PKEY));
81  if (ret == NULL) {
82    OPENSSL_PUT_ERROR(EVP, EVP_PKEY_new, ERR_R_MALLOC_FAILURE);
83    return NULL;
84  }
85
86  memset(ret, 0, sizeof(EVP_PKEY));
87  ret->type = EVP_PKEY_NONE;
88  ret->references = 1;
89
90  return ret;
91}
92
93static 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;
98  }
99}
100
101void EVP_PKEY_free(EVP_PKEY *pkey) {
102  if (pkey == NULL) {
103    return;
104  }
105
106  if (CRYPTO_add(&pkey->references, -1, CRYPTO_LOCK_EVP_PKEY)) {
107    return;
108  }
109
110  free_it(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);*/
115    assert(0);
116  }
117  OPENSSL_free(pkey);
118}
119
120int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
121  if (pkey->ameth && pkey->ameth->pkey_opaque) {
122    return pkey->ameth->pkey_opaque(pkey);
123  }
124  return 0;
125}
126
127int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
128  if (a->type != b->type) {
129    return -1;
130  }
131
132  if (a->ameth) {
133    int ret;
134    /* Compare parameters if the algorithm has them */
135    if (a->ameth->param_cmp) {
136      ret = a->ameth->param_cmp(a, b);
137      if (ret <= 0)
138        return ret;
139    }
140
141    if (a->ameth->pub_cmp) {
142      return a->ameth->pub_cmp(a, b);
143    }
144  }
145
146  return -2;
147}
148
149EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey) {
150  CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
151  return pkey;
152}
153
154int 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);
157    goto err;
158  }
159
160  if (EVP_PKEY_missing_parameters(from)) {
161    OPENSSL_PUT_ERROR(EVP, EVP_PKEY_copy_parameters, EVP_R_MISSING_PARAMETERS);
162    goto err;
163  }
164
165  if (from->ameth && from->ameth->param_copy) {
166    return from->ameth->param_copy(to, from);
167  }
168
169err:
170  return 0;
171}
172
173int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
174  if (pkey->ameth && pkey->ameth->param_missing) {
175    return pkey->ameth->param_missing(pkey);
176  }
177  return 0;
178}
179
180int EVP_PKEY_size(const EVP_PKEY *pkey) {
181  if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
182    return pkey->ameth->pkey_size(pkey);
183  }
184  return 0;
185}
186
187int EVP_PKEY_bits(EVP_PKEY *pkey) {
188  if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
189    return pkey->ameth->pkey_bits(pkey);
190  }
191  return 0;
192}
193
194int EVP_PKEY_id(const EVP_PKEY *pkey) {
195  return pkey->type;
196}
197
198/* TODO(fork): remove the first argument. */
199const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pengine, int nid) {
200  switch (nid) {
201    case EVP_PKEY_RSA:
202    case EVP_PKEY_RSA2:
203      return &rsa_asn1_meth;
204    case EVP_PKEY_HMAC:
205      return &hmac_asn1_meth;
206    case EVP_PKEY_EC:
207      return &ec_asn1_meth;
208    default:
209      return NULL;
210  }
211}
212
213int EVP_PKEY_type(int nid) {
214  const EVP_PKEY_ASN1_METHOD *meth = EVP_PKEY_asn1_find(NULL, nid);
215  if (meth == NULL) {
216    return NID_undef;
217  }
218  return meth->pkey_id;
219}
220
221EVP_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;
225
226  mac_ctx = EVP_PKEY_CTX_new_id(type, e);
227  if (!mac_ctx) {
228    return NULL;
229  }
230
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) {
236    ret = NULL;
237    goto merr;
238  }
239
240merr:
241  if (mac_ctx)
242    EVP_PKEY_CTX_free(mac_ctx);
243  return ret;
244}
245
246int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
247  if (EVP_PKEY_assign_RSA(pkey, key)) {
248    RSA_up_ref(key);
249    return 1;
250  }
251  return 0;
252}
253
254int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
255  return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
256}
257
258RSA *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);
261    return NULL;
262  }
263  RSA_up_ref(pkey->pkey.rsa);
264  return pkey->pkey.rsa;
265}
266
267int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
268  if (EVP_PKEY_assign_DSA(pkey, key)) {
269    DSA_up_ref(key);
270    return 1;
271  }
272  return 0;
273}
274
275int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
276  return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
277}
278
279DSA *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);
282    return NULL;
283  }
284  DSA_up_ref(pkey->pkey.dsa);
285  return pkey->pkey.dsa;
286}
287
288int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
289  if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
290    EC_KEY_up_ref(key);
291    return 1;
292  }
293  return 0;
294}
295
296int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
297  return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
298}
299
300EC_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);
303    return NULL;
304  }
305  EC_KEY_up_ref(pkey->pkey.ec);
306  return pkey->pkey.ec;
307}
308
309int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) {
310  if (EVP_PKEY_assign_DH(pkey, key)) {
311    DH_up_ref(key);
312    return 1;
313  }
314  return 0;
315}
316
317int EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key) {
318  return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
319}
320
321DH *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);
324    return NULL;
325  }
326  DH_up_ref(pkey->pkey.dh);
327  return pkey->pkey.dh;
328}
329
330int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
331  if (!EVP_PKEY_set_type(pkey, type)) {
332    return 0;
333  }
334  pkey->pkey.ptr = key;
335  return key != NULL;
336}
337
338const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pengine,
339                                                   const char *name,
340                                                   size_t len) {
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;
347  }
348  return NULL;
349}
350
351int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
352  const EVP_PKEY_ASN1_METHOD *ameth;
353
354  if (pkey && pkey->pkey.ptr) {
355    free_it(pkey);
356  }
357
358  ameth = EVP_PKEY_asn1_find(NULL, type);
359  if (ameth == NULL) {
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));
362    return 0;
363  }
364
365  if (pkey) {
366    pkey->ameth = ameth;
367    pkey->type = pkey->ameth->pkey_id;
368  }
369
370  return 1;
371}
372
373
374
375int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
376  if (a->type != b->type) {
377    return -1;
378  }
379  if (a->ameth && a->ameth->param_cmp) {
380    return a->ameth->param_cmp(a, b);
381  }
382  return -2;
383}
384
385static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
386                             const char *kstr) {
387  BIO_indent(out, indent, 128);
388  BIO_printf(out, "%s algorithm \"%s\" unsupported\n", kstr,
389             OBJ_nid2ln(pkey->type));
390  return 1;
391}
392
393int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
394                          ASN1_PCTX *pctx) {
395  if (pkey->ameth && pkey->ameth->pub_print) {
396    return pkey->ameth->pub_print(out, pkey, indent, pctx);
397  }
398
399  return print_unsupported(out, pkey, indent, "Public Key");
400}
401
402int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
403                           ASN1_PCTX *pctx) {
404  if (pkey->ameth && pkey->ameth->priv_print) {
405    return pkey->ameth->priv_print(out, pkey, indent, pctx);
406  }
407
408  return print_unsupported(out, pkey, indent, "Private Key");
409}
410
411int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
412                          ASN1_PCTX *pctx) {
413  if (pkey->ameth && pkey->ameth->param_print) {
414    return pkey->ameth->param_print(out, pkey, indent, pctx);
415  }
416
417  return print_unsupported(out, pkey, indent, "Parameters");
418}
419
420int 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,
422                           (void *)md);
423}
424
425int 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,
427                           0, (void *)out_md);
428}
429
430void OpenSSL_add_all_algorithms(void) {}
431
432void EVP_cleanup(void) {}
433