p12_utl.c 7.4 KB

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