Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 62 | #include <openssl/dsa.h> |
| 63 | #include <openssl/ec.h> |
| 64 | #include <openssl/err.h> |
| 65 | #include <openssl/mem.h> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 66 | #include <openssl/nid.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 67 | #include <openssl/rsa.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 68 | #include <openssl/thread.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 69 | |
| 70 | #include "internal.h" |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 71 | #include "../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 72 | |
| 73 | |
Pete Bentley | ebd3e96 | 2019-09-26 10:51:12 +0100 | [diff] [blame^] | 74 | // Node depends on |EVP_R_NOT_XOF_OR_INVALID_LENGTH|. |
| 75 | // |
| 76 | // TODO(davidben): Fix Node to not touch the error queue itself and remove this. |
| 77 | OPENSSL_DECLARE_ERROR_REASON(EVP, NOT_XOF_OR_INVALID_LENGTH) |
| 78 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 79 | EVP_PKEY *EVP_PKEY_new(void) { |
| 80 | EVP_PKEY *ret; |
| 81 | |
| 82 | ret = OPENSSL_malloc(sizeof(EVP_PKEY)); |
| 83 | if (ret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 84 | OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 85 | return NULL; |
| 86 | } |
| 87 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 88 | OPENSSL_memset(ret, 0, sizeof(EVP_PKEY)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 89 | ret->type = EVP_PKEY_NONE; |
| 90 | ret->references = 1; |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | static void free_it(EVP_PKEY *pkey) { |
| 96 | if (pkey->ameth && pkey->ameth->pkey_free) { |
| 97 | pkey->ameth->pkey_free(pkey); |
| 98 | pkey->pkey.ptr = NULL; |
| 99 | pkey->type = EVP_PKEY_NONE; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void EVP_PKEY_free(EVP_PKEY *pkey) { |
| 104 | if (pkey == NULL) { |
| 105 | return; |
| 106 | } |
| 107 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 108 | if (!CRYPTO_refcount_dec_and_test_zero(&pkey->references)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
| 112 | free_it(pkey); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 113 | OPENSSL_free(pkey); |
| 114 | } |
| 115 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 116 | int EVP_PKEY_up_ref(EVP_PKEY *pkey) { |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 117 | CRYPTO_refcount_inc(&pkey->references); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 118 | return 1; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 121 | int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) { |
| 122 | if (pkey->ameth && pkey->ameth->pkey_opaque) { |
| 123 | return pkey->ameth->pkey_opaque(pkey); |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 128 | int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) { |
| 129 | if (a->type != b->type) { |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | if (a->ameth) { |
| 134 | int ret; |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 135 | // Compare parameters if the algorithm has them |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 136 | if (a->ameth->param_cmp) { |
| 137 | ret = a->ameth->param_cmp(a, b); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 138 | if (ret <= 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 139 | return ret; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 140 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (a->ameth->pub_cmp) { |
| 144 | return a->ameth->pub_cmp(a, b); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return -2; |
| 149 | } |
| 150 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 151 | int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) { |
| 152 | if (to->type != from->type) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 153 | OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 154 | goto err; |
| 155 | } |
| 156 | |
| 157 | if (EVP_PKEY_missing_parameters(from)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 158 | OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 159 | goto err; |
| 160 | } |
| 161 | |
| 162 | if (from->ameth && from->ameth->param_copy) { |
| 163 | return from->ameth->param_copy(to, from); |
| 164 | } |
| 165 | |
| 166 | err: |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) { |
| 171 | if (pkey->ameth && pkey->ameth->param_missing) { |
| 172 | return pkey->ameth->param_missing(pkey); |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int EVP_PKEY_size(const EVP_PKEY *pkey) { |
| 178 | if (pkey && pkey->ameth && pkey->ameth->pkey_size) { |
| 179 | return pkey->ameth->pkey_size(pkey); |
| 180 | } |
| 181 | return 0; |
| 182 | } |
| 183 | |
Robert Sloan | 1f278ae | 2018-09-04 13:56:45 -0700 | [diff] [blame] | 184 | int EVP_PKEY_bits(const EVP_PKEY *pkey) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 185 | if (pkey && pkey->ameth && pkey->ameth->pkey_bits) { |
| 186 | return pkey->ameth->pkey_bits(pkey); |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | int EVP_PKEY_id(const EVP_PKEY *pkey) { |
| 192 | return pkey->type; |
| 193 | } |
| 194 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 195 | // evp_pkey_asn1_find returns the ASN.1 method table for the given |nid|, which |
| 196 | // should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is |
| 197 | // unknown. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 198 | static const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int nid) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 199 | switch (nid) { |
| 200 | case EVP_PKEY_RSA: |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 201 | return &rsa_asn1_meth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 202 | case EVP_PKEY_EC: |
| 203 | return &ec_asn1_meth; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 204 | case EVP_PKEY_DSA: |
| 205 | return &dsa_asn1_meth; |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 206 | case EVP_PKEY_ED25519: |
| 207 | return &ed25519_asn1_meth; |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame] | 208 | case EVP_PKEY_X25519: |
| 209 | return &x25519_asn1_meth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 210 | default: |
| 211 | return NULL; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | int EVP_PKEY_type(int nid) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 216 | const EVP_PKEY_ASN1_METHOD *meth = evp_pkey_asn1_find(nid); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 217 | if (meth == NULL) { |
| 218 | return NID_undef; |
| 219 | } |
| 220 | return meth->pkey_id; |
| 221 | } |
| 222 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 223 | int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) { |
| 224 | if (EVP_PKEY_assign_RSA(pkey, key)) { |
| 225 | RSA_up_ref(key); |
| 226 | return 1; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) { |
| 232 | return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key); |
| 233 | } |
| 234 | |
Robert Sloan | 1f278ae | 2018-09-04 13:56:45 -0700 | [diff] [blame] | 235 | RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 236 | if (pkey->type != EVP_PKEY_RSA) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 237 | OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 238 | return NULL; |
| 239 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 240 | return pkey->pkey.rsa; |
| 241 | } |
| 242 | |
Robert Sloan | 1f278ae | 2018-09-04 13:56:45 -0700 | [diff] [blame] | 243 | RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 244 | RSA *rsa = EVP_PKEY_get0_RSA(pkey); |
| 245 | if (rsa != NULL) { |
| 246 | RSA_up_ref(rsa); |
| 247 | } |
| 248 | return rsa; |
| 249 | } |
| 250 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 251 | int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) { |
| 252 | if (EVP_PKEY_assign_DSA(pkey, key)) { |
| 253 | DSA_up_ref(key); |
| 254 | return 1; |
| 255 | } |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) { |
| 260 | return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key); |
| 261 | } |
| 262 | |
Robert Sloan | 1f278ae | 2018-09-04 13:56:45 -0700 | [diff] [blame] | 263 | DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 264 | if (pkey->type != EVP_PKEY_DSA) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 265 | OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 266 | return NULL; |
| 267 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 268 | return pkey->pkey.dsa; |
| 269 | } |
| 270 | |
Robert Sloan | 1f278ae | 2018-09-04 13:56:45 -0700 | [diff] [blame] | 271 | DSA *EVP_PKEY_get1_DSA(const EVP_PKEY *pkey) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 272 | DSA *dsa = EVP_PKEY_get0_DSA(pkey); |
| 273 | if (dsa != NULL) { |
| 274 | DSA_up_ref(dsa); |
| 275 | } |
| 276 | return dsa; |
| 277 | } |
| 278 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 279 | int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) { |
| 280 | if (EVP_PKEY_assign_EC_KEY(pkey, key)) { |
| 281 | EC_KEY_up_ref(key); |
| 282 | return 1; |
| 283 | } |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) { |
| 288 | return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key); |
| 289 | } |
| 290 | |
Robert Sloan | 1f278ae | 2018-09-04 13:56:45 -0700 | [diff] [blame] | 291 | EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 292 | if (pkey->type != EVP_PKEY_EC) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 293 | OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_EC_KEY_KEY); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 294 | return NULL; |
| 295 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 296 | return pkey->pkey.ec; |
| 297 | } |
| 298 | |
Robert Sloan | 1f278ae | 2018-09-04 13:56:45 -0700 | [diff] [blame] | 299 | EC_KEY *EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 300 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey); |
| 301 | if (ec_key != NULL) { |
| 302 | EC_KEY_up_ref(ec_key); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 303 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 304 | return ec_key; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Robert Sloan | 1f278ae | 2018-09-04 13:56:45 -0700 | [diff] [blame] | 307 | DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey) { return NULL; } |
| 308 | DH *EVP_PKEY_get1_DH(const EVP_PKEY *pkey) { return NULL; } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 309 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 310 | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) { |
| 311 | if (!EVP_PKEY_set_type(pkey, type)) { |
| 312 | return 0; |
| 313 | } |
| 314 | pkey->pkey.ptr = key; |
| 315 | return key != NULL; |
| 316 | } |
| 317 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 318 | int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) { |
| 319 | const EVP_PKEY_ASN1_METHOD *ameth; |
| 320 | |
| 321 | if (pkey && pkey->pkey.ptr) { |
| 322 | free_it(pkey); |
| 323 | } |
| 324 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 325 | ameth = evp_pkey_asn1_find(type); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 326 | if (ameth == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 327 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 328 | ERR_add_error_dataf("algorithm %d", type); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | if (pkey) { |
| 333 | pkey->ameth = ameth; |
| 334 | pkey->type = pkey->ameth->pkey_id; |
| 335 | } |
| 336 | |
| 337 | return 1; |
| 338 | } |
| 339 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame] | 340 | EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused, |
| 341 | const uint8_t *in, size_t len) { |
| 342 | EVP_PKEY *ret = EVP_PKEY_new(); |
| 343 | if (ret == NULL || |
| 344 | !EVP_PKEY_set_type(ret, type)) { |
| 345 | goto err; |
| 346 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 347 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame] | 348 | if (ret->ameth->set_priv_raw == NULL) { |
| 349 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 350 | goto err; |
| 351 | } |
| 352 | |
| 353 | if (!ret->ameth->set_priv_raw(ret, in, len)) { |
| 354 | goto err; |
| 355 | } |
| 356 | |
| 357 | return ret; |
| 358 | |
| 359 | err: |
| 360 | EVP_PKEY_free(ret); |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *unused, |
| 365 | const uint8_t *in, size_t len) { |
| 366 | EVP_PKEY *ret = EVP_PKEY_new(); |
| 367 | if (ret == NULL || |
| 368 | !EVP_PKEY_set_type(ret, type)) { |
| 369 | goto err; |
| 370 | } |
| 371 | |
| 372 | if (ret->ameth->set_pub_raw == NULL) { |
| 373 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 374 | goto err; |
| 375 | } |
| 376 | |
| 377 | if (!ret->ameth->set_pub_raw(ret, in, len)) { |
| 378 | goto err; |
| 379 | } |
| 380 | |
| 381 | return ret; |
| 382 | |
| 383 | err: |
| 384 | EVP_PKEY_free(ret); |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, uint8_t *out, |
| 389 | size_t *out_len) { |
| 390 | if (pkey->ameth->get_priv_raw == NULL) { |
| 391 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | return pkey->ameth->get_priv_raw(pkey, out, out_len); |
| 396 | } |
| 397 | |
| 398 | int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, uint8_t *out, |
| 399 | size_t *out_len) { |
| 400 | if (pkey->ameth->get_pub_raw == NULL) { |
| 401 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | return pkey->ameth->get_pub_raw(pkey, out, out_len); |
| 406 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 407 | |
| 408 | int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) { |
| 409 | if (a->type != b->type) { |
| 410 | return -1; |
| 411 | } |
| 412 | if (a->ameth && a->ameth->param_cmp) { |
| 413 | return a->ameth->param_cmp(a, b); |
| 414 | } |
| 415 | return -2; |
| 416 | } |
| 417 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 418 | int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) { |
| 419 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0, |
| 420 | (void *)md); |
| 421 | } |
| 422 | |
| 423 | int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) { |
| 424 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD, |
| 425 | 0, (void *)out_md); |
| 426 | } |
| 427 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 428 | void OpenSSL_add_all_algorithms(void) {} |
| 429 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 430 | void OPENSSL_add_all_algorithms_conf(void) {} |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 431 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 432 | void OpenSSL_add_all_ciphers(void) {} |
| 433 | |
| 434 | void OpenSSL_add_all_digests(void) {} |
| 435 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 436 | void EVP_cleanup(void) {} |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame] | 437 | |
| 438 | int EVP_PKEY_base_id(const EVP_PKEY *pkey) { |
| 439 | // OpenSSL has two notions of key type because it supports multiple OIDs for |
| 440 | // the same algorithm: NID_rsa vs NID_rsaEncryption and five distinct spelling |
| 441 | // of DSA. We do not support these, so the base ID is simply the ID. |
| 442 | return EVP_PKEY_id(pkey); |
| 443 | } |