Security-163.tar.gz
[apple/security.git] / Keychain / Identity.cpp
index 1f5b1652c00e030859da6251c4d8dc7c0683db9a..46d6d9baabc1123c8881e754c428f571aaca5159 100644 (file)
 //
 #include <Security/Identity.h>
 
+#include <Security/KeySchema.h>
+#include <Security/KCCursor.h>
+
 using namespace KeychainCore;
 
-Identity::Identity(const RefPointer<KeyItem> &privateKey,
-               const RefPointer<Certificate> &certificate) :
+Identity::Identity(const SecPointer<KeyItem> &privateKey,
+               const SecPointer<Certificate> &certificate) :
        mPrivateKey(privateKey),
        mCertificate(certificate)
 {
 }
 
-Identity::~Identity()
+Identity::Identity(const StorageManager::KeychainList &keychains, const SecPointer<Certificate> &certificate) :
+       mCertificate(certificate)
+{
+       // Find a key whose label matches the publicKeyHash of the public key in the certificate.
+       KCCursor keyCursor(keychains, CSSM_DL_DB_RECORD_PRIVATE_KEY, NULL);
+       keyCursor->add(CSSM_DB_EQUAL, KeySchema::Label, certificate->publicKeyHash());
+
+       Item key;
+       if (!keyCursor->next(key))
+               MacOSError::throwMe(errSecItemNotFound);
+
+       SecPointer<KeyItem> keyItem(static_cast<KeyItem *>(&*key));
+       mPrivateKey = keyItem;
+}
+
+Identity::~Identity() throw()
 {
 }
 
-RefPointer<KeyItem>
+SecPointer<KeyItem>
 Identity::privateKey() const
 {
        return mPrivateKey;
 }
 
-RefPointer<Certificate>
+SecPointer<Certificate>
 Identity::certificate() const
 {
        return mCertificate;