]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_client/genkey.cpp
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_client / genkey.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // genkey - client interface to CSSM sign/verify contexts
21 //
22 #include <Security/genkey.h>
23
24 using namespace CssmClient;
25
26
27 GenerateKey::GenerateKey(const CSP &csp, CSSM_ALGORITHMS alg, uint32 size)
28 : Context(csp, alg), mKeySize(size), mSeed(NULL), mSalt(NULL), mParams(NULL), mInitialAcl(NULL)
29 {
30 }
31
32 void
33 GenerateKey::database(const Db &inDb)
34 {
35 mDb = inDb;
36 if (mDb && isActive())
37 set(CSSM_ATTRIBUTE_DL_DB_HANDLE, mDb->handle());
38 }
39
40 void GenerateKey::activate()
41 {
42 if (!mActive)
43 {
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.
48 mActive = true;
49 if (mDb)
50 set(CSSM_ATTRIBUTE_DL_DB_HANDLE, mDb->handle());
51 }
52 }
53
54 Key GenerateKey::operator () (const KeySpec &spec)
55 {
56 Key key;
57
58 check(CSSM_GenerateKey(handle(), spec.usage, spec.attributes, spec.label,
59 mInitialAcl, key.makeNewKey(attachment())));
60
61 key->activate();
62
63 return key;
64 }
65
66 void GenerateKey::operator () (CssmKey &key, const KeySpec &spec)
67 {
68 check(CSSM_GenerateKey(handle(), spec.usage, spec.attributes, spec.label, mInitialAcl, &key));
69
70 }
71
72 void GenerateKey::operator () (Key &publicKey, const KeySpec &pubSpec,
73 Key &privateKey, const KeySpec &privSpec)
74 {
75 check(CSSM_GenerateKeyPair(handle(),
76 pubSpec.usage, pubSpec.attributes,
77 pubSpec.label, publicKey.makeNewKey(attachment()),
78 privSpec.usage, privSpec.attributes,
79 privSpec.label, mInitialAcl, privateKey.makeNewKey(attachment())));
80
81 publicKey->activate();
82 privateKey->activate();
83
84 }
85
86 void GenerateKey::operator () (CssmKey &publicKey, const KeySpec &pubSpec,
87 CssmKey &privateKey, const KeySpec &privSpec)
88 {
89 check(CSSM_GenerateKeyPair(handle(),
90 pubSpec.usage, pubSpec.attributes, pubSpec.label, &publicKey,
91 privSpec.usage, privSpec.attributes, privSpec.label, mInitialAcl, &privateKey));
92 }
93