1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 @file der_decode_utf8_string.c
13 ASN.1 DER, encode a UTF8 STRING, Tom St Denis
21 @param in The DER encoded UTF8 STRING
22 @param inlen The size of the DER UTF8 STRING
23 @param out [out] The array of utf8s stored (one per char)
24 @param outlen [in/out] The number of utf8s stored
25 @return CRYPT_OK if successful
27 int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
28 wchar_t *out, unsigned long *outlen)
31 unsigned long x, y, z, len;
34 LTC_ARGCHK(in != NULL);
35 LTC_ARGCHK(out != NULL);
36 LTC_ARGCHK(outlen != NULL);
38 /* must have header at least */
40 return CRYPT_INVALID_PACKET;
44 if ((in[0] & 0x1F) != 0x0C) {
45 return CRYPT_INVALID_PACKET;
49 /* decode the length */
51 /* valid # of bytes in length are 1,2,3 */
53 if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
54 return CRYPT_INVALID_PACKET;
57 /* read the length in */
61 len = (len << 8) | in[x++];
67 if (len + x > inlen) {
68 return CRYPT_INVALID_PACKET;
71 /* proceed to decode */
72 for (y = 0; x < inlen; ) {
76 /* count number of bytes */
77 for (z = 0; (tmp & 0x80) && (z <= 4); z++, tmp = (tmp << 1) & 0xFF);
79 if (z > 4 || (x + (z - 1) > inlen)) {
80 return CRYPT_INVALID_PACKET;
83 /* decode, grab upper bits */
86 /* grab remaining bytes */
89 if ((in[x] & 0xC0) != 0x80) {
90 return CRYPT_INVALID_PACKET;
92 tmp = (tmp << 6) | ((wchar_t)in[x++] & 0x3F);
101 err = CRYPT_BUFFER_OVERFLOW;
112 /* ref: $Format:%D$ */
113 /* git commit: $Format:%H$ */
114 /* commit time: $Format:%ai$ */