comparison libtomcrypt/src/pk/asn1/der/octet/der_decode_octet_string.c @ 285:1b9e69c058d2

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3) to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:58 +0000
parents
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11 #include "tomcrypt.h"
12
13 /**
14 @file der_decode_octet_string.c
15 ASN.1 DER, encode a OCTET STRING, Tom St Denis
16 */
17
18
19 #ifdef LTC_DER
20
21 /**
22 Store a OCTET STRING
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
28 */
29 int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
30 unsigned char *out, unsigned long *outlen)
31 {
32 unsigned long x, y, len;
33
34 LTC_ARGCHK(in != NULL);
35 LTC_ARGCHK(out != NULL);
36 LTC_ARGCHK(outlen != NULL);
37
38 /* must have header at least */
39 if (inlen < 2) {
40 return CRYPT_INVALID_PACKET;
41 }
42
43 /* check for 0x04 */
44 if ((in[0] & 0x1F) != 0x04) {
45 return CRYPT_INVALID_PACKET;
46 }
47 x = 1;
48
49 /* decode the length */
50 if (in[x] & 0x80) {
51 /* valid # of bytes in length are 1,2,3 */
52 y = in[x] & 0x7F;
53 if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
54 return CRYPT_INVALID_PACKET;
55 }
56
57 /* read the length in */
58 len = 0;
59 ++x;
60 while (y--) {
61 len = (len << 8) | in[x++];
62 }
63 } else {
64 len = in[x++] & 0x7F;
65 }
66
67 /* is it too long? */
68 if (len > *outlen) {
69 return CRYPT_BUFFER_OVERFLOW;
70 }
71
72 if (len + x > inlen) {
73 return CRYPT_INVALID_PACKET;
74 }
75
76 /* read the data */
77 for (y = 0; y < len; y++) {
78 out[y] = in[x++];
79 }
80
81 *outlen = y;
82
83 return CRYPT_OK;
84 }
85
86 #endif
87
88 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/octet/der_decode_octet_string.c,v $ */
89 /* $Revision: 1.1 $ */
90 /* $Date: 2005/05/16 15:08:11 $ */