EVP_PKEY_fromdata.3ossl 13 KB

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