209
|
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_encode_ia5_string.c |
|
15 ASN.1 DER, encode a IA5 STRING, Tom St Denis |
|
16 */ |
|
17 |
|
18 #ifdef LTC_DER |
|
19 |
|
20 /** |
|
21 Store an IA5 STRING |
|
22 @param in The array of IA5 to store (one per char) |
|
23 @param inlen The number of IA5 to store |
|
24 @param out [out] The destination for the DER encoded IA5 STRING |
|
25 @param outlen [in/out] The max size and resulting size of the DER IA5 STRING |
|
26 @return CRYPT_OK if successful |
|
27 */ |
|
28 int der_encode_ia5_string(const unsigned char *in, unsigned long inlen, |
|
29 unsigned char *out, unsigned long *outlen) |
|
30 { |
|
31 unsigned long x, y, len; |
|
32 int err; |
|
33 |
|
34 LTC_ARGCHK(in != NULL); |
|
35 LTC_ARGCHK(out != NULL); |
|
36 LTC_ARGCHK(outlen != NULL); |
|
37 |
|
38 /* get the size */ |
|
39 if ((err = der_length_ia5_string(in, inlen, &len)) != CRYPT_OK) { |
|
40 return err; |
|
41 } |
|
42 |
|
43 /* too big? */ |
|
44 if (len > *outlen) { |
|
45 return CRYPT_BUFFER_OVERFLOW; |
|
46 } |
|
47 |
|
48 /* encode the header+len */ |
|
49 x = 0; |
|
50 out[x++] = 0x16; |
|
51 if (inlen < 128) { |
|
52 out[x++] = inlen; |
|
53 } else if (inlen < 256) { |
|
54 out[x++] = 0x81; |
|
55 out[x++] = inlen; |
|
56 } else if (inlen < 65536UL) { |
|
57 out[x++] = 0x82; |
|
58 out[x++] = (inlen>>8)&255; |
|
59 out[x++] = inlen&255; |
|
60 } else if (inlen < 16777216UL) { |
|
61 out[x++] = 0x83; |
|
62 out[x++] = (inlen>>16)&255; |
|
63 out[x++] = (inlen>>8)&255; |
|
64 out[x++] = inlen&255; |
|
65 } else { |
|
66 return CRYPT_INVALID_ARG; |
|
67 } |
|
68 |
|
69 /* store octets */ |
|
70 for (y = 0; y < inlen; y++) { |
|
71 out[x++] = der_ia5_char_encode(in[y]); |
|
72 } |
|
73 |
|
74 /* retun length */ |
|
75 *outlen = x; |
|
76 |
|
77 return CRYPT_OK; |
|
78 } |
|
79 |
|
80 #endif |
|
81 |
|
82 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/ia5/der_encode_ia5_string.c,v $ */ |
|
83 /* $Revision: 1.1 $ */ |
|
84 /* $Date: 2005/05/16 15:08:11 $ */ |