2 * Copyright (c) 2004,2011-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // SDContext.h - Security Server contexts
31 #include <security_cdsa_plugin/CSPsession.h>
32 #include <securityd_client/ssclient.h>
33 #include <security_cdsa_utilities/digestobject.h>
34 #include <security_cdsa_client/cspclient.h>
37 // Parent class for all CSPContexts implemented in this CSP. Currently the
38 // only thing we add is a reference to our creator's session.
43 class SDContext
: public CSPFullPluginSession::CSPContext
46 SDContext(SDCSPSession
&session
);
47 ~SDContext() { clearOutBuf(); }
48 virtual void init(const Context
&context
, bool encoding
);
51 SecurityServer::ClientSession
&clientSession();
52 SDCSPSession
&mSession
;
54 // mOutBuf provides a holding tank for implied final() operations
55 // resulting from an outputSize(true, 0). This form of outputSize()
56 // is understood to only occur just prior to the final() call. To avoid
57 // an extra RPC (just to perform the outputSize(), most subclasses of
58 // SDContext actually perform the final() operation at this time,
59 // storing the result in mOutBuf. At final(), mOutBuf() is just copied
60 // to the caller's supplied output buffer.
63 // We remember a pointer to the passed in context and assume it will
64 // remain a valid from init(), update() all the way though the call to
66 const Context
*mContext
;
69 void copyOutBuf(CssmData
&out
);
72 // context for signature (sign and verify)
73 class SDSignatureContext
: public SDContext
76 SDSignatureContext(SDCSPSession
&session
);
77 ~SDSignatureContext();
78 virtual void init(const Context
&context
, bool signing
);
79 virtual void update(const CssmData
&data
);
80 virtual size_t outputSize(bool final
, size_t inSize
);
83 void sign(CssmData
&sig
);
84 virtual void final(CssmData
&out
);
87 virtual void final(const CssmData
&in
);
89 /* for raw sign/verify - optionally called after init */
90 virtual void setDigestAlgorithm(CSSM_ALGORITHMS digestAlg
);
93 /* stash the context's key for final sign/verify */
94 SecurityServer::KeyHandle mKeyHandle
;
96 /* alg-dependent, calculated at init time */
97 CSSM_ALGORITHMS mSigAlg
; // raw signature alg
98 CSSM_ALGORITHMS mDigestAlg
; // digest
100 /* exactly one of these is used to collect updates */
101 NullDigest
*mNullDigest
;
102 CssmClient::Digest
*mDigest
;
105 // Context for GenerateRandom operations
106 class SDRandomContext
: public SDContext
109 SDRandomContext(SDCSPSession
&session
);
110 virtual void init(const Context
&context
, bool);
111 virtual size_t outputSize(bool final
, size_t inSize
);
112 virtual void final(CssmData
&out
);
115 uint32 mOutSize
; // spec'd in context at init() time
118 // Context for Encrypt and Decrypt operations
119 class SDCryptContext
: public SDContext
122 SDCryptContext(SDCSPSession
&session
);
124 virtual void init(const Context
&context
, bool encoding
);
125 virtual size_t inputSize(size_t outSize
);
126 virtual size_t outputSize(bool final
, size_t inSize
);
127 virtual void minimumProgress(size_t &in
, size_t &out
);
128 virtual void update(void *inp
, size_t &inSize
, void *outp
,
130 virtual void final(CssmData
&out
);
133 SecurityServer::KeyHandle mKeyHandle
;
134 NullDigest mNullDigest
; // accumulator
137 // Digest, using raw CSP
138 class SDDigestContext
: public SDContext
141 SDDigestContext(SDCSPSession
&session
);
143 virtual void init(const Context
&context
, bool);
144 virtual void update(const CssmData
&data
);
145 virtual void final(CssmData
&out
);
146 virtual size_t outputSize(bool final
, size_t inSize
);
149 CssmClient::Digest
*mDigest
;
152 // common class for MAC generate, verify
153 class SDMACContext
: public SDContext
156 SDMACContext(SDCSPSession
&session
);
157 virtual void init(const Context
&context
, bool);
158 virtual void update(const CssmData
&data
);
159 virtual size_t outputSize(bool final
, size_t inSize
);
162 void genMac(CssmData
&mac
);
163 virtual void final(CssmData
&out
);
165 virtual void final(const CssmData
&in
);
168 SecurityServer::KeyHandle mKeyHandle
;
169 NullDigest mNullDigest
; // accumulator
173 #endif // _H_SD_CONTEXT