2 * Copyright (c) 2004,2006 Apple Computer, 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@
25 * asnUtils.cpp - ASN.1-related utilities.
27 * Created 20 May 2004 by Doug Mitchell.
30 #include <Security/nameTemplates.h>
31 #include <Security/SecAsn1Coder.h>
34 #include <Security/Security.h>
35 #include <Security/oidsattr.h>
36 #include <security_cdsa_utils/cuCdsaUtils.h>
38 static CSSM_CL_HANDLE gClHand
= 0;
40 static CSSM_CL_HANDLE
getClHand()
45 gClHand
= cuClStartup();
49 unsigned pkiNssArraySize(
62 const CSSM_DATA
*data1
,
63 const CSSM_DATA
*data2
)
65 if((data1
== NULL
) || (data1
->Data
== NULL
) ||
66 (data2
== NULL
) || (data2
->Data
== NULL
) ||
67 (data1
->Length
!= data2
->Length
)) {
70 if(data1
->Length
!= data2
->Length
) {
73 if(memcmp(data1
->Data
, data2
->Data
, data1
->Length
) == 0) {
85 char *cp
= (char *)str
->Data
;
86 for(i
=0; i
<str
->Length
; i
++) {
95 for(unsigned dex
=0; dex
<cd
->Length
; dex
++) {
96 printf("%02X", cd
->Data
[dex
]);
110 const CSSM_OID
*oid
= &atv
->type
;
111 const char *fieldName
= "Other";
112 if(compareCssmData(oid
, &CSSMOID_CountryName
)) {
113 fieldName
= "Country ";
115 else if(compareCssmData(oid
, &CSSMOID_OrganizationName
)) {
118 else if(compareCssmData(oid
, &CSSMOID_LocalityName
)) {
119 fieldName
= "Locality ";
121 else if(compareCssmData(oid
, &CSSMOID_OrganizationalUnitName
)) {
122 fieldName
= "OrgUnit ";
124 else if(compareCssmData(oid
, &CSSMOID_CommonName
)) {
125 fieldName
= "Common Name ";
127 else if(compareCssmData(oid
, &CSSMOID_Surname
)) {
128 fieldName
= "Surname ";
130 else if(compareCssmData(oid
, &CSSMOID_Title
)) {
131 fieldName
= "Title ";
133 else if(compareCssmData(oid
, &CSSMOID_Surname
)) {
134 fieldName
= "Surname ";
136 else if(compareCssmData(oid
, &CSSMOID_StateProvinceName
)) {
137 fieldName
= "State ";
139 else if(compareCssmData(oid
, &CSSMOID_CollectiveStateProvinceName
)) {
140 fieldName
= "Coll. State ";
142 else if(compareCssmData(oid
, &CSSMOID_EmailAddress
)) {
143 /* deprecated, used by Thawte */
144 fieldName
= "Email addrs ";
147 fieldName
= "Other name ";
149 printf(" %s : ", fieldName
);
150 switch(atv
->value
.tag
) {
151 case SEC_ASN1_PRINTABLE_STRING
:
152 case SEC_ASN1_IA5_STRING
:
153 case SEC_ASN1_T61_STRING
: // mostly printable....
154 case SEC_ASN1_UTF8_STRING
: // ditto
155 printString(&atv
->value
.item
);
158 printData(&atv
->value
.item
);
164 * Print contents of an encoded Name (e.g. from an IssuerAndSerialNumber).
171 SecAsn1CoderRef coder
;
172 if(SecAsn1CoderCreate(&coder
)) {
173 printf("*****Screwup in SecAsn1CoderCreate\n");
176 CSSM_DATA der
= {nameLen
, name
};
179 if(SecAsn1DecodeData(coder
, &der
, kSecAsn1NameTemplate
, &nssName
)) {
180 printf("***Error decoding %s\n", title
);
183 printf(" %s:\n", title
);
184 unsigned numRdns
= pkiNssArraySize((const void **)nssName
.rdns
);
185 for(unsigned rdnDex
=0; rdnDex
<numRdns
; rdnDex
++) {
186 NSS_RDN
*rdn
= nssName
.rdns
[rdnDex
];
187 unsigned numAtvs
= pkiNssArraySize((const void **)rdn
->atvs
);
188 for(unsigned atvDex
=0; atvDex
<numAtvs
; atvDex
++) {
189 printAtv(rdn
->atvs
[atvDex
]);
194 static void printOneCertName(
195 CSSM_CL_HANDLE clHand
,
196 CSSM_HANDLE cacheHand
,
200 CSSM_HANDLE resultHand
= 0;
201 CSSM_DATA_PTR field
= NULL
;
205 crtn
= CSSM_CL_CertGetFirstCachedFieldValue(clHand
, cacheHand
,
206 oid
, &resultHand
, &numFields
, &field
);
208 printf("***Error parsing cert\n");
209 cssmPerror("CSSM_CL_CertGetFirstCachedFieldValue", crtn
);
212 printName(title
, field
->Data
, field
->Length
);
213 CSSM_CL_FreeFieldValue(clHand
, oid
, field
);
217 * Print subject and/or issuer of a cert.
220 const unsigned char *cert
,
224 CSSM_CL_HANDLE clHand
= getClHand();
225 CSSM_HANDLE cacheHand
;
226 CSSM_DATA certData
= {certLen
, (uint8
*)cert
};
228 bool printSubj
= false;
229 bool printIssuer
= false;
243 printf("***BRRZAP! Illegal whichName argument\n");
247 crtn
= CSSM_CL_CertCache(clHand
, &certData
, &cacheHand
);
249 printf("***Error parsing cert\n");
250 cssmPerror("CSSM_CL_CertCache", crtn
);
255 printOneCertName(clHand
, cacheHand
, "Subject", &CSSMOID_X509V1SubjectNameStd
);
258 printOneCertName(clHand
, cacheHand
, "Issuer", &CSSMOID_X509V1IssuerNameStd
);
260 CSSM_CL_CertAbortCache(clHand
, cacheHand
);