2 * Copyright (c) 2002-2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 // Identity.cpp - Working with Identities
27 #include <security_keychain/Identity.h>
29 #include <security_cdsa_utilities/KeySchema.h>
30 #include <security_keychain/KCCursor.h>
32 using namespace KeychainCore
;
34 Identity::Identity(const SecPointer
<KeyItem
> &privateKey
,
35 const SecPointer
<Certificate
> &certificate
) :
36 mPrivateKey(privateKey
),
37 mCertificate(certificate
)
41 Identity::Identity(const StorageManager::KeychainList
&keychains
, const SecPointer
<Certificate
> &certificate
) :
42 mCertificate(certificate
)
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());
49 if (!keyCursor
->next(key
))
50 MacOSError::throwMe(errSecItemNotFound
);
52 SecPointer
<KeyItem
> keyItem(static_cast<KeyItem
*>(&*key
));
53 mPrivateKey
= keyItem
;
56 Identity::~Identity() throw()
61 Identity::privateKey() const
66 SecPointer
<Certificate
>
67 Identity::certificate() const
73 Identity::operator < (const Identity
&other
) const
75 // Certificates in different keychains are considered equal if data is equal
76 return (mCertificate
< other
.mCertificate
);
80 Identity::operator == (const Identity
&other
) const
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
);
89 Identity::equal(SecCFObject
&other
)
91 return (*this) == (const Identity
&)other
;