]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/c++/asn-octs.cpp
250b198fb9f8c788df698a792a947547ef9f3d32
[apple/security.git] / SecuritySNACCRuntime / c++-lib / c++ / asn-octs.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 // file: .../c++-lib/src/asn-octs.C - ASN.1 OCTET STRING methods for AsnOcts Class
20 //
21 // Mike Sample
22 // 92/07/02
23 // Copyright (C) 1992 Michael Sample and the University of British Columbia
24 //
25 // This library is free software; you can redistribute it and/or
26 // modify it provided that this copyright/license information is retained
27 // in original form.
28 //
29 // If you modify this file, you must clearly indicate your changes.
30 //
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.
34 //
35 // $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/c++-lib/c++/asn-octs.cpp,v 1.3 2002/03/21 05:38:44 dmitch Exp $
36 // $Log: asn-octs.cpp,v $
37 // Revision 1.3 2002/03/21 05:38:44 dmitch
38 // Radar 2868524: no more setjmp/longjmp in SNACC-generated code.
39 //
40 // Revision 1.2.44.1 2002/03/20 00:36:49 dmitch
41 // Radar 2868524: SNACC-generated code now uses throw/catch instead of setjmp/longjmp.
42 //
43 // Revision 1.2 2001/06/27 23:09:15 dmitch
44 // Pusuant to Radar 2664258, avoid all cerr-based output in NDEBUG configuration.
45 //
46 // Revision 1.1.1.1 2001/05/18 23:14:06 mb
47 // Move from private repository to open source repository
48 //
49 // Revision 1.3 2001/05/05 00:59:17 rmurphy
50 // Adding darwin license headers
51 //
52 // Revision 1.2 2000/12/07 22:16:14 dmitch
53 // Thread-safe mods: added a local StrStk strStkG to the routines which need it.
54 //
55 // Revision 1.1 2000/06/15 18:44:57 dmitch
56 // These snacc-generated source files are now checked in to allow cross-platform build.
57 //
58 // Revision 1.2 2000/06/08 20:05:35 dmitch
59 // Mods for X port. These files are actually machine generated and probably don't need to be in CVS....
60 //
61 // Revision 1.1.1.1 2000/03/09 01:00:06 rmurphy
62 // Base Fortissimo Tree
63 //
64 // Revision 1.4 1999/04/08 21:21:25 aram
65 // Fixed AsnOcts destructor.
66 //
67 // Revision 1.3 1999/03/21 02:07:36 mb
68 // Added Copy to every AsnType.
69 //
70 // Revision 1.2 1999/02/26 00:23:40 mb
71 // Fixed for Mac OS 8
72 //
73 // Revision 1.1 1999/02/25 05:21:53 mb
74 // Added snacc c++ library
75 //
76 // Revision 1.6 1997/02/28 13:39:45 wan
77 // Modifications collected for new version 1.3: Bug fixes, tk4.2.
78 //
79 // Revision 1.5 1995/08/17 15:33:33 rj
80 // augment binify()'s error message
81 //
82 // Revision 1.4 1995/07/24 20:23:32 rj
83 // use memcmpeq that is defined in .../snacc.h to use either memcmp or bcmp.
84 //
85 // code extracted from AsnOcts::TclGetVal and AsnOcts::TclSetVal into ::debinify and ::binify in meta.C
86 //
87 // #if TCL ... #endif wrapped into #if META ... #endif
88 //
89 // call constructor with additional pdu and create arguments.
90 //
91 // changed `_' to `-' in file names.
92 //
93 // Revision 1.3 1994/10/08 04:18:27 rj
94 // code for meta structures added (provides information about the generated code itself).
95 //
96 // code for Tcl interface added (makes use of the above mentioned meta code).
97 //
98 // 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.
99 //
100 // made Print() const (and some other, mainly comparison functions).
101 //
102 // several `unsigned long int' turned into `size_t'.
103 //
104 // Revision 1.2 1994/08/28 10:01:16 rj
105 // comment leader fixed.
106 //
107 // Revision 1.1 1994/08/28 09:21:05 rj
108 // first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
109
110 #include "asn-config.h"
111 #include "asn-len.h"
112 #include "asn-tag.h"
113 #include "asn-type.h"
114 #include "asn-bits.h" /* for TO_HEX */
115 #include "asn-octs.h"
116 #include "str-stk.h"
117
118 #ifndef __APPLE__
119 extern StrStk strStkG;
120 #endif
121
122 AsnOcts::~AsnOcts()
123 {
124 Asn1Free( octs );
125 }
126
127 AsnType *AsnOcts::Clone() const
128 {
129 return new AsnOcts;
130 }
131
132 AsnType *AsnOcts::Copy() const
133 {
134 return new AsnOcts (*this);
135 }
136
137 // Initialize the AsnOcts with a char * and length.
138 // copies the string str.
139 void AsnOcts::Set (const char *str, size_t len)
140 {
141 octetLen = len;
142 #ifndef _IBM_ENC_
143 octs = new char[octetLen];
144 #else
145 octs = (char *) mem_mgr_ptr->Get (octetLen+1); /* Guido Grassel, 11.8.93 */
146 #endif /* _IBM_ENC_ */
147 memcpy (octs, str, octetLen);
148 }
149
150 // Initialize the AsnOcts from another AsnOcts
151 // copies the string from o.
152 void AsnOcts::Set (const AsnOcts &o)
153 {
154 if (&o != this) // avoid o = o;
155 {
156 octetLen = o.octetLen;
157 #ifndef _IBM_ENC_
158 octs = new char[octetLen];
159 #else
160 octs = (char *) mem_mgr_ptr->Get (octetLen+1); /* Guido Grassel, 11.8.93 */
161 #endif /* _IBM_ENC_ */
162 memcpy (octs, o.octs, octetLen);
163 }
164 }
165
166 // Initialize the AsnOcts from a Null terminated string.
167 // copies the string str.
168 void AsnOcts::Set (const char *str)
169 {
170 octetLen = strlen (str);
171 #ifndef _IBM_ENC_
172 octs = new char[octetLen];
173 #else
174 octs = (char *) mem_mgr_ptr->Get (octetLen+1); /* Guido Grassel, 11.8.93 */
175 #endif /* _IBM_ENC_ */
176 memcpy (octs, str, octetLen);
177 }
178
179
180 // free old octs value and tehn set the new
181 // octs and octetLen values with the given char *and length.
182 // copies the string str.
183 void AsnOcts::ReSet (const char *str, size_t len)
184 {
185 if (str != octs)
186 {
187 #ifndef _IBM_ENC_
188 delete octs;
189 Set (str, len);
190 #else
191 mem_mgr_ptr->Put ((void *)octs); /* Guido Grassel, 11.8.93 */
192 Set (str, len);
193 #endif /* _IBM_ENC_ */
194 }
195 }
196
197 // Free old octs value and then set the new
198 // octs and octetLen from the given AsnOcts
199 // copies the string in o.
200 void AsnOcts::ReSet (const AsnOcts &o)
201 {
202 if (&o != this) // avoid s = s;
203 {
204 #ifndef _IBM_ENC_
205 delete octs;
206 Set (o);
207 #else
208 mem_mgr_ptr->Put ((void *)octs); /* Guido Grassel, 11.8.93 */
209 Set (o);
210 #endif /* _IBM_ENC_ */
211 }
212 }
213
214 // Free old octs value and then set the new
215 // octs and octetLen values from a null terminated string.
216 // copies the string str.
217 void AsnOcts::ReSet (const char *str)
218 {
219 if (str != octs)
220 {
221 #ifndef _IBM_ENC_
222 delete octs;
223 Set (str);
224 #else
225 mem_mgr_ptr->Put ((void *)octs); /* Guido Grassel, 11.8.93 */
226 Set (str);
227 #endif /* _IBM_ENC_ */
228 }
229 }
230
231
232 // Prints the AsnOcts to the given ostream in Value Notation.
233 void AsnOcts::Print (ostream &os) const
234 {
235 #ifndef NDEBUG
236 unsigned i;
237 os << "'";
238 for (i = 0; i < octetLen; i++)
239 os << TO_HEX (octs[i] >> 4) << (TO_HEX (octs[i]));
240
241 os << "'H -- \"";
242
243 /* put printable parts in ASN.1 comment */
244 for (i = 0; i < octetLen; i++)
245 {
246 if (isspace (octs[i]))
247 os << " "; /* newlines->space (so don't screw up ASN.1 comment) */
248 else if (isprint (octs[i]))
249 os <<octs[i];
250 else
251 os << ".";
252 }
253 os << "\" --";
254 #endif /* NDEBUG */
255 } /* AsnOcts::Print */
256
257
258 AsnLen AsnOcts::BEncContent (BUF_TYPE b)
259 {
260 b.PutSegRvs (octs, octetLen);
261 return octetLen;
262 }
263
264 // Decodes a BER OCTET STRING value and puts it in this object.
265 // Constructed OCTET STRINGs are always concatenated into primitive ones.
266 void AsnOcts::BDecContent (BUF_TYPE b, AsnTag tagId, AsnLen elmtLen, AsnLen &bytesDecoded, ENV_TYPE env)
267 {
268 /*
269 * tagId is encoded tag shifted into long int.
270 * if CONS bit is set then constructed octet string
271 */
272 if (tagId & 0x20000000)
273 BDecConsOcts (b, elmtLen, bytesDecoded, env);
274
275 else /* primitive octet string */
276 {
277 octetLen = elmtLen;
278 #ifndef _IBM_ENC_
279 octs = Asn1Alloc (elmtLen+1);
280 #else
281 octs = (char *) mem_mgr_ptr->Get (elmtLen+1); /* Guido Grassel, 11.8.93 */
282 #endif /* _IBM_ENC_ */
283 b.CopyOut (octs, elmtLen);
284
285 if (b.ReadError())
286 {
287 Asn1Error << "BDecOctetString: ERROR - decoded past end of data" << endl;
288 #if SNACC_EXCEPTION_ENABLE
289 SnaccExcep::throwMe(-14);
290 #else
291 longjmp (env, -14);
292 #endif
293 }
294
295 /* add null terminator - this is not included in the str's len */
296 octs[elmtLen] = '\0';
297 bytesDecoded += elmtLen;
298 }
299
300 } /* AsnOcts::BDecContent */
301
302 AsnLen AsnOcts::BEnc (BUF_TYPE b)
303 {
304 AsnLen l;
305 l = BEncContent (b);
306 l += BEncDefLen (b, l);
307 l += BEncTag1 (b, UNIV, PRIM, OCTETSTRING_TAG_CODE);
308 return l;
309 }
310
311 void AsnOcts::BDec (BUF_TYPE b, AsnLen &bytesDecoded, ENV_TYPE env)
312 {
313 AsnLen elmtLen;
314 AsnTag tag;
315
316 tag = BDecTag (b, bytesDecoded, env);
317 if ((tag != MAKE_TAG_ID (UNIV, PRIM, OCTETSTRING_TAG_CODE)) &&
318 (tag != MAKE_TAG_ID (UNIV, CONS, OCTETSTRING_TAG_CODE)))
319 {
320 Asn1Error << "AsnOcts::BDec: ERROR tag on OCTET STRING is wrong." << endl;
321 #if SNACC_EXCEPTION_ENABLE
322 SnaccExcep::throwMe(-56);
323 #else
324 longjmp (env,-56);
325 #endif
326 }
327 elmtLen = BDecLen (b, bytesDecoded, env);
328 BDecContent (b, tag, elmtLen, bytesDecoded, env);
329 }
330
331
332
333 /*
334 * Used for concatenating constructed OCTET STRING values when decoding
335 *
336 * fills string stack with references to the pieces of a
337 * construced octet string
338 */
339 #ifdef __APPLE__
340 static void
341 FillOctetStringStk (BUF_TYPE b, AsnLen elmtLen0, AsnLen &bytesDecoded, ENV_TYPE env, StrStk &strStkG)
342 #else
343 static void
344 FillOctetStringStk (BUF_TYPE b, AsnLen elmtLen0, AsnLen &bytesDecoded, ENV_TYPE env)
345 #endif
346 {
347 size_t refdLen;
348 size_t totalRefdLen;
349 char *strPtr;
350 AsnLen totalElmtsLen1 = 0;
351 unsigned long int tagId1;
352 AsnLen elmtLen1;
353
354 for (; (totalElmtsLen1 < elmtLen0) || (elmtLen0 == INDEFINITE_LEN);)
355 {
356 tagId1 = BDecTag (b, totalElmtsLen1, env);
357
358 if ((tagId1 == EOC_TAG_ID) && (elmtLen0 == INDEFINITE_LEN))
359 {
360 BDEC_2ND_EOC_OCTET (b, totalElmtsLen1, env);
361 break;
362 }
363
364 elmtLen1 = BDecLen (b, totalElmtsLen1, env);
365 if (tagId1 == MAKE_TAG_ID (UNIV, PRIM, OCTETSTRING_TAG_CODE))
366 {
367 /*
368 * primitive part of string, put references to piece (s) in
369 * str stack
370 */
371 totalRefdLen = 0;
372 refdLen = elmtLen1;
373 while (1)
374 {
375 strPtr = b.GetSeg (&refdLen);
376
377 strStkG.Push (strPtr, refdLen);
378 totalRefdLen += refdLen;
379 if (totalRefdLen == elmtLen1)
380 break; /* exit this while loop */
381
382 if (refdLen == 0) /* end of data */
383 {
384 Asn1Error << "BDecConsOctetString: ERROR - attempt to decode past end of data" << endl;
385 #if SNACC_EXCEPTION_ENABLE
386 SnaccExcep::throwMe(-15);
387 #else
388 longjmp (env, -15);
389 #endif
390 }
391 refdLen = elmtLen1 - totalRefdLen;
392 }
393 totalElmtsLen1 += elmtLen1;
394 }
395
396
397 else if (tagId1 == MAKE_TAG_ID (UNIV, CONS, OCTETSTRING_TAG_CODE))
398 {
399 /*
400 * constructed octets string embedding in this constructed
401 * octet string. decode it.
402 */
403 #ifdef __APPLE__
404 FillOctetStringStk (b, elmtLen1, totalElmtsLen1, env,
405 strStkG);
406 #else
407 FillOctetStringStk (b, elmtLen1, totalElmtsLen1, env);
408 #endif
409 }
410 else /* wrong tag */
411 {
412 Asn1Error << "BDecConsOctetString: ERROR - decoded non-OCTET STRING tag inside a constructed OCTET STRING" << endl;
413 #if SNACC_EXCEPTION_ENABLE
414 SnaccExcep::throwMe(-16);
415 #else
416 longjmp (env, -16);
417 #endif
418 }
419 } /* end of for */
420
421 bytesDecoded += totalElmtsLen1;
422
423 } /* AsnOcts::FillOctetStringStk */
424
425
426
427 /*
428 * decodes a seq of universally tagged octets until either EOC is
429 * encountered or the given len decoded. Return them in a
430 * single concatenated octet string
431 */
432 void AsnOcts::BDecConsOcts (BUF_TYPE b, AsnLen elmtLen, AsnLen &bytesDecoded, ENV_TYPE env)
433 {
434 #ifdef __APPLE__
435 StrStk strStkG(128, 64);
436 #endif
437 strStkG.Reset();
438
439 /*
440 * decode each piece of the octet string, puting
441 * an entry in the octet/bit string stack for each
442 */
443 #ifdef __APPLE__
444 FillOctetStringStk (b, elmtLen, bytesDecoded, env, strStkG);
445 #else
446 FillOctetStringStk (b, elmtLen, bytesDecoded, env);
447 #endif
448
449 /* alloc single str long enough for combined octetstring */
450 octetLen = strStkG.totalByteLen;
451
452 #ifndef _IBM_ENC_
453 octs = Asn1Alloc (octetLen);
454 #else
455 octs = (char *) mem_mgr_ptr->Get (octetLen); /* Guido Grassel, 11.8.93 */
456 #endif /* _IBM_ENC_ */
457
458 strStkG.CopyOut (octs);
459
460 } /* BDecConsOcts */
461
462 int AsnOcts::OctsEquiv (const AsnOcts &o) const
463 {
464 return o.octetLen == octetLen && !memcmpeq (o.octs, octs, octetLen);
465 }
466
467 #if META
468
469 const AsnOctsTypeDesc AsnOcts::_desc (NULL, NULL, false, AsnTypeDesc::OCTET_STRING, NULL);
470
471 const AsnTypeDesc *AsnOcts::_getdesc() const
472 {
473 return &_desc;
474 }
475
476 #if TCL
477
478 int AsnOcts::TclGetVal (Tcl_Interp *interp) const
479 {
480 return debinify (interp, octs, octetLen);
481 }
482
483 int AsnOcts::TclSetVal (Tcl_Interp *interp, const char *valstr)
484 {
485 char* buf;
486 size_t len;
487 int result = TCL_OK;
488
489 #ifndef _IBM_ENC_
490 buf = new char[strlen (valstr)];
491 #else
492 buf = (char *) mem_mgr_ptr->Get (strlen (valstr)+1);
493 #endif /* _IBM_ENC_ */
494
495 if (!buf)
496 {
497 Tcl_AppendResult (interp, "Malloc failed for type ", _getdesc()->getmodule()->name, ".", _getdesc()->getname(), NULL);
498 return TCL_ERROR;
499 }
500 if (binify (interp, valstr, buf, &len) != TCL_OK)
501 {
502 Tcl_AppendResult (interp, " for type ", _getdesc()->getmodule()->name, ".", _getdesc()->getname(), NULL);
503 result = TCL_ERROR;
504 }
505 else
506 ReSet (buf, len);
507
508 #ifndef _IBM_ENC_
509 delete buf;
510 #else
511 mem_mgr_ptr->Put ((void*) buf);
512 #endif /* _IBM_ENC_ */
513
514 return result;
515 }
516
517 #endif /* TCL */
518 #endif /* META */