1 /* Copyright (c) 2005-2007 Apple Inc. All Rights Reserved. */
4 * printFeilds.h - print various DER objects
6 * Created Nov. 9 2005 by dmitch
9 #include <libDERUtils/printFields.h>
10 #include <libDER/DER_Decode.h>
11 #include <libDER/asn1Types.h>
12 #include <libDER/DER_Keys.h>
13 #include <libDERUtils/libDERUtils.h>
18 static int indentLevel
= 0;
23 for (i
= 0; i
<indentLevel
; i
++) {
38 #define TO_PRINT_MAX 12
44 unsigned toPrint
= item
->length
;
46 printf("<%u> ", item
->length
);
47 if(toPrint
> TO_PRINT_MAX
) {
48 toPrint
= TO_PRINT_MAX
;
50 for(dex
=0; dex
<toPrint
; dex
++) {
51 printf("%02x ", item
->data
[dex
]);
53 if(item
->length
> TO_PRINT_MAX
) {
63 unsigned toPrint
= item
->length
;
64 DERItem bitStringBytes
;
68 drtn
= DERParseBitString(item
, &bitStringBytes
, &numUnused
);
70 DERPerror("DERParseBitString", drtn
);
74 printf("<%u, %u> ", bitStringBytes
.length
, numUnused
);
75 toPrint
= bitStringBytes
.length
;
76 if(toPrint
> TO_PRINT_MAX
) {
77 toPrint
= TO_PRINT_MAX
;
79 for(dex
=0; dex
<toPrint
; dex
++) {
80 printf("%02x ", bitStringBytes
.data
[dex
]);
82 if(item
->length
> TO_PRINT_MAX
) {
92 char *cp
= (char *)item
->data
;
94 for(dex
=0; dex
<item
->length
; dex
++) {
101 #define COLON_COLUMN 20
104 * Print line header, with current indent, followed by specified label, followed
105 * by a ':' in column COLON_COLUMN, followed by one space.
114 numPrinted
= indentLevel
+ strlen(label
);
115 if(numPrinted
< COLON_COLUMN
) {
116 unsigned numSpaces
= COLON_COLUMN
- numPrinted
;
118 for(dex
=0; dex
<numSpaces
; dex
++) {
129 DERTag tag
, // maybe from decoding, maybe the real tag underlying
130 // an implicitly tagged item
131 DERItem
*item
) // content
133 DERTag tagClass
= tag
& ASN1_CLASS_MASK
;
134 DERTag tagNum
= tag
& ASN1_TAGNUM_MASK
;
136 char *asnType
= NULL
;
140 if((itemType
== IT_Branch
) && !verbose
) {
146 break; // proceed with normal tags */
147 case ASN1_APPLICATION
:
148 printf("APPLICATION (tag %u) ", tagNum
);
151 case ASN1_CONTEXT_SPECIFIC
:
152 printf("CONTEXT SPECIFIC (tag %u) ", tagNum
);
156 printf("PRIVATE (tag %u) ", tagNum
);
167 case ASN1_BIT_STRING
:
168 /* special case here... */
169 printf("BIT STRING ");
170 printBitString(item
);
172 case ASN1_OCTET_STRING
:
173 asnType
= "OCTET STRING";
181 case ASN1_OBJECT_DESCRIPTOR
:
182 asnType
= "OBJECT_DESCRIPTOR";
187 case ASN1_ENUMERATED
:
190 case ASN1_EMBEDDED_PDV
:
191 asnType
= "EMBEDDED_PDV";
193 case ASN1_UTF8_STRING
:
194 asnType
= "UTF8 STRING";
195 /* FIXME print these too */
203 case ASN1_NUMERIC_STRING
:
204 asnType
= "NUMERIC_STRING";
206 case ASN1_PRINTABLE_STRING
:
207 asnType
= "PRINTABLE_STRING";
210 case ASN1_T61_STRING
:
211 asnType
= "T61_STRING";
214 case ASN1_VIDEOTEX_STRING
:
215 asnType
= "VIDEOTEX_STRING";
218 case ASN1_IA5_STRING
:
219 asnType
= "IA5_STRING";
223 asnType
= "UTC_TIME";
226 case ASN1_GENERALIZED_TIME
:
227 asnType
= "GENERALIZED_TIME";
230 case ASN1_GRAPHIC_STRING
:
231 asnType
= "GRAPHIC_STRING";
233 case ASN1_VISIBLE_STRING
:
234 asnType
= "VISIBLE_STRING";
236 case ASN1_GENERAL_STRING
:
237 asnType
= "GENERAL_STRING";
239 case ASN1_UNIVERSAL_STRING
:
240 asnType
= "UNIVERSAL_STRING";
242 case ASN1_BMP_STRING
:
243 asnType
= "BMP_STRING";
246 asnType
= "[unknown]";
249 printf("%s ", asnType
);
259 const DERItem
*content
,
263 DERAlgorithmId algId
;
265 drtn
= DERParseSequenceContent(content
,
266 DERNumAlgorithmIdItemSpecs
, DERAlgorithmIdItemSpecs
,
267 &algId
, sizeof(algId
));
269 DERPerror("DERParseSequenceContent(algId)", drtn
);
272 printItem("alg", IT_Leaf
, verbose
, ASN1_OBJECT_ID
, &algId
.oid
);
273 if(algId
.params
.data
) {
274 printItem("params", IT_Leaf
, verbose
, algId
.params
.data
[0], &algId
.params
);
278 void printSubjPubKeyInfo(
279 const DERItem
*content
,
283 DERSubjPubKeyInfo pubKeyInfo
;
284 DERRSAPubKeyPKCS1 pkcs1Key
;
285 DERItem bitStringContents
;
288 drtn
= DERParseSequenceContent(content
,
289 DERNumSubjPubKeyInfoItemSpecs
, DERSubjPubKeyInfoItemSpecs
,
290 &pubKeyInfo
, sizeof(pubKeyInfo
));
292 DERPerror("DERParseSequenceContent(pubKeyInfo)", drtn
);
295 printItem("algId", IT_Branch
, verbose
, ASN1_CONSTR_SEQUENCE
, &pubKeyInfo
.algId
);
297 printAlgId(&pubKeyInfo
.algId
, verbose
);
300 printItem("pubKey", IT_Branch
, verbose
, ASN1_BIT_STRING
, &pubKeyInfo
.pubKey
);
303 * The contents of that bit string are a PKCS1 format RSA key.
305 drtn
= DERParseBitString(&pubKeyInfo
.pubKey
, &bitStringContents
, &numUnused
);
307 DERPerror("DERParseBitString(pubKeyInfo.pubKey)", drtn
);
311 drtn
= DERParseSequence(&bitStringContents
,
312 DERNumRSAPubKeyPKCS1ItemSpecs
, DERRSAPubKeyPKCS1ItemSpecs
,
313 &pkcs1Key
, sizeof(pkcs1Key
));
315 DERPerror("DERParseSequenceContent(pubKeyBits)", drtn
);
320 printItem("modulus", IT_Leaf
, verbose
, ASN1_INTEGER
, &pkcs1Key
.modulus
);
321 printItem("pubExponent", IT_Leaf
, verbose
, ASN1_INTEGER
, &pkcs1Key
.pubExponent
);
326 /* decode one item and print it */
327 void decodePrintItem(
333 DERDecodedInfo decoded
;
336 drtn
= DERDecodeItem(derItem
, &decoded
);
338 DERPerror("DERDecodeItem()", drtn
);
341 printItem(label
, IT_Leaf
, 0, decoded
.tag
, &decoded
.content
);