encoder_common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright 2019-2020 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 <openssl/opensslconf.h> /* SIXTY_FOUR_BIT_LONG, ... */
  10. #include <openssl/err.h>
  11. #include <openssl/pem.h> /* PEM_BUFSIZE */
  12. #include <openssl/pkcs12.h> /* PKCS8_encrypt() */
  13. #include <openssl/types.h>
  14. #include <openssl/x509.h> /* i2d_X509_PUBKEY_bio() */
  15. #include "crypto/bn.h" /* bn_get_words() */
  16. #include "crypto/ctype.h"
  17. #include "crypto/ecx.h"
  18. #include "prov/bio.h" /* ossl_prov_bio_printf() */
  19. #include "prov/implementations.h"
  20. #include "prov/providercommonerr.h" /* PROV_R_READ_KEY */
  21. #include "encoder_local.h"
  22. static PKCS8_PRIV_KEY_INFO *
  23. ossl_prov_p8info_from_obj(const void *obj, int obj_nid,
  24. void *params,
  25. int params_type,
  26. int (*k2d)(const void *obj,
  27. unsigned char **pder))
  28. {
  29. /* der, derlen store the key DER output and its length */
  30. unsigned char *der = NULL;
  31. int derlen;
  32. /* The final PKCS#8 info */
  33. PKCS8_PRIV_KEY_INFO *p8info = NULL;
  34. if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL
  35. || (derlen = k2d(obj, &der)) <= 0
  36. || !PKCS8_pkey_set0(p8info, OBJ_nid2obj(obj_nid), 0,
  37. params_type, params, der, derlen)) {
  38. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  39. PKCS8_PRIV_KEY_INFO_free(p8info);
  40. OPENSSL_free(der);
  41. p8info = NULL;
  42. }
  43. return p8info;
  44. }
  45. static X509_SIG *ossl_prov_encp8_from_p8info(PKCS8_PRIV_KEY_INFO *p8info,
  46. struct pkcs8_encrypt_ctx_st *ctx)
  47. {
  48. X509_SIG *p8 = NULL;
  49. char buf[PEM_BUFSIZE];
  50. const void *kstr = ctx->cipher_pass;
  51. size_t klen = ctx->cipher_pass_length;
  52. if (ctx->cipher == NULL)
  53. return NULL;
  54. if (kstr == NULL) {
  55. if (!ctx->cb(buf, sizeof(buf), &klen, NULL, ctx->cbarg)) {
  56. ERR_raise(ERR_LIB_PROV, PROV_R_READ_KEY);
  57. return NULL;
  58. }
  59. kstr = buf;
  60. }
  61. /* NID == -1 means "standard" */
  62. p8 = PKCS8_encrypt(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info);
  63. if (kstr == buf)
  64. OPENSSL_cleanse(buf, klen);
  65. return p8;
  66. }
  67. static X509_SIG *ossl_prov_encp8_from_obj(const void *obj, int obj_nid,
  68. void *params,
  69. int params_type,
  70. int (*k2d)(const void *obj,
  71. unsigned char **pder),
  72. struct pkcs8_encrypt_ctx_st *ctx)
  73. {
  74. PKCS8_PRIV_KEY_INFO *p8info =
  75. ossl_prov_p8info_from_obj(obj, obj_nid, params, params_type, k2d);
  76. X509_SIG *p8 = ossl_prov_encp8_from_p8info(p8info, ctx);
  77. PKCS8_PRIV_KEY_INFO_free(p8info);
  78. return p8;
  79. }
  80. static X509_PUBKEY *ossl_prov_pubkey_from_obj(const void *obj, int obj_nid,
  81. void *params,
  82. int params_type,
  83. int (*k2d)(const void *obj,
  84. unsigned char **pder))
  85. {
  86. /* der, derlen store the key DER output and its length */
  87. unsigned char *der = NULL;
  88. int derlen;
  89. /* The final X509_PUBKEY */
  90. X509_PUBKEY *xpk = NULL;
  91. if ((xpk = X509_PUBKEY_new()) == NULL
  92. || (derlen = k2d(obj, &der)) <= 0
  93. || !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(obj_nid),
  94. params_type, params, der, derlen)) {
  95. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  96. X509_PUBKEY_free(xpk);
  97. OPENSSL_free(der);
  98. xpk = NULL;
  99. }
  100. return xpk;
  101. }
  102. OSSL_FUNC_keymgmt_new_fn *ossl_prov_get_keymgmt_new(const OSSL_DISPATCH *fns)
  103. {
  104. /* Pilfer the keymgmt dispatch table */
  105. for (; fns->function_id != 0; fns++)
  106. if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW)
  107. return OSSL_FUNC_keymgmt_new(fns);
  108. return NULL;
  109. }
  110. OSSL_FUNC_keymgmt_free_fn *ossl_prov_get_keymgmt_free(const OSSL_DISPATCH *fns)
  111. {
  112. /* Pilfer the keymgmt dispatch table */
  113. for (; fns->function_id != 0; fns++)
  114. if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE)
  115. return OSSL_FUNC_keymgmt_free(fns);
  116. return NULL;
  117. }
  118. OSSL_FUNC_keymgmt_import_fn *ossl_prov_get_keymgmt_import(const OSSL_DISPATCH *fns)
  119. {
  120. /* Pilfer the keymgmt dispatch table */
  121. for (; fns->function_id != 0; fns++)
  122. if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT)
  123. return OSSL_FUNC_keymgmt_import(fns);
  124. return NULL;
  125. }
  126. OSSL_FUNC_keymgmt_export_fn *ossl_prov_get_keymgmt_export(const OSSL_DISPATCH *fns)
  127. {
  128. /* Pilfer the keymgmt dispatch table */
  129. for (; fns->function_id != 0; fns++)
  130. if (fns->function_id == OSSL_FUNC_KEYMGMT_EXPORT)
  131. return OSSL_FUNC_keymgmt_export(fns);
  132. return NULL;
  133. }
  134. # ifdef SIXTY_FOUR_BIT_LONG
  135. # define BN_FMTu "%lu"
  136. # define BN_FMTx "%lx"
  137. # endif
  138. # ifdef SIXTY_FOUR_BIT
  139. # define BN_FMTu "%llu"
  140. # define BN_FMTx "%llx"
  141. # endif
  142. # ifdef THIRTY_TWO_BIT
  143. # define BN_FMTu "%u"
  144. # define BN_FMTx "%x"
  145. # endif
  146. int ossl_prov_print_labeled_bignum(BIO *out, const char *label,
  147. const BIGNUM *bn)
  148. {
  149. int ret = 0, use_sep = 0;
  150. char *hex_str = NULL, *p;
  151. const char spaces[] = " ";
  152. const char *post_label_spc = " ";
  153. const char *neg = "";
  154. int bytes;
  155. if (bn == NULL)
  156. return 0;
  157. if (label == NULL) {
  158. label = "";
  159. post_label_spc = "";
  160. }
  161. if (BN_is_zero(bn))
  162. return BIO_printf(out, "%s%s0\n", label, post_label_spc);
  163. if (BN_num_bytes(bn) <= BN_BYTES) {
  164. BN_ULONG *words = bn_get_words(bn);
  165. if (BN_is_negative(bn))
  166. neg = "-";
  167. return BIO_printf(out, "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",
  168. label, post_label_spc, neg, words[0], neg, words[0]);
  169. }
  170. hex_str = BN_bn2hex(bn);
  171. p = hex_str;
  172. if (*p == '-') {
  173. ++p;
  174. neg = " (Negative)";
  175. }
  176. if (BIO_printf(out, "%s%s\n", label, neg) <= 0)
  177. goto err;
  178. /* Keep track of how many bytes we have printed out so far */
  179. bytes = 0;
  180. if (BIO_printf(out, "%s", spaces) <= 0)
  181. goto err;
  182. /* Add a leading 00 if the top bit is set */
  183. if (*p >= '8') {
  184. if (BIO_printf(out, "%02x", 0) <= 0)
  185. goto err;
  186. ++bytes;
  187. use_sep = 1;
  188. }
  189. while (*p != '\0') {
  190. /* Do a newline after every 15 hex bytes + add the space indent */
  191. if ((bytes % 15) == 0 && bytes > 0) {
  192. if (BIO_printf(out, ":\n%s", spaces) <= 0)
  193. goto err;
  194. use_sep = 0; /* The first byte on the next line doesnt have a : */
  195. }
  196. if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "",
  197. ossl_tolower(p[0]), ossl_tolower(p[1])) <= 0)
  198. goto err;
  199. ++bytes;
  200. p += 2;
  201. use_sep = 1;
  202. }
  203. if (BIO_printf(out, "\n") <= 0)
  204. goto err;
  205. ret = 1;
  206. err:
  207. OPENSSL_free(hex_str);
  208. return ret;
  209. }
  210. /* Number of octets per line */
  211. #define LABELED_BUF_PRINT_WIDTH 15
  212. int ossl_prov_print_labeled_buf(BIO *out, const char *label,
  213. const unsigned char *buf, size_t buflen)
  214. {
  215. size_t i;
  216. if (BIO_printf(out, "%s\n", label) <= 0)
  217. return 0;
  218. for (i = 0; i < buflen; i++) {
  219. if ((i % LABELED_BUF_PRINT_WIDTH) == 0) {
  220. if (i > 0 && BIO_printf(out, "\n") <= 0)
  221. return 0;
  222. if (BIO_printf(out, " ") <= 0)
  223. return 0;
  224. }
  225. if (BIO_printf(out, "%02x%s", buf[i],
  226. (i == buflen - 1) ? "" : ":") <= 0)
  227. return 0;
  228. }
  229. if (BIO_printf(out, "\n") <= 0)
  230. return 0;
  231. return 1;
  232. }
  233. /* p2s = param to asn1, k2d = key to der */
  234. int ossl_prov_write_priv_der_from_obj(BIO *out, const void *obj, int obj_nid,
  235. int (*p2s)(const void *obj, int nid,
  236. void **str,
  237. int *strtype),
  238. int (*k2d)(const void *obj,
  239. unsigned char **pder),
  240. struct pkcs8_encrypt_ctx_st *ctx)
  241. {
  242. int ret = 0;
  243. void *str = NULL;
  244. int strtype = V_ASN1_UNDEF;
  245. if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
  246. return 0;
  247. if (ctx->cipher_intent) {
  248. X509_SIG *p8 =
  249. ossl_prov_encp8_from_obj(obj, obj_nid, str, strtype, k2d, ctx);
  250. if (p8 != NULL)
  251. ret = i2d_PKCS8_bio(out, p8);
  252. X509_SIG_free(p8);
  253. } else {
  254. PKCS8_PRIV_KEY_INFO *p8info =
  255. ossl_prov_p8info_from_obj(obj, obj_nid, str, strtype, k2d);
  256. if (p8info != NULL)
  257. ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info);
  258. PKCS8_PRIV_KEY_INFO_free(p8info);
  259. }
  260. return ret;
  261. }
  262. int ossl_prov_write_priv_pem_from_obj(BIO *out, const void *obj, int obj_nid,
  263. int (*p2s)(const void *obj, int nid,
  264. void **str,
  265. int *strtype),
  266. int (*k2d)(const void *obj,
  267. unsigned char **pder),
  268. struct pkcs8_encrypt_ctx_st *ctx)
  269. {
  270. int ret = 0;
  271. void *str = NULL;
  272. int strtype = V_ASN1_UNDEF;
  273. if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
  274. return 0;
  275. if (ctx->cipher_intent) {
  276. X509_SIG *p8 = ossl_prov_encp8_from_obj(obj, obj_nid, str, strtype,
  277. k2d, ctx);
  278. if (p8 != NULL)
  279. ret = PEM_write_bio_PKCS8(out, p8);
  280. X509_SIG_free(p8);
  281. } else {
  282. PKCS8_PRIV_KEY_INFO *p8info =
  283. ossl_prov_p8info_from_obj(obj, obj_nid, str, strtype, k2d);
  284. if (p8info != NULL)
  285. ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info);
  286. PKCS8_PRIV_KEY_INFO_free(p8info);
  287. }
  288. return ret;
  289. }
  290. int ossl_prov_write_pub_der_from_obj(BIO *out, const void *obj, int obj_nid,
  291. int (*p2s)(const void *obj, int nid,
  292. void **str,
  293. int *strtype),
  294. int (*k2d)(const void *obj,
  295. unsigned char **pder))
  296. {
  297. int ret = 0;
  298. void *str = NULL;
  299. int strtype = V_ASN1_UNDEF;
  300. X509_PUBKEY *xpk = NULL;
  301. if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
  302. return 0;
  303. xpk = ossl_prov_pubkey_from_obj(obj, obj_nid, str, strtype, k2d);
  304. if (xpk != NULL)
  305. ret = i2d_X509_PUBKEY_bio(out, xpk);
  306. /* Also frees |str| */
  307. X509_PUBKEY_free(xpk);
  308. return ret;
  309. }
  310. int ossl_prov_write_pub_pem_from_obj(BIO *out, const void *obj, int obj_nid,
  311. int (*p2s)(const void *obj, int nid,
  312. void **str,
  313. int *strtype),
  314. int (*k2d)(const void *obj,
  315. unsigned char **pder))
  316. {
  317. int ret = 0;
  318. void *str = NULL;
  319. int strtype = V_ASN1_UNDEF;
  320. X509_PUBKEY *xpk = NULL;
  321. if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
  322. return 0;
  323. xpk = ossl_prov_pubkey_from_obj(obj, obj_nid, str, strtype, k2d);
  324. if (xpk != NULL)
  325. ret = PEM_write_bio_X509_PUBKEY(out, xpk);
  326. /* Also frees |str| */
  327. X509_PUBKEY_free(xpk);
  328. return ret;
  329. }