2 * Copyright (c) 2002 Apple Computer, 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.
19 // TrustStore.h - Abstract interface to permanent user trust assignments
21 #include <Security/TrustItem.h>
22 #include <Security/Schema.h>
23 #include <Security/SecCFTypes.h>
27 namespace KeychainCore
{
31 // Construct a UserTrustItem from attributes and initial content
33 UserTrustItem::UserTrustItem(Certificate
*cert
, Policy
*policy
, const TrustData
&trustData
) :
34 ItemImpl(CSSM_DL_DB_RECORD_USER_TRUST
,
35 reinterpret_cast<SecKeychainAttributeList
*>(NULL
),
36 UInt32(sizeof(trustData
)),
37 reinterpret_cast<const void *>(&trustData
)),
38 mCertificate(cert
), mPolicy(policy
)
40 debug("usertrust", "create %p (%p,%p) = %d", this, cert
, policy
, trustData
.trust
);
47 UserTrustItem::~UserTrustItem()
49 debug("usertrust", "destroy %p", this);
54 // Retrieve the trust value from a UserTrustItem
56 UserTrustItem::TrustData
UserTrustItem::trust()
58 CssmDataContainer data
;
60 if (data
.length() != sizeof(TrustData
))
61 MacOSError::throwMe(errSecInvalidTrustSetting
);
62 return *data
.interpretedAs
<TrustData
>();
67 // Add item to keychain
69 PrimaryKey
UserTrustItem::add(Keychain
&keychain
)
71 // If we already have a Keychain we can't be added.
73 MacOSError::throwMe(errSecDuplicateItem
);
77 CSSM_DB_RECORDTYPE recordType
= mDbAttributes
->recordType();
79 Db
db(keychain
->database());
80 // add the item to the (regular) db
83 mUniqueId
= db
->insert(recordType
, mDbAttributes
.get(), mData
.get());
84 debug("usertrust", "%p inserted", this);
86 catch (const CssmError
&e
)
88 if (e
.cssmError() != CSSMERR_DL_INVALID_RECORDTYPE
)
91 // Create the cert relation and try again.
92 debug("usertrust", "adding schema relation for user trusts");
93 db
->createRelation(CSSM_DL_DB_RECORD_USER_TRUST
, "CSSM_DL_DB_RECORD_USER_TRUST",
94 Schema::UserTrustSchemaAttributeCount
,
95 Schema::UserTrustSchemaAttributeList
,
96 Schema::UserTrustSchemaIndexCount
,
97 Schema::UserTrustSchemaIndexList
);
99 mUniqueId
= db
->insert(recordType
, mDbAttributes
.get(), mData
.get());
100 debug("usertrust", "%p inserted now", this);
103 mPrimaryKey
= keychain
->makePrimaryKey(recordType
, mUniqueId
);
104 mKeychain
= keychain
;
110 void UserTrustItem::populateAttributes()
112 const CssmData
&certData
= mCertificate
->data();
113 const CssmOid
&policyOid
= mPolicy
->oid();
114 mDbAttributes
->add(Schema::attributeInfo(kSecTrustCertAttr
), certData
);
115 mDbAttributes
->add(Schema::attributeInfo(kSecTrustPolicyAttr
), policyOid
);
119 } // end namespace KeychainCore
120 } // end namespace Security