Rich Salz | 6286757 | 2016-05-17 14:24:46 -0400 | [diff] [blame] | 1 | /* |
Matt Caswell | b0edda1 | 2018-03-20 13:00:17 +0000 | [diff] [blame] | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 6286757 | 2016-05-17 14:24:46 -0400 | [diff] [blame] | 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
Richard Levitte | b39fc56 | 2015-05-14 16:56:48 +0200 | [diff] [blame] | 11 | #include "internal/cryptlib.h" |
Pauli | cd420b0 | 2017-08-22 07:17:35 +1000 | [diff] [blame] | 12 | #include "internal/refcount.h" |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 13 | #include <openssl/bn.h> |
| 14 | #include <openssl/err.h> |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 15 | #include <openssl/objects.h> |
| 16 | #include <openssl/evp.h> |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 17 | #include <openssl/x509.h> |
Rich Salz | 3c27208 | 2016-03-18 14:30:20 -0400 | [diff] [blame] | 18 | #include <openssl/rsa.h> |
| 19 | #include <openssl/dsa.h> |
| 20 | #include <openssl/dh.h> |
Matt Caswell | b3831fb | 2018-03-05 17:23:57 +0000 | [diff] [blame] | 21 | #include <openssl/cmac.h> |
Rich Salz | 3c27208 | 2016-03-18 14:30:20 -0400 | [diff] [blame] | 22 | #include <openssl/engine.h> |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 23 | |
Dr. Stephen Henson | 5fe736e | 2015-03-23 18:42:42 +0000 | [diff] [blame] | 24 | #include "internal/asn1_int.h" |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 25 | #include "internal/evp_int.h" |
Dr. Stephen Henson | 18e377b | 2006-03-22 17:59:49 +0000 | [diff] [blame] | 26 | |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 27 | static void EVP_PKEY_free_it(EVP_PKEY *x); |
Ben Laurie | bb2297a | 2001-08-03 18:48:35 +0000 | [diff] [blame] | 28 | |
Dr. Stephen Henson | 8900f3e | 2016-08-17 13:50:48 +0100 | [diff] [blame] | 29 | int EVP_PKEY_bits(const EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 30 | { |
| 31 | if (pkey && pkey->ameth && pkey->ameth->pkey_bits) |
| 32 | return pkey->ameth->pkey_bits(pkey); |
| 33 | return 0; |
| 34 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 35 | |
Dr. Stephen Henson | 2514fa7 | 2014-01-18 14:51:40 +0000 | [diff] [blame] | 36 | int EVP_PKEY_security_bits(const EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 37 | { |
| 38 | if (pkey == NULL) |
| 39 | return 0; |
| 40 | if (!pkey->ameth || !pkey->ameth->pkey_security_bits) |
| 41 | return -2; |
| 42 | return pkey->ameth->pkey_security_bits(pkey); |
| 43 | } |
Dr. Stephen Henson | 2514fa7 | 2014-01-18 14:51:40 +0000 | [diff] [blame] | 44 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 45 | int EVP_PKEY_size(EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 46 | { |
| 47 | if (pkey && pkey->ameth && pkey->ameth->pkey_size) |
| 48 | return pkey->ameth->pkey_size(pkey); |
| 49 | return 0; |
| 50 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 51 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 52 | int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 53 | { |
Richard Levitte | cf1b7d9 | 2001-02-19 16:06:34 +0000 | [diff] [blame] | 54 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 55 | if (pkey->type == EVP_PKEY_DSA) { |
| 56 | int ret = pkey->save_parameters; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 57 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 58 | if (mode >= 0) |
| 59 | pkey->save_parameters = mode; |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 60 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 61 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 62 | #endif |
Bodo Möller | 5488bb6 | 2002-08-12 08:47:41 +0000 | [diff] [blame] | 63 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 64 | if (pkey->type == EVP_PKEY_EC) { |
| 65 | int ret = pkey->save_parameters; |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 66 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 67 | if (mode >= 0) |
| 68 | pkey->save_parameters = mode; |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 69 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 70 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 71 | #endif |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 72 | return 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 73 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 74 | |
Richard Levitte | a8b7284 | 2003-04-07 10:09:44 +0000 | [diff] [blame] | 75 | int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 76 | { |
Dr. Stephen Henson | 2986ecd | 2015-12-13 17:28:40 +0000 | [diff] [blame] | 77 | if (to->type == EVP_PKEY_NONE) { |
| 78 | if (EVP_PKEY_set_type(to, from->type) == 0) |
| 79 | return 0; |
| 80 | } else if (to->type != from->type) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 81 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES); |
| 82 | goto err; |
| 83 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 84 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 85 | if (EVP_PKEY_missing_parameters(from)) { |
| 86 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS); |
| 87 | goto err; |
| 88 | } |
Dr. Stephen Henson | f72f00d | 2016-05-27 14:18:40 +0100 | [diff] [blame] | 89 | |
| 90 | if (!EVP_PKEY_missing_parameters(to)) { |
| 91 | if (EVP_PKEY_cmp_parameters(to, from) == 1) |
| 92 | return 1; |
| 93 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS); |
| 94 | return 0; |
| 95 | } |
| 96 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 97 | if (from->ameth && from->ameth->param_copy) |
| 98 | return from->ameth->param_copy(to, from); |
| 99 | err: |
| 100 | return 0; |
| 101 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 102 | |
Richard Levitte | af0f0f3 | 2003-04-06 15:31:18 +0000 | [diff] [blame] | 103 | int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 104 | { |
| 105 | if (pkey->ameth && pkey->ameth->param_missing) |
| 106 | return pkey->ameth->param_missing(pkey); |
| 107 | return 0; |
| 108 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 109 | |
Richard Levitte | af0f0f3 | 2003-04-06 15:31:18 +0000 | [diff] [blame] | 110 | int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 111 | { |
| 112 | if (a->type != b->type) |
| 113 | return -1; |
| 114 | if (a->ameth && a->ameth->param_cmp) |
| 115 | return a->ameth->param_cmp(a, b); |
| 116 | return -2; |
| 117 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 118 | |
Richard Levitte | af0f0f3 | 2003-04-06 15:31:18 +0000 | [diff] [blame] | 119 | int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 120 | { |
| 121 | if (a->type != b->type) |
| 122 | return -1; |
Richard Levitte | e6526fb | 2003-04-03 22:27:24 +0000 | [diff] [blame] | 123 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 124 | if (a->ameth) { |
| 125 | int ret; |
| 126 | /* Compare parameters if the algorithm has them */ |
| 127 | if (a->ameth->param_cmp) { |
| 128 | ret = a->ameth->param_cmp(a, b); |
| 129 | if (ret <= 0) |
| 130 | return ret; |
| 131 | } |
Richard Levitte | a8b7284 | 2003-04-07 10:09:44 +0000 | [diff] [blame] | 132 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 133 | if (a->ameth->pub_cmp) |
| 134 | return a->ameth->pub_cmp(a, b); |
| 135 | } |
Richard Levitte | e6526fb | 2003-04-03 22:27:24 +0000 | [diff] [blame] | 136 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 137 | return -2; |
| 138 | } |
Richard Levitte | e6526fb | 2003-04-03 22:27:24 +0000 | [diff] [blame] | 139 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 140 | EVP_PKEY *EVP_PKEY_new(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 141 | { |
FdaSilvaYY | a2d0baa | 2016-02-14 18:44:30 +0100 | [diff] [blame] | 142 | EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret)); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 143 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 144 | if (ret == NULL) { |
| 145 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); |
Alessandro Ghedini | 03273d6 | 2016-02-26 12:21:15 +0000 | [diff] [blame] | 146 | return NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 147 | } |
| 148 | ret->type = EVP_PKEY_NONE; |
| 149 | ret->save_type = EVP_PKEY_NONE; |
| 150 | ret->references = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 151 | ret->save_parameters = 1; |
Alessandro Ghedini | 03273d6 | 2016-02-26 12:21:15 +0000 | [diff] [blame] | 152 | ret->lock = CRYPTO_THREAD_lock_new(); |
| 153 | if (ret->lock == NULL) { |
| 154 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); |
| 155 | OPENSSL_free(ret); |
| 156 | return NULL; |
| 157 | } |
| 158 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 159 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 160 | |
FdaSilvaYY | c5ebfca | 2016-03-07 22:45:58 +0100 | [diff] [blame] | 161 | int EVP_PKEY_up_ref(EVP_PKEY *pkey) |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 162 | { |
Alessandro Ghedini | 03273d6 | 2016-02-26 12:21:15 +0000 | [diff] [blame] | 163 | int i; |
FdaSilvaYY | c5ebfca | 2016-03-07 22:45:58 +0100 | [diff] [blame] | 164 | |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 165 | if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0) |
FdaSilvaYY | c5ebfca | 2016-03-07 22:45:58 +0100 | [diff] [blame] | 166 | return 0; |
| 167 | |
| 168 | REF_PRINT_COUNT("EVP_PKEY", pkey); |
| 169 | REF_ASSERT_ISNT(i < 2); |
| 170 | return ((i > 1) ? 1 : 0); |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 173 | /* |
| 174 | * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey |
| 175 | * is NULL just return 1 or 0 if the algorithm exists. |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 176 | */ |
| 177 | |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 178 | static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str, |
| 179 | int len) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 180 | { |
| 181 | const EVP_PKEY_ASN1_METHOD *ameth; |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 182 | ENGINE **eptr = (e == NULL) ? &e : NULL; |
| 183 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 184 | if (pkey) { |
| 185 | if (pkey->pkey.ptr) |
| 186 | EVP_PKEY_free_it(pkey); |
| 187 | /* |
| 188 | * If key type matches and a method exists then this lookup has |
| 189 | * succeeded once so just indicate success. |
| 190 | */ |
| 191 | if ((type == pkey->save_type) && pkey->ameth) |
| 192 | return 1; |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 193 | #ifndef OPENSSL_NO_ENGINE |
Dr. Stephen Henson | d19b01a | 2017-10-09 15:21:11 +0100 | [diff] [blame] | 194 | /* If we have ENGINEs release them */ |
Rich Salz | 7c96dbc | 2016-02-25 12:09:06 -0500 | [diff] [blame] | 195 | ENGINE_finish(pkey->engine); |
| 196 | pkey->engine = NULL; |
Dr. Stephen Henson | d19b01a | 2017-10-09 15:21:11 +0100 | [diff] [blame] | 197 | ENGINE_finish(pkey->pmeth_engine); |
| 198 | pkey->pmeth_engine = NULL; |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 199 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 200 | } |
| 201 | if (str) |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 202 | ameth = EVP_PKEY_asn1_find_str(eptr, str, len); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 203 | else |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 204 | ameth = EVP_PKEY_asn1_find(eptr, type); |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 205 | #ifndef OPENSSL_NO_ENGINE |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 206 | if (pkey == NULL && eptr != NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 207 | ENGINE_finish(e); |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 208 | #endif |
Rich Salz | 7c96dbc | 2016-02-25 12:09:06 -0500 | [diff] [blame] | 209 | if (ameth == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 210 | EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); |
| 211 | return 0; |
| 212 | } |
| 213 | if (pkey) { |
| 214 | pkey->ameth = ameth; |
| 215 | pkey->engine = e; |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 216 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 217 | pkey->type = pkey->ameth->pkey_id; |
| 218 | pkey->save_type = type; |
| 219 | } |
| 220 | return 1; |
| 221 | } |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 222 | |
Matt Caswell | f929439 | 2018-03-15 12:19:16 +0000 | [diff] [blame] | 223 | EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e, |
| 224 | const unsigned char *priv, |
| 225 | size_t len) |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 226 | { |
| 227 | EVP_PKEY *ret = EVP_PKEY_new(); |
| 228 | |
| 229 | if (ret == NULL |
| 230 | || !pkey_set_type(ret, e, type, NULL, -1)) { |
| 231 | /* EVPerr already called */ |
| 232 | goto err; |
| 233 | } |
| 234 | |
| 235 | if (ret->ameth->set_priv_key == NULL) { |
Matt Caswell | f929439 | 2018-03-15 12:19:16 +0000 | [diff] [blame] | 236 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 237 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 238 | goto err; |
| 239 | } |
| 240 | |
| 241 | if (!ret->ameth->set_priv_key(ret, priv, len)) { |
Matt Caswell | f929439 | 2018-03-15 12:19:16 +0000 | [diff] [blame] | 242 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED); |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 243 | goto err; |
| 244 | } |
| 245 | |
| 246 | return ret; |
| 247 | |
| 248 | err: |
| 249 | EVP_PKEY_free(ret); |
| 250 | return NULL; |
| 251 | } |
| 252 | |
Matt Caswell | f929439 | 2018-03-15 12:19:16 +0000 | [diff] [blame] | 253 | EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e, |
| 254 | const unsigned char *pub, |
| 255 | size_t len) |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 256 | { |
| 257 | EVP_PKEY *ret = EVP_PKEY_new(); |
| 258 | |
| 259 | if (ret == NULL |
| 260 | || !pkey_set_type(ret, e, type, NULL, -1)) { |
| 261 | /* EVPerr already called */ |
| 262 | goto err; |
| 263 | } |
| 264 | |
| 265 | if (ret->ameth->set_pub_key == NULL) { |
Matt Caswell | f929439 | 2018-03-15 12:19:16 +0000 | [diff] [blame] | 266 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 267 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 268 | goto err; |
| 269 | } |
| 270 | |
| 271 | if (!ret->ameth->set_pub_key(ret, pub, len)) { |
Matt Caswell | f929439 | 2018-03-15 12:19:16 +0000 | [diff] [blame] | 272 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED); |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 273 | goto err; |
| 274 | } |
| 275 | |
| 276 | return ret; |
| 277 | |
| 278 | err: |
| 279 | EVP_PKEY_free(ret); |
| 280 | return NULL; |
| 281 | } |
| 282 | |
Matt Caswell | b3831fb | 2018-03-05 17:23:57 +0000 | [diff] [blame] | 283 | EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, |
| 284 | size_t len, const EVP_CIPHER *cipher) |
| 285 | { |
Matt Caswell | df6d51e | 2018-03-19 10:35:16 +0000 | [diff] [blame] | 286 | #ifndef OPENSSL_NO_CMAC |
Matt Caswell | b3831fb | 2018-03-05 17:23:57 +0000 | [diff] [blame] | 287 | EVP_PKEY *ret = EVP_PKEY_new(); |
| 288 | CMAC_CTX *cmctx = CMAC_CTX_new(); |
| 289 | |
| 290 | if (ret == NULL |
| 291 | || cmctx == NULL |
| 292 | || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) { |
| 293 | /* EVPerr already called */ |
| 294 | goto err; |
| 295 | } |
| 296 | |
| 297 | if (!CMAC_Init(cmctx, priv, len, cipher, e)) { |
| 298 | EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED); |
| 299 | goto err; |
| 300 | } |
| 301 | |
| 302 | ret->pkey.ptr = cmctx; |
| 303 | return ret; |
| 304 | |
| 305 | err: |
| 306 | EVP_PKEY_free(ret); |
| 307 | CMAC_CTX_free(cmctx); |
| 308 | return NULL; |
Matt Caswell | df6d51e | 2018-03-19 10:35:16 +0000 | [diff] [blame] | 309 | #else |
| 310 | EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, |
| 311 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 312 | return NULL; |
| 313 | #endif |
Matt Caswell | b3831fb | 2018-03-05 17:23:57 +0000 | [diff] [blame] | 314 | } |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 315 | |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 316 | int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 317 | { |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 318 | return pkey_set_type(pkey, NULL, type, NULL, -1); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 319 | } |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 320 | |
| 321 | int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 322 | { |
Matt Caswell | a08802c | 2018-03-05 14:06:41 +0000 | [diff] [blame] | 323 | return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 324 | } |
Dr. Stephen Henson | d19b01a | 2017-10-09 15:21:11 +0100 | [diff] [blame] | 325 | #ifndef OPENSSL_NO_ENGINE |
| 326 | int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e) |
| 327 | { |
| 328 | if (e != NULL) { |
| 329 | if (!ENGINE_init(e)) { |
| 330 | EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB); |
| 331 | return 0; |
| 332 | } |
| 333 | if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) { |
| 334 | ENGINE_finish(e); |
| 335 | EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM); |
| 336 | return 0; |
| 337 | } |
| 338 | } |
| 339 | ENGINE_finish(pkey->pmeth_engine); |
| 340 | pkey->pmeth_engine = e; |
| 341 | return 1; |
| 342 | } |
| 343 | #endif |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 344 | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 345 | { |
Emilia Kasper | e34c66c | 2015-09-01 16:31:55 +0200 | [diff] [blame] | 346 | if (pkey == NULL || !EVP_PKEY_set_type(pkey, type)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 347 | return 0; |
| 348 | pkey->pkey.ptr = key; |
| 349 | return (key != NULL); |
| 350 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 351 | |
Dr. Stephen Henson | 3aeb934 | 2016-01-19 00:21:12 +0000 | [diff] [blame] | 352 | void *EVP_PKEY_get0(const EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 353 | { |
| 354 | return pkey->pkey.ptr; |
| 355 | } |
Dr. Stephen Henson | db98bbc | 2006-03-24 13:46:58 +0000 | [diff] [blame] | 356 | |
Nathaniel McCallum | ebad0b0 | 2016-06-15 14:02:04 -0400 | [diff] [blame] | 357 | const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len) |
| 358 | { |
| 359 | ASN1_OCTET_STRING *os = NULL; |
| 360 | if (pkey->type != EVP_PKEY_HMAC) { |
| 361 | EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY); |
| 362 | return NULL; |
| 363 | } |
| 364 | os = EVP_PKEY_get0(pkey); |
| 365 | *len = os->length; |
| 366 | return os->data; |
| 367 | } |
| 368 | |
Todd Short | 52ad5b6 | 2016-12-16 17:00:43 -0500 | [diff] [blame] | 369 | #ifndef OPENSSL_NO_POLY1305 |
| 370 | const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len) |
| 371 | { |
| 372 | ASN1_OCTET_STRING *os = NULL; |
| 373 | if (pkey->type != EVP_PKEY_POLY1305) { |
| 374 | EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY); |
| 375 | return NULL; |
| 376 | } |
| 377 | os = EVP_PKEY_get0(pkey); |
| 378 | *len = os->length; |
| 379 | return os->data; |
| 380 | } |
| 381 | #endif |
| 382 | |
Todd Short | 3f5616d | 2017-01-11 16:38:44 -0500 | [diff] [blame] | 383 | #ifndef OPENSSL_NO_SIPHASH |
| 384 | const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len) |
| 385 | { |
| 386 | ASN1_OCTET_STRING *os = NULL; |
| 387 | |
| 388 | if (pkey->type != EVP_PKEY_SIPHASH) { |
| 389 | EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY); |
| 390 | return NULL; |
| 391 | } |
| 392 | os = EVP_PKEY_get0(pkey); |
| 393 | *len = os->length; |
| 394 | return os->data; |
| 395 | } |
| 396 | #endif |
| 397 | |
Richard Levitte | cf1b7d9 | 2001-02-19 16:06:34 +0000 | [diff] [blame] | 398 | #ifndef OPENSSL_NO_RSA |
Dr. Stephen Henson | c7cb16a | 2000-02-26 01:55:33 +0000 | [diff] [blame] | 399 | int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) |
Dr. Stephen Henson | 52664f5 | 1999-11-21 22:28:31 +0000 | [diff] [blame] | 400 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 401 | int ret = EVP_PKEY_assign_RSA(pkey, key); |
| 402 | if (ret) |
| 403 | RSA_up_ref(key); |
| 404 | return ret; |
Dr. Stephen Henson | 52664f5 | 1999-11-21 22:28:31 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 407 | RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 408 | { |
| 409 | if (pkey->type != EVP_PKEY_RSA) { |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 410 | EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 411 | return NULL; |
| 412 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 413 | return pkey->pkey.rsa; |
Dr. Stephen Henson | f769ce3 | 1999-10-25 02:00:09 +0000 | [diff] [blame] | 414 | } |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 415 | |
| 416 | RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) |
| 417 | { |
| 418 | RSA *ret = EVP_PKEY_get0_RSA(pkey); |
| 419 | if (ret != NULL) |
| 420 | RSA_up_ref(ret); |
| 421 | return ret; |
| 422 | } |
Dr. Stephen Henson | f769ce3 | 1999-10-25 02:00:09 +0000 | [diff] [blame] | 423 | #endif |
| 424 | |
Richard Levitte | cf1b7d9 | 2001-02-19 16:06:34 +0000 | [diff] [blame] | 425 | #ifndef OPENSSL_NO_DSA |
Dr. Stephen Henson | c7cb16a | 2000-02-26 01:55:33 +0000 | [diff] [blame] | 426 | int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) |
Dr. Stephen Henson | 52664f5 | 1999-11-21 22:28:31 +0000 | [diff] [blame] | 427 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 428 | int ret = EVP_PKEY_assign_DSA(pkey, key); |
| 429 | if (ret) |
| 430 | DSA_up_ref(key); |
| 431 | return ret; |
Dr. Stephen Henson | 52664f5 | 1999-11-21 22:28:31 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 434 | DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 435 | { |
| 436 | if (pkey->type != EVP_PKEY_DSA) { |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 437 | EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 438 | return NULL; |
| 439 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 440 | return pkey->pkey.dsa; |
Dr. Stephen Henson | f769ce3 | 1999-10-25 02:00:09 +0000 | [diff] [blame] | 441 | } |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 442 | |
| 443 | DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) |
| 444 | { |
| 445 | DSA *ret = EVP_PKEY_get0_DSA(pkey); |
| 446 | if (ret != NULL) |
| 447 | DSA_up_ref(ret); |
| 448 | return ret; |
| 449 | } |
Dr. Stephen Henson | f769ce3 | 1999-10-25 02:00:09 +0000 | [diff] [blame] | 450 | #endif |
| 451 | |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 452 | #ifndef OPENSSL_NO_EC |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 453 | |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 454 | int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 455 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 456 | int ret = EVP_PKEY_assign_EC_KEY(pkey, key); |
| 457 | if (ret) |
| 458 | EC_KEY_up_ref(key); |
| 459 | return ret; |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 462 | EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 463 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 464 | if (pkey->type != EVP_PKEY_EC) { |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 465 | EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 466 | return NULL; |
| 467 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 468 | return pkey->pkey.ec; |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 469 | } |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 470 | |
| 471 | EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) |
| 472 | { |
| 473 | EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey); |
| 474 | if (ret != NULL) |
| 475 | EC_KEY_up_ref(ret); |
| 476 | return ret; |
| 477 | } |
Bodo Möller | 4d94ae0 | 2002-02-13 18:21:51 +0000 | [diff] [blame] | 478 | #endif |
| 479 | |
Richard Levitte | cf1b7d9 | 2001-02-19 16:06:34 +0000 | [diff] [blame] | 480 | #ifndef OPENSSL_NO_DH |
Dr. Stephen Henson | 52664f5 | 1999-11-21 22:28:31 +0000 | [diff] [blame] | 481 | |
Dr. Stephen Henson | c7cb16a | 2000-02-26 01:55:33 +0000 | [diff] [blame] | 482 | int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) |
Dr. Stephen Henson | 52664f5 | 1999-11-21 22:28:31 +0000 | [diff] [blame] | 483 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 484 | int ret = EVP_PKEY_assign_DH(pkey, key); |
| 485 | if (ret) |
| 486 | DH_up_ref(key); |
| 487 | return ret; |
Dr. Stephen Henson | 52664f5 | 1999-11-21 22:28:31 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 490 | DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 491 | { |
| 492 | if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) { |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 493 | EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 494 | return NULL; |
| 495 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 496 | return pkey->pkey.dh; |
Dr. Stephen Henson | f769ce3 | 1999-10-25 02:00:09 +0000 | [diff] [blame] | 497 | } |
Dr. Stephen Henson | 2872dbe | 2015-12-13 17:57:01 +0000 | [diff] [blame] | 498 | |
| 499 | DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) |
| 500 | { |
| 501 | DH *ret = EVP_PKEY_get0_DH(pkey); |
| 502 | if (ret != NULL) |
| 503 | DH_up_ref(ret); |
| 504 | return ret; |
| 505 | } |
Dr. Stephen Henson | f769ce3 | 1999-10-25 02:00:09 +0000 | [diff] [blame] | 506 | #endif |
| 507 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 508 | int EVP_PKEY_type(int type) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 509 | { |
| 510 | int ret; |
| 511 | const EVP_PKEY_ASN1_METHOD *ameth; |
| 512 | ENGINE *e; |
| 513 | ameth = EVP_PKEY_asn1_find(&e, type); |
| 514 | if (ameth) |
| 515 | ret = ameth->pkey_id; |
| 516 | else |
| 517 | ret = NID_undef; |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 518 | #ifndef OPENSSL_NO_ENGINE |
Rich Salz | 7c96dbc | 2016-02-25 12:09:06 -0500 | [diff] [blame] | 519 | ENGINE_finish(e); |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 520 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 521 | return ret; |
| 522 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 523 | |
Dr. Stephen Henson | 7f57b07 | 2006-05-11 21:33:00 +0000 | [diff] [blame] | 524 | int EVP_PKEY_id(const EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 525 | { |
| 526 | return pkey->type; |
| 527 | } |
Dr. Stephen Henson | 7f57b07 | 2006-05-11 21:33:00 +0000 | [diff] [blame] | 528 | |
| 529 | int EVP_PKEY_base_id(const EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 530 | { |
| 531 | return EVP_PKEY_type(pkey->type); |
| 532 | } |
Dr. Stephen Henson | 7f57b07 | 2006-05-11 21:33:00 +0000 | [diff] [blame] | 533 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 534 | void EVP_PKEY_free(EVP_PKEY *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 535 | { |
| 536 | int i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 537 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 538 | if (x == NULL) |
| 539 | return; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 540 | |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 541 | CRYPTO_DOWN_REF(&x->references, &i, x->lock); |
Rich Salz | f3f1cf8 | 2016-01-30 12:04:25 -0500 | [diff] [blame] | 542 | REF_PRINT_COUNT("EVP_PKEY", x); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 543 | if (i > 0) |
| 544 | return; |
Rich Salz | f3f1cf8 | 2016-01-30 12:04:25 -0500 | [diff] [blame] | 545 | REF_ASSERT_ISNT(i < 0); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 546 | EVP_PKEY_free_it(x); |
Alex Gaynor | d65c361 | 2016-09-07 09:41:20 -0400 | [diff] [blame] | 547 | CRYPTO_THREAD_lock_free(x->lock); |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 548 | sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 549 | OPENSSL_free(x); |
| 550 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 551 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 552 | static void EVP_PKEY_free_it(EVP_PKEY *x) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 553 | { |
Rich Salz | c5ba2d9 | 2015-03-28 10:54:15 -0400 | [diff] [blame] | 554 | /* internal function; x is never NULL */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 555 | if (x->ameth && x->ameth->pkey_free) { |
| 556 | x->ameth->pkey_free(x); |
| 557 | x->pkey.ptr = NULL; |
| 558 | } |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 559 | #ifndef OPENSSL_NO_ENGINE |
Rich Salz | 7c96dbc | 2016-02-25 12:09:06 -0500 | [diff] [blame] | 560 | ENGINE_finish(x->engine); |
| 561 | x->engine = NULL; |
Dr. Stephen Henson | d19b01a | 2017-10-09 15:21:11 +0100 | [diff] [blame] | 562 | ENGINE_finish(x->pmeth_engine); |
| 563 | x->pmeth_engine = NULL; |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 564 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 565 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 566 | |
Dr. Stephen Henson | 35208f3 | 2006-03-22 13:09:35 +0000 | [diff] [blame] | 567 | static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 568 | const char *kstr) |
| 569 | { |
| 570 | BIO_indent(out, indent, 128); |
| 571 | BIO_printf(out, "%s algorithm \"%s\" unsupported\n", |
| 572 | kstr, OBJ_nid2ln(pkey->type)); |
| 573 | return 1; |
| 574 | } |
Dr. Stephen Henson | 35208f3 | 2006-03-22 13:09:35 +0000 | [diff] [blame] | 575 | |
| 576 | int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 577 | int indent, ASN1_PCTX *pctx) |
| 578 | { |
| 579 | if (pkey->ameth && pkey->ameth->pub_print) |
| 580 | return pkey->ameth->pub_print(out, pkey, indent, pctx); |
| 581 | |
| 582 | return unsup_alg(out, pkey, indent, "Public Key"); |
| 583 | } |
Dr. Stephen Henson | 35208f3 | 2006-03-22 13:09:35 +0000 | [diff] [blame] | 584 | |
| 585 | int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 586 | int indent, ASN1_PCTX *pctx) |
| 587 | { |
| 588 | if (pkey->ameth && pkey->ameth->priv_print) |
| 589 | return pkey->ameth->priv_print(out, pkey, indent, pctx); |
| 590 | |
| 591 | return unsup_alg(out, pkey, indent, "Private Key"); |
| 592 | } |
Dr. Stephen Henson | 35208f3 | 2006-03-22 13:09:35 +0000 | [diff] [blame] | 593 | |
| 594 | int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 595 | int indent, ASN1_PCTX *pctx) |
| 596 | { |
| 597 | if (pkey->ameth && pkey->ameth->param_print) |
| 598 | return pkey->ameth->param_print(out, pkey, indent, pctx); |
| 599 | return unsup_alg(out, pkey, indent, "Parameters"); |
| 600 | } |
Dr. Stephen Henson | 0391968 | 2006-05-07 17:09:39 +0000 | [diff] [blame] | 601 | |
Dr. Stephen Henson | 5d6aaf8 | 2016-08-10 22:30:43 +0100 | [diff] [blame] | 602 | static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2) |
| 603 | { |
| 604 | if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL) |
| 605 | return -2; |
| 606 | return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2); |
| 607 | } |
| 608 | |
Dr. Stephen Henson | 0391968 | 2006-05-07 17:09:39 +0000 | [diff] [blame] | 609 | int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 610 | { |
Dr. Stephen Henson | 5d6aaf8 | 2016-08-10 22:30:43 +0100 | [diff] [blame] | 611 | return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid); |
| 612 | } |
| 613 | |
| 614 | int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, |
| 615 | const unsigned char *pt, size_t ptlen) |
| 616 | { |
| 617 | if (ptlen > INT_MAX) |
| 618 | return 0; |
| 619 | if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen, |
| 620 | (void *)pt) <= 0) |
| 621 | return 0; |
| 622 | return 1; |
| 623 | } |
| 624 | |
| 625 | size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt) |
| 626 | { |
| 627 | int rv; |
| 628 | rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt); |
| 629 | if (rv <= 0) |
| 630 | return 0; |
| 631 | return rv; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 632 | } |