2  * Copyright (c) 2000-2001,2011-2012,2014 Apple 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() 
  42     StLock
<Mutex
> _(mActivateMutex
); 
  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. 
  51                         set(CSSM_ATTRIBUTE_DL_DB_HANDLE
, mDb
->handle()); 
  55 Key 
GenerateKey::operator () (const KeySpec 
&spec
) 
  59         check(CSSM_GenerateKey(handle(), spec
.usage
, spec
.attributes
, spec
.label
, 
  60                    &compositeRcc(), key
.makeNewKey(attachment()))); 
  67 void GenerateKey::operator () (CssmKey 
&key
, const KeySpec 
&spec
) 
  69         check(CSSM_GenerateKey(handle(), spec
.usage
, spec
.attributes
, spec
.label
, &compositeRcc(), &key
)); 
  73 void GenerateKey::operator () (Key 
&publicKey
, const KeySpec 
&pubSpec
, 
  74                 Key 
&privateKey
, const KeySpec 
&privSpec
) 
  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()))); 
  82         publicKey
->activate(); 
  83         privateKey
->activate(); 
  87 void GenerateKey::operator () (CssmKey 
&publicKey
, const KeySpec 
&pubSpec
, 
  88                 CssmKey 
&privateKey
, const KeySpec 
&privSpec
) 
  90         check(CSSM_GenerateKeyPair(handle(), 
  91                 pubSpec
.usage
, pubSpec
.attributes
, pubSpec
.label
, &publicKey
, 
  92                 privSpec
.usage
, privSpec
.attributes
, privSpec
.label
, &compositeRcc(), &privateKey
));