1 /* Copyright (c) 1998,2005-2006 Apple Computer, Inc.
3 * makeCertPolicy.cpp - create a self signed cert with a Cert Policies extension
6 #include <utilLib/common.h>
7 #include <utilLib/cspwrap.h>
8 #include <security_cdsa_utils/cuFileIo.h>
9 #include <clAppUtils/CertBuilderApp.h>
10 #include <clAppUtils/clutils.h>
14 #include <Security/cssm.h>
15 #include <Security/x509defs.h>
16 #include <Security/oidsattr.h>
17 #include <Security/oidscert.h>
18 #include <Security/oidsalg.h>
19 #include <Security/certextensions.h>
20 #include <Security/cssmapple.h>
23 #define ROOT_KEY_LABEL "rootKey"
24 /* default key and signature algorithm */
25 #define SIG_ALG_DEFAULT CSSM_ALGID_SHA1WithRSA
26 #define KEY_ALG_DEFAULT CSSM_ALGID_RSA
28 #define CPS_URI "http://www.foo.com"
30 static void usage(char **argv
)
32 printf("Usage: %s outFileName\n", argv
[0]);
37 * RDN components for root, subject
39 CB_NameOid rootRdn
[] =
41 { "Apple Computer DEBUG", &CSSMOID_OrganizationName
},
42 { "Cert Policy Demo", &CSSMOID_CommonName
}
44 #define NUM_ROOT_NAMES (sizeof(rootRdn) / sizeof(CB_NameOid))
46 int main(int argc
, char **argv
)
48 CSSM_CL_HANDLE clHand
; // CL handle
49 CSSM_X509_NAME
*rootName
;
50 CSSM_X509_TIME
*notBefore
; // UTC-style "not before" time
51 CSSM_X509_TIME
*notAfter
; // UTC-style "not after" time
52 CSSM_DATA_PTR rawCert
; // from CSSM_CL_CertCreateTemplate
53 CSSM_DATA signedRootCert
; // from CSSM_CL_CertSign
54 CSSM_CSP_HANDLE cspHand
; // CSP handle
55 CSSM_KEY rootPubKey
; // root's RSA public key blob
56 CSSM_KEY rootPrivKey
; // root's RSA private key - ref format
58 CSSM_CC_HANDLE signContext
; // for signing/verifying the cert
60 /* user-spec'd variables */
61 const char *outFileName
;
66 outFileName
= argv
[1];
71 CSSM_X509_EXTENSION ext
;
73 CE_PolicyInformation cpi
;
74 CE_PolicyQualifierInfo cpqi
;
76 /* connect to CL and CSP */
81 cspHand
= cspStartup();
86 /* subsequent errors to abort: to detach */
88 /* cook up an RSA key pair */
89 crtn
= cspGenKeyPair(cspHand
,
92 strlen(ROOT_KEY_LABEL
),
95 CSSM_FALSE
, // pubIsRef - should work both ways, but not yet
97 CSSM_KEYBLOB_RAW_FORMAT_NONE
,
99 CSSM_FALSE
, // privIsRef
101 CSSM_KEYBLOB_RAW_FORMAT_NONE
,
104 printf("Error creatingt key pair, aborting.\n");
109 * Cook up various cert fields.
110 * First, the RDNs for subject and issuer.
112 rootName
= CB_BuildX509Name(rootRdn
, NUM_ROOT_NAMES
);
113 if(rootName
== NULL
) {
114 printf("CB_BuildX509Name failure");
118 /* not before/after in generalized time format */
119 notBefore
= CB_BuildX509Time(0);
120 notAfter
= CB_BuildX509Time(10000);
122 /* Here's what we do */
123 ext
.extnId
= CSSMOID_CertificatePolicies
;
124 ext
.critical
= CSSM_FALSE
;
125 ext
.format
= CSSM_X509_DATAFORMAT_PARSED
;
127 cpqi
.policyQualifierId
= CSSMOID_QT_CPS
;
128 cpqi
.qualifier
.Data
= (uint8
*)CPS_URI
;
129 cpqi
.qualifier
.Length
= strlen(CPS_URI
);
131 cpi
.certPolicyId
= CSSMOID_APPLE_CERT_POLICY
; /* what I'm testing today */
132 cpi
.numPolicyQualifiers
= 1;
133 cpi
.policyQualifiers
= &cpqi
;
138 ext
.value
.parsedValue
= &cp
;
139 ext
.BERvalue
.Data
= NULL
;
140 ext
.BERvalue
.Length
= 0;
142 /* cook up root cert */
143 printf("Creating root cert...\n");
144 rawCert
= CB_MakeCertTemplate(clHand
,
145 0x12345678, // serial number
152 NULL
, // subjUniqueId
153 NULL
, // issuerUniqueId
157 if(rawCert
== NULL
) {
158 printf("CB_MakeCertTemplate failure");
162 crtn
= CSSM_CSP_CreateSignatureContext(cspHand
,
168 printError("CSSM_CSP_CreateSignatureContext", crtn
);
171 signedRootCert
.Data
= NULL
;
172 signedRootCert
.Length
= 0;
173 crtn
= CSSM_CL_CertSign(clHand
,
175 rawCert
, // CertToBeSigned
180 printError("CSSM_CL_CertSign", crtn
);
183 crtn
= CSSM_DeleteContext(signContext
);
185 printError("CSSM_DeleteContext", crtn
);
188 appFreeCssmData(rawCert
, CSSM_TRUE
);
189 writeFile(outFileName
, signedRootCert
.Data
, signedRootCert
.Length
);
190 printf("...wrote %lu bytes to %s\n", signedRootCert
.Length
, outFileName
);
192 /* Free the stuff we allocd to get here */
193 CB_FreeX509Name(rootName
);
194 CB_FreeX509Time(notBefore
);
195 CB_FreeX509Time(notAfter
);
196 appFreeCssmData(&signedRootCert
, CSSM_FALSE
);
198 cspFreeKey(cspHand
, &rootPubKey
);
199 cspFreeKey(cspHand
, &rootPrivKey
);