2 * Copyright (c) 2005-2007,2011-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 * printFeilds.h - print various DER objects
30 #include <libDERUtils/printFields.h>
31 #include <libDER/DER_Decode.h>
32 #include <libDER/asn1Types.h>
33 #include <libDER/DER_Keys.h>
34 #include <libDERUtils/libDERUtils.h>
39 static int indentLevel
= 0;
44 for (i
= 0; i
<indentLevel
; i
++) {
59 #define TO_PRINT_MAX 12
65 unsigned long toPrint
= item
->length
;
67 printf("<%lu> ", item
->length
);
68 if(toPrint
> TO_PRINT_MAX
) {
69 toPrint
= TO_PRINT_MAX
;
71 for(dex
=0; dex
<toPrint
; dex
++) {
72 printf("%02x ", item
->data
[dex
]);
74 if(item
->length
> TO_PRINT_MAX
) {
84 DERSize toPrint
= item
->length
;
85 DERItem bitStringBytes
;
89 drtn
= DERParseBitString(item
, &bitStringBytes
, &numUnused
);
91 DERPerror("DERParseBitString", drtn
);
95 printf("<%lu, %lu> ", (unsigned long)bitStringBytes
.length
, (unsigned long)numUnused
);
96 toPrint
= bitStringBytes
.length
;
97 if(toPrint
> TO_PRINT_MAX
) {
98 toPrint
= TO_PRINT_MAX
;
100 for(dex
=0; dex
<toPrint
; dex
++) {
101 printf("%02x ", bitStringBytes
.data
[dex
]);
103 if(item
->length
> TO_PRINT_MAX
) {
113 char *cp
= (char *)item
->data
;
115 for(dex
=0; dex
<item
->length
; dex
++) {
122 #define COLON_COLUMN 20
125 * Print line header, with current indent, followed by specified label, followed
126 * by a ':' in column COLON_COLUMN, followed by one space.
135 numPrinted
= indentLevel
+ strlen(label
);
136 if(numPrinted
< COLON_COLUMN
) {
137 size_t numSpaces
= COLON_COLUMN
- numPrinted
;
139 for(dex
=0; dex
<numSpaces
; dex
++) {
150 DERTag tag
, // maybe from decoding, maybe the real tag underlying
151 // an implicitly tagged item
152 DERItem
*item
) // content
154 DERTag tagClass
= tag
& ASN1_CLASS_MASK
;
155 DERTag tagNum
= tag
& ASN1_TAGNUM_MASK
;
157 char *asnType
= NULL
;
161 if((itemType
== IT_Branch
) && !verbose
) {
167 break; // proceed with normal tags */
168 case ASN1_APPLICATION
:
169 printf("APPLICATION (tag %u) ", tagNum
);
172 case ASN1_CONTEXT_SPECIFIC
:
173 printf("CONTEXT SPECIFIC (tag %u) ", tagNum
);
177 printf("PRIVATE (tag %u) ", tagNum
);
188 case ASN1_BIT_STRING
:
189 /* special case here... */
190 printf("BIT STRING ");
191 printBitString(item
);
193 case ASN1_OCTET_STRING
:
194 asnType
= "OCTET STRING";
202 case ASN1_OBJECT_DESCRIPTOR
:
203 asnType
= "OBJECT_DESCRIPTOR";
208 case ASN1_ENUMERATED
:
211 case ASN1_EMBEDDED_PDV
:
212 asnType
= "EMBEDDED_PDV";
214 case ASN1_UTF8_STRING
:
215 asnType
= "UTF8 STRING";
216 /* FIXME print these too */
224 case ASN1_NUMERIC_STRING
:
225 asnType
= "NUMERIC_STRING";
227 case ASN1_PRINTABLE_STRING
:
228 asnType
= "PRINTABLE_STRING";
231 case ASN1_T61_STRING
:
232 asnType
= "T61_STRING";
235 case ASN1_VIDEOTEX_STRING
:
236 asnType
= "VIDEOTEX_STRING";
239 case ASN1_IA5_STRING
:
240 asnType
= "IA5_STRING";
244 asnType
= "UTC_TIME";
247 case ASN1_GENERALIZED_TIME
:
248 asnType
= "GENERALIZED_TIME";
251 case ASN1_GRAPHIC_STRING
:
252 asnType
= "GRAPHIC_STRING";
254 case ASN1_VISIBLE_STRING
:
255 asnType
= "VISIBLE_STRING";
257 case ASN1_GENERAL_STRING
:
258 asnType
= "GENERAL_STRING";
260 case ASN1_UNIVERSAL_STRING
:
261 asnType
= "UNIVERSAL_STRING";
263 case ASN1_BMP_STRING
:
264 asnType
= "BMP_STRING";
267 asnType
= "[unknown]";
270 printf("%s ", asnType
);
280 const DERItem
*content
,
284 DERAlgorithmId algId
;
286 drtn
= DERParseSequenceContent(content
,
287 DERNumAlgorithmIdItemSpecs
, DERAlgorithmIdItemSpecs
,
288 &algId
, sizeof(algId
));
290 DERPerror("DERParseSequenceContent(algId)", drtn
);
293 printItem("alg", IT_Leaf
, verbose
, ASN1_OBJECT_ID
, &algId
.oid
);
294 if(algId
.params
.data
) {
295 printItem("params", IT_Leaf
, verbose
, algId
.params
.data
[0], &algId
.params
);
299 void printSubjPubKeyInfo(
300 const DERItem
*content
,
304 DERSubjPubKeyInfo pubKeyInfo
;
305 DERRSAPubKeyPKCS1 pkcs1Key
;
306 DERItem bitStringContents
;
309 drtn
= DERParseSequence(content
,
310 DERNumSubjPubKeyInfoItemSpecs
, DERSubjPubKeyInfoItemSpecs
,
311 &pubKeyInfo
, sizeof(pubKeyInfo
));
313 DERPerror("DERParseSequenceContent(pubKeyInfo)", drtn
);
316 printItem("algId", IT_Branch
, verbose
, ASN1_CONSTR_SEQUENCE
, &pubKeyInfo
.algId
);
318 printAlgId(&pubKeyInfo
.algId
, verbose
);
321 printItem("pubKey", IT_Branch
, verbose
, ASN1_BIT_STRING
, &pubKeyInfo
.pubKey
);
324 * The contents of that bit string are a PKCS1 format RSA key.
326 drtn
= DERParseBitString(&pubKeyInfo
.pubKey
, &bitStringContents
, &numUnused
);
328 DERPerror("DERParseBitString(pubKeyInfo.pubKey)", drtn
);
332 drtn
= DERParseSequence(&bitStringContents
,
333 DERNumRSAPubKeyPKCS1ItemSpecs
, DERRSAPubKeyPKCS1ItemSpecs
,
334 &pkcs1Key
, sizeof(pkcs1Key
));
336 DERPerror("DERParseSequenceContent(pubKeyBits)", drtn
);
341 printItem("modulus", IT_Leaf
, verbose
, ASN1_INTEGER
, &pkcs1Key
.modulus
);
342 printItem("pubExponent", IT_Leaf
, verbose
, ASN1_INTEGER
, &pkcs1Key
.pubExponent
);
347 /* decode one item and print it */
348 void decodePrintItem(
354 DERDecodedInfo decoded
;
357 drtn
= DERDecodeItem(derItem
, &decoded
);
359 DERPerror("DERDecodeItem()", drtn
);
362 printItem(label
, IT_Leaf
, 0, decoded
.tag
, &decoded
.content
);