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 // AppleCSPContext.h - CSP-wide contexts
22 #ifndef _H_APPLE_CSP_CONTEXT
23 #define _H_APPLE_CSP_CONTEXT
25 #include <Security/utilities.h>
26 #include <Security/CSPsession.h>
27 #include "BinaryKey.h"
30 // Parent class for all CSPContexts implemented in this CSP.
31 // Currently the only thing we add is a reference to our
34 class AppleCSPSession
;
36 class AppleCSPContext
: public CSPFullPluginSession::CSPContext
39 AppleCSPContext(AppleCSPSession
&session
)
40 : mSession(session
) {}
45 AppleCSPSession
&session() { return mSession
; }
48 * get symmetric key bits - context.key can be either ref or raw.
49 * A convenience routine typically used by symmetric contexts'
52 void symmetricKeyBits(
53 const Context
&context
,
54 CSSM_ALGORITHMS requiredAlg
, // throws if this doesn't match key alg
55 CSSM_KEYUSE intendedUse
, // throws if key usage doesn't match this
56 UInt8
*&keyBits
, // RETURNED (not mallocd or copied)
57 UInt32
&keyLen
); // RETURNED
60 AppleCSPSession
&mSession
;
64 // Context for CSSM_ALGID_APPLE_YARROW.
66 class YarrowContext
: public AppleCSPContext
69 YarrowContext(AppleCSPSession
&session
);
70 virtual ~YarrowContext();
71 virtual void init(const Context
&context
, bool encoding
= true);
72 void final(CssmData
&out
);
73 size_t outputSize(bool final
, size_t inSize
) { return outSize
; }
80 // Classes which inherit from AppleCSPContext and which also perform
81 // key pair generation inherit from this class as well.
83 class AppleKeyPairGenContext
{
86 // Subclass implements generate(const Context &, CssmKey &,
87 // CssmKey &). That method (called from CSPFullPluginSession)
88 // allocates two subclass-specific BinaryKeys and calls this
89 // method. This will eventually call down to generate(const Context &,
90 // BinaryKey &, BinaryKey &) and optionally to
91 // BinaryKey::generateKeyBlob.
94 const Context
&context
,
95 AppleCSPSession
&session
, // for ref keys
99 BinaryKey
*privBinKey
);
102 // Subclasses must implement this. It cooks up a key pair.
103 virtual void generate(
104 const Context
&context
,
105 BinaryKey
&pubBinKey
, // valid on successful return
106 BinaryKey
&privBinKey
, // ditto
107 uint32
&keySize
) = 0; // ditto
111 // Classes which inherit from AppleCSPContext and which also perform
112 // symmetric key generation inherit from this class as well.
114 class AppleSymmKeyGenContext
{
117 // Subclass implements generate(const Context &, CssmKey &,
118 // CssmKey &). Note that the second CssmKey is a dummy
119 // argument. That method merely calls generateSymKey, allowing us
120 // to get to the associated AppleCSPSession if we need to
121 // store reference keys. We take care of all attribute and
122 // usage validation and of header formatting. Parameters for
123 // validation typlically specified in constructor via an
124 // algorithm factory.
126 AppleSymmKeyGenContext(
127 uint32 minSize
, // in bits
128 uint32 maxSize
, // ditto
129 bool byteSized
) // true --> key size must
130 // be multiple of 8 bits
131 : minSizeInBits(minSize
),
132 maxSizeInBits(maxSize
),
133 mustBeByteSized(byteSized
) {}
136 const Context
&context
,
137 AppleCSPSession
&session
, // for ref keys
138 CssmKey
&cssmKey
); // RETURNED
141 uint32 minSizeInBits
;
142 uint32 maxSizeInBits
;
143 bool mustBeByteSized
;
148 * Generic symmetric key generation context, for algorithms whose
149 * requirements can be expressed in min/max key size and
150 * mustBeByteSized. Such algorithms just need create one of these
151 * from an algorithm factory.
153 class AppleSymmKeyGenerator
: public AppleCSPContext
, private AppleSymmKeyGenContext
{
155 AppleSymmKeyGenerator(
156 AppleCSPSession
&session
,
157 uint32 minSize
, // in bits
158 uint32 maxSize
, // ditto
159 bool byteSized
) : // true --> key size must
160 // be multiple of 8 bits
161 AppleCSPContext(session
),
162 AppleSymmKeyGenContext(minSize
, maxSize
, byteSized
) { }
164 void init(const Context
&context
, bool encoding
= true) { }
166 /* this just passes the request up to AppleSymmKeyGenContext */
168 const Context
&context
,
171 AppleSymmKeyGenContext::generateSymKey(
179 #endif /* _H_APPLE_CSP_CONTEXT */