1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1 christos EVP_PKEY_fromdata_init, EVP_PKEY_fromdata, EVP_PKEY_fromdata_settable 6 1.1 christos - functions to create keys and key parameters from user data 7 1.1 christos 8 1.1 christos =head1 SYNOPSIS 9 1.1 christos 10 1.1 christos #include <openssl/evp.h> 11 1.1 christos 12 1.1 christos int EVP_PKEY_fromdata_init(EVP_PKEY_CTX *ctx); 13 1.1 christos int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection, 14 1.1 christos OSSL_PARAM params[]); 15 1.1 christos const OSSL_PARAM *EVP_PKEY_fromdata_settable(EVP_PKEY_CTX *ctx, int selection); 16 1.1 christos 17 1.1 christos =head1 DESCRIPTION 18 1.1 christos 19 1.1 christos The functions described here are used to create new keys from user 20 1.1 christos provided key data, such as I<n>, I<e> and I<d> for a minimal RSA 21 1.1 christos keypair. 22 1.1 christos 23 1.1 christos These functions use an B<EVP_PKEY_CTX> context, which should primarily 24 1.1 christos be created with L<EVP_PKEY_CTX_new_from_name(3)> or 25 1.1 christos L<EVP_PKEY_CTX_new_id(3)>. 26 1.1 christos 27 1.1 christos The exact key data that the user can pass depends on the key type. 28 1.1 christos These are passed as an L<OSSL_PARAM(3)> array. 29 1.1 christos 30 1.1 christos EVP_PKEY_fromdata_init() initializes a public key algorithm context 31 1.1 christos for creating a key or key parameters from user data. 32 1.1 christos 33 1.1 christos EVP_PKEY_fromdata() creates the structure to store a key or key parameters, 34 1.1 christos given data from I<params>, I<selection> and a context that's been initialized 35 1.1 christos with EVP_PKEY_fromdata_init(). The result is written to I<*ppkey>. 36 1.1 christos I<selection> is described in L</Selections>. 37 1.1 christos The parameters that can be used for various types of key are as described by the 38 1.1 christos diverse "Common parameters" sections of the 39 1.1 christos L<B<EVP_PKEY-RSA>(7)|EVP_PKEY-RSA(7)/Common RSA parameters>, 40 1.1 christos L<B<EVP_PKEY-DSA>(7)|EVP_PKEY-DSA(7)/Common DSA & DH parameters>, 41 1.1 christos L<B<EVP_PKEY-DH>(7)|EVP_PKEY-DH(7)/Common DH parameters>, 42 1.1 christos L<B<EVP_PKEY-EC>(7)|EVP_PKEY-EC(7)/Common EC parameters>, 43 1.1 christos L<B<EVP_PKEY-ED448>(7)|EVP_PKEY-ED448(7)/Common X25519, X448, ED25519 and ED448 parameters>, 44 1.1 christos L<B<EVP_PKEY-X25519>(7)|EVP_PKEY-X25519(7)/Common X25519, X448, ED25519 and ED448 parameters>, 45 1.1 christos L<B<EVP_PKEY-X448>(7)|EVP_PKEY-X448(7)/Common X25519, X448, ED25519 and ED448 parameters>, 46 1.1 christos and L<B<EVP_PKEY-ED25519>(7)|EVP_PKEY-ED25519(7)/Common X25519, X448, ED25519 and ED448 parameters> pages. 47 1.1 christos 48 1.1 christos =for comment the awful list of links above is made this way so we get nice 49 1.1 christos rendering as a man-page while still getting proper links in HTML 50 1.1 christos 51 1.1 christos EVP_PKEY_fromdata_settable() gets a constant L<OSSL_PARAM(3)> array that describes 52 1.1 christos the settable parameters that can be used with EVP_PKEY_fromdata(). 53 1.1 christos I<selection> is described in L</Selections>. 54 1.1 christos 55 1.1 christos Parameters in the I<params> array that are not among the settable parameters 56 1.1 christos for the given I<selection> are ignored. 57 1.1 christos 58 1.1 christos =head2 Selections 59 1.1 christos 60 1.1 christos The following constants can be used for I<selection>: 61 1.1 christos 62 1.1 christos =over 4 63 1.1 christos 64 1.1 christos =item B<EVP_PKEY_KEY_PARAMETERS> 65 1.1 christos 66 1.1 christos Only key parameters will be selected. 67 1.1 christos 68 1.1 christos =item B<EVP_PKEY_PUBLIC_KEY> 69 1.1 christos 70 1.1 christos Only public key components will be selected. This includes optional key 71 1.1 christos parameters. 72 1.1 christos 73 1.1 christos =item B<EVP_PKEY_KEYPAIR> 74 1.1 christos 75 1.1 christos Any keypair components will be selected. This includes the private key, 76 1.1 christos public key and key parameters. 77 1.1 christos 78 1.1 christos =back 79 1.1 christos 80 1.1 christos =head1 NOTES 81 1.1 christos 82 1.1 christos These functions only work with key management methods coming from a provider. 83 1.1 christos This is the mirror function to L<EVP_PKEY_todata(3)>. 84 1.1 christos 85 1.1 christos =for comment We may choose to make this available for legacy methods too... 86 1.1 christos 87 1.1 christos =head1 RETURN VALUES 88 1.1 christos 89 1.1 christos EVP_PKEY_fromdata_init() and EVP_PKEY_fromdata() return 1 for success and 0 or 90 1.1 christos a negative value for failure. In particular a return value of -2 indicates the 91 1.1 christos operation is not supported by the public key algorithm. 92 1.1 christos 93 1.1 christos =head1 EXAMPLES 94 1.1 christos 95 1.1 christos These examples are very terse for the sake of staying on topic, which 96 1.1 christos is the EVP_PKEY_fromdata() set of functions. In real applications, 97 1.1 christos BIGNUMs would be handled and converted to byte arrays with 98 1.1 christos BN_bn2nativepad(), but that's off topic here. 99 1.1 christos 100 1.1 christos =begin comment 101 1.1 christos 102 1.1 christos TODO Write a set of cookbook documents and link to them. 103 1.1 christos 104 1.1 christos =end comment 105 1.1 christos 106 1.1 christos =head2 Creating an RSA keypair using raw key data 107 1.1 christos 108 1.1 christos #include <openssl/evp.h> 109 1.1 christos 110 1.1 christos /* 111 1.1 christos * These are extremely small to make this example simple. A real 112 1.1 christos * and secure application will not use such small numbers. A real 113 1.1 christos * and secure application is expected to use BIGNUMs, and to build 114 1.1 christos * this array dynamically. 115 1.1 christos */ 116 1.1 christos unsigned long rsa_n = 0xbc747fc5; 117 1.1 christos unsigned long rsa_e = 0x10001; 118 1.1 christos unsigned long rsa_d = 0x7b133399; 119 1.1 christos OSSL_PARAM params[] = { 120 1.1 christos OSSL_PARAM_ulong("n", &rsa_n), 121 1.1 christos OSSL_PARAM_ulong("e", &rsa_e), 122 1.1 christos OSSL_PARAM_ulong("d", &rsa_d), 123 1.1 christos OSSL_PARAM_END 124 1.1 christos }; 125 1.1 christos 126 1.1 christos int main() 127 1.1 christos { 128 1.1 christos EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL); 129 1.1 christos EVP_PKEY *pkey = NULL; 130 1.1 christos 131 1.1 christos if (ctx == NULL 132 1.1 christos || EVP_PKEY_fromdata_init(ctx) <= 0 133 1.1 christos || EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) 134 1.1 christos exit(1); 135 1.1 christos 136 1.1 christos /* Do what you want with |pkey| */ 137 1.1 christos } 138 1.1 christos 139 1.1 christos =head2 Creating an ECC keypair using raw key data 140 1.1 christos 141 1.1 christos #include <openssl/evp.h> 142 1.1 christos #include <openssl/param_build.h> 143 1.1 christos #include <openssl/ec.h> 144 1.1 christos 145 1.1 christos /* 146 1.1 christos * Fixed data to represent the private and public key. 147 1.1 christos */ 148 1.1 christos const unsigned char priv_data[] = { 149 1.1 christos 0xb9, 0x2f, 0x3c, 0xe6, 0x2f, 0xfb, 0x45, 0x68, 150 1.1 christos 0x39, 0x96, 0xf0, 0x2a, 0xaf, 0x6c, 0xda, 0xf2, 151 1.1 christos 0x89, 0x8a, 0x27, 0xbf, 0x39, 0x9b, 0x7e, 0x54, 152 1.1 christos 0x21, 0xc2, 0xa1, 0xe5, 0x36, 0x12, 0x48, 0x5d 153 1.1 christos }; 154 1.1 christos /* UNCOMPRESSED FORMAT */ 155 1.1 christos const unsigned char pub_data[] = { 156 1.1 christos POINT_CONVERSION_UNCOMPRESSED, 157 1.1 christos 0xcf, 0x20, 0xfb, 0x9a, 0x1d, 0x11, 0x6c, 0x5e, 158 1.1 christos 0x9f, 0xec, 0x38, 0x87, 0x6c, 0x1d, 0x2f, 0x58, 159 1.1 christos 0x47, 0xab, 0xa3, 0x9b, 0x79, 0x23, 0xe6, 0xeb, 160 1.1 christos 0x94, 0x6f, 0x97, 0xdb, 0xa3, 0x7d, 0xbd, 0xe5, 161 1.1 christos 0x26, 0xca, 0x07, 0x17, 0x8d, 0x26, 0x75, 0xff, 162 1.1 christos 0xcb, 0x8e, 0xb6, 0x84, 0xd0, 0x24, 0x02, 0x25, 163 1.1 christos 0x8f, 0xb9, 0x33, 0x6e, 0xcf, 0x12, 0x16, 0x2f, 164 1.1 christos 0x5c, 0xcd, 0x86, 0x71, 0xa8, 0xbf, 0x1a, 0x47 165 1.1 christos }; 166 1.1 christos 167 1.1 christos int main() 168 1.1 christos { 169 1.1 christos EVP_PKEY_CTX *ctx; 170 1.1 christos EVP_PKEY *pkey = NULL; 171 1.1 christos BIGNUM *priv; 172 1.1 christos OSSL_PARAM_BLD *param_bld; 173 1.1 christos OSSL_PARAM *params = NULL; 174 1.1 christos int exitcode = 0; 175 1.1 christos 176 1.1 christos priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL); 177 1.1 christos 178 1.1 christos param_bld = OSSL_PARAM_BLD_new(); 179 1.1 christos if (priv != NULL && param_bld != NULL 180 1.1 christos && OSSL_PARAM_BLD_push_utf8_string(param_bld, "group", 181 1.1 christos "prime256v1", 0) 182 1.1 christos && OSSL_PARAM_BLD_push_BN(param_bld, "priv", priv) 183 1.1 christos && OSSL_PARAM_BLD_push_octet_string(param_bld, "pub", 184 1.1 christos pub_data, sizeof(pub_data))) 185 1.1 christos params = OSSL_PARAM_BLD_to_param(param_bld); 186 1.1 christos 187 1.1 christos ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL); 188 1.1 christos if (ctx == NULL 189 1.1 christos || params == NULL 190 1.1 christos || EVP_PKEY_fromdata_init(ctx) <= 0 191 1.1 christos || EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) { 192 1.1 christos exitcode = 1; 193 1.1 christos } else { 194 1.1 christos /* Do what you want with |pkey| */ 195 1.1 christos } 196 1.1 christos 197 1.1 christos EVP_PKEY_free(pkey); 198 1.1 christos EVP_PKEY_CTX_free(ctx); 199 1.1 christos OSSL_PARAM_free(params); 200 1.1 christos OSSL_PARAM_BLD_free(param_bld); 201 1.1 christos BN_free(priv); 202 1.1 christos 203 1.1 christos exit(exitcode); 204 1.1 christos } 205 1.1 christos 206 1.1 christos =head2 Finding out params for an unknown key type 207 1.1 christos 208 1.1 christos #include <openssl/evp.h> 209 1.1 christos #include <openssl/core.h> 210 1.1 christos 211 1.1 christos /* Program expects a key type as first argument */ 212 1.1 christos int main(int argc, char *argv[]) 213 1.1 christos { 214 1.1 christos EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, argv[1], NULL); 215 1.1 christos const OSSL_PARAM *settable_params = NULL; 216 1.1 christos 217 1.1 christos if (ctx == NULL) 218 1.1 christos exit(1); 219 1.1 christos settable_params = EVP_PKEY_fromdata_settable(ctx, EVP_PKEY_KEYPAIR); 220 1.1 christos if (settable_params == NULL) 221 1.1 christos exit(1); 222 1.1 christos 223 1.1 christos for (; settable_params->key != NULL; settable_params++) { 224 1.1 christos const char *datatype = NULL; 225 1.1 christos 226 1.1 christos switch (settable_params->data_type) { 227 1.1 christos case OSSL_PARAM_INTEGER: 228 1.1 christos datatype = "integer"; 229 1.1 christos break; 230 1.1 christos case OSSL_PARAM_UNSIGNED_INTEGER: 231 1.1 christos datatype = "unsigned integer"; 232 1.1 christos break; 233 1.1 christos case OSSL_PARAM_UTF8_STRING: 234 1.1 christos datatype = "printable string (utf-8 encoding expected)"; 235 1.1 christos break; 236 1.1 christos case OSSL_PARAM_UTF8_PTR: 237 1.1 christos datatype = "printable string pointer (utf-8 encoding expected)"; 238 1.1 christos break; 239 1.1 christos case OSSL_PARAM_OCTET_STRING: 240 1.1 christos datatype = "octet string"; 241 1.1 christos break; 242 1.1 christos case OSSL_PARAM_OCTET_PTR: 243 1.1 christos datatype = "octet string pointer"; 244 1.1 christos break; 245 1.1 christos } 246 1.1 christos printf("%s : %s ", settable_params->key, datatype); 247 1.1 christos if (settable_params->data_size == 0) 248 1.1 christos printf("(unlimited size)\n"); 249 1.1 christos else 250 1.1 christos printf("(maximum size %zu)\n", settable_params->data_size); 251 1.1 christos } 252 1.1 christos } 253 1.1 christos 254 1.1 christos The descriptor L<OSSL_PARAM(3)> returned by 255 1.1 christos EVP_PKEY_fromdata_settable() may also be used programmatically, for 256 1.1 christos example with L<OSSL_PARAM_allocate_from_text(3)>. 257 1.1 christos 258 1.1 christos =head1 SEE ALSO 259 1.1 christos 260 1.1 christos L<EVP_PKEY_CTX_new(3)>, L<provider(7)>, L<EVP_PKEY_gettable_params(3)>, 261 1.1 christos L<OSSL_PARAM(3)>, L<EVP_PKEY_todata(3)>, 262 1.1 christos L<EVP_PKEY-RSA(7)>, L<EVP_PKEY-DSA(7)>, L<EVP_PKEY-DH(7)>, L<EVP_PKEY-EC(7)>, 263 1.1 christos L<EVP_PKEY-ED448(7)>, L<EVP_PKEY-X25519(7)>, L<EVP_PKEY-X448(7)>, 264 1.1 christos L<EVP_PKEY-ED25519(7)> 265 1.1 christos 266 1.1 christos =head1 HISTORY 267 1.1 christos 268 1.1 christos These functions were added in OpenSSL 3.0. 269 1.1 christos 270 1.1 christos =head1 COPYRIGHT 271 1.1 christos 272 1.1 christos Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved. 273 1.1 christos 274 1.1 christos Licensed under the Apache License 2.0 (the "License"). You may not use 275 1.1 christos this file except in compliance with the License. You can obtain a copy 276 1.1 christos in the file LICENSE in the source distribution or at 277 1.1 christos L<https://www.openssl.org/source/license.html>. 278 1.1 christos 279 1.1 christos =cut 280 1.1 christos 281