1 #include <Security/Security.h>
2 #include "printCertName.h"
3 #include <clAppUtils/clutils.h>
4 #include <utilLib/common.h>
6 static CSSM_CL_HANDLE gClHand
= 0;
8 static CSSM_CL_HANDLE
getClHand()
13 gClHand
= clStartup();
17 static void printString(
21 char *cp
= (char *)str
->Data
;
22 for(i
=0; i
<str
->Length
; i
++) {
28 static void printData(
31 for(unsigned dex
=0; dex
<cd
->Length
; dex
++) {
32 printf("%02X", cd
->Data
[dex
]);
41 * Print an CSSM_X509_TYPE_VALUE_PAIR
44 const CSSM_X509_TYPE_VALUE_PAIR_PTR atv
)
46 const CSSM_OID
*oid
= &atv
->type
;
47 const char *fieldName
= "Other";
48 if(appCompareCssmData(oid
, &CSSMOID_CountryName
)) {
49 fieldName
= "Country ";
51 else if(appCompareCssmData(oid
, &CSSMOID_OrganizationName
)) {
54 else if(appCompareCssmData(oid
, &CSSMOID_LocalityName
)) {
55 fieldName
= "Locality ";
57 else if(appCompareCssmData(oid
, &CSSMOID_OrganizationalUnitName
)) {
58 fieldName
= "OrgUnit ";
60 else if(appCompareCssmData(oid
, &CSSMOID_CommonName
)) {
61 fieldName
= "Common Name ";
63 else if(appCompareCssmData(oid
, &CSSMOID_Surname
)) {
64 fieldName
= "Surname ";
66 else if(appCompareCssmData(oid
, &CSSMOID_Title
)) {
69 else if(appCompareCssmData(oid
, &CSSMOID_Surname
)) {
70 fieldName
= "Surname ";
72 else if(appCompareCssmData(oid
, &CSSMOID_StateProvinceName
)) {
75 else if(appCompareCssmData(oid
, &CSSMOID_CollectiveStateProvinceName
)) {
76 fieldName
= "Coll. State ";
78 else if(appCompareCssmData(oid
, &CSSMOID_EmailAddress
)) {
79 /* deprecated, used by Thawte */
80 fieldName
= "Email addrs ";
83 fieldName
= "Other name ";
85 printf(" %s : ", fieldName
);
86 switch(atv
->valueType
) {
87 case BER_TAG_PRINTABLE_STRING
:
88 case BER_TAG_IA5_STRING
:
89 case BER_TAG_T61_STRING
: // mostly printable....
90 case BER_TAG_PKIX_UTF8_STRING
: // ditto
91 printString(&atv
->value
);
94 printData(&atv
->value
);
100 * Print contents of a CSSM_X509_NAME.
102 static void printName(
104 const CSSM_X509_NAME
*name
)
106 printf(" %s:\n", title
);
107 unsigned numRdns
= name
->numberOfRDNs
;
108 for(unsigned rdnDex
=0; rdnDex
<numRdns
; rdnDex
++) {
109 const CSSM_X509_RDN
*rdn
= &name
->RelativeDistinguishedName
[rdnDex
];
110 unsigned numAtvs
= rdn
->numberOfPairs
;
111 for(unsigned atvDex
=0; atvDex
<numAtvs
; atvDex
++) {
112 printAtv(&rdn
->AttributeTypeAndValue
[atvDex
]);
117 static void printOneCertName(
118 CSSM_CL_HANDLE clHand
,
119 CSSM_HANDLE cacheHand
,
123 CSSM_HANDLE resultHand
= 0;
124 CSSM_DATA_PTR field
= NULL
;
128 crtn
= CSSM_CL_CertGetFirstCachedFieldValue(clHand
, cacheHand
,
129 oid
, &resultHand
, &numFields
, &field
);
131 printf("***Error parsing cert\n");
132 cssmPerror("CSSM_CL_CertGetFirstCachedFieldValue", crtn
);
135 printName(title
, (CSSM_X509_NAME_PTR
)field
->Data
);
136 CSSM_CL_FreeFieldValue(clHand
, oid
, field
);
137 CSSM_CL_CertAbortQuery(clHand
, resultHand
);
141 * Print subject and/or issuer of a cert.
144 const unsigned char *cert
,
148 CSSM_CL_HANDLE clHand
= getClHand();
149 CSSM_HANDLE cacheHand
;
150 CSSM_DATA certData
= {certLen
, (uint8
*)cert
};
152 bool printSubj
= false;
153 bool printIssuer
= false;
167 printf("***BRRZAP! Illegal whichName argument\n");
171 crtn
= CSSM_CL_CertCache(clHand
, &certData
, &cacheHand
);
173 printf("***Error parsing cert\n");
174 cssmPerror("CSSM_CL_CertCache", crtn
);
179 printOneCertName(clHand
, cacheHand
, "Subject", &CSSMOID_X509V1SubjectNameCStruct
);
182 printOneCertName(clHand
, cacheHand
, "Issuer", &CSSMOID_X509V1IssuerNameCStruct
);
184 CSSM_CL_CertAbortCache(clHand
, cacheHand
);