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 "SnaccUtils.h"
29 #include "cldebugging.h"
30 #include "CSPAttacher.h"
31 #include <Security/oidscert.h>
32 #include <Security/cssmapple.h>
33 #include <Security/cssmerrno.h>
34 #include <Security/cdsaUtils.h>
37 * Given a DER-encoded cert, obtain a fully usable CSSM_KEY representing
38 * the cert's public key.
41 AppleX509CLSession::CertGetKeyInfo(
45 DecodedCert
decodedCert(*this, Cert
);
46 Key
= decodedCert
.extractCSSMKey(*this);
50 * Given a DER-encoded cert and a fully specified crypto context, verify
51 * cert's TBS and signature.
54 AppleX509CLSession::CertVerifyWithKey(
55 CSSM_CC_HANDLE CCHandle
,
56 const CssmData
&CertToBeVerified
)
58 CssmAutoData
tbs(*this);
59 CssmAutoData
algId(*this);
60 CssmAutoData
sig(*this);
61 CL_certDecodeComponents(CertToBeVerified
, tbs
, algId
, sig
);
62 verifyData(CCHandle
, tbs
, sig
);
66 * Verify a DER-encoded cert, obtaining crypto context from either
67 * caller-specified context or by inference from SignerCert.
70 AppleX509CLSession::CertVerify(
71 CSSM_CC_HANDLE CCHandle
,
72 const CssmData
&CertToBeVerified
,
73 const CssmData
*SignerCert
,
74 const CSSM_FIELD
*VerifyScope
,
77 if((VerifyScope
!= NULL
) || (ScopeSize
!= 0)) {
78 CssmError::throwMe(CSSMERR_CL_SCOPE_NOT_SUPPORTED
);
80 if((CCHandle
== CSSM_INVALID_HANDLE
) && (SignerCert
== NULL
)) {
81 /* need one or the other */
82 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
85 /* get top-level components */
86 CssmAutoData
tbs(*this); // in DER format
87 CssmAutoData
algId(*this); // in DER format
88 CssmAutoData
sig(*this); // in DER format
89 CL_certDecodeComponents(CertToBeVerified
, tbs
, algId
, sig
);
91 /* these must be explicitly freed upon exit */
92 CSSM_KEY_PTR signerPubKey
= NULL
;
93 CSSM_CONTEXT_PTR context
= NULL
;
94 CSSM_CSP_HANDLE cspHand
= CSSM_INVALID_HANDLE
;
95 CSSM_CC_HANDLE ourCcHand
= CSSM_INVALID_HANDLE
;
97 /* SignerCert optional; if present, obtain its subject key */
98 if(SignerCert
!= NULL
) {
99 CertGetKeyInfo(*SignerCert
, signerPubKey
);
102 /* signerPubKey must be explicitly freed in any case */
104 if(CCHandle
!= CSSM_INVALID_HANDLE
) {
106 * We'll use this CCHandle for the sig verify, but
107 * make sure it matches possible incoming SignerCert parameters
109 if(SignerCert
!= NULL
) {
112 /* extract signer's public key as a CSSM_KEY from context */
113 crtn
= CSSM_GetContext(CCHandle
, &context
);
115 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
117 CSSM_CONTEXT_ATTRIBUTE_PTR attr
;
118 crtn
= CSSM_GetContextAttribute(context
,
122 errorLog0("CertVerify: valid CCHandle but no key!\n");
123 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
126 CASSERT(signerPubKey
!= NULL
);
127 CSSM_KEY_PTR contextPubKey
= attr
->Attribute
.Key
;
128 if(contextPubKey
->KeyHeader
.AlgorithmId
!=
129 signerPubKey
->KeyHeader
.AlgorithmId
) {
130 errorLog0("CertVerify: AlgorithmId mismatch!\n");
131 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
134 /* TBD - check key size, when we have a CSP which can report it */
135 /* TBD - anything else? */
136 } /* verifying multiple contexts */
137 /* OK to use CCHandle as is for verify context */
138 } /* valid CCHandle */
141 * All we have is signer cert. We already have its public key;
142 * get signature alg from CertToBeVerified's Cert.algID (which
143 * we currently have in DER form).
145 CASSERT(SignerCert
!= NULL
);
146 CASSERT(signerPubKey
!= NULL
);
148 AlgorithmIdentifier snaccAlgId
;
149 //CL_decodeAlgId(algId, snaccAlgId);
150 SC_decodeAsnObj(algId
, snaccAlgId
);
151 CSSM_ALGORITHMS vfyAlg
= CL_snaccOidToCssmAlg(snaccAlgId
.algorithm
);
153 /* attach to CSP, cook up a context */
154 cspHand
= getGlobalCspHand(true);
156 crtn
= CSSM_CSP_CreateSignatureContext(cspHand
,
158 NULL
, // Access Creds
161 CCHandle
= ourCcHand
;
162 } /* inferring sig verify context from SignerCert */
163 verifyData(CCHandle
, tbs
, sig
);
166 /* FIXME - isn't there a better way to do this? Save the
167 * exception as a CSSM_RETURN and throw it if nonzero later?
169 if(context
!= NULL
) {
170 CSSM_FreeContext(context
);
172 DecodedCert::freeCSSMKey(signerPubKey
, *this);
173 if(ourCcHand
!= CSSM_INVALID_HANDLE
) {
174 CSSM_DeleteContext(ourCcHand
);
178 if(context
!= NULL
) {
179 CSSM_FreeContext(context
);
181 DecodedCert::freeCSSMKey(signerPubKey
, *this);
182 if(ourCcHand
!= CSSM_INVALID_HANDLE
) {
183 CSSM_DeleteContext(ourCcHand
);
188 * Given a DER-encoded TBSCert and a fully specified crypto context,
189 * sign the TBSCert and return the resulting DER-encoded Cert.
192 AppleX509CLSession::CertSign(
193 CSSM_CC_HANDLE CCHandle
,
194 const CssmData
&CertTemplate
,
195 const CSSM_FIELD
*SignScope
,
197 CssmData
&SignedCert
)
199 if((SignScope
!= NULL
) || (ScopeSize
!= 0)) {
200 CssmError::throwMe(CSSMERR_CL_SCOPE_NOT_SUPPORTED
);
202 if(CCHandle
== CSSM_INVALID_HANDLE
) {
203 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
206 /* cook up algId from context->(signing key, sig algorithm) */
207 CSSM_CONTEXT_PTR context
= NULL
; // must be freed
209 crtn
= CSSM_GetContext(CCHandle
, &context
);
211 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
213 CSSM_CONTEXT_ATTRIBUTE_PTR attr
; // not freed
214 crtn
= CSSM_GetContextAttribute(context
,
218 errorLog0("CertSign: valid CCHandle but no signing key!\n");
219 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
221 CSSM_KEY_PTR signingKey
= attr
->Attribute
.Key
;
222 if(signingKey
== NULL
) {
223 errorLog0("CertSign: valid CCHandle, NULL signing key!\n");
224 CssmError::throwMe(CSSMERR_CL_INVALID_CONTEXT_HANDLE
);
227 AlgorithmIdentifier snaccAlgId
;
228 CssmAutoData
encAlgId(*this);
229 CssmAutoData
rawSig(*this);
230 CssmAutoData
fullCert(*this);
232 /* CSSM alg --> snacc-style AlgorithmIdentifier object */
233 CL_cssmAlgToSnaccOid(context
->AlgorithmType
,
234 snaccAlgId
.algorithm
);
235 /* NULL params - FIXME - is this OK? */
236 CL_nullAlgParams(snaccAlgId
);
237 /* DER-encode the algID */
238 SC_encodeAsnObj(snaccAlgId
, encAlgId
, 128);
239 /* sign TBS --> sig */
240 signData(CCHandle
, CertTemplate
, rawSig
);
241 /* put it all together */
242 CL_certEncodeComponents(CertTemplate
, encAlgId
, rawSig
, fullCert
);
245 CSSM_FreeContext(context
);
248 CSSM_FreeContext(context
);
249 SignedCert
= fullCert
.release();
252 /*** Private functions ***/
255 * Sign a CssmData with the specified signing context. Used for
256 * signing both certs and CRLs; this routine doesn't know anything
260 AppleX509CLSession::signData(
261 CSSM_CC_HANDLE ccHand
,
263 CssmOwnedData
&sig
) // mallocd and returned
268 crtn
= CSSM_SignData(
272 CSSM_ALGID_NONE
, // DigestAlgorithm,
275 errorLog1("AppleX509CLSession::CSSM_SignData: %s\n",
276 cssmErrorString(crtn
).c_str());
277 CssmError::throwMe(crtn
);
283 * Verify a block of data given a crypto context and a signature.
284 * Used for verifying certs and CRLs. Returns a CSSM_RETURN (callers
285 * always need to clean up after calling us).
287 void AppleX509CLSession::verifyData(
288 CSSM_CC_HANDLE ccHand
,
294 crtn
= CSSM_VerifyData(ccHand
,
297 CSSM_ALGID_NONE
, // Digest alg
300 // errorLog1("AppleX509CLSession::verifyData: %s\n",
301 // cssmErrorString(crtn).c_str());
302 if(crtn
== CSSMERR_CSP_VERIFY_FAILED
) {
303 /* CSP and CL report this differently */
304 CssmError::throwMe(CSSMERR_CL_VERIFICATION_FAILURE
);
307 CssmError::throwMe(crtn
);