p12_utl.c 7.1 KB

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