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