2 * Copyright (c) 2000-2001,2004,2011-2012,2014 Apple 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@
29 #include "PrimaryKey.h"
31 using namespace KeychainCore
;
32 using namespace CssmClient
;
35 PrimaryKeyImpl::PrimaryKeyImpl(const CSSM_DATA
&data
)
36 : CssmDataContainer(data
.Data
, data
.Length
), mMutex(Mutex::recursive
)
39 //@@@ do bounds checking here, throw if invalid
43 PrimaryKeyImpl::PrimaryKeyImpl(const DbAttributes
&primaryKeyAttrs
) : mMutex(Mutex::recursive
)
45 Length
= sizeof(uint32
);
46 for (uint32 ix
= 0; ix
< primaryKeyAttrs
.size(); ++ix
)
48 if (primaryKeyAttrs
.at(ix
).size() == 0)
49 MacOSError::throwMe(errSecInvalidKeychain
);
51 Length
+= sizeof(uint32
) + primaryKeyAttrs
.at(ix
).Value
[0].Length
;
54 // Careful with exceptions
55 Data
= mAllocator
.alloc
<uint8
>((UInt32
)Length
);
58 putUInt32(p
, primaryKeyAttrs
.recordType());
59 for (uint32 ix
= 0; ix
< primaryKeyAttrs
.size(); ++ix
)
61 UInt32 len
= (UInt32
)primaryKeyAttrs
.at(ix
).Value
[0].Length
;
63 memcpy(p
, primaryKeyAttrs
.at(ix
).Value
[0].Data
, len
);
69 PrimaryKeyImpl::createCursor(const Keychain
&keychain
)
71 StLock
<Mutex
>_(mMutex
);
72 DbCursor
cursor(keychain
->database());
74 // @@@ Set up cursor to find item with this.
76 uint32 left
= (uint32
)Length
;
77 if (left
< sizeof(*p
))
78 MacOSError::throwMe(errSecNoSuchAttr
); // XXX Not really but whatever.
80 CSSM_DB_RECORDTYPE rt
= getUInt32(p
, left
);
81 const CssmAutoDbRecordAttributeInfo
&infos
= keychain
->primaryKeyInfosFor(rt
);
83 cursor
->recordType(rt
);
84 cursor
->conjunctive(CSSM_DB_AND
);
85 for (uint32 ix
= 0; ix
< infos
.size(); ++ix
)
87 uint32 len
= getUInt32(p
, left
);
90 MacOSError::throwMe(errSecNoSuchAttr
); // XXX Not really but whatever.
92 CssmData
value(p
, len
);
96 cursor
->add(CSSM_DB_EQUAL
, infos
.at(ix
), value
);
104 PrimaryKeyImpl::putUInt32(uint8
*&p
, uint32 value
)
106 *p
++ = (value
>> 24);
107 *p
++ = (value
>> 16) & 0xff;
108 *p
++ = (value
>> 8) & 0xff;
113 PrimaryKeyImpl::getUInt32(uint8
*&p
, uint32
&left
) const
115 if (left
< sizeof(uint32
))
116 MacOSError::throwMe(errSecNoSuchAttr
); // XXX Not really but whatever.
119 // @@@ Assumes data written in big endian.
120 uint32 value
= (p
[0] << 24) + (p
[1] << 16) + (p
[2] << 8) + p
[3];
122 left
-= sizeof(uint32
);
129 PrimaryKeyImpl::recordType() const
132 uint32 length
= (uint32
)Length
;
133 return getUInt32(data
, length
);