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 // SSContext - cryptographic contexts for the security server
22 #include "SSContext.h"
24 #include "SSCSPSession.h"
26 #include <Security/debugging.h>
28 #define ssCryptDebug(args...) debug("ssCrypt", ## args)
30 using namespace SecurityServer
;
35 SSContext::SSContext(SSCSPSession
&session
)
36 : mSession(session
), mContext(NULL
)
40 void SSContext::clearOutBuf()
43 mSession
.free(mOutBuf
.Data
);
48 void SSContext::copyOutBuf(CssmData
&out
)
50 if(out
.length() < mOutBuf
.length()) {
51 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
53 memmove(out
.Data
, mOutBuf
.Data
, mOutBuf
.Length
);
54 out
.Length
= mOutBuf
.Length
;
59 SSContext::init(const Context
&context
,
60 bool /* encoding */) // @@@ should be removed from API since it's already in mDirection
66 SecurityServer::ClientSession
&
67 SSContext::clientSession()
69 return mSession
.clientSession();
74 // SSRandomContext -- Context for GenerateRandom operations
76 SSRandomContext::SSRandomContext(SSCSPSession
&session
) : SSContext(session
) {}
79 SSRandomContext::init(const Context
&context
, bool encoding
)
81 SSContext::init(context
, encoding
);
83 // set/freeze output size
84 mOutSize
= context
.getInt(CSSM_ATTRIBUTE_OUTPUT_SIZE
, CSSMERR_CSP_MISSING_ATTR_OUTPUT_SIZE
);
87 // seed the PRNG (if specified)
88 if (const CssmCryptoData
*seed
= context
.get
<CssmCryptoData
>(CSSM_ATTRIBUTE_SEED
)) {
89 const CssmData
&seedValue
= (*seed
)();
90 clientSession().seedRandom(seedValue
);
96 SSRandomContext::outputSize(bool final
, size_t inSize
)
102 SSRandomContext::final(CssmData
&out
)
104 clientSession().generateRandom(out
);
108 // signature contexts
109 SSSignatureContext::SSSignatureContext(SSCSPSession
&session
)
110 : SSContext(session
),
115 /* nothing else for now */
118 SSSignatureContext::~SSSignatureContext()
124 void SSSignatureContext::init(const Context
&context
, bool signing
)
126 SSContext::init(context
, signing
);
128 /* reusable: skip everything except resetting digest state */
129 if((mNullDigest
!= NULL
) || (mDigest
!= NULL
)) {
130 if(mNullDigest
!= NULL
) {
131 mNullDigest
->digestInit();
136 /* snag key from context */
137 const CssmKey
&keyInContext
=
138 context
.get
<const CssmKey
>(CSSM_ATTRIBUTE_KEY
,
139 CSSMERR_CSP_MISSING_ATTR_KEY
);
140 mKeyHandle
= mSession
.lookupKey(keyInContext
).keyHandle();
142 /* get digest alg and sig alg from Context.algorithm */
143 switch(context
.algorithm()) {
145 case CSSM_ALGID_SHA1WithDSA
:
146 mDigestAlg
= CSSM_ALGID_SHA1
;
147 mSigAlg
= CSSM_ALGID_DSA
;
149 case CSSM_ALGID_DSA
: // Raw
150 mDigestAlg
= CSSM_ALGID_NONE
;
151 mSigAlg
= CSSM_ALGID_DSA
;
154 case CSSM_ALGID_SHA1WithRSA
:
155 mDigestAlg
= CSSM_ALGID_SHA1
;
156 mSigAlg
= CSSM_ALGID_RSA
;
158 case CSSM_ALGID_MD5WithRSA
:
159 mDigestAlg
= CSSM_ALGID_MD5
;
160 mSigAlg
= CSSM_ALGID_RSA
;
162 case CSSM_ALGID_MD2WithRSA
:
163 mDigestAlg
= CSSM_ALGID_MD2
;
164 mSigAlg
= CSSM_ALGID_RSA
;
166 case CSSM_ALGID_RSA
: // Raw
167 mDigestAlg
= CSSM_ALGID_NONE
;
168 mSigAlg
= CSSM_ALGID_RSA
;
171 case CSSM_ALGID_FEE_SHA1
:
172 mDigestAlg
= CSSM_ALGID_SHA1
;
173 mSigAlg
= CSSM_ALGID_FEE
;
175 case CSSM_ALGID_FEE_MD5
:
176 mDigestAlg
= CSSM_ALGID_MD5
;
177 mSigAlg
= CSSM_ALGID_FEE
;
179 case CSSM_ALGID_FEE
: // Raw
180 mDigestAlg
= CSSM_ALGID_NONE
;
181 mSigAlg
= CSSM_ALGID_FEE
;
184 case CSSM_ALGID_SHA1WithECDSA
:
185 mDigestAlg
= CSSM_ALGID_SHA1
;
186 mSigAlg
= CSSM_ALGID_ECDSA
;
188 case CSSM_ALGID_ECDSA
: // Raw
189 mDigestAlg
= CSSM_ALGID_NONE
;
190 mSigAlg
= CSSM_ALGID_ECDSA
;
193 CssmError::throwMe(CSSMERR_CSP_INVALID_ALGORITHM
);
196 /* set up mNullDigest or mDigest */
197 if(mDigestAlg
== CSSM_ALGID_NONE
) {
198 mNullDigest
= new NullDigest();
201 mDigest
= new CssmClient::Digest(mSession
.mRawCsp
, mDigestAlg
);
206 * for raw sign/verify - optionally called after init.
207 * Note that in init (in this case), we set mDigestAlg to ALGID_NONE and set up
208 * a NullDigest. We now overwrite mDigestAlg, and we'll useĆthis
209 * new value when we do the actual sign/vfy.
211 void SSSignatureContext::setDigestAlgorithm(CSSM_ALGORITHMS digestAlg
)
213 mDigestAlg
= digestAlg
;
216 void SSSignatureContext::update(const CssmData
&data
)
218 /* Note that for this context, we really can not deal with an out-of-sequence
219 * update --> final(true, 0) --> update since we lose the pending digest state
220 * when we perform the implied final() during outputSize(true, 0). */
221 assert(mOutBuf
.Data
== NULL
);
223 /* add incoming data to digest or accumulator */
225 mNullDigest
->digestUpdate(data
.data(), data
.length());
228 mDigest
->digest(data
);
232 size_t SSSignatureContext::outputSize(bool final
, size_t inSize
)
235 ssCryptDebug("===sig outputSize !final\n");
239 ssCryptDebug("===sig outputSize final, !encoding\n");
240 /* don't see why this is even called... */
245 * This is the implied signal to go for it. Note that in this case,
246 * we can not go back and re-do the op in case of an unexpected
247 * sequence of update/outputSize(final, 0)/final - we lose the digest
248 * state. Perhaps we should save the digest...? But still it would
249 * be impossible to do another update.
253 ssCryptDebug("===sig outputSize(pre-op) %u", (unsigned)mOutBuf
.Length
);
254 return (size_t)mOutBuf
.Length
;
257 /* out-of-band case, ask CSP via SS */
258 uint32 outSize
= clientSession().getOutputSize(*mContext
,
260 /* FIXME - what to use for inSize here - we don't want to
261 * interrogate mDigest, as that would result in another RPC...
262 * and signature size is not related to input size...right? */
265 ssCryptDebug("===sig outputSize(RPC) %u", (unsigned)outSize
);
266 return (size_t)outSize
;
272 /* first the common routine shared by final and outputSize */
273 void SSSignatureContext::sign(CssmData
&sig
)
275 /* we have to pass down a modified Context, thus.... */
276 Context tempContext
= *mContext
;
277 tempContext
.AlgorithmType
= mSigAlg
;
280 CssmData
dData(const_cast<void *>(mNullDigest
->digestPtr()),
281 mNullDigest
->digestSizeInBytes());
282 clientSession().generateSignature(tempContext
,
289 clientSession().generateSignature(tempContext
,
297 /* this is the one called by CSPFullPluginSession */
298 void SSSignatureContext::final(CssmData
&sig
)
301 /* normal final case in which the actual RPC via SS was done in the
302 * previous outputSize() call. */
303 ssCryptDebug("===final via pre-op and copy");
308 ssCryptDebug("===final via RPC");
314 SSSignatureContext::final(const CssmData
&sig
)
316 /* we have to pass down a modified Context, thus.... */
317 Context tempContext
= *mContext
;
318 tempContext
.AlgorithmType
= mSigAlg
;
321 CssmData
dData(const_cast<void *>(mNullDigest
->digestPtr()),
322 mNullDigest
->digestSizeInBytes());
323 clientSession().verifySignature(tempContext
,
330 clientSession().verifySignature(tempContext
,
340 // SSCryptContext -- Context for Encrypt and Decrypt operations
342 SSCryptContext::SSCryptContext(SSCSPSession
&session
)
343 : SSContext(session
), mKeyHandle(noKey
)
345 /* nothing for now */
349 SSCryptContext::~SSCryptContext()
351 /* nothing for now */
355 SSCryptContext::init(const Context
&context
, bool encoding
)
357 ssCryptDebug("===init");
358 SSContext::init(context
, encoding
);
360 /* reusable; reset accumulator */
361 mNullDigest
.digestInit();
363 const CssmKey
&keyInContext
=
364 context
.get
<const CssmKey
>(CSSM_ATTRIBUTE_KEY
,
365 CSSMERR_CSP_MISSING_ATTR_KEY
);
366 mKeyHandle
= mSession
.lookupKey(keyInContext
).keyHandle();
370 SSCryptContext::inputSize(size_t outSize
)
372 ssCryptDebug("===inputSize outSize=%u", (unsigned)outSize
);
377 SSCryptContext::outputSize(bool final
, size_t inSize
)
379 ssCryptDebug("===outputSize final %d inSize=%u", final
, (unsigned)inSize
);
381 /* we buffer until final; no intermediate output */
384 size_t inBufSize
= mNullDigest
.digestSizeInBytes();
386 /* This is the implied signal to go for it */
391 const CssmData
in(const_cast<void *>(mNullDigest
.digestPtr()), inBufSize
);
393 clientSession().encrypt(*mContext
, mKeyHandle
, in
, mOutBuf
);
396 clientSession().decrypt(*mContext
, mKeyHandle
, in
, mOutBuf
);
398 /* leave the accumulator as is in case of unexpected sequence */
399 ssCryptDebug(" ===outSize(pre-op) %u", (unsigned)mOutBuf
.Length
);
400 return mOutBuf
.Length
;
403 /* out-of-band case, ask CSP via SS */
404 uint32 outSize
= clientSession().getOutputSize(*mContext
,
408 ssCryptDebug(" ===outSize(RPC) %u", (unsigned)outSize
);
409 return (size_t)outSize
;
414 SSCryptContext::minimumProgress(size_t &in
, size_t &out
)
421 SSCryptContext::update(void *inp
, size_t &inSize
, void *outp
, size_t &outSize
)
423 ssCryptDebug("===update inSize=%u", (unsigned)inSize
);
424 /* add incoming data to accumulator */
425 mNullDigest
.digestUpdate(inp
, inSize
);
431 SSCryptContext::final(CssmData
&out
)
433 if(mOutBuf
.Data
!= NULL
) {
434 /* normal final case in which the actual RPC via SS was done in the
435 * previous outputSize() call. A memcpy is needed here because
436 * CSPFullPluginSession has just allocated the buf size we need. */
437 ssCryptDebug("===final via pre-op and copy");
442 /* when is this path taken...? */
443 ssCryptDebug("===final via RPC");
444 size_t inSize
= mNullDigest
.digestSizeInBytes();
447 const CssmData
in(const_cast<void *>(mNullDigest
.digestPtr()), inSize
);
448 unsigned origOutSize
= out
.length();
450 clientSession().encrypt(*mContext
, mKeyHandle
, in
, out
);
453 clientSession().decrypt(*mContext
, mKeyHandle
, in
, out
);
455 assert(out
.length() <= origOutSize
);
456 mNullDigest
.digestInit();
459 // Digest, using raw CSP
460 SSDigestContext::SSDigestContext(SSCSPSession
&session
)
461 : SSContext(session
), mDigest(NULL
)
466 SSDigestContext::~SSDigestContext()
471 void SSDigestContext::init(const Context
&context
, bool encoding
)
475 SSContext::init(context
, encoding
);
476 alg
= context
.algorithm();
477 mDigest
= new CssmClient::Digest(mSession
.mRawCsp
, alg
);
480 void SSDigestContext::update(const CssmData
&data
)
482 mDigest
->digest(data
);
485 void SSDigestContext::final(CssmData
&out
)
490 size_t SSDigestContext::outputSize(bool final
, size_t inSize
)
496 return (size_t)mDigest
->getOutputSize(inSize
);
500 // MACContext - common class for MAC generate, verify
501 SSMACContext::SSMACContext(SSCSPSession
&session
)
502 : SSContext(session
), mKeyHandle(noKey
)
507 void SSMACContext::init(const Context
&context
, bool encoding
)
509 SSContext::init(context
, encoding
);
511 /* reusable; reset accumulator */
512 mNullDigest
.digestInit();
514 /* snag key from context */
515 const CssmKey
&keyInContext
=
516 context
.get
<const CssmKey
>(CSSM_ATTRIBUTE_KEY
,
517 CSSMERR_CSP_MISSING_ATTR_KEY
);
518 mKeyHandle
= mSession
.lookupKey(keyInContext
).keyHandle();
521 void SSMACContext::update(const CssmData
&data
)
523 /* add incoming data to accumulator */
524 mNullDigest
.digestUpdate(data
.data(), data
.length());
527 size_t SSMACContext::outputSize(bool final
, size_t inSize
)
530 ssCryptDebug("===mac outputSize !final\n");
534 ssCryptDebug("===mac outputSize final, !encoding\n");
535 /* don't see why this is even called... */
540 * This is the implied signal to go for it.
544 ssCryptDebug("===mac outputSize(pre-op) %u", (unsigned)mOutBuf
.Length
);
545 return (size_t)mOutBuf
.Length
;
548 /* out-of-band case, ask CSP via SS */
549 uint32 outSize
= clientSession().getOutputSize(*mContext
,
551 inSize
+ mNullDigest
.digestSizeInBytes(),
553 ssCryptDebug("===mac outputSize(RPC) %u", (unsigned)outSize
);
554 return (size_t)outSize
;
560 /* first the common routine used by final() and outputSize() */
561 void SSMACContext::genMac(CssmData
&mac
)
563 CssmData
allData(const_cast<void *>(mNullDigest
.digestPtr()),
564 mNullDigest
.digestSizeInBytes());
565 clientSession().generateMac(*mContext
, mKeyHandle
, allData
, mac
);
568 void SSMACContext::final(CssmData
&mac
)
574 void SSMACContext::final(const CssmData
&mac
)
576 CssmData
allData(const_cast<void *>(mNullDigest
.digestPtr()),
577 mNullDigest
.digestSizeInBytes());
578 clientSession().verifyMac(*mContext
, mKeyHandle
, allData
, mac
);