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 // genkey - client interface to CSSM sign/verify contexts
22 #include <security_cdsa_client/genkey.h>
24 using namespace CssmClient
;
27 GenerateKey::GenerateKey(const CSP
&csp
, CSSM_ALGORITHMS alg
, uint32 size
)
28 : Context(csp
, alg
), mKeySize(size
), mSeed(NULL
), mSalt(NULL
), mParams(NULL
)
33 GenerateKey::database(const Db
&inDb
)
36 if (mDb
&& isActive())
37 set(CSSM_ATTRIBUTE_DL_DB_HANDLE
, mDb
->handle());
40 void GenerateKey::activate()
44 check(CSSM_CSP_CreateKeyGenContext(attachment()->handle(), mAlgorithm
,
45 mKeySize
, mSeed
, mSalt
, NULL
, NULL
, mParams
, &mHandle
));
46 // Must be done before calling set() since is does nothing unless we are active.
47 // Also we are technically active even if set() throws since we already created a context.
50 set(CSSM_ATTRIBUTE_DL_DB_HANDLE
, mDb
->handle());
54 Key
GenerateKey::operator () (const KeySpec
&spec
)
58 check(CSSM_GenerateKey(handle(), spec
.usage
, spec
.attributes
, spec
.label
,
59 &compositeRcc(), key
.makeNewKey(attachment())));
66 void GenerateKey::operator () (CssmKey
&key
, const KeySpec
&spec
)
68 check(CSSM_GenerateKey(handle(), spec
.usage
, spec
.attributes
, spec
.label
, &compositeRcc(), &key
));
72 void GenerateKey::operator () (Key
&publicKey
, const KeySpec
&pubSpec
,
73 Key
&privateKey
, const KeySpec
&privSpec
)
75 check(CSSM_GenerateKeyPair(handle(),
76 pubSpec
.usage
, pubSpec
.attributes
,
77 pubSpec
.label
, publicKey
.makeNewKey(attachment()),
78 privSpec
.usage
, privSpec
.attributes
,
79 privSpec
.label
, &compositeRcc(), privateKey
.makeNewKey(attachment())));
81 publicKey
->activate();
82 privateKey
->activate();
86 void GenerateKey::operator () (CssmKey
&publicKey
, const KeySpec
&pubSpec
,
87 CssmKey
&privateKey
, const KeySpec
&privSpec
)
89 check(CSSM_GenerateKeyPair(handle(),
90 pubSpec
.usage
, pubSpec
.attributes
, pubSpec
.label
, &publicKey
,
91 privSpec
.usage
, privSpec
.attributes
, privSpec
.label
, &compositeRcc(), &privateKey
));