p12_utl.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright 1999-2023 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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/pkcs12.h>
  12. #include "p12_local.h"
  13. #include "crypto/pkcs7/pk7_local.h"
  14. /* Cheap and nasty Unicode stuff */
  15. unsigned char *OPENSSL_asc2uni(const char *asc, int asclen,
  16. unsigned char **uni, int *unilen)
  17. {
  18. int ulen, i;
  19. unsigned char *unitmp;
  20. if (asclen == -1)
  21. asclen = strlen(asc);
  22. if (asclen < 0)
  23. return NULL;
  24. ulen = asclen * 2 + 2;
  25. if ((unitmp = OPENSSL_malloc(ulen)) == NULL)
  26. return NULL;
  27. for (i = 0; i < ulen - 2; i += 2) {
  28. unitmp[i] = 0;
  29. unitmp[i + 1] = asc[i >> 1];
  30. }
  31. /* Make result double null terminated */
  32. unitmp[ulen - 2] = 0;
  33. unitmp[ulen - 1] = 0;
  34. if (unilen)
  35. *unilen = ulen;
  36. if (uni)
  37. *uni = unitmp;
  38. return unitmp;
  39. }
  40. char *OPENSSL_uni2asc(const unsigned char *uni, int unilen)
  41. {
  42. int asclen, i;
  43. char *asctmp;
  44. /* string must contain an even number of bytes */
  45. if (unilen & 1)
  46. return NULL;
  47. if (unilen < 0)
  48. return NULL;
  49. asclen = unilen / 2;
  50. /* If no terminating zero allow for one */
  51. if (!unilen || uni[unilen - 1])
  52. asclen++;
  53. uni++;
  54. if ((asctmp = OPENSSL_malloc(asclen)) == NULL)
  55. return NULL;
  56. for (i = 0; i < unilen; i += 2)
  57. asctmp[i >> 1] = uni[i];
  58. asctmp[asclen - 1] = 0;
  59. return asctmp;
  60. }
  61. /*
  62. * OPENSSL_{utf82uni|uni2utf8} perform conversion between UTF-8 and
  63. * PKCS#12 BMPString format, which is specified as big-endian UTF-16.
  64. * One should keep in mind that even though BMPString is passed as
  65. * unsigned char *, it's not the kind of string you can exercise e.g.
  66. * strlen on. Caller also has to keep in mind that its length is
  67. * expressed not in number of UTF-16 characters, but in number of
  68. * bytes the string occupies, and treat it, the length, accordingly.
  69. */
  70. unsigned char *OPENSSL_utf82uni(const char *asc, int asclen,
  71. unsigned char **uni, int *unilen)
  72. {
  73. int ulen, i, j;
  74. unsigned char *unitmp, *ret;
  75. unsigned long utf32chr = 0;
  76. if (asclen == -1)
  77. asclen = strlen(asc);
  78. for (ulen = 0, i = 0; i < asclen; i += j) {
  79. j = UTF8_getc((const unsigned char *)asc+i, asclen-i, &utf32chr);
  80. /*
  81. * Following condition is somewhat opportunistic is sense that
  82. * decoding failure is used as *indirect* indication that input
  83. * string might in fact be extended ASCII/ANSI/ISO-8859-X. The
  84. * fallback is taken in hope that it would allow to process
  85. * files created with previous OpenSSL version, which used the
  86. * naive OPENSSL_asc2uni all along. It might be worth noting
  87. * that probability of false positive depends on language. In
  88. * cases covered by ISO Latin 1 probability is very low, because
  89. * any printable non-ASCII alphabet letter followed by another
  90. * or any ASCII character will trigger failure and fallback.
  91. * In other cases situation can be intensified by the fact that
  92. * English letters are not part of alternative keyboard layout,
  93. * but even then there should be plenty of pairs that trigger
  94. * decoding failure...
  95. */
  96. if (j < 0)
  97. return OPENSSL_asc2uni(asc, asclen, uni, unilen);
  98. if (utf32chr > 0x10FFFF) /* UTF-16 cap */
  99. return NULL;
  100. if (utf32chr >= 0x10000) /* pair of UTF-16 characters */
  101. ulen += 2*2;
  102. else /* or just one */
  103. ulen += 2;
  104. }
  105. ulen += 2; /* for trailing UTF16 zero */
  106. if ((ret = OPENSSL_malloc(ulen)) == NULL)
  107. return NULL;
  108. /* re-run the loop writing down UTF-16 characters in big-endian order */
  109. for (unitmp = ret, i = 0; i < asclen; i += j) {
  110. j = UTF8_getc((const unsigned char *)asc+i, asclen-i, &utf32chr);
  111. if (utf32chr >= 0x10000) { /* pair if UTF-16 characters */
  112. unsigned int hi, lo;
  113. utf32chr -= 0x10000;
  114. hi = 0xD800 + (utf32chr>>10);
  115. lo = 0xDC00 + (utf32chr&0x3ff);
  116. *unitmp++ = (unsigned char)(hi>>8);
  117. *unitmp++ = (unsigned char)(hi);
  118. *unitmp++ = (unsigned char)(lo>>8);
  119. *unitmp++ = (unsigned char)(lo);
  120. } else { /* or just one */
  121. *unitmp++ = (unsigned char)(utf32chr>>8);
  122. *unitmp++ = (unsigned char)(utf32chr);
  123. }
  124. }
  125. /* Make result double null terminated */
  126. *unitmp++ = 0;
  127. *unitmp++ = 0;
  128. if (unilen)
  129. *unilen = ulen;
  130. if (uni)
  131. *uni = ret;
  132. return ret;
  133. }
  134. static int bmp_to_utf8(char *str, const unsigned char *utf16, int len)
  135. {
  136. unsigned long utf32chr;
  137. if (len == 0) return 0;
  138. if (len < 2) return -1;
  139. /* pull UTF-16 character in big-endian order */
  140. utf32chr = (utf16[0]<<8) | utf16[1];
  141. if (utf32chr >= 0xD800 && utf32chr < 0xE000) { /* two chars */
  142. unsigned int lo;
  143. if (len < 4) return -1;
  144. utf32chr -= 0xD800;
  145. utf32chr <<= 10;
  146. lo = (utf16[2]<<8) | utf16[3];
  147. if (lo < 0xDC00 || lo >= 0xE000) return -1;
  148. utf32chr |= lo-0xDC00;
  149. utf32chr += 0x10000;
  150. }
  151. return UTF8_putc((unsigned char *)str, len > 4 ? 4 : len, utf32chr);
  152. }
  153. char *OPENSSL_uni2utf8(const unsigned char *uni, int unilen)
  154. {
  155. int asclen, i, j;
  156. char *asctmp;
  157. /* string must contain an even number of bytes */
  158. if (unilen & 1)
  159. return NULL;
  160. for (asclen = 0, i = 0; i < unilen; ) {
  161. j = bmp_to_utf8(NULL, uni+i, unilen-i);
  162. /*
  163. * falling back to OPENSSL_uni2asc makes lesser sense [than
  164. * falling back to OPENSSL_asc2uni in OPENSSL_utf82uni above],
  165. * it's done rather to maintain symmetry...
  166. */
  167. if (j < 0) return OPENSSL_uni2asc(uni, unilen);
  168. if (j == 4) i += 4;
  169. else i += 2;
  170. asclen += j;
  171. }
  172. /* If no terminating zero allow for one */
  173. if (!unilen || (uni[unilen-2]||uni[unilen - 1]))
  174. asclen++;
  175. if ((asctmp = OPENSSL_malloc(asclen)) == NULL)
  176. return NULL;
  177. /* re-run the loop emitting UTF-8 string */
  178. for (asclen = 0, i = 0; i < unilen; ) {
  179. j = bmp_to_utf8(asctmp+asclen, uni+i, unilen-i);
  180. if (j == 4) i += 4;
  181. else i += 2;
  182. asclen += j;
  183. }
  184. /* If no terminating zero write one */
  185. if (!unilen || (uni[unilen-2]||uni[unilen - 1]))
  186. asctmp[asclen] = '\0';
  187. return asctmp;
  188. }
  189. int i2d_PKCS12_bio(BIO *bp, const PKCS12 *p12)
  190. {
  191. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS12), bp, p12);
  192. }
  193. #ifndef OPENSSL_NO_STDIO
  194. int i2d_PKCS12_fp(FILE *fp, const PKCS12 *p12)
  195. {
  196. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS12), fp, p12);
  197. }
  198. #endif
  199. PKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12)
  200. {
  201. OSSL_LIB_CTX *libctx = NULL;
  202. const char *propq = NULL;
  203. const PKCS7_CTX *p7ctx = NULL;
  204. if (p12 != NULL) {
  205. p7ctx = ossl_pkcs12_get0_pkcs7ctx(*p12);
  206. if (p7ctx != NULL) {
  207. libctx = ossl_pkcs7_ctx_get0_libctx(p7ctx);
  208. propq = ossl_pkcs7_ctx_get0_propq(p7ctx);
  209. }
  210. }
  211. return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(PKCS12), bp, p12, libctx, propq);
  212. }
  213. #ifndef OPENSSL_NO_STDIO
  214. PKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **p12)
  215. {
  216. OSSL_LIB_CTX *libctx = NULL;
  217. const char *propq = NULL;
  218. const PKCS7_CTX *p7ctx = NULL;
  219. if (p12 != NULL) {
  220. p7ctx = ossl_pkcs12_get0_pkcs7ctx(*p12);
  221. if (p7ctx != NULL) {
  222. libctx = ossl_pkcs7_ctx_get0_libctx(p7ctx);
  223. propq = ossl_pkcs7_ctx_get0_propq(p7ctx);
  224. }
  225. }
  226. return ASN1_item_d2i_fp_ex(ASN1_ITEM_rptr(PKCS12), fp, p12, libctx, propq);
  227. }
  228. #endif