comparison src/pk/asn1/der/ia5/der_decode_ia5_string.c @ 209:39d5d58461d6 libtomcrypt-orig LTC_1.05

Import of libtomcrypt 1.05
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jul 2005 03:53:40 +0000
parents
children d5faf4814ddb
comparison
equal deleted inserted replaced
191:1c15b283127b 209:39d5d58461d6
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_ia5_string.c
15 ASN.1 DER, encode a IA5 STRING, Tom St Denis
16 */
17
18
19 #ifdef LTC_DER
20
21 /**
22 Store a IA5 STRING
23 @param in The DER encoded IA5 STRING
24 @param inlen The size of the DER IA5 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_ia5_string(const unsigned char *in, unsigned long inlen,
30 unsigned char *out, unsigned long *outlen)
31 {
32 unsigned long x, y, len;
33 int t;
34
35 LTC_ARGCHK(in != NULL);
36 LTC_ARGCHK(out != NULL);
37 LTC_ARGCHK(outlen != NULL);
38
39 /* must have header at least */
40 if (inlen < 2) {
41 return CRYPT_INVALID_PACKET;
42 }
43
44 /* check for 0x16 */
45 if ((in[0] & 0x1F) != 0x16) {
46 return CRYPT_INVALID_PACKET;
47 }
48 x = 1;
49
50 /* decode the length */
51 if (in[x] & 0x80) {
52 /* valid # of bytes in length are 1,2,3 */
53 y = in[x] & 0x7F;
54 if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
55 return CRYPT_INVALID_PACKET;
56 }
57
58 /* read the length in */
59 len = 0;
60 ++x;
61 while (y--) {
62 len = (len << 8) | in[x++];
63 }
64 } else {
65 len = in[x++] & 0x7F;
66 }
67
68 /* is it too long? */
69 if (len > *outlen) {
70 return CRYPT_BUFFER_OVERFLOW;
71 }
72
73 if (len + x > inlen) {
74 return CRYPT_INVALID_PACKET;
75 }
76
77 /* read the data */
78 for (y = 0; y < len; y++) {
79 t = der_ia5_value_decode(in[x++]);
80 if (t == -1) {
81 return CRYPT_INVALID_ARG;
82 }
83 out[y] = t;
84 }
85
86 *outlen = y;
87
88 return CRYPT_OK;
89 }
90
91 #endif
92
93 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/ia5/der_decode_ia5_string.c,v $ */
94 /* $Revision: 1.1 $ */
95 /* $Date: 2005/05/16 15:08:11 $ */