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_octet_string.c
13 ASN.1 DER, encode a OCTET STRING, Tom St Denis
21 @param in The DER encoded OCTET STRING
22 @param inlen The size of the DER OCTET STRING
23 @param out [out] The array of octets stored (one per char)
24 @param outlen [in/out] The number of octets stored
25 @return CRYPT_OK if successful
27 int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
28 unsigned char *out, unsigned long *outlen)
30 unsigned long x, y, len;
32 LTC_ARGCHK(in != NULL);
33 LTC_ARGCHK(out != NULL);
34 LTC_ARGCHK(outlen != NULL);
36 /* must have header at least */
38 return CRYPT_INVALID_PACKET;
42 if ((in[0] & 0x1F) != 0x04) {
43 return CRYPT_INVALID_PACKET;
47 /* decode the length */
49 /* valid # of bytes in length are 1,2,3 */
51 if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
52 return CRYPT_INVALID_PACKET;
55 /* read the length in */
59 len = (len << 8) | in[x++];
68 return CRYPT_BUFFER_OVERFLOW;
71 if (len + x > inlen) {
72 return CRYPT_INVALID_PACKET;
76 for (y = 0; y < len; y++) {
87 /* ref: $Format:%D$ */
88 /* git commit: $Format:%H$ */
89 /* commit time: $Format:%ai$ */