]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/asn-octs.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 * .../c-lib/src/asn-octs.c - BER encode, decode, print and free routines for the ASN.1 OCTET STRING type.
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/src/asn-octs.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
36 * $Log: asn-octs.c,v $
37 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
38 * Move from private repository to open source repository
40 * Revision 1.2 2001/05/05 00:59:25 rmurphy
41 * Adding darwin license headers
43 * Revision 1.1.1.1 1999/03/16 18:06:31 aram
44 * Originals from SMIME Free Library.
46 * Revision 1.3 1995/07/27 09:00:32 rj
47 * use memcmpeq that is defined in .../snacc.h to use either memcmp or bcmp.
49 * changed `_' to `-' in file names.
51 * Revision 1.2 1994/09/01 00:06:15 rj
52 * reduce the risk of unwanted surprises with macro expansion by properly separating the C tokens.
54 * Revision 1.1 1994/08/28 09:45:58 rj
55 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
61 #include "asn-config.h"
65 #include "asn-bits.h" /* for TO_HEX macro */
70 * encodes universal TAG LENGTH and Contents of and ASN.1 OCTET STRING
73 BEncAsnOcts
PARAMS ((b
, data
),
79 len
= BEncAsnOctsContent (b
, data
);
80 len
+= BEncDefLen (b
, len
);
81 len
+= BEncTag1 (b
, UNIV
, PRIM
, OCTETSTRING_TAG_CODE
);
87 * decodes universal TAG LENGTH and Contents of and ASN.1 OCTET STRING
90 BDecAsnOcts
PARAMS ((b
, result
, bytesDecoded
, env
),
93 AsnLen
*bytesDecoded _AND_
99 if (((tag
= BDecTag (b
, bytesDecoded
, env
)) != MAKE_TAG_ID (UNIV
, PRIM
, OCTETSTRING_TAG_CODE
)) && (tag
!= MAKE_TAG_ID (UNIV
, CONS
, OCTETSTRING_TAG_CODE
)))
101 Asn1Error ("BDecAsnOcts: ERROR - wrong tag on OCTET STRING.\n");
105 elmtLen
= BDecLen (b
, bytesDecoded
, env
);
106 BDecAsnOctsContent (b
, tag
, elmtLen
, result
, bytesDecoded
, env
);
111 * BER encodes just the content of an OCTET STRING.
114 BEncAsnOctsContent
PARAMS ((b
, o
),
118 BufPutSegRvs (b
, o
->octs
, o
->octetLen
);
120 } /* BEncAsnOctsContent */
125 * Used for decoding constructed OCTET STRING values into
126 * a contiguous local rep.
127 * fills string stack with references to the pieces of a
128 * construced octet string
131 FillOctetStringStk
PARAMS ((b
, elmtLen0
, bytesDecoded
, env
),
133 AsnLen elmtLen0 _AND_
134 AsnLen
*bytesDecoded _AND_
137 unsigned long int refdLen
;
138 unsigned long int totalRefdLen
;
140 unsigned long int totalElmtsLen1
= 0;
141 unsigned long int tagId1
;
142 unsigned long int elmtLen1
;
144 for (; (totalElmtsLen1
< elmtLen0
) || (elmtLen0
== INDEFINITE_LEN
); )
146 tagId1
= BDecTag (b
, &totalElmtsLen1
, env
);
148 if ((tagId1
== EOC_TAG_ID
) && (elmtLen0
== INDEFINITE_LEN
))
150 BDEC_2ND_EOC_OCTET (b
, &totalElmtsLen1
, env
);
154 elmtLen1
= BDecLen (b
, &totalElmtsLen1
, env
);
155 if (tagId1
== MAKE_TAG_ID (UNIV
, PRIM
, OCTETSTRING_TAG_CODE
))
158 * primitive part of string, put references to piece (s) in
165 strPtr
= BufGetSeg (b
, &refdLen
);
167 PUSH_STR (strPtr
, refdLen
, env
);
168 totalRefdLen
+= refdLen
;
169 if (totalRefdLen
== elmtLen1
)
170 break; /* exit this while loop */
172 if (refdLen
== 0) /* end of data */
174 Asn1Error ("BDecConsOctetString: ERROR - attempt to decode past end of data\n");
177 refdLen
= elmtLen1
- totalRefdLen
;
179 totalElmtsLen1
+= elmtLen1
;
183 else if (tagId1
== MAKE_TAG_ID (UNIV
, CONS
, OCTETSTRING_TAG_CODE
))
186 * constructed octets string embedding in this constructed
187 * octet string. decode it.
189 FillOctetStringStk (b
, elmtLen1
, &totalElmtsLen1
, env
);
193 Asn1Error ("BDecConsOctetString: ERROR - decoded non-OCTET STRING tag inside a constructed OCTET STRING\n");
198 (*bytesDecoded
) += totalElmtsLen1
;
200 } /* FillOctetStringStk */
204 * Decodes a seq of universally tagged octets strings until either EOC is
205 * encountered or the given len is decoded. Merges them into a single
206 * string. puts a NULL terminator on the string but does not include
207 * this in the length.
210 BDecConsAsnOcts
PARAMS ((b
, len
, result
, bytesDecoded
, env
),
213 AsnOcts
*result _AND_
214 AsnLen
*bytesDecoded _AND_
218 unsigned long int curr
;
223 * decode each piece of the octet string, puting
224 * an entry in the octet string stack for each
226 FillOctetStringStk (b
, len
, bytesDecoded
, env
);
228 result
->octetLen
= strStkG
.totalByteLen
;
230 /* alloc str for all octs pieces with extra byte for null terminator */
231 bufCurr
= result
->octs
= Asn1Alloc (strStkG
.totalByteLen
+1);
233 /* copy octet str pieces into single blk */
234 for (curr
= 0; curr
< strStkG
.nextFreeElmt
; curr
++)
236 memcpy (bufCurr
, strStkG
.stk
[curr
].str
, strStkG
.stk
[curr
].len
);
237 bufCurr
+= strStkG
.stk
[curr
].len
;
240 /* add null terminator - this is not included in the str's len */
243 } /* BDecConsAsnOcts */
246 * Decodes the content of a BER OCTET STRING value
249 BDecAsnOctsContent
PARAMS ((b
, tagId
, len
, result
, bytesDecoded
, env
),
253 AsnOcts
*result _AND_
254 AsnLen
*bytesDecoded _AND_
258 * tagId is encoded tag shifted into long int.
259 * if CONS bit is set then constructed octet string
261 if (TAG_IS_CONS (tagId
))
262 BDecConsAsnOcts (b
, len
, result
, bytesDecoded
, env
);
264 else /* primitive octet string */
266 result
->octetLen
= len
;
267 result
->octs
= Asn1Alloc (len
+1);
268 BufCopy (result
->octs
, b
, len
);
270 if (BufReadError (b
))
272 Asn1Error ("BDecOctetString: ERROR - decoded past end of data\n");
276 /* add null terminator - this is not included in the str's len */
277 result
->octs
[len
] = '\0';
278 (*bytesDecoded
) += len
;
280 } /* BDecAsnOctsContent */
284 * Frees the string part of the given OCTET STRING
287 FreeAsnOcts
PARAMS ((v
),
294 * Prints the given OCTET STRING value to the given FILE * in ASN.1
295 * Value Notation. Since the value notation uses the hard to read
296 * hex format, the ASCII version is included in an ASN.1 comment.
299 PrintAsnOcts
PARAMS ((f
,v
, indent
),
302 unsigned short indent
)
306 /* print hstring value */
309 for (i
= 0; i
< v
->octetLen
; i
++)
310 fprintf (f
,"%c%c", TO_HEX (v
->octs
[i
] >> 4), TO_HEX (v
->octs
[i
]));
314 /* show printable chars in comment */
315 fprintf (f
," -- \"");
317 for (i
= 0; i
< v
->octetLen
; i
++)
319 if (isprint (v
->octs
[i
]))
320 fprintf (f
,"%c", v
->octs
[i
]);
329 * Returns TRUE if the given OCTET STRING values are identical.
330 * Returns FALSE otherwise.
333 AsnOctsEquiv
PARAMS ((o1
, o2
),
337 return o1
->octetLen
== o2
->octetLen
&& !memcmpeq (o1
->octs
, o2
->octs
, o1
->octetLen
);