]> git.saurik.com Git - apple/security.git/blame - OSX/include/security_cdsa_client/genkey.cpp
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / include / security_cdsa_client / genkey.cpp
CommitLineData
b1ab9ed8 1/*
d8f41ccd 2 * Copyright (c) 2000-2001,2011-2012,2014 Apple Inc. All Rights Reserved.
b1ab9ed8
A
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_cdsa_client/genkey.h>
23
24using namespace CssmClient;
25
26
27GenerateKey::GenerateKey(const CSP &csp, CSSM_ALGORITHMS alg, uint32 size)
28: Context(csp, alg), mKeySize(size), mSeed(NULL), mSalt(NULL), mParams(NULL)
29{
30}
31
32void
33GenerateKey::database(const Db &inDb)
34{
35 mDb = inDb;
36 if (mDb && isActive())
37 set(CSSM_ATTRIBUTE_DL_DB_HANDLE, mDb->handle());
38}
39
40void GenerateKey::activate()
41{
313fa17b 42 StLock<Mutex> _(mActivateMutex);
b1ab9ed8
A
43 if (!mActive)
44 {
45 check(CSSM_CSP_CreateKeyGenContext(attachment()->handle(), mAlgorithm,
46 mKeySize, mSeed, mSalt, NULL, NULL, mParams, &mHandle));
47 // Must be done before calling set() since is does nothing unless we are active.
48 // Also we are technically active even if set() throws since we already created a context.
49 mActive = true;
50 if (mDb)
51 set(CSSM_ATTRIBUTE_DL_DB_HANDLE, mDb->handle());
52 }
53}
54
55Key GenerateKey::operator () (const KeySpec &spec)
56{
57 Key key;
58
59 check(CSSM_GenerateKey(handle(), spec.usage, spec.attributes, spec.label,
60 &compositeRcc(), key.makeNewKey(attachment())));
61
62 key->activate();
63
64 return key;
65}
66
67void GenerateKey::operator () (CssmKey &key, const KeySpec &spec)
68{
69 check(CSSM_GenerateKey(handle(), spec.usage, spec.attributes, spec.label, &compositeRcc(), &key));
70
71}
72
73void GenerateKey::operator () (Key &publicKey, const KeySpec &pubSpec,
74 Key &privateKey, const KeySpec &privSpec)
75{
76 check(CSSM_GenerateKeyPair(handle(),
77 pubSpec.usage, pubSpec.attributes,
78 pubSpec.label, publicKey.makeNewKey(attachment()),
79 privSpec.usage, privSpec.attributes,
80 privSpec.label, &compositeRcc(), privateKey.makeNewKey(attachment())));
81
82 publicKey->activate();
83 privateKey->activate();
84
85}
86
87void GenerateKey::operator () (CssmKey &publicKey, const KeySpec &pubSpec,
88 CssmKey &privateKey, const KeySpec &privSpec)
89{
90 check(CSSM_GenerateKeyPair(handle(),
91 pubSpec.usage, pubSpec.attributes, pubSpec.label, &publicKey,
92 privSpec.usage, privSpec.attributes, privSpec.label, &compositeRcc(), &privateKey));
93}
94