]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/c++/asn-oid.cpp
11009e298ebf69e268caf0f9810346477ec2573e
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.
19 // file: .../c++-lib/src/asn-oid.C - OBJECT IDENTIFIER
23 // Copyright (C) 1992 Michael Sample and the University of British Columbia
25 // This library is free software; you can redistribute it and/or
26 // modify it provided that this copyright/license information is retained
29 // If you modify this file, you must clearly indicate your changes.
31 // This source code is distributed in the hope that it will be
32 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
33 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35 // $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c++-lib/c++/asn-oid.cpp,v 1.4 2002/03/21 05:38:45 dmitch Exp $
36 // $Log: asn-oid.cpp,v $
37 // Revision 1.4 2002/03/21 05:38:45 dmitch
38 // Radar 2868524: no more setjmp/longjmp in SNACC-generated code.
40 // Revision 1.3.44.1 2002/03/20 00:36:50 dmitch
41 // Radar 2868524: SNACC-generated code now uses throw/catch instead of setjmp/longjmp.
43 // Revision 1.3 2001/06/27 23:09:15 dmitch
44 // Pusuant to Radar 2664258, avoid all cerr-based output in NDEBUG configuration.
46 // Revision 1.2 2001/06/21 19:51:43 dmitch
47 // Made AsnOid::Set(unsigned long...) thread-safe by making static arc arrays local.
49 // Revision 1.1.1.1 2001/05/18 23:14:06 mb
50 // Move from private repository to open source repository
52 // Revision 1.4 2001/05/05 00:59:19 rmurphy
53 // Adding darwin license headers
55 // Revision 1.3 2000/06/08 20:05:35 dmitch
56 // Mods for X port. These files are actually machine generated and probably don't need to be in CVS....
58 // Revision 1.1.1.1 2000/03/09 01:00:06 rmurphy
59 // Base Fortissimo Tree
61 // Revision 1.3 1999/03/21 02:07:37 mb
62 // Added Copy to every AsnType.
64 // Revision 1.2 1999/02/26 00:23:40 mb
67 // Revision 1.1 1999/02/25 05:21:53 mb
68 // Added snacc c++ library
70 // Revision 1.7 1997/02/28 13:39:46 wan
71 // Modifications collected for new version 1.3: Bug fixes, tk4.2.
73 // Revision 1.6 1997/02/16 12:32:49 rj
74 // name lookup of \for' scoping
76 // Revision 1.5 1995/08/17 15:31:14 rj
77 // set Tcl's errorCode variable
79 // Revision 1.4 1995/07/24 20:25:38 rj
80 // #if TCL ... #endif wrapped into #if META ... #endif
82 // call constructor with additional pdu and create arguments.
84 // changed `_' to `-' in file names.
86 // Revision 1.3 1994/10/08 04:18:28 rj
87 // code for meta structures added (provides information about the generated code itself).
89 // code for Tcl interface added (makes use of the above mentioned meta code).
91 // virtual inline functions (the destructor, the Clone() function, BEnc(), BDec() and Print()) moved from inc/*.h to src/*.C because g++ turns every one of them into a static non-inline function in every file where the .h file gets included.
93 // made Print() const (and some other, mainly comparison functions).
95 // several `unsigned long int' turned into `size_t'.
97 // Revision 1.2 1994/08/28 10:01:17 rj
98 // comment leader fixed.
100 // Revision 1.1 1994/08/28 09:21:06 rj
101 // first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
103 #include "asn-config.h"
105 #include <strstream.h>
109 #include "asn-type.h"
118 AsnType
* AsnOid :: Clone () const
123 AsnType
* AsnOid :: Copy () const
125 return new AsnOid (* this );
128 // Initializes an AsnOid with a string and it's length.
129 // The string should hold the encoded OID.
130 // The string is copied
132 void AsnOid :: Set ( const char * encOid
, size_t len
)
134 void AsnOid :: Set ( const char * encOid
, const size_t len
)
135 #endif /* _IBM_ENC_ */
141 oid
= new char [ octetLen
];
143 oid
= ( char *) mem_mgr_ptr
-> Get ( octetLen
); /* Guido Grassel, 11.8.93 */
144 #endif /* _IBM_ENC_ */
145 memcpy ( oid
, encOid
, octetLen
);
149 // Inits an AsnOid from another OID.
150 // The oid string is copied.
151 void AsnOid :: Set ( const AsnOid
& o
)
155 octetLen
= o
. octetLen
;
157 oid
= new char [ octetLen
];
159 oid
= ( char *) mem_mgr_ptr
-> Get ( octetLen
); /* Guido Grassel, 11.8.93 */
160 #endif /* _IBM_ENC_ */
161 memcpy ( oid
, o
. oid
, octetLen
);
166 // Given some arc numbers, an AsnOid is built.
167 // Set (1, 2, 3, 4, 5, -1, -1, -1, -1, -1, -1) results in
168 // oid { 1 2 3 4 5 }. The first negative arc number represnts
169 // the end of the arc numbers - at least 2 are required.
170 // The prototype in the AsnOid class provides default -1 parameters
171 // so you only need to provide the number of arc number in the oid
172 // as params. (eg Set (1,2,3,4,5))
173 void AsnOid :: Set ( unsigned long int a1
, unsigned long int a2
, long int a3
, long int a4
, long int a5
, long int a6
, long int a7
, long int a8
, long int a9
, long int a10
, long int a11
)
175 long int arcNumArr
[ 11 ];
176 char buf
[ 11 * 5 ]; /* make big enough for max oid with 11 arcs*/
197 // munge together first oid arc numbers
198 headArcNum
= tmpArcNum
= ( arcNumArr
[ 0 ] * 40 ) + arcNumArr
[ 1 ];
200 // figure encoded length for this arc number
201 for ( elmtLen
= 1 ; ( tmpArcNum
>>= 7 ) != 0 ; elmtLen
++)
204 // write bytes except the last/least significant of the head arc number
208 for ( i
= 1 ; i
< elmtLen
; i
++)
210 *( tmpBuf
++) = 0x80 | ( headArcNum
>> (( elmtLen
- i
)* 7 ));
213 // write least significant (more bit is off)
214 *( tmpBuf
++) = 0x7f & headArcNum
;
216 // repeat for the rest of the arc numbers
217 for ( i
= 2 ; ( i
< 11 ) && ( arcNumArr
[ i
] > 0 ); i
++)
219 tmpArcNum
= arcNumArr
[ i
];
220 for ( elmtLen
= 1 ; ( tmpArcNum
>>= 7 ) != 0 ; elmtLen
++)
223 tmpArcNum
= arcNumArr
[ i
];
224 for ( unsigned j
= 1 ; j
< elmtLen
; j
++)
226 *( tmpBuf
++) = 0x80 | ( tmpArcNum
>> (( elmtLen
- j
)* 7 ));
228 *( tmpBuf
++) = 0x7f & tmpArcNum
;
232 oid
= Asn1Alloc ( totalLen
);
234 oid
= ( char *) mem_mgr_ptr
-> Get ( totalLen
); /* Guido Grassel, 11.8.93 */
235 #endif /* _IBM_ENC_ */
236 memcpy ( oid
, buf
, totalLen
);
243 // Like Set except frees old oid value first
245 void AsnOid :: ReSet ( const char * encOid
, size_t len
)
247 void AsnOid :: ReSet ( const char * encOid
, const size_t len
)
248 #endif /* _IBM_ENC_ */
255 mem_mgr_ptr
-> Put (( void *) oid
); /* Guido Grassel, 11.8.93 */
256 #endif /* _IBM_ENC_ */
261 // Like Set except frees old oid value first
262 void AsnOid :: ReSet ( const AsnOid
& o
)
269 mem_mgr_ptr
-> Put (( void *) oid
); /* Guido Grassel, 11.8.93 */
270 #endif /* _IBM_ENC_ */
275 void AsnOid :: ReSet ( unsigned long int a1
, unsigned long int a2
, long int a3
, long int a4
, long int a5
, long int a6
, long int a7
, long int a8
, long int a9
, long int a10
, long int a11
)
280 mem_mgr_ptr
-> Put (( void *) oid
); /* Guido Grassel, 11.8.93 */
281 #endif /* _IBM_ENC_ */
282 Set ( a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
, a9
, a10
, a11
);
286 // returns the number of arc numbers in the OID value
287 unsigned long int AsnOid :: NumArcs () const
292 for ( numArcs
= 0 , i
= 0 ; i
< octetLen
; )
294 // skip octets in this arc num with the 'more' bit set
295 for (; ( i
< octetLen
) && ( oid
[ i
] & 0x80 ); i
++)
298 // skip last octet in this arc num (no more bit)
304 // add one to return value because the first two arcs are
305 // crunched together into a single one.
308 } /* AsnOid::NumArcs */
310 int AsnOid :: OidEquiv ( const AsnOid
& o
) const
312 return o
. octetLen
== octetLen
&& ! strncmp ( o
. oid
, oid
, octetLen
);
317 // Decodes the content of a BER OBJECT IDENTIFIER value and puts
318 // the results in this AsnOid object.
319 void AsnOid :: BDecContent ( BUF_TYPE b
, AsnTag tagId
, AsnLen elmtLen
, AsnLen
& bytesDecoded
, ENV_TYPE env
)
321 /* treat like primitive octet string */
324 oid
= Asn1Alloc ( elmtLen
);
326 oid
= ( char *) mem_mgr_ptr
-> Get ( elmtLen
); /* Guido Grassel, 11.8.93 */
327 #endif /* _IBM_ENC_ */
328 b
. CopyOut ( oid
, elmtLen
);
332 Asn1Error
<< "BDecOctetString: ERROR - decoded past end of data" << endl
;
333 #if SNACC_EXCEPTION_ENABLE
334 SnaccExcep :: throwMe (- 17 );
339 bytesDecoded
+= elmtLen
;
340 } /* AsnOid::BDecContent */
342 AsnLen
AsnOid :: BEnc ( BUF_TYPE b
)
346 l
+= BEncDefLen ( b
, l
);
347 l
+= BEncTag1 ( b
, UNIV
, PRIM
, OID_TAG_CODE
);
351 void AsnOid :: BDec ( BUF_TYPE b
, AsnLen
& bytesDecoded
, ENV_TYPE env
)
354 if ( BDecTag ( b
, bytesDecoded
, env
) != MAKE_TAG_ID ( UNIV
, PRIM
, OID_TAG_CODE
))
356 Asn1Error
<< "AsnOid::BDec: ERROR tag on OBJECT IDENTIFIER is wrong." << endl
;
357 #if SNACC_EXCEPTION_ENABLE
358 SnaccExcep :: throwMe (- 57 );
363 elmtLen
= BDecLen ( b
, bytesDecoded
, env
);
365 BDecContent ( b
, MAKE_TAG_ID ( UNIV
, PRIM
, OID_TAG_CODE
), elmtLen
, bytesDecoded
, env
);
368 AsnLen
AsnOid :: BEncContent ( BUF_TYPE b
)
370 b
. PutSegRvs ( oid
, octetLen
);
374 // Prints an AsnOid in ASN.1 Value Notation.
375 // Decodes the oid to get the individual arc numbers
376 void AsnOid :: Print ( ostream
& os
) const
379 unsigned short int firstArcNum
;
380 unsigned long int arcNum
;
388 // un-munge first two arc numbers
389 for ( arcNum
= 0 , i
= 0 ; ( i
< octetLen
) && ( oid
[ i
] & 0x80 ); i
++)
390 arcNum
= ( arcNum
<< 7 ) + ( oid
[ i
] & 0x7f );
392 arcNum
= ( arcNum
<< 7 ) + ( oid
[ i
] & 0x7f );
394 firstArcNum
= arcNum
/ 40 ;
398 os
<< firstArcNum
<< " " << arcNum
- ( firstArcNum
* 40 );
400 for (; i
< octetLen
; )
402 for ( arcNum
= 0 ; ( i
< octetLen
) && ( oid
[ i
] & 0x80 ); i
++)
403 arcNum
= ( arcNum
<< 7 ) + ( oid
[ i
] & 0x7f );
405 arcNum
= ( arcNum
<< 7 ) + ( oid
[ i
] & 0x7f );
419 const AsnOidTypeDesc
AsnOid :: _desc ( NULL
, NULL
, false , AsnTypeDesc :: OBJECT_IDENTIFIER
, NULL
);
421 const AsnTypeDesc
* AsnOid :: _getdesc () const
428 int AsnOid :: TclGetVal ( Tcl_Interp
* interp
) const
434 buf
. str ()[ strlen ( buf
. str ())- 1 ] = '\0' ; // chop the trailing '}'
435 Tcl_SetResult ( interp
, buf
. str ()+ 1 , TCL_VOLATILE
); // copy without leading '{'
440 int AsnOid :: TclSetVal ( Tcl_Interp
* interp
, const char * valstr
)
451 if ( Tcl_SplitList ( interp
, ( char *) valstr
, & arc
. c
, & arc
. v
) != TCL_OK
)
455 Tcl_AppendResult ( interp
, "oid arc must contain at least two numbers" , NULL
);
456 Tcl_SetErrorCode ( interp
, "SNACC" , "ILLARC" , "<2" , NULL
);
461 Tcl_AppendResult ( interp
, "snacc limits oid arcs to no more than 11 numbers" , NULL
);
462 Tcl_SetErrorCode ( interp
, "SNACC" , "ILLARC" , ">11" , NULL
);
467 for ( i
= 0 ; i
< arc
. c
; i
++)
468 if ( Tcl_GetInt ( interp
, arc
. v
[ i
], na
+ i
) != TCL_OK
)
473 ReSet ( na
[ 0 ], na
[ 1 ], na
[ 2 ], na
[ 3 ], na
[ 4 ], na
[ 5 ], na
[ 6 ], na
[ 7 ], na
[ 8 ], na
[ 9 ], na
[ 10 ]);