]> git.saurik.com Git - apple/security.git/blob - libsecurity_keychain/lib/Identity.cpp
Security-55179.13.tar.gz
[apple/security.git] / libsecurity_keychain / lib / Identity.cpp
1 /*
2 * Copyright (c) 2002-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 //
25 // Identity.cpp - Working with Identities
26 //
27 #include <security_keychain/Identity.h>
28
29 #include <security_cdsa_utilities/KeySchema.h>
30 #include <security_keychain/KCCursor.h>
31
32 using namespace KeychainCore;
33
34 Identity::Identity(const SecPointer<KeyItem> &privateKey,
35 const SecPointer<Certificate> &certificate) :
36 mPrivateKey(privateKey),
37 mCertificate(certificate)
38 {
39 }
40
41 Identity::Identity(const StorageManager::KeychainList &keychains, const SecPointer<Certificate> &certificate) :
42 mCertificate(certificate)
43 {
44 // Find a key whose label matches the publicKeyHash of the public key in the certificate.
45 KCCursor keyCursor(keychains, CSSM_DL_DB_RECORD_PRIVATE_KEY, NULL);
46 keyCursor->add(CSSM_DB_EQUAL, KeySchema::Label, certificate->publicKeyHash());
47
48 Item key;
49 if (!keyCursor->next(key))
50 MacOSError::throwMe(errSecItemNotFound);
51
52 SecPointer<KeyItem> keyItem(static_cast<KeyItem *>(&*key));
53 mPrivateKey = keyItem;
54 }
55
56 Identity::~Identity() throw()
57 {
58 }
59
60 SecPointer<KeyItem>
61 Identity::privateKey() const
62 {
63 return mPrivateKey;
64 }
65
66 SecPointer<Certificate>
67 Identity::certificate() const
68 {
69 return mCertificate;
70 }
71
72 bool
73 Identity::operator < (const Identity &other) const
74 {
75 // Certificates in different keychains are considered equal if data is equal
76 return (mCertificate < other.mCertificate);
77 }
78
79 bool
80 Identity::operator == (const Identity &other) const
81 {
82 // Certificates in different keychains are considered equal if data is equal;
83 // however, if their keys are in different keychains, the identities should
84 // not be considered equal (according to mb)
85 return (mCertificate == other.mCertificate && mPrivateKey == other.mPrivateKey);
86 }
87
88 bool
89 Identity::equal(SecCFObject &other)
90 {
91 return (*this) == (const Identity &)other;
92 }
93