decode_pem2der.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright 2020-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. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/core_dispatch.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/core_object.h>
  18. #include <openssl/crypto.h>
  19. #include <openssl/err.h>
  20. #include <openssl/params.h>
  21. #include <openssl/pem.h>
  22. #include <openssl/proverr.h>
  23. #include "internal/nelem.h"
  24. #include "internal/sizes.h"
  25. #include "prov/bio.h"
  26. #include "prov/decoders.h"
  27. #include "prov/implementations.h"
  28. #include "endecoder_local.h"
  29. static int read_pem(PROV_CTX *provctx, OSSL_CORE_BIO *cin,
  30. char **pem_name, char **pem_header,
  31. unsigned char **data, long *len)
  32. {
  33. BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
  34. int ok;
  35. if (in == NULL)
  36. return 0;
  37. ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);
  38. BIO_free(in);
  39. return ok;
  40. }
  41. static OSSL_FUNC_decoder_newctx_fn pem2der_newctx;
  42. static OSSL_FUNC_decoder_freectx_fn pem2der_freectx;
  43. static OSSL_FUNC_decoder_decode_fn pem2der_decode;
  44. /*
  45. * Context used for PEM to DER decoding.
  46. */
  47. struct pem2der_ctx_st {
  48. PROV_CTX *provctx;
  49. char data_structure[OSSL_MAX_CODEC_STRUCT_SIZE];
  50. char propq[OSSL_MAX_PROPQUERY_SIZE];
  51. };
  52. static void *pem2der_newctx(void *provctx)
  53. {
  54. struct pem2der_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  55. if (ctx != NULL)
  56. ctx->provctx = provctx;
  57. return ctx;
  58. }
  59. static void pem2der_freectx(void *vctx)
  60. {
  61. struct pem2der_ctx_st *ctx = vctx;
  62. OPENSSL_free(ctx);
  63. }
  64. static const OSSL_PARAM *pem2der_settable_ctx_params(ossl_unused void *provctx)
  65. {
  66. static const OSSL_PARAM settables[] = {
  67. OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0),
  68. OSSL_PARAM_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, NULL, 0),
  69. OSSL_PARAM_END
  70. };
  71. return settables;
  72. }
  73. static int pem2der_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  74. {
  75. struct pem2der_ctx_st *ctx = vctx;
  76. const OSSL_PARAM *p;
  77. char *str;
  78. p = OSSL_PARAM_locate_const(params, OSSL_DECODER_PARAM_PROPERTIES);
  79. str = ctx->propq;
  80. if (p != NULL
  81. && !OSSL_PARAM_get_utf8_string(p, &str, sizeof(ctx->propq)))
  82. return 0;
  83. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE);
  84. str = ctx->data_structure;
  85. if (p != NULL
  86. && !OSSL_PARAM_get_utf8_string(p, &str, sizeof(ctx->data_structure)))
  87. return 0;
  88. return 1;
  89. }
  90. /* pem_password_cb compatible function */
  91. struct pem2der_pass_data_st {
  92. OSSL_PASSPHRASE_CALLBACK *cb;
  93. void *cbarg;
  94. };
  95. static int pem2der_pass_helper(char *buf, int num, int w, void *data)
  96. {
  97. struct pem2der_pass_data_st *pass_data = data;
  98. size_t plen;
  99. if (pass_data == NULL
  100. || pass_data->cb == NULL
  101. || !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))
  102. return -1;
  103. return (int)plen;
  104. }
  105. static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
  106. OSSL_CALLBACK *data_cb, void *data_cbarg,
  107. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  108. {
  109. /*
  110. * PEM names we recognise. Other PEM names should be recognised by
  111. * other decoder implementations.
  112. */
  113. static struct pem_name_map_st {
  114. const char *pem_name;
  115. int object_type;
  116. const char *data_type;
  117. const char *data_structure;
  118. } pem_name_map[] = {
  119. /* PKCS#8 and SubjectPublicKeyInfo */
  120. { PEM_STRING_PKCS8, OSSL_OBJECT_PKEY, NULL, "EncryptedPrivateKeyInfo" },
  121. { PEM_STRING_PKCS8INF, OSSL_OBJECT_PKEY, NULL, "PrivateKeyInfo" },
  122. #define PKCS8_LAST_IDX 1
  123. { PEM_STRING_PUBLIC, OSSL_OBJECT_PKEY, NULL, "SubjectPublicKeyInfo" },
  124. #define SPKI_LAST_IDX 2
  125. /* Our set of type specific PEM types */
  126. { PEM_STRING_DHPARAMS, OSSL_OBJECT_PKEY, "DH", "type-specific" },
  127. { PEM_STRING_DHXPARAMS, OSSL_OBJECT_PKEY, "X9.42 DH", "type-specific" },
  128. { PEM_STRING_DSA, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
  129. { PEM_STRING_DSA_PUBLIC, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
  130. { PEM_STRING_DSAPARAMS, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
  131. { PEM_STRING_ECPRIVATEKEY, OSSL_OBJECT_PKEY, "EC", "type-specific" },
  132. { PEM_STRING_ECPARAMETERS, OSSL_OBJECT_PKEY, "EC", "type-specific" },
  133. { PEM_STRING_SM2PRIVATEKEY, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
  134. { PEM_STRING_SM2PARAMETERS, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
  135. { PEM_STRING_RSA, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
  136. { PEM_STRING_RSA_PUBLIC, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
  137. /*
  138. * A few others that there is at least have an object type for, even
  139. * though there is no provider interface to handle such objects, yet.
  140. * However, this is beneficial for the OSSL_STORE result handler.
  141. */
  142. { PEM_STRING_X509, OSSL_OBJECT_CERT, NULL, "Certificate" },
  143. { PEM_STRING_X509_TRUSTED, OSSL_OBJECT_CERT, NULL, "Certificate" },
  144. { PEM_STRING_X509_OLD, OSSL_OBJECT_CERT, NULL, "Certificate" },
  145. { PEM_STRING_X509_CRL, OSSL_OBJECT_CRL, NULL, "CertificateList" }
  146. };
  147. struct pem2der_ctx_st *ctx = vctx;
  148. char *pem_name = NULL, *pem_header = NULL;
  149. size_t i;
  150. unsigned char *der = NULL;
  151. long der_len = 0;
  152. int ok = 0;
  153. int objtype = OSSL_OBJECT_UNKNOWN;
  154. ok = read_pem(ctx->provctx, cin, &pem_name, &pem_header,
  155. &der, &der_len) > 0;
  156. /* We return "empty handed". This is not an error. */
  157. if (!ok)
  158. return 1;
  159. /*
  160. * 10 is the number of characters in "Proc-Type:", which
  161. * PEM_get_EVP_CIPHER_INFO() requires to be present.
  162. * If the PEM header has less characters than that, it's
  163. * not worth spending cycles on it.
  164. */
  165. if (strlen(pem_header) > 10) {
  166. EVP_CIPHER_INFO cipher;
  167. struct pem2der_pass_data_st pass_data;
  168. ok = 0; /* Assume that we fail */
  169. pass_data.cb = pw_cb;
  170. pass_data.cbarg = pw_cbarg;
  171. if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
  172. || !PEM_do_header(&cipher, der, &der_len,
  173. pem2der_pass_helper, &pass_data))
  174. goto end;
  175. }
  176. /*
  177. * Indicated that we successfully decoded something, or not at all.
  178. * Ending up "empty handed" is not an error.
  179. */
  180. ok = 1;
  181. /* Have a look to see if we recognise anything */
  182. for (i = 0; i < OSSL_NELEM(pem_name_map); i++)
  183. if (strcmp(pem_name, pem_name_map[i].pem_name) == 0)
  184. break;
  185. if (i < OSSL_NELEM(pem_name_map)) {
  186. OSSL_PARAM params[5], *p = params;
  187. /* We expect these to be read only so casting away the const is ok */
  188. char *data_type = (char *)pem_name_map[i].data_type;
  189. char *data_structure = (char *)pem_name_map[i].data_structure;
  190. /*
  191. * Since this may perform decryption, we need to check the selection to
  192. * avoid password prompts for objects of no interest.
  193. */
  194. if (i <= PKCS8_LAST_IDX
  195. && ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
  196. || OPENSSL_strcasecmp(ctx->data_structure, "EncryptedPrivateKeyInfo") == 0
  197. || OPENSSL_strcasecmp(ctx->data_structure, "PrivateKeyInfo") == 0)) {
  198. ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb,
  199. data_cbarg, pw_cb, pw_cbarg,
  200. PROV_LIBCTX_OF(ctx->provctx),
  201. ctx->propq);
  202. goto end;
  203. }
  204. if (i <= SPKI_LAST_IDX
  205. && ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
  206. || OPENSSL_strcasecmp(ctx->data_structure, "SubjectPublicKeyInfo") == 0)) {
  207. ok = ossl_spki2typespki_der_decode(der, der_len, selection, data_cb,
  208. data_cbarg, pw_cb, pw_cbarg,
  209. PROV_LIBCTX_OF(ctx->provctx),
  210. ctx->propq);
  211. goto end;
  212. }
  213. objtype = pem_name_map[i].object_type;
  214. if (data_type != NULL)
  215. *p++ =
  216. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  217. data_type, 0);
  218. /* We expect this to be read only so casting away the const is ok */
  219. if (data_structure != NULL)
  220. *p++ =
  221. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
  222. data_structure, 0);
  223. *p++ =
  224. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
  225. der, der_len);
  226. *p++ =
  227. OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
  228. *p = OSSL_PARAM_construct_end();
  229. ok = data_cb(params, data_cbarg);
  230. }
  231. end:
  232. OPENSSL_free(pem_name);
  233. OPENSSL_free(pem_header);
  234. OPENSSL_free(der);
  235. return ok;
  236. }
  237. const OSSL_DISPATCH ossl_pem_to_der_decoder_functions[] = {
  238. { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))pem2der_newctx },
  239. { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))pem2der_freectx },
  240. { OSSL_FUNC_DECODER_DECODE, (void (*)(void))pem2der_decode },
  241. { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,
  242. (void (*)(void))pem2der_settable_ctx_params },
  243. { OSSL_FUNC_DECODER_SET_CTX_PARAMS,
  244. (void (*)(void))pem2der_set_ctx_params },
  245. OSSL_DISPATCH_END
  246. };