annotate src/pk/asn1/der/object_identifier/der_decode_object_identifier.c @ 222:36160290a1b2 libtomcrypt LTC_DB_0.46

* change include path of options.h * don't use the constant ROL/ROR operations, since compilers seem to have problems
author Matt Johnston <matt@ucc.asn.au>
date Fri, 08 Jul 2005 19:13:24 +0000
parents 39d5d58461d6
children d5faf4814ddb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 *
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * LibTomCrypt is a library that provides various cryptographic
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * algorithms in a highly modular and flexible manner.
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 *
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * The library is free for all purposes without any express
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * guarantee it works.
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 *
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * Tom St Denis, [email protected], http://libtomcrypt.org
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 #include "tomcrypt.h"
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 /**
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 @file der_decode_object_identifier.c
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 ASN.1 DER, Decode Object Identifier, Tom St Denis
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 #ifdef LTC_DER
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 /**
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 Decode OID data and store the array of integers in words
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 @param in The OID DER encoded data
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 @param inlen The length of the OID data
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 @param words [out] The destination of the OID words
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 @param outlen [in/out] The number of OID words
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 @return CRYPT_OK if successful
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 unsigned long *words, unsigned long *outlen)
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 unsigned long x, y, t, len;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 LTC_ARGCHK(in != NULL);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 LTC_ARGCHK(words != NULL);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 LTC_ARGCHK(outlen != NULL);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 /* header is at least 3 bytes */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 if (inlen < 3) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 return CRYPT_INVALID_PACKET;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 /* must be room for at least two words */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 if (*outlen < 2) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 return CRYPT_BUFFER_OVERFLOW;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 /* decode the packet header */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 x = 0;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 if ((in[x++] & 0x1F) != 0x06) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 return CRYPT_INVALID_PACKET;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 /* get the length */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 if (in[x] < 128) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 len = in[x++];
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 } else {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 if (in[x] < 0x81 || in[x] > 0x82) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 return CRYPT_INVALID_PACKET;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 y = in[x++] & 0x7F;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 len = 0;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 while (y--) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 len = (len << 8) | (unsigned long)in[x++];
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 if (len < 1 || (len + x) > inlen) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 return CRYPT_INVALID_PACKET;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 /* decode word1 and word2 */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 --len;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 t = in[x++];
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 words[0] = t/40;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 words[1] = t%40;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 /* decode rest */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 y = 2;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 t = 0;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 while (len--) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 t = (t << 7) | (in[x] & 0x7F);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 if (!(in[x++] & 0x80)) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 /* store t */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 if (y >= *outlen) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 return CRYPT_BUFFER_OVERFLOW;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 words[y++] = t;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 t = 0;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 *outlen = y;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 return CRYPT_OK;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 #endif
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/object_identifier/der_decode_object_identifier.c,v $ */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 /* $Revision: 1.1 $ */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 /* $Date: 2005/05/16 15:08:11 $ */