EVP_PKEY_fromdata.3ossl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "EVP_PKEY_FROMDATA 3ossl"
  58. .TH EVP_PKEY_FROMDATA 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. EVP_PKEY_fromdata_init, EVP_PKEY_fromdata, EVP_PKEY_fromdata_settable
  65. \&\- functions to create keys and key parameters from user data
  66. .SH SYNOPSIS
  67. .IX Header "SYNOPSIS"
  68. .Vb 1
  69. \& #include <openssl/evp.h>
  70. \&
  71. \& int EVP_PKEY_fromdata_init(EVP_PKEY_CTX *ctx);
  72. \& int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection,
  73. \& OSSL_PARAM params[]);
  74. \& const OSSL_PARAM *EVP_PKEY_fromdata_settable(EVP_PKEY_CTX *ctx, int selection);
  75. .Ve
  76. .SH DESCRIPTION
  77. .IX Header "DESCRIPTION"
  78. The functions described here are used to create new keys from user
  79. provided key data, such as \fIn\fR, \fIe\fR and \fId\fR for a minimal RSA
  80. keypair.
  81. .PP
  82. These functions use an \fBEVP_PKEY_CTX\fR context, which should primarily
  83. be created with \fBEVP_PKEY_CTX_new_from_name\fR\|(3) or
  84. \&\fBEVP_PKEY_CTX_new_id\fR\|(3).
  85. .PP
  86. The exact key data that the user can pass depends on the key type.
  87. These are passed as an \fBOSSL_PARAM\fR\|(3) array.
  88. .PP
  89. \&\fBEVP_PKEY_fromdata_init()\fR initializes a public key algorithm context
  90. for creating a key or key parameters from user data.
  91. .PP
  92. \&\fBEVP_PKEY_fromdata()\fR creates the structure to store a key or key parameters,
  93. given data from \fIparams\fR, \fIselection\fR and a context that's been initialized
  94. with \fBEVP_PKEY_fromdata_init()\fR. The result is written to \fI*ppkey\fR.
  95. \&\fIselection\fR is described in "Selections".
  96. The parameters that can be used for various types of key are as described by the
  97. diverse "Common parameters" sections of the
  98. \&\fBEVP_PKEY\-RSA\fR(7),
  99. \&\fBEVP_PKEY\-DSA\fR(7),
  100. \&\fBEVP_PKEY\-DH\fR(7),
  101. \&\fBEVP_PKEY\-EC\fR(7),
  102. \&\fBEVP_PKEY\-ED448\fR(7),
  103. \&\fBEVP_PKEY\-X25519\fR(7),
  104. \&\fBEVP_PKEY\-X448\fR(7),
  105. and \fBEVP_PKEY\-ED25519\fR(7) pages.
  106. .PP
  107. \&\fBEVP_PKEY_fromdata_settable()\fR gets a constant \fBOSSL_PARAM\fR\|(3) array that describes
  108. the settable parameters that can be used with \fBEVP_PKEY_fromdata()\fR.
  109. \&\fIselection\fR is described in "Selections".
  110. .PP
  111. Parameters in the \fIparams\fR array that are not among the settable parameters
  112. for the given \fIselection\fR are ignored.
  113. .SS Selections
  114. .IX Subsection "Selections"
  115. The following constants can be used for \fIselection\fR:
  116. .IP \fBEVP_PKEY_KEY_PARAMETERS\fR 4
  117. .IX Item "EVP_PKEY_KEY_PARAMETERS"
  118. Only key parameters will be selected.
  119. .IP \fBEVP_PKEY_PUBLIC_KEY\fR 4
  120. .IX Item "EVP_PKEY_PUBLIC_KEY"
  121. Only public key components will be selected. This includes optional key
  122. parameters.
  123. .IP \fBEVP_PKEY_KEYPAIR\fR 4
  124. .IX Item "EVP_PKEY_KEYPAIR"
  125. Any keypair components will be selected. This includes the private key,
  126. public key and key parameters.
  127. .SH NOTES
  128. .IX Header "NOTES"
  129. These functions only work with key management methods coming from a provider.
  130. This is the mirror function to \fBEVP_PKEY_todata\fR\|(3).
  131. .SH "RETURN VALUES"
  132. .IX Header "RETURN VALUES"
  133. \&\fBEVP_PKEY_fromdata_init()\fR and \fBEVP_PKEY_fromdata()\fR return 1 for success and 0 or
  134. a negative value for failure. In particular a return value of \-2 indicates the
  135. operation is not supported by the public key algorithm.
  136. .SH EXAMPLES
  137. .IX Header "EXAMPLES"
  138. These examples are very terse for the sake of staying on topic, which
  139. is the \fBEVP_PKEY_fromdata()\fR set of functions. In real applications,
  140. BIGNUMs would be handled and converted to byte arrays with
  141. \&\fBBN_bn2nativepad()\fR, but that's off topic here.
  142. .SS "Creating an RSA keypair using raw key data"
  143. .IX Subsection "Creating an RSA keypair using raw key data"
  144. .Vb 1
  145. \& #include <openssl/evp.h>
  146. \&
  147. \& /*
  148. \& * These are extremely small to make this example simple. A real
  149. \& * and secure application will not use such small numbers. A real
  150. \& * and secure application is expected to use BIGNUMs, and to build
  151. \& * this array dynamically.
  152. \& */
  153. \& unsigned long rsa_n = 0xbc747fc5;
  154. \& unsigned long rsa_e = 0x10001;
  155. \& unsigned long rsa_d = 0x7b133399;
  156. \& OSSL_PARAM params[] = {
  157. \& OSSL_PARAM_ulong("n", &rsa_n),
  158. \& OSSL_PARAM_ulong("e", &rsa_e),
  159. \& OSSL_PARAM_ulong("d", &rsa_d),
  160. \& OSSL_PARAM_END
  161. \& };
  162. \&
  163. \& int main()
  164. \& {
  165. \& EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
  166. \& EVP_PKEY *pkey = NULL;
  167. \&
  168. \& if (ctx == NULL
  169. \& || EVP_PKEY_fromdata_init(ctx) <= 0
  170. \& || EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
  171. \& exit(1);
  172. \&
  173. \& /* Do what you want with |pkey| */
  174. \& }
  175. .Ve
  176. .SS "Creating an ECC keypair using raw key data"
  177. .IX Subsection "Creating an ECC keypair using raw key data"
  178. .Vb 3
  179. \& #include <openssl/evp.h>
  180. \& #include <openssl/param_build.h>
  181. \& #include <openssl/ec.h>
  182. \&
  183. \& /*
  184. \& * Fixed data to represent the private and public key.
  185. \& */
  186. \& const unsigned char priv_data[] = {
  187. \& 0xb9, 0x2f, 0x3c, 0xe6, 0x2f, 0xfb, 0x45, 0x68,
  188. \& 0x39, 0x96, 0xf0, 0x2a, 0xaf, 0x6c, 0xda, 0xf2,
  189. \& 0x89, 0x8a, 0x27, 0xbf, 0x39, 0x9b, 0x7e, 0x54,
  190. \& 0x21, 0xc2, 0xa1, 0xe5, 0x36, 0x12, 0x48, 0x5d
  191. \& };
  192. \& /* UNCOMPRESSED FORMAT */
  193. \& const unsigned char pub_data[] = {
  194. \& POINT_CONVERSION_UNCOMPRESSED,
  195. \& 0xcf, 0x20, 0xfb, 0x9a, 0x1d, 0x11, 0x6c, 0x5e,
  196. \& 0x9f, 0xec, 0x38, 0x87, 0x6c, 0x1d, 0x2f, 0x58,
  197. \& 0x47, 0xab, 0xa3, 0x9b, 0x79, 0x23, 0xe6, 0xeb,
  198. \& 0x94, 0x6f, 0x97, 0xdb, 0xa3, 0x7d, 0xbd, 0xe5,
  199. \& 0x26, 0xca, 0x07, 0x17, 0x8d, 0x26, 0x75, 0xff,
  200. \& 0xcb, 0x8e, 0xb6, 0x84, 0xd0, 0x24, 0x02, 0x25,
  201. \& 0x8f, 0xb9, 0x33, 0x6e, 0xcf, 0x12, 0x16, 0x2f,
  202. \& 0x5c, 0xcd, 0x86, 0x71, 0xa8, 0xbf, 0x1a, 0x47
  203. \& };
  204. \&
  205. \& int main()
  206. \& {
  207. \& EVP_PKEY_CTX *ctx;
  208. \& EVP_PKEY *pkey = NULL;
  209. \& BIGNUM *priv;
  210. \& OSSL_PARAM_BLD *param_bld;
  211. \& OSSL_PARAM *params = NULL;
  212. \& int exitcode = 0;
  213. \&
  214. \& priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL);
  215. \&
  216. \& param_bld = OSSL_PARAM_BLD_new();
  217. \& if (priv != NULL && param_bld != NULL
  218. \& && OSSL_PARAM_BLD_push_utf8_string(param_bld, "group",
  219. \& "prime256v1", 0)
  220. \& && OSSL_PARAM_BLD_push_BN(param_bld, "priv", priv)
  221. \& && OSSL_PARAM_BLD_push_octet_string(param_bld, "pub",
  222. \& pub_data, sizeof(pub_data)))
  223. \& params = OSSL_PARAM_BLD_to_param(param_bld);
  224. \&
  225. \& ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
  226. \& if (ctx == NULL
  227. \& || params == NULL
  228. \& || EVP_PKEY_fromdata_init(ctx) <= 0
  229. \& || EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) {
  230. \& exitcode = 1;
  231. \& } else {
  232. \& /* Do what you want with |pkey| */
  233. \& }
  234. \&
  235. \& EVP_PKEY_free(pkey);
  236. \& EVP_PKEY_CTX_free(ctx);
  237. \& OSSL_PARAM_free(params);
  238. \& OSSL_PARAM_BLD_free(param_bld);
  239. \& BN_free(priv);
  240. \&
  241. \& exit(exitcode);
  242. \& }
  243. .Ve
  244. .SS "Finding out params for an unknown key type"
  245. .IX Subsection "Finding out params for an unknown key type"
  246. .Vb 2
  247. \& #include <openssl/evp.h>
  248. \& #include <openssl/core.h>
  249. \&
  250. \& /* Program expects a key type as first argument */
  251. \& int main(int argc, char *argv[])
  252. \& {
  253. \& EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, argv[1], NULL);
  254. \& const OSSL_PARAM *settable_params = NULL;
  255. \&
  256. \& if (ctx == NULL)
  257. \& exit(1);
  258. \& settable_params = EVP_PKEY_fromdata_settable(ctx, EVP_PKEY_KEYPAIR);
  259. \& if (settable_params == NULL)
  260. \& exit(1);
  261. \&
  262. \& for (; settable_params\->key != NULL; settable_params++) {
  263. \& const char *datatype = NULL;
  264. \&
  265. \& switch (settable_params\->data_type) {
  266. \& case OSSL_PARAM_INTEGER:
  267. \& datatype = "integer";
  268. \& break;
  269. \& case OSSL_PARAM_UNSIGNED_INTEGER:
  270. \& datatype = "unsigned integer";
  271. \& break;
  272. \& case OSSL_PARAM_UTF8_STRING:
  273. \& datatype = "printable string (utf\-8 encoding expected)";
  274. \& break;
  275. \& case OSSL_PARAM_UTF8_PTR:
  276. \& datatype = "printable string pointer (utf\-8 encoding expected)";
  277. \& break;
  278. \& case OSSL_PARAM_OCTET_STRING:
  279. \& datatype = "octet string";
  280. \& break;
  281. \& case OSSL_PARAM_OCTET_PTR:
  282. \& datatype = "octet string pointer";
  283. \& break;
  284. \& }
  285. \& printf("%s : %s ", settable_params\->key, datatype);
  286. \& if (settable_params\->data_size == 0)
  287. \& printf("(unlimited size)\en");
  288. \& else
  289. \& printf("(maximum size %zu)\en", settable_params\->data_size);
  290. \& }
  291. \& }
  292. .Ve
  293. .PP
  294. The descriptor \fBOSSL_PARAM\fR\|(3) returned by
  295. \&\fBEVP_PKEY_fromdata_settable()\fR may also be used programmatically, for
  296. example with \fBOSSL_PARAM_allocate_from_text\fR\|(3).
  297. .SH "SEE ALSO"
  298. .IX Header "SEE ALSO"
  299. \&\fBEVP_PKEY_CTX_new\fR\|(3), \fBprovider\fR\|(7), \fBEVP_PKEY_gettable_params\fR\|(3),
  300. \&\fBOSSL_PARAM\fR\|(3), \fBEVP_PKEY_todata\fR\|(3),
  301. \&\fBEVP_PKEY\-RSA\fR\|(7), \fBEVP_PKEY\-DSA\fR\|(7), \fBEVP_PKEY\-DH\fR\|(7), \fBEVP_PKEY\-EC\fR\|(7),
  302. \&\fBEVP_PKEY\-ED448\fR\|(7), \fBEVP_PKEY\-X25519\fR\|(7), \fBEVP_PKEY\-X448\fR\|(7),
  303. \&\fBEVP_PKEY\-ED25519\fR\|(7)
  304. .SH HISTORY
  305. .IX Header "HISTORY"
  306. These functions were added in OpenSSL 3.0.
  307. .SH COPYRIGHT
  308. .IX Header "COPYRIGHT"
  309. Copyright 2019\-2021 The OpenSSL Project Authors. All Rights Reserved.
  310. .PP
  311. Licensed under the Apache License 2.0 (the "License"). You may not use
  312. this file except in compliance with the License. You can obtain a copy
  313. in the file LICENSE in the source distribution or at
  314. <https://www.openssl.org/source/license.html>.