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
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file der_decode_octet_string.c
15 ASN.1 DER, encode a OCTET STRING, Tom St Denis
23 @param in The DER encoded OCTET STRING
24 @param inlen The size of the DER OCTET STRING
25 @param out [out] The array of octets stored (one per char)
26 @param outlen [in/out] The number of octets stored
27 @return CRYPT_OK if successful
29 int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
30 unsigned char *out, unsigned long *outlen)
32 unsigned long x, y, 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) != 0x04) {
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++];
70 return CRYPT_BUFFER_OVERFLOW;
73 if (len + x > inlen) {
74 return CRYPT_INVALID_PACKET;
78 for (y = 0; y < len; y++) {
89 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/octet/der_decode_octet_string.c,v $ */
90 /* $Revision: 1.3 $ */
91 /* $Date: 2006/06/16 21:53:41 $ */