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...) secdebug("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 CssmAutoData
d (mDigest
->allocator ());
290 d
.set((*mDigest
) ());
292 clientSession().generateSignature(tempContext
,
300 /* this is the one called by CSPFullPluginSession */
301 void SSSignatureContext::final(CssmData
&sig
)
304 /* normal final case in which the actual RPC via SS was done in the
305 * previous outputSize() call. */
306 ssCryptDebug("===final via pre-op and copy");
311 ssCryptDebug("===final via RPC");
317 SSSignatureContext::final(const CssmData
&sig
)
319 /* we have to pass down a modified Context, thus.... */
320 Context tempContext
= *mContext
;
321 tempContext
.AlgorithmType
= mSigAlg
;
324 CssmData
dData(const_cast<void *>(mNullDigest
->digestPtr()),
325 mNullDigest
->digestSizeInBytes());
326 clientSession().verifySignature(tempContext
,
333 clientSession().verifySignature(tempContext
,
343 // SSCryptContext -- Context for Encrypt and Decrypt operations
345 SSCryptContext::SSCryptContext(SSCSPSession
&session
)
346 : SSContext(session
), mKeyHandle(noKey
)
348 /* nothing for now */
352 SSCryptContext::~SSCryptContext()
354 /* nothing for now */
358 SSCryptContext::init(const Context
&context
, bool encoding
)
360 ssCryptDebug("===init");
361 SSContext::init(context
, encoding
);
363 /* reusable; reset accumulator */
364 mNullDigest
.digestInit();
366 const CssmKey
&keyInContext
=
367 context
.get
<const CssmKey
>(CSSM_ATTRIBUTE_KEY
,
368 CSSMERR_CSP_MISSING_ATTR_KEY
);
369 mKeyHandle
= mSession
.lookupKey(keyInContext
).keyHandle();
373 SSCryptContext::inputSize(size_t outSize
)
375 ssCryptDebug("===inputSize outSize=%u", (unsigned)outSize
);
380 SSCryptContext::outputSize(bool final
, size_t inSize
)
382 ssCryptDebug("===outputSize final %d inSize=%u", final
, (unsigned)inSize
);
384 /* we buffer until final; no intermediate output */
387 size_t inBufSize
= mNullDigest
.digestSizeInBytes();
389 /* This is the implied signal to go for it */
394 const CssmData
in(const_cast<void *>(mNullDigest
.digestPtr()), inBufSize
);
396 clientSession().encrypt(*mContext
, mKeyHandle
, in
, mOutBuf
);
399 clientSession().decrypt(*mContext
, mKeyHandle
, in
, mOutBuf
);
401 /* leave the accumulator as is in case of unexpected sequence */
402 ssCryptDebug(" ===outSize(pre-op) %u", (unsigned)mOutBuf
.Length
);
403 return mOutBuf
.Length
;
406 /* out-of-band case, ask CSP via SS */
407 uint32 outSize
= clientSession().getOutputSize(*mContext
,
411 ssCryptDebug(" ===outSize(RPC) %u", (unsigned)outSize
);
412 return (size_t)outSize
;
417 SSCryptContext::minimumProgress(size_t &in
, size_t &out
)
424 SSCryptContext::update(void *inp
, size_t &inSize
, void *outp
, size_t &outSize
)
426 ssCryptDebug("===update inSize=%u", (unsigned)inSize
);
427 /* add incoming data to accumulator */
428 mNullDigest
.digestUpdate(inp
, inSize
);
434 SSCryptContext::final(CssmData
&out
)
436 if(mOutBuf
.Data
!= NULL
) {
437 /* normal final case in which the actual RPC via SS was done in the
438 * previous outputSize() call. A memcpy is needed here because
439 * CSPFullPluginSession has just allocated the buf size we need. */
440 ssCryptDebug("===final via pre-op and copy");
445 /* when is this path taken...? */
446 ssCryptDebug("===final via RPC");
447 size_t inSize
= mNullDigest
.digestSizeInBytes();
450 const CssmData
in(const_cast<void *>(mNullDigest
.digestPtr()), inSize
);
451 IFDEBUG(unsigned origOutSize
= out
.length());
453 clientSession().encrypt(*mContext
, mKeyHandle
, in
, out
);
456 clientSession().decrypt(*mContext
, mKeyHandle
, in
, out
);
458 assert(out
.length() <= origOutSize
);
459 mNullDigest
.digestInit();
462 // Digest, using raw CSP
463 SSDigestContext::SSDigestContext(SSCSPSession
&session
)
464 : SSContext(session
), mDigest(NULL
)
469 SSDigestContext::~SSDigestContext()
474 void SSDigestContext::init(const Context
&context
, bool encoding
)
478 SSContext::init(context
, encoding
);
479 alg
= context
.algorithm();
480 mDigest
= new CssmClient::Digest(mSession
.mRawCsp
, alg
);
483 void SSDigestContext::update(const CssmData
&data
)
485 mDigest
->digest(data
);
488 void SSDigestContext::final(CssmData
&out
)
493 size_t SSDigestContext::outputSize(bool final
, size_t inSize
)
499 return (size_t)mDigest
->getOutputSize(inSize
);
503 // MACContext - common class for MAC generate, verify
504 SSMACContext::SSMACContext(SSCSPSession
&session
)
505 : SSContext(session
), mKeyHandle(noKey
)
510 void SSMACContext::init(const Context
&context
, bool encoding
)
512 SSContext::init(context
, encoding
);
514 /* reusable; reset accumulator */
515 mNullDigest
.digestInit();
517 /* snag key from context */
518 const CssmKey
&keyInContext
=
519 context
.get
<const CssmKey
>(CSSM_ATTRIBUTE_KEY
,
520 CSSMERR_CSP_MISSING_ATTR_KEY
);
521 mKeyHandle
= mSession
.lookupKey(keyInContext
).keyHandle();
524 void SSMACContext::update(const CssmData
&data
)
526 /* add incoming data to accumulator */
527 mNullDigest
.digestUpdate(data
.data(), data
.length());
530 size_t SSMACContext::outputSize(bool final
, size_t inSize
)
533 ssCryptDebug("===mac outputSize !final\n");
537 ssCryptDebug("===mac outputSize final, !encoding\n");
538 /* don't see why this is even called... */
543 * This is the implied signal to go for it.
547 ssCryptDebug("===mac outputSize(pre-op) %u", (unsigned)mOutBuf
.Length
);
548 return (size_t)mOutBuf
.Length
;
551 /* out-of-band case, ask CSP via SS */
552 uint32 outSize
= clientSession().getOutputSize(*mContext
,
554 inSize
+ mNullDigest
.digestSizeInBytes(),
556 ssCryptDebug("===mac outputSize(RPC) %u", (unsigned)outSize
);
557 return (size_t)outSize
;
563 /* first the common routine used by final() and outputSize() */
564 void SSMACContext::genMac(CssmData
&mac
)
566 CssmData
allData(const_cast<void *>(mNullDigest
.digestPtr()),
567 mNullDigest
.digestSizeInBytes());
568 clientSession().generateMac(*mContext
, mKeyHandle
, allData
, mac
);
571 void SSMACContext::final(CssmData
&mac
)
577 void SSMACContext::final(const CssmData
&mac
)
579 CssmData
allData(const_cast<void *>(mNullDigest
.digestPtr()),
580 mNullDigest
.digestSizeInBytes());
581 clientSession().verifyMac(*mContext
, mKeyHandle
, allData
, mac
);