ml_kem_codecs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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
  8. */
  9. #include <string.h>
  10. #include <openssl/byteorder.h>
  11. #include <openssl/proverr.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/core_names.h>
  14. #include "internal/encoder.h"
  15. #include "prov/ml_kem.h"
  16. #include "ml_kem_codecs.h"
  17. /* Tables describing supported ASN.1 input/output formats. */
  18. /*-
  19. * ML-KEM-512:
  20. * Public key bytes: 800 (0x0320)
  21. * Private key bytes: 1632 (0x0660)
  22. */
  23. static const ML_COMMON_SPKI_FMT ml_kem_512_spkifmt = {
  24. { 0x30, 0x82, 0x03, 0x32, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48,
  25. 0x01, 0x65, 0x03, 0x04, 0x04, 0x01, 0x03, 0x82, 0x03, 0x21, 0x00, }
  26. };
  27. static const ML_COMMON_PKCS8_FMT ml_kem_512_p8fmt[NUM_PKCS8_FORMATS] = {
  28. { "seed-priv", 0x06aa, 0, 0x308206a6, 0x0440, 6, 0x40, 0x04820660, 0x4a, 0x0660, 0, 0 },
  29. { "priv-only", 0x0664, 0, 0x04820660, 0, 0, 0, 0, 0x04, 0x0660, 0, 0 },
  30. { "oqskeypair", 0x0984, 0, 0x04820980, 0, 0, 0, 0, 0x04, 0x0660, 0x0664, 0x0320 },
  31. { "seed-only", 0x0042, 2, 0x8040, 0, 2, 0x40, 0, 0, 0, 0, 0 },
  32. { "bare-priv", 0x0660, 4, 0, 0, 0, 0, 0, 0, 0x0660, 0, 0 },
  33. { "bare-seed", 0x0040, 4, 0, 0, 0, 0x40, 0, 0, 0, 0, 0 },
  34. };
  35. /*-
  36. * ML-KEM-768:
  37. * Public key bytes: 1184 (0x04a0)
  38. * Private key bytes: 2400 (0x0960)
  39. */
  40. static const ML_COMMON_SPKI_FMT ml_kem_768_spkifmt = {
  41. { 0x30, 0x82, 0x04, 0xb2, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48,
  42. 0x01, 0x65, 0x03, 0x04, 0x04, 0x02, 0x03, 0x82, 0x04, 0xa1, 0x00, }
  43. };
  44. static const ML_COMMON_PKCS8_FMT ml_kem_768_p8fmt[NUM_PKCS8_FORMATS] = {
  45. { "seed-priv", 0x09aa, 0, 0x308209a6, 0x0440, 6, 0x40, 0x04820960, 0x4a, 0x0960, 0, 0, },
  46. { "priv-only", 0x0964, 0, 0x04820960, 0, 0, 0, 0, 0x04, 0x0960, 0, 0, },
  47. { "oqskeypair", 0x0e04, 0, 0x04820e00, 0, 0, 0, 0, 0x04, 0x0960, 0x0964, 0x04a0 },
  48. { "seed-only", 0x0042, 2, 0x8040, 0, 2, 0x40, 0, 0, 0, 0, 0, },
  49. { "bare-priv", 0x0960, 4, 0, 0, 0, 0, 0, 0, 0x0960, 0, 0, },
  50. { "bare-seed", 0x0040, 4, 0, 0, 0, 0x40, 0, 0, 0, 0, 0, },
  51. };
  52. /*-
  53. * ML-KEM-1024:
  54. * Private key bytes: 3168 (0x0c60)
  55. * Public key bytes: 1568 (0x0620)
  56. */
  57. static const ML_COMMON_SPKI_FMT ml_kem_1024_spkifmt = {
  58. { 0x30, 0x82, 0x06, 0x32, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48,
  59. 0x01, 0x65, 0x03, 0x04, 0x04, 0x03, 0x03, 0x82, 0x06, 0x21, 0x00, }
  60. };
  61. static const ML_COMMON_PKCS8_FMT ml_kem_1024_p8fmt[NUM_PKCS8_FORMATS] = {
  62. { "seed-priv", 0x0caa, 0, 0x30820ca6, 0x0440, 6, 0x40, 0x04820c60, 0x4a, 0x0c60, 0, 0 },
  63. { "priv-only", 0x0c64, 0, 0x04820c60, 0, 0, 0, 0, 0x04, 0x0c60, 0, 0 },
  64. { "oqskeypair", 0x1284, 0, 0x04821280, 0, 0, 0, 0, 0x04, 0x0c60, 0x0c64, 0x0620 },
  65. { "seed-only", 0x0042, 2, 0x8040, 0, 2, 0x40, 0, 0, 0, 0, 0 },
  66. { "bare-priv", 0x0c60, 4, 0, 0, 0, 0, 0, 0, 0x0c60, 0, 0 },
  67. { "bare-seed", 0x0040, 4, 0, 0, 0, 0x40, 0, 0, 0, 0, 0 },
  68. };
  69. /* Indices of slots in the `codecs` table below */
  70. #define ML_KEM_512_CODEC 0
  71. #define ML_KEM_768_CODEC 1
  72. #define ML_KEM_1024_CODEC 2
  73. /*
  74. * Per-variant fixed parameters
  75. */
  76. static const ML_COMMON_CODEC codecs[3] = {
  77. { &ml_kem_512_spkifmt, ml_kem_512_p8fmt },
  78. { &ml_kem_768_spkifmt, ml_kem_768_p8fmt },
  79. { &ml_kem_1024_spkifmt, ml_kem_1024_p8fmt }
  80. };
  81. /* Retrieve the parameters of one of the ML-KEM variants */
  82. static const ML_COMMON_CODEC *ml_kem_get_codec(int evp_type)
  83. {
  84. switch (evp_type) {
  85. case EVP_PKEY_ML_KEM_512:
  86. return &codecs[ML_KEM_512_CODEC];
  87. case EVP_PKEY_ML_KEM_768:
  88. return &codecs[ML_KEM_768_CODEC];
  89. case EVP_PKEY_ML_KEM_1024:
  90. return &codecs[ML_KEM_1024_CODEC];
  91. }
  92. return NULL;
  93. }
  94. ML_KEM_KEY *
  95. ossl_ml_kem_d2i_PUBKEY(const uint8_t *pubenc, int publen, int evp_type,
  96. PROV_CTX *provctx, const char *propq)
  97. {
  98. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
  99. const ML_KEM_VINFO *v;
  100. const ML_COMMON_CODEC *codec;
  101. const ML_COMMON_SPKI_FMT *vspki;
  102. ML_KEM_KEY *ret;
  103. if ((v = ossl_ml_kem_get_vinfo(evp_type)) == NULL
  104. || (codec = ml_kem_get_codec(evp_type)) == NULL)
  105. return NULL;
  106. vspki = codec->spkifmt;
  107. if (publen != ML_COMMON_SPKI_OVERHEAD + (ossl_ssize_t) v->pubkey_bytes
  108. || memcmp(pubenc, vspki->asn1_prefix, ML_COMMON_SPKI_OVERHEAD) != 0)
  109. return NULL;
  110. publen -= ML_COMMON_SPKI_OVERHEAD;
  111. pubenc += ML_COMMON_SPKI_OVERHEAD;
  112. if ((ret = ossl_ml_kem_key_new(libctx, propq, evp_type)) == NULL)
  113. return NULL;
  114. if (!ossl_ml_kem_parse_public_key(pubenc, (size_t) publen, ret)) {
  115. ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
  116. "errror parsing %s public key from input SPKI",
  117. v->algorithm_name);
  118. ossl_ml_kem_key_free(ret);
  119. return NULL;
  120. }
  121. return ret;
  122. }
  123. ML_KEM_KEY *
  124. ossl_ml_kem_d2i_PKCS8(const uint8_t *prvenc, int prvlen,
  125. int evp_type, PROV_CTX *provctx,
  126. const char *propq)
  127. {
  128. const ML_KEM_VINFO *v;
  129. const ML_COMMON_CODEC *codec;
  130. ML_COMMON_PKCS8_FMT_PREF *fmt_slots = NULL, *slot;
  131. const ML_COMMON_PKCS8_FMT *p8fmt;
  132. ML_KEM_KEY *key = NULL, *ret = NULL;
  133. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  134. const uint8_t *buf, *pos;
  135. const X509_ALGOR *alg = NULL;
  136. const char *formats;
  137. int len, ptype;
  138. uint32_t magic;
  139. uint16_t seed_magic;
  140. /* Which ML-KEM variant? */
  141. if ((v = ossl_ml_kem_get_vinfo(evp_type)) == NULL
  142. || (codec = ml_kem_get_codec(evp_type)) == NULL)
  143. return 0;
  144. /* Extract the key OID and any parameters. */
  145. if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &prvenc, prvlen)) == NULL)
  146. return 0;
  147. /* Shortest prefix is 4 bytes: seq tag/len + octet string tag/len */
  148. if (!PKCS8_pkey_get0(NULL, &buf, &len, &alg, p8inf))
  149. goto end;
  150. /* Bail out early if this is some other key type. */
  151. if (OBJ_obj2nid(alg->algorithm) != evp_type)
  152. goto end;
  153. /* Get the list of enabled decoders. Their order is not important here. */
  154. formats = ossl_prov_ctx_get_param(
  155. provctx, OSSL_PKEY_PARAM_ML_KEM_INPUT_FORMATS, NULL);
  156. fmt_slots = ossl_ml_common_pkcs8_fmt_order(v->algorithm_name, codec->p8fmt,
  157. "input", formats);
  158. if (fmt_slots == NULL)
  159. goto end;
  160. /* Parameters must be absent. */
  161. X509_ALGOR_get0(NULL, &ptype, NULL, alg);
  162. if (ptype != V_ASN1_UNDEF) {
  163. ERR_raise_data(ERR_LIB_PROV, PROV_R_UNEXPECTED_KEY_PARAMETERS,
  164. "unexpected parameters with a PKCS#8 %s private key",
  165. v->algorithm_name);
  166. goto end;
  167. }
  168. if ((ossl_ssize_t)len < (ossl_ssize_t)sizeof(magic))
  169. goto end;
  170. /* Find the matching p8 info slot, that also has the expected length. */
  171. pos = OPENSSL_load_u32_be(&magic, buf);
  172. for (slot = fmt_slots; (p8fmt = slot->fmt) != NULL; ++slot) {
  173. if (len != (ossl_ssize_t)p8fmt->p8_bytes)
  174. continue;
  175. if (p8fmt->p8_shift == sizeof(magic)
  176. || (magic >> (p8fmt->p8_shift * 8)) == p8fmt->p8_magic) {
  177. pos -= p8fmt->p8_shift;
  178. break;
  179. }
  180. }
  181. if (p8fmt == NULL
  182. || (p8fmt->seed_length > 0 && p8fmt->seed_length != ML_KEM_SEED_BYTES)
  183. || (p8fmt->priv_length > 0 && p8fmt->priv_length != v->prvkey_bytes)
  184. || (p8fmt->pub_length > 0 && p8fmt->pub_length != v->pubkey_bytes)) {
  185. ERR_raise_data(ERR_LIB_PROV, PROV_R_ML_KEM_NO_FORMAT,
  186. "no matching enabled %s private key input formats",
  187. v->algorithm_name);
  188. goto end;
  189. }
  190. if (p8fmt->seed_length > 0) {
  191. /* Check |seed| tag/len, if not subsumed by |magic|. */
  192. if (pos + sizeof(uint16_t) == buf + p8fmt->seed_offset) {
  193. pos = OPENSSL_load_u16_be(&seed_magic, pos);
  194. if (seed_magic != p8fmt->seed_magic)
  195. goto end;
  196. } else if (pos != buf + p8fmt->seed_offset) {
  197. goto end;
  198. }
  199. pos += ML_KEM_SEED_BYTES;
  200. }
  201. if (p8fmt->priv_length > 0) {
  202. /* Check |priv| tag/len */
  203. if (pos + sizeof(uint32_t) == buf + p8fmt->priv_offset) {
  204. pos = OPENSSL_load_u32_be(&magic, pos);
  205. if (magic != p8fmt->priv_magic)
  206. goto end;
  207. } else if (pos != buf + p8fmt->priv_offset) {
  208. goto end;
  209. }
  210. pos += v->prvkey_bytes;
  211. }
  212. if (p8fmt->pub_length > 0) {
  213. if (pos != buf + p8fmt->pub_offset)
  214. goto end;
  215. pos += v->pubkey_bytes;
  216. }
  217. if (pos != buf + len)
  218. goto end;
  219. /*
  220. * Collect the seed and/or key into a "decoded" private key object,
  221. * to be turned into a real key on provider "load" or "import".
  222. */
  223. if ((key = ossl_prov_ml_kem_new(provctx, propq, evp_type)) == NULL)
  224. goto end;
  225. if (p8fmt->seed_length > 0) {
  226. if (!ossl_ml_kem_set_seed(buf + p8fmt->seed_offset,
  227. ML_KEM_SEED_BYTES, key)) {
  228. ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR,
  229. "error storing %s private key seed",
  230. v->algorithm_name);
  231. goto end;
  232. }
  233. }
  234. if (p8fmt->priv_length > 0) {
  235. if ((key->encoded_dk = OPENSSL_malloc(p8fmt->priv_length)) == NULL) {
  236. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
  237. "error parsing %s private key",
  238. v->algorithm_name);
  239. goto end;
  240. }
  241. memcpy(key->encoded_dk, buf + p8fmt->priv_offset, p8fmt->priv_length);
  242. }
  243. /* Any OQS public key content is ignored */
  244. ret = key;
  245. end:
  246. OPENSSL_free(fmt_slots);
  247. PKCS8_PRIV_KEY_INFO_free(p8inf);
  248. if (ret == NULL)
  249. ossl_ml_kem_key_free(key);
  250. return ret;
  251. }
  252. /* Same as ossl_ml_kem_encode_pubkey, but allocates the output buffer. */
  253. int ossl_ml_kem_i2d_pubkey(const ML_KEM_KEY *key, unsigned char **out)
  254. {
  255. size_t publen;
  256. if (!ossl_ml_kem_have_pubkey(key)) {
  257. ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY,
  258. "no %s public key data available",
  259. key->vinfo->algorithm_name);
  260. return 0;
  261. }
  262. publen = key->vinfo->pubkey_bytes;
  263. if (out != NULL
  264. && (*out = OPENSSL_malloc(publen)) == NULL)
  265. return 0;
  266. if (!ossl_ml_kem_encode_public_key(*out, publen, key)) {
  267. ERR_raise_data(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR,
  268. "error encoding %s public key",
  269. key->vinfo->algorithm_name);
  270. OPENSSL_free(*out);
  271. return 0;
  272. }
  273. return (int)publen;
  274. }
  275. /* Allocate and encode PKCS#8 private key payload. */
  276. int ossl_ml_kem_i2d_prvkey(const ML_KEM_KEY *key, uint8_t **out,
  277. PROV_CTX *provctx)
  278. {
  279. const ML_KEM_VINFO *v = key->vinfo;
  280. const ML_COMMON_CODEC *codec;
  281. ML_COMMON_PKCS8_FMT_PREF *fmt_slots, *slot;
  282. const ML_COMMON_PKCS8_FMT *p8fmt;
  283. uint8_t *buf = NULL, *pos;
  284. const char *formats;
  285. int len = ML_KEM_SEED_BYTES;
  286. int ret = 0;
  287. /* Not ours to handle */
  288. if ((codec = ml_kem_get_codec(v->evp_type)) == NULL)
  289. return 0;
  290. if (!ossl_ml_kem_have_prvkey(key)) {
  291. ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY,
  292. "no %s private key data available",
  293. key->vinfo->algorithm_name);
  294. return 0;
  295. }
  296. formats = ossl_prov_ctx_get_param(
  297. provctx, OSSL_PKEY_PARAM_ML_KEM_OUTPUT_FORMATS, NULL);
  298. fmt_slots = ossl_ml_common_pkcs8_fmt_order(v->algorithm_name, codec->p8fmt,
  299. "output", formats);
  300. if (fmt_slots == NULL)
  301. return 0;
  302. /* If we don't have a seed, skip seedful entries */
  303. for (slot = fmt_slots; (p8fmt = slot->fmt) != NULL; ++slot)
  304. if (ossl_ml_kem_have_seed(key) || p8fmt->seed_length == 0)
  305. break;
  306. /* No matching table entries, give up */
  307. if (p8fmt == NULL
  308. || (p8fmt->seed_length > 0 && p8fmt->seed_length != ML_KEM_SEED_BYTES)
  309. || (p8fmt->priv_length > 0 && p8fmt->priv_length != v->prvkey_bytes)
  310. || (p8fmt->pub_length > 0 && p8fmt->pub_length != v->pubkey_bytes)) {
  311. ERR_raise_data(ERR_LIB_PROV, PROV_R_ML_KEM_NO_FORMAT,
  312. "no matching enabled %s private key output formats",
  313. v->algorithm_name);
  314. goto end;
  315. }
  316. len = p8fmt->p8_bytes;
  317. if (out == NULL) {
  318. ret = len;
  319. goto end;
  320. }
  321. if ((pos = buf = OPENSSL_malloc((size_t) len)) == NULL)
  322. goto end;
  323. switch (p8fmt->p8_shift) {
  324. case 0:
  325. pos = OPENSSL_store_u32_be(pos, p8fmt->p8_magic);
  326. break;
  327. case 2:
  328. pos = OPENSSL_store_u16_be(pos, (uint16_t)p8fmt->p8_magic);
  329. break;
  330. case 4:
  331. break;
  332. default:
  333. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  334. "error encoding %s private key",
  335. v->algorithm_name);
  336. goto end;
  337. }
  338. if (p8fmt->seed_length != 0) {
  339. /*
  340. * Either the tag/len were already included in |magic| or they require
  341. * us to write two bytes now.
  342. */
  343. if (pos + sizeof(uint16_t) == buf + p8fmt->seed_offset)
  344. pos = OPENSSL_store_u16_be(pos, p8fmt->seed_magic);
  345. if (pos != buf + p8fmt->seed_offset
  346. || !ossl_ml_kem_encode_seed(pos, ML_KEM_SEED_BYTES, key)) {
  347. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  348. "error encoding %s private key",
  349. v->algorithm_name);
  350. goto end;
  351. }
  352. pos += ML_KEM_SEED_BYTES;
  353. }
  354. if (p8fmt->priv_length != 0) {
  355. if (pos + sizeof(uint32_t) == buf + p8fmt->priv_offset)
  356. pos = OPENSSL_store_u32_be(pos, p8fmt->priv_magic);
  357. if (pos != buf + p8fmt->priv_offset
  358. || !ossl_ml_kem_encode_private_key(pos, v->prvkey_bytes, key)) {
  359. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  360. "error encoding %s private key",
  361. v->algorithm_name);
  362. goto end;
  363. }
  364. pos += v->prvkey_bytes;
  365. }
  366. /* OQS form output with tacked-on public key */
  367. if (p8fmt->pub_length != 0) {
  368. /* The OQS pubkey is never separately DER-wrapped */
  369. if (pos != buf + p8fmt->pub_offset
  370. || !ossl_ml_kem_encode_public_key(pos, v->pubkey_bytes, key)) {
  371. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  372. "error encoding %s private key",
  373. v->algorithm_name);
  374. goto end;
  375. }
  376. pos += v->pubkey_bytes;
  377. }
  378. if (pos == buf + len) {
  379. *out = buf;
  380. ret = len;
  381. }
  382. end:
  383. OPENSSL_free(fmt_slots);
  384. if (ret == 0)
  385. OPENSSL_free(buf);
  386. return ret;
  387. }
  388. int ossl_ml_kem_key_to_text(BIO *out, const ML_KEM_KEY *key, int selection)
  389. {
  390. uint8_t seed[ML_KEM_SEED_BYTES], *prvenc = NULL, *pubenc = NULL;
  391. size_t publen, prvlen;
  392. const char *type_label = NULL;
  393. int ret = 0;
  394. if (out == NULL || key == NULL) {
  395. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  396. return 0;
  397. }
  398. type_label = key->vinfo->algorithm_name;
  399. publen = key->vinfo->pubkey_bytes;
  400. prvlen = key->vinfo->prvkey_bytes;
  401. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  402. && (ossl_ml_kem_have_prvkey(key)
  403. || ossl_ml_kem_have_seed(key))) {
  404. if (BIO_printf(out, "%s Private-Key:\n", type_label) <= 0)
  405. return 0;
  406. if (ossl_ml_kem_have_seed(key)) {
  407. if (!ossl_ml_kem_encode_seed(seed, sizeof(seed), key))
  408. goto end;
  409. if (!ossl_bio_print_labeled_buf(out, "seed:", seed, sizeof(seed)))
  410. goto end;
  411. }
  412. if (ossl_ml_kem_have_prvkey(key)) {
  413. if ((prvenc = OPENSSL_malloc(prvlen)) == NULL)
  414. return 0;
  415. if (!ossl_ml_kem_encode_private_key(prvenc, prvlen, key))
  416. goto end;
  417. if (!ossl_bio_print_labeled_buf(out, "dk:", prvenc, prvlen))
  418. goto end;
  419. }
  420. ret = 1;
  421. }
  422. /* The public key is output regardless of the selection */
  423. if (ossl_ml_kem_have_pubkey(key)) {
  424. /* If we did not output private key bits, this is a public key */
  425. if (ret == 0 && BIO_printf(out, "%s Public-Key:\n", type_label) <= 0)
  426. goto end;
  427. if ((pubenc = OPENSSL_malloc(key->vinfo->pubkey_bytes)) == NULL
  428. || !ossl_ml_kem_encode_public_key(pubenc, publen, key)
  429. || !ossl_bio_print_labeled_buf(out, "ek:", pubenc, publen))
  430. goto end;
  431. ret = 1;
  432. }
  433. /* If we got here, and ret == 0, there was no key material */
  434. if (ret == 0)
  435. ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_KEY,
  436. "no %s key material available",
  437. type_label);
  438. end:
  439. OPENSSL_free(pubenc);
  440. OPENSSL_free(prvenc);
  441. return ret;
  442. }