]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/asn-oid.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * asn_oid.c - BER encode, decode, print and free routines for the
21 * ASN.1 OBJECT IDENTIFIER type.
24 * Copyright (C) 1992 Michael Sample and the University of British Columbia
26 * This library is free software; you can redistribute it and/or
27 * modify it provided that this copyright/license information is retained
30 * If you modify this file, you must clearly indicate your changes.
32 * This source code is distributed in the hope that it will be
33 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
34 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
36 * $Header: /cvs/root/Security/SecuritySNACCRuntime/c-lib/src/Attic/asn-oid.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
38 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
39 * Move from private repository to open source repository
41 * Revision 1.2 2001/05/05 00:59:25 rmurphy
42 * Adding darwin license headers
44 * Revision 1.1.1.1 1999/03/16 18:06:31 aram
45 * Originals from SMIME Free Library.
47 * Revision 1.3 1995/07/24 21:04:53 rj
48 * changed `_' to `-' in file names.
50 * Revision 1.2 1994/09/01 00:06:21 rj
51 * reduce the risk of unwanted surprises with macro expansion by properly separating the C tokens.
53 * Revision 1.1 1994/08/28 09:45:59 rj
54 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
58 #include "asn-config.h"
66 * encodes universal TAG LENGTH and Contents of and ASN.1 OBJECT ID
69 BEncAsnOid
PARAMS ((b
, data
),
75 len
= BEncAsnOidContent (b
, data
);
76 len
+= BEncDefLen (b
, len
);
77 len
+= BEncTag1 (b
, UNIV
, PRIM
, OID_TAG_CODE
);
83 * decodes universal TAG LENGTH and Contents of and ASN.1 OBJECT ID
86 BDecAsnOid
PARAMS ((b
, result
, bytesDecoded
, env
),
89 AsnLen
*bytesDecoded _AND_
95 if ((tag
= BDecTag (b
, bytesDecoded
, env
)) != MAKE_TAG_ID (UNIV
, PRIM
, INTEGER_TAG_CODE
))
97 Asn1Error ("BDecAsnOid: ERROR - wrong tag on OBJECT IDENTIFIER.\n");
101 elmtLen
= BDecLen (b
, bytesDecoded
, env
);
102 BDecAsnOidContent (b
, tag
, elmtLen
, result
, bytesDecoded
, env
);
109 * Decodes just the content of the OID.
110 * AsnOid is handled the same as a primtive octet string
113 BDecAsnOidContent
PARAMS ((b
, tagId
, len
, result
, bytesDecoded
, env
),
118 AsnLen
*bytesDecoded _AND_
121 result
->octetLen
= len
;
122 result
->octs
= Asn1Alloc (len
);
123 BufCopy (result
->octs
, b
, len
);
124 if (BufReadError (b
))
126 Asn1Error ("BDecAsnOidContent: ERROR - decoded past end of data\n");
129 (*bytesDecoded
) += len
;
130 } /* BDecAsnOidContent */
135 * Prints the given OID to the given FILE * in ASN.1 Value Notation.
136 * Since the internal rep of an OID is 'encoded', this routine
137 * decodes each individual arc number to print it.
140 PrintAsnOid
PARAMS ((f
,v
, indent
),
143 unsigned short int indent
)
145 unsigned short int firstArcNum
;
146 unsigned long int arcNum
;
151 /* un-munge first two arc numbers */
152 for (arcNum
= 0, i
=0; (i
< v
->octetLen
) && (v
->octs
[i
] & 0x80);i
++)
153 arcNum
= (arcNum
<< 7) + (v
->octs
[i
] & 0x7f);
155 arcNum
= (arcNum
<< 7) + (v
->octs
[i
] & 0x7f);
157 firstArcNum
= arcNum
/40;
161 fprintf (f
,"%u %u", firstArcNum
, arcNum
- (firstArcNum
* 40));
163 for (; i
< v
->octetLen
; )
165 for (arcNum
= 0; (i
< v
->octetLen
) && (v
->octs
[i
] & 0x80);i
++)
166 arcNum
= (arcNum
<< 7) + (v
->octs
[i
] & 0x7f);
168 arcNum
= (arcNum
<< 7) + (v
->octs
[i
] & 0x7f);
170 fprintf (f
," %u", arcNum
);
179 * given an OID, figures out the length for the encoded version
182 EncodedOidLen
PARAMS ((oid
),
186 unsigned long headArcNum
;
187 unsigned long tmpArcNum
;
191 * oid must have at least 2 elmts
193 if (oid
->next
== NULL
)
196 headArcNum
= (oid
->arcNum
* 40) + oid
->next
->arcNum
;
199 * figure out total encoded length of oid
201 tmpArcNum
= headArcNum
;
202 for (totalLen
= 1; (tmpArcNum
>>= 7) != 0; totalLen
++)
204 for (tmpOid
= oid
->next
->next
; tmpOid
!= NULL
; tmpOid
= tmpOid
->next
)
207 tmpArcNum
= tmpOid
->arcNum
;
208 for (; (tmpArcNum
>>= 7) != 0; totalLen
++)
214 } /* EncodedOidLen */
218 * given an oid list and a pre-allocated ENC_OID
219 * (use EncodedOidLen to figure out byte length needed)
220 * fills the ENC_OID with a BER encoded version
224 BuildEncodedOid
PARAMS ((oid
, result
),
229 unsigned long headArcNum
;
230 unsigned long tmpArcNum
;
238 * oid must have at least 2 elmts
240 if (oid
->next
== NULL
)
243 * munge together first two arcNum
244 * note first arcnum must be <= 2
245 * and second must be < 39 if first = 0 or 1
246 * see (X.209) for ref to this stupidity
248 headArcNum
= (oid
->arcNum
* 40) + oid
->next
->arcNum
;
250 tmpArcNum
= headArcNum
;
253 * calc # bytes needed for head arc num
255 for (len
= 0; (tmpArcNum
>>= 7) != 0; len
++)
259 * write more signifcant bytes (if any) of head arc num
260 * with 'more' bit set
262 for (i
=0; i
< len
; i
++)
263 *(buf
++) = 0x80 | (headArcNum
>> ((len
-i
)*7));
266 * write least significant byte of head arc num
268 *(buf
++) = 0x7f & headArcNum
;
272 * write following arc nums, if any
274 for (tmpOid
= oid
->next
->next
; tmpOid
!= NULL
; tmpOid
= tmpOid
->next
)
277 * figure out encoded length -1 of this arcNum
279 tmpArcNum
= tmpOid
->arcNum
;
280 for (len
= 0; (tmpArcNum
>>= 7) != 0; len
++)
285 * write more signifcant bytes (if any)
286 * with 'more' bit set
288 for (i
=0; i
< len
; i
++)
289 *(buf
++) = 0x80 | (tmpOid
->arcNum
>> ((len
-i
)*7));
292 * write least significant byte
294 *(buf
++) = 0x7f & tmpOid
->arcNum
;
297 } /* BuildEncodedOid */
301 * convert an AsnOid into an OID (linked list)
302 * NOT RECOMMENDED for use in protocol implementations
305 UnbuildEncodedOid
PARAMS ((eoid
, result
),
316 for (arcNum
= 0, i
=0; (i
< eoid
->octetLen
) && (eoid
->octs
[i
] & 0x80);i
++)
317 arcNum
= (arcNum
<< 7) + (eoid
->octs
[i
] & 0x7f);
319 arcNum
= (arcNum
<< 7) + (eoid
->octs
[i
] & 0x7f);
322 firstArcNum
= arcNum
/ 40;
326 secondArcNum
= arcNum
- (firstArcNum
* 40);
328 headOid
= (OID
*)malloc (sizeof (OID
));
329 headOid
->arcNum
= firstArcNum
;
330 headOid
->next
= (OID
*)malloc (sizeof (OID
));
331 headOid
->next
->arcNum
= secondArcNum
;
332 nextOid
= &headOid
->next
->next
;
334 for (; i
< eoid
->octetLen
; )
336 for (arcNum
= 0; (i
< eoid
->octetLen
) && (eoid
->octs
[i
] & 0x80); i
++)
337 arcNum
= (arcNum
<< 7) + (eoid
->octs
[i
] & 0x7f);
339 arcNum
= (arcNum
<< 7) + (eoid
->octs
[i
] & 0x7f);
341 *nextOid
= (OID
*)malloc (sizeof (OID
));
342 (*nextOid
)->arcNum
= arcNum
;
343 nextOid
= &(*nextOid
)->next
;
348 } /* UnbuildEncodedOid */