]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/compiler/core/oid.c
Security-54.tar.gz
[apple/security.git] / SecuritySNACCRuntime / compiler / core / oid.c
1 /*
2 * compiler/core/oid.c - routines for:
3 * converting an arc number list to an ENC_OID
4 * converting an ENC_OID to an arc number list
5 * arcName mapping routine
6 *
7 * does not handle OID's with unresolved valueRefs instead of arcNums
8 *
9 * MS 91
10 *
11 * Copyright (C) 1991, 1992 Michael Sample
12 * and the University of British Columbia
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/compiler/core/oid.c,v 1.1 2001/06/20 21:27:58 dmitch Exp $
20 * $Log: oid.c,v $
21 * Revision 1.1 2001/06/20 21:27:58 dmitch
22 * Adding missing snacc compiler files.
23 *
24 * Revision 1.1.1.1 1999/03/16 18:06:51 aram
25 * Originals from SMIME Free Library.
26 *
27 * Revision 1.3 1995/07/25 19:41:41 rj
28 * changed `_' to `-' in file names.
29 *
30 * Revision 1.2 1994/09/01 00:41:33 rj
31 * snacc_config.h removed; oid.h includet.
32 *
33 * Revision 1.1 1994/08/28 09:49:26 rj
34 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
35 *
36 */
37
38 #include <stdio.h> /* for FILE * */
39
40 #include "asn-incl.h"
41 #include "oid.h"
42
43 typedef struct ArcNameMapElmt
44 {
45 char *arcName;
46 int arcNum;
47 } ArcNameMapElmt;
48
49
50 /*
51 * these are the CCITT and ISO pre-defined arc names for the
52 * OBJECT IDENTIFIER tree.
53 * Ref: CCITT X.208 1988 - Annexes B C and D
54 *
55 * NOTE: the last entry must have a NULL string and a
56 * -1 arcnumber to indicate the end of the array.
57 */
58 ArcNameMapElmt oidArcNameMapG[] =
59 {
60 "ccitt", 0,
61 "iso", 1,
62 "joint-iso-ccitt", 2,
63 "standard", 0,
64 "registration-authority", 1,
65 "member-body", 2,
66 "identified-organization", 3,
67 "recommendation", 0,
68 "question", 1,
69 "administration", 2,
70 "network-operator", 3,
71 NULL,-1
72 };
73
74
75 /*
76 * returns the arcnum (>0) of the given name if it
77 * is a defined oid arc name like "iso" or "ccitt"
78 * returns -1 if the name was not found
79 *
80 * name must be null terminated.
81 */
82 int
83 OidArcNameToNum PARAMS ((name),
84 char *name)
85 {
86 int i;
87 for (i= 0; oidArcNameMapG[i].arcName != NULL; i++)
88 {
89 if (strcmp (name, oidArcNameMapG[i].arcName) == 0)
90 return oidArcNameMapG[i].arcNum;
91 }
92 return -1;
93 } /* OidArcNameToNum */
94
95
96
97 /*
98 * Takes and OBJECT IDENTIFER in the linked format
99 * (produced by parser) and returns the number of octets
100 * that are needed to hold the encoded version of that
101 * OBJECT IDENTIFIER.
102 */
103 unsigned long int
104 EncodedOidLen PARAMS ((oid),
105 OID *oid)
106 {
107 unsigned long totalLen;
108 unsigned long headArcNum;
109 unsigned long tmpArcNum;
110 OID *tmpOid;
111
112 /*
113 * oid must have at least 2 elmts
114 */
115 if (oid->next == NULL)
116 return 0;
117
118 headArcNum = (oid->arcNum * 40) + oid->next->arcNum;
119
120 /*
121 * figure out total encoded length of oid
122 */
123 tmpArcNum = headArcNum;
124 for (totalLen = 1; (tmpArcNum >>= 7) != 0; totalLen++)
125 ;
126 for (tmpOid = oid->next->next; tmpOid != NULL; tmpOid = tmpOid->next)
127 {
128 totalLen++;
129 tmpArcNum = tmpOid->arcNum;
130 for (; (tmpArcNum >>= 7) != 0; totalLen++)
131 ;
132 }
133
134 return totalLen;
135
136 } /* EncodedOidLen */
137
138
139 /*
140 * Given an oid arc number list and a pre-allocated ENC_OID
141 * (use EncodedOidLen to figure out byte length needed)
142 * fills the ENC_OID with a BER encoded version
143 * of the oid.
144 */
145 void
146 BuildEncodedOid PARAMS ((oid, result),
147 OID *oid _AND_
148 AsnOid *result)
149 {
150 unsigned long len;
151 unsigned long headArcNum;
152 unsigned long tmpArcNum;
153 char *buf;
154 int i;
155 OID *tmpOid;
156
157 buf = result->octs;
158
159 /*
160 * oid must have at least 2 elmts
161 */
162 if (oid->next == NULL)
163 return;
164 /*
165 * munge together first two arcNum
166 * note first arcnum must be <= 2
167 * and second must be < 39 if first = 0 or 1
168 * see (X.209) for ref to this stupidity
169 */
170 headArcNum = (oid->arcNum * 40) + oid->next->arcNum;
171
172 tmpArcNum = headArcNum;
173
174 /*
175 * calc # bytes needed for head arc num
176 */
177 for (len = 0; (tmpArcNum >>= 7) != 0; len++)
178 ;
179
180 /*
181 * write more signifcant bytes (if any) of head arc num
182 * with 'more' bit set
183 */
184 for (i=0; i < len; i++)
185 *(buf++) = 0x80 | (headArcNum >> ((len-i)*7));
186
187 /*
188 * write least significant byte of head arc num
189 */
190 *(buf++) = 0x7f & headArcNum;
191
192
193 /*
194 * write following arc nums, if any
195 */
196 for (tmpOid = oid->next->next; tmpOid != NULL; tmpOid = tmpOid->next)
197 {
198 /*
199 * figure out encoded length -1 of this arcNum
200 */
201 tmpArcNum = tmpOid->arcNum;
202 for (len = 0; (tmpArcNum >>= 7) != 0; len++)
203 ;
204
205
206 /*
207 * write more signifcant bytes (if any)
208 * with 'more' bit set
209 */
210 for (i=0; i < len; i++)
211 *(buf++) = 0x80 | (tmpOid->arcNum >> ((len-i)*7));
212
213 /*
214 * write least significant byte
215 */
216 *(buf++) = 0x7f & tmpOid->arcNum;
217 }
218
219 } /* BuildEncodedOid */
220
221
222 /*
223 * Given an ENC_OID, this routine converts it into a
224 * linked oid (OID).
225 */
226 void
227 UnbuildEncodedOid PARAMS ((eoid, result),
228 AsnOid *eoid _AND_
229 OID **result)
230 {
231 OID **nextOid;
232 OID *headOid;
233 int arcNum;
234 int i;
235 int firstArcNum;
236 int secondArcNum;
237
238 for (arcNum = 0, i=0; (i < eoid->octetLen) && (eoid->octs[i] & 0x80);i++)
239 arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);
240
241 arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);
242 i++;
243
244 firstArcNum = arcNum / 40;
245 if (firstArcNum > 2)
246 firstArcNum = 2;
247
248 secondArcNum = arcNum - (firstArcNum * 40);
249
250 headOid = (OID*)Malloc (sizeof (OID));
251 headOid->arcNum = firstArcNum;
252 headOid->next = (OID*)Malloc (sizeof (OID));
253 headOid->next->arcNum = secondArcNum;
254 nextOid = &headOid->next->next;
255
256 for ( ; i < eoid->octetLen; )
257 {
258 for (arcNum = 0; (i < eoid->octetLen) && (eoid->octs[i] & 0x80);i++)
259 arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);
260
261 arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);
262 i++;
263 *nextOid = (OID*)Malloc (sizeof (OID));
264 (*nextOid)->arcNum = arcNum;
265 nextOid = &(*nextOid)->next;
266 }
267
268 *result = headOid;
269
270 } /* UnbuildEncodedOid */