]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/lib/PrimaryKey.cpp
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / lib / PrimaryKey.cpp
1 /*
2 * Copyright (c) 2000-2001,2004,2011-2012,2014 Apple 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 //
26 // PrimaryKey.cpp
27 //
28
29 #include "PrimaryKey.h"
30
31 using namespace KeychainCore;
32 using namespace CssmClient;
33
34
35 PrimaryKeyImpl::PrimaryKeyImpl(const CSSM_DATA &data)
36 : CssmDataContainer(data.Data, data.Length), mMutex(Mutex::recursive)
37 {
38
39 //@@@ do bounds checking here, throw if invalid
40
41 }
42
43 PrimaryKeyImpl::PrimaryKeyImpl(const DbAttributes &primaryKeyAttrs) : mMutex(Mutex::recursive)
44 {
45 Length = sizeof(uint32);
46 for (uint32 ix = 0; ix < primaryKeyAttrs.size(); ++ix)
47 {
48 if (primaryKeyAttrs.at(ix).size() == 0)
49 MacOSError::throwMe(errSecInvalidKeychain);
50
51 Length += sizeof(uint32) + primaryKeyAttrs.at(ix).Value[0].Length;
52 }
53
54 // Careful with exceptions
55 Data = mAllocator.alloc<uint8>((UInt32)Length);
56 uint8 *p = Data;
57
58 putUInt32(p, primaryKeyAttrs.recordType());
59 for (uint32 ix = 0; ix < primaryKeyAttrs.size(); ++ix)
60 {
61 UInt32 len = (UInt32)primaryKeyAttrs.at(ix).Value[0].Length;
62 putUInt32(p, len);
63 memcpy(p, primaryKeyAttrs.at(ix).Value[0].Data, len);
64 p += len;
65 }
66 }
67
68 CssmClient::DbCursor
69 PrimaryKeyImpl::createCursor(const Keychain &keychain)
70 {
71 StLock<Mutex>_(mMutex);
72 DbCursor cursor(keychain->database());
73
74 // @@@ Set up cursor to find item with this.
75 uint8 *p = Data;
76 uint32 left = (uint32)Length;
77 if (left < sizeof(*p))
78 MacOSError::throwMe(errSecNoSuchAttr); // XXX Not really but whatever.
79
80 CSSM_DB_RECORDTYPE rt = getUInt32(p, left);
81 const CssmAutoDbRecordAttributeInfo &infos = keychain->primaryKeyInfosFor(rt);
82
83 cursor->recordType(rt);
84 cursor->conjunctive(CSSM_DB_AND);
85 for (uint32 ix = 0; ix < infos.size(); ++ix)
86 {
87 uint32 len = getUInt32(p, left);
88
89 if (left < len)
90 MacOSError::throwMe(errSecNoSuchAttr); // XXX Not really but whatever.
91
92 CssmData value(p, len);
93 left -= len;
94 p += len;
95
96 cursor->add(CSSM_DB_EQUAL, infos.at(ix), value);
97 }
98
99 return cursor;
100 }
101
102
103 void
104 PrimaryKeyImpl::putUInt32(uint8 *&p, uint32 value)
105 {
106 *p++ = (value >> 24);
107 *p++ = (value >> 16) & 0xff;
108 *p++ = (value >> 8) & 0xff;
109 *p++ = value & 0xff;
110 }
111
112 uint32
113 PrimaryKeyImpl::getUInt32(uint8 *&p, uint32 &left) const
114 {
115 if (left < sizeof(uint32))
116 MacOSError::throwMe(errSecNoSuchAttr); // XXX Not really but whatever.
117
118
119 // @@@ Assumes data written in big endian.
120 uint32 value = (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
121 p += sizeof(uint32);
122 left -= sizeof(uint32);
123 return value;
124 }
125
126
127
128 CSSM_DB_RECORDTYPE
129 PrimaryKeyImpl::recordType() const
130 {
131 uint8 *data = Data;
132 uint32 length = (uint32)Length;
133 return getUInt32(data, length);
134 }