2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * Session_Crypto.cpp: CL session functions: sign, verify, CSSM_KEY extraction.
22 * Created 9/1/2000 by Doug Mitchell.
23 * Copyright (c) 2000 by Apple Computer.
26 #include "AppleX509CLSession.h"
27 #include "DecodedCert.h"
28 #include "cldebugging.h"
29 #include "CSPAttacher.h"
30 #include "clNssUtils.h"
31 #include <SecurityNssAsn1/keyTemplates.h>
32 #include <SecurityNssAsn1/nssUtils.h>
33 #include <Security/oidscert.h>
34 #include <Security/cssmapple.h>
35 #include <Security/cssmerrno.h>
38 * Given a DER-encoded cert, obtain a fully usable CSSM_KEY representing
39 * the cert's public key.
42 AppleX509CLSession::CertGetKeyInfo(
46 DecodedCert
decodedCert(*this, Cert
);
47 Key
= decodedCert
.extractCSSMKey(*this);
51 * Given a DER-encoded cert and a fully specified crypto context, verify
52 * cert's TBS and signature.
55 AppleX509CLSession::CertVerifyWithKey(
56 CSSM_CC_HANDLE CCHandle
,
57 const CssmData
&CertToBeVerified
)
59 CssmAutoData
tbs(*this);
60 CssmAutoData
algId(*this);
61 CssmAutoData
sig(*this);
62 CL_certCrlDecodeComponents(CertToBeVerified
, tbs
, algId
, sig
);
63 verifyData(CCHandle
, tbs
, sig
);
67 * Verify a DER-encoded cert, obtaining crypto context from either
68 * caller-specified context or by inference from SignerCert.
71 AppleX509CLSession::CertVerify(
72 CSSM_CC_HANDLE CCHandle
,
73 const CssmData
&CertToBeVerified
,
74 const CssmData
*SignerCert
,
75 const CSSM_FIELD
*VerifyScope
,
78 if((VerifyScope
!= NULL
) || (ScopeSize
!= 0)) {
79 CssmError::throwMe(CSSMERR_CL_SCOPE_NOT_SUPPORTED
);
81 if((CCHandle
== CSSM_INVALID_HANDLE
) && (SignerCert
== NULL
)) {
82 /* need one or the other */
83 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
86 /* get top-level components */
87 CssmAutoData
tbs(*this); // in DER format
88 CssmAutoData
algId(*this); // in DER format
89 CssmAutoData
sig(*this); // in DER format
90 CL_certCrlDecodeComponents(CertToBeVerified
, tbs
, algId
, sig
);
92 /* these must be explicitly freed upon exit */
93 CSSM_KEY_PTR signerPubKey
= NULL
;
94 CSSM_CONTEXT_PTR context
= NULL
;
95 CSSM_CSP_HANDLE cspHand
= CSSM_INVALID_HANDLE
;
96 CSSM_CC_HANDLE ourCcHand
= CSSM_INVALID_HANDLE
;
98 /* SignerCert optional; if present, obtain its subject key */
99 if(SignerCert
!= NULL
) {
100 CertGetKeyInfo(*SignerCert
, signerPubKey
);
103 /* signerPubKey must be explicitly freed in any case */
105 if(CCHandle
!= CSSM_INVALID_HANDLE
) {
107 * We'll use this CCHandle for the sig verify, but
108 * make sure it matches possible incoming SignerCert parameters
110 if(SignerCert
!= NULL
) {
113 /* extract signer's public key as a CSSM_KEY from context */
114 crtn
= CSSM_GetContext(CCHandle
, &context
);
116 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
118 CSSM_CONTEXT_ATTRIBUTE_PTR attr
;
119 crtn
= CSSM_GetContextAttribute(context
,
123 clErrorLog("CertVerify: valid CCHandle but no key!\n");
124 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
127 assert(signerPubKey
!= NULL
);
128 CSSM_KEY_PTR contextPubKey
= attr
->Attribute
.Key
;
129 if(contextPubKey
->KeyHeader
.AlgorithmId
!=
130 signerPubKey
->KeyHeader
.AlgorithmId
) {
131 clErrorLog("CertVerify: AlgorithmId mismatch!\n");
132 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
135 /* TBD - check key size, when we have a CSP which can report it */
136 /* TBD - anything else? */
137 } /* verifying multiple contexts */
138 /* OK to use CCHandle as is for verify context */
139 } /* valid CCHandle */
142 * All we have is signer cert. We already have its public key;
143 * get signature alg from CertToBeVerified's Cert.algID, which
144 * we currently have in DER form. Decode it into temp memory.
146 assert(SignerCert
!= NULL
);
147 assert(signerPubKey
!= NULL
);
149 CSSM_X509_ALGORITHM_IDENTIFIER cssmAlgId
;
153 CssmData
&algIdData
= algId
.get();
154 memset(&cssmAlgId
, 0, sizeof(cssmAlgId
));
155 prtn
= coder
.decode(algIdData
.data(), algIdData
.length(),
156 NSS_AlgorithmIDTemplate
, &cssmAlgId
);
158 CssmError::throwMe(CSSMERR_CL_UNKNOWN_FORMAT
);
161 CSSM_ALGORITHMS vfyAlg
= CL_oidToAlg(cssmAlgId
.algorithm
);
163 /* attach to CSP, cook up a context */
164 cspHand
= getGlobalCspHand(true);
166 crtn
= CSSM_CSP_CreateSignatureContext(cspHand
,
168 NULL
, // Access Creds
171 CCHandle
= ourCcHand
;
172 } /* inferring sig verify context from SignerCert */
173 verifyData(CCHandle
, tbs
, sig
);
176 /* FIXME - isn't there a better way to do this? Save the
177 * exception as a CSSM_RETURN and throw it if nonzero later?
179 if(context
!= NULL
) {
180 CSSM_FreeContext(context
);
182 CL_freeCSSMKey(signerPubKey
, *this);
183 if(ourCcHand
!= CSSM_INVALID_HANDLE
) {
184 CSSM_DeleteContext(ourCcHand
);
188 if(context
!= NULL
) {
189 CSSM_FreeContext(context
);
191 CL_freeCSSMKey(signerPubKey
, *this);
192 if(ourCcHand
!= CSSM_INVALID_HANDLE
) {
193 CSSM_DeleteContext(ourCcHand
);
198 * Given a DER-encoded TBSCert and a fully specified crypto context,
199 * sign the TBSCert and return the resulting DER-encoded Cert.
202 AppleX509CLSession::CertSign(
203 CSSM_CC_HANDLE CCHandle
,
204 const CssmData
&CertTemplate
,
205 const CSSM_FIELD
*SignScope
,
207 CssmData
&SignedCert
)
209 if((SignScope
!= NULL
) || (ScopeSize
!= 0)) {
210 CssmError::throwMe(CSSMERR_CL_SCOPE_NOT_SUPPORTED
);
212 if(CCHandle
== CSSM_INVALID_HANDLE
) {
213 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
216 /* cook up algId from context->(signing key, sig algorithm) */
217 CSSM_CONTEXT_PTR context
= NULL
; // must be freed
219 crtn
= CSSM_GetContext(CCHandle
, &context
);
221 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
223 CSSM_CONTEXT_ATTRIBUTE_PTR attr
; // not freed
224 crtn
= CSSM_GetContextAttribute(context
,
228 clErrorLog("CertSign: valid CCHandle but no signing key!\n");
229 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
231 CSSM_KEY_PTR signingKey
= attr
->Attribute
.Key
;
232 if(signingKey
== NULL
) {
233 clErrorLog("CertSign: valid CCHandle, NULL signing key!\n");
234 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
237 CssmAutoData
encAlgId(*this);
238 CssmAutoData
rawSig(*this);
239 CssmAutoData
fullCert(*this);
242 * FIXME: we really should break up the template and ensure that its
243 * signature algId matches the one we're signing with, or just use
244 * that algId here....for now, this is up to the app to make sure.
247 /* temp allocs/encode into here */
250 /* CSSM alg --> CSSM_X509_ALGORITHM_IDENTIFIER */
251 CSSM_X509_ALGORITHM_IDENTIFIER algId
;
252 memset(&algId
, 0, sizeof(algId
));
253 const CSSM_OID
*oid
= cssmAlgToOid(context
->AlgorithmType
);
256 clErrorLog("CertSIgn: unknown alg (%u)\n",
257 (unsigned)context
->AlgorithmType
);
258 CssmError::throwMe(CSSMERR_CL_UNKNOWN_FORMAT
);
260 algId
.algorithm
= *oid
;
262 /* NULL params - FIXME - is this OK? */
263 CL_nullAlgParams(algId
);
264 /* DER-encode the algID */
266 prtn
= SecNssEncodeItemOdata(&algId
, NSS_AlgorithmIDTemplate
,
269 CssmError::throwMe(CSSMERR_CL_MEMORY_ERROR
);
272 /* sign TBS --> rawSig */
273 signData(CCHandle
, CertTemplate
, rawSig
);
274 /* put it all together */
275 CL_certEncodeComponents(CertTemplate
, encAlgId
, rawSig
, fullCert
);
278 CSSM_FreeContext(context
);
281 CSSM_FreeContext(context
);
282 SignedCert
= fullCert
.release();
285 /*** Private functions ***/
288 * Sign a CssmData with the specified signing context. Used for
289 * signing both certs and CRLs; this routine doesn't know anything
293 AppleX509CLSession::signData(
294 CSSM_CC_HANDLE ccHand
,
296 CssmOwnedData
&sig
) // mallocd and returned
301 crtn
= CSSM_SignData(
305 CSSM_ALGID_NONE
, // DigestAlgorithm,
308 clErrorLog("AppleX509CLSession::CSSM_SignData: %s\n",
309 cssmErrorString(crtn
).c_str());
310 CssmError::throwMe(crtn
);
316 * Verify a block of data given a crypto context and a signature.
317 * Used for verifying certs and CRLs. Returns a CSSM_RETURN (callers
318 * always need to clean up after calling us).
320 void AppleX509CLSession::verifyData(
321 CSSM_CC_HANDLE ccHand
,
327 crtn
= CSSM_VerifyData(ccHand
,
330 CSSM_ALGID_NONE
, // Digest alg
333 if(crtn
== CSSMERR_CSP_VERIFY_FAILED
) {
334 /* CSP and CL report this differently */
335 CssmError::throwMe(CSSMERR_CL_VERIFICATION_FAILURE
);
338 CssmError::throwMe(crtn
);