]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurityd/lib/ssblob.h
Security-57337.60.2.tar.gz
[apple/security.git] / OSX / libsecurityd / lib / ssblob.h
1 /*
2 * Copyright (c) 2000-2006,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 // ssblob - objects to represent persistent blobs used by SecurityServer
27 //
28 #ifndef _H_SSBLOB
29 #define _H_SSBLOB
30
31 #include <securityd_client/ssclient.h>
32 #include <Security/cssm.h>
33 #include <security_utilities/utilities.h>
34 #include <security_cdsa_utilities/cssmacl.h>
35 #include <security_utilities/memutils.h>
36 #include <security_utilities/endian.h>
37
38
39 namespace Security {
40 namespace SecurityServer {
41
42 using LowLevelMemoryUtilities::increment;
43
44
45 //
46 // A generic blob.
47 // Note that Blob and its subclasses are meant to be Byte Order Corrected.
48 // Make sure all non-byte fields are Endian<> qualified.
49 //
50 class Blob {
51 public:
52 typedef Endian<uint32> uint32e;
53 typedef Endian<sint32> sint32e;
54
55 protected:
56 template <class T>
57 T *at(off_t offset) { return LowLevelMemoryUtilities::increment<T>(this, offset); }
58 void *at(off_t offset) { return LowLevelMemoryUtilities::increment(this, (ptrdiff_t)offset); }
59
60 template <class T>
61 const T *at(off_t offset) const { return LowLevelMemoryUtilities::increment<T>(this, offset); }
62 const void *at(off_t offset) const { return LowLevelMemoryUtilities::increment(this, (ptrdiff_t)offset); }
63 };
64
65
66 //
67 // The common features of our blobs
68 //
69 class CommonBlob : public Blob {
70 public:
71 // initial fixed fields for versioning
72 uint32e magic; // magic number
73 uint32e blobVersion; // version code
74 uint32 version() const { return blobVersion; }
75
76 static const uint32 magicNumber = 0xfade0711;
77
78 static const uint32 version_MacOS_10_0 = 0x00000100; // MacOS 10.0.x
79 static const uint32 version_MacOS_10_1 = 0x00000101; // MacOS 10.1.x and on
80 static const uint32 version_partition = 0x00000200; // MacOS 10.11.2 and on, supporting partitioning
81 static const uint32 currentVersion = version_partition;
82
83 static uint32 getCurrentVersion();
84 public:
85 void initialize();
86 void initialize(uint32 version);
87 bool isValid() const;
88 void validate(CSSM_RETURN failureCode) const;
89
90 void *data() { return at(0); }
91 const void *data() const { return at(0); }
92 };
93
94
95 //
96 // A Database blob
97 //
98 class DbBlob : public CommonBlob {
99 public:
100 struct Signature {
101 uint8 bytes[16];
102
103 bool operator < (const Signature &sig) const
104 { return memcmp(bytes, sig.bytes, sizeof(bytes)) < 0; }
105 bool operator == (const Signature &sig) const
106 { return memcmp(bytes, sig.bytes, sizeof(bytes)) == 0; }
107 };
108
109 struct PrivateBlob : public Blob {
110 typedef uint8 EncryptionKey[24];
111 typedef uint8 SigningKey[20];
112
113 EncryptionKey encryptionKey; // master encryption key
114 SigningKey signingKey; // master signing key
115
116 // private ACL blob follows, to the end
117 void *privateAclBlob() { return at(sizeof(PrivateBlob)); }
118 };
119
120 public:
121 // position separators between variable-length fields (see below)
122 uint32e startCryptoBlob; // end of public ACL; start of crypto blob
123 uint32e totalLength; // end of crypto blob; end of entire blob
124
125 Signature randomSignature; // randomizing database signature
126 uint32e sequence; // database sequence number
127 DBParameters params; // database settable parameters
128
129 uint8 salt[20]; // derivation salt
130 uint8 iv[8]; // encryption iv
131
132 uint8 blobSignature[20]; // HMAC/SHA1 of entire blob except itself
133
134 // variable length fields:
135 void *publicAclBlob() { return at(sizeof(DbBlob)); }
136 const void *publicAclBlob() const { return at(sizeof(DbBlob)); }
137 size_t publicAclBlobLength() const
138 { return startCryptoBlob - sizeof(DbBlob); }
139
140 void *cryptoBlob() { return at(startCryptoBlob); }
141 const void *cryptoBlob() const { return at(startCryptoBlob); }
142 size_t cryptoBlobLength() const { return totalLength - startCryptoBlob; }
143
144 uint32 length() const { return totalLength; }
145
146 DbBlob *copy(Allocator &alloc = Allocator::standard()) const
147 {
148 DbBlob *blob = alloc.malloc<DbBlob>(length());
149 memcpy(blob, this, length());
150 return blob;
151 }
152 };
153
154
155 //
156 // A key blob
157 //
158 class KeyBlob : public CommonBlob {
159 public:
160 uint32e startCryptoBlob; // end of public ACL; start of crypto blob
161 uint32e totalLength; // end of crypto blob; end of entire blob
162
163 uint8 iv[8]; // encryption iv
164
165 CssmKey::Header header; // key header as-is
166 struct WrappedFields {
167 Endian<CSSM_KEYBLOB_TYPE> blobType;
168 Endian<CSSM_KEYBLOB_FORMAT> blobFormat;
169 Endian<CSSM_ALGORITHMS> wrapAlgorithm;
170 Endian<CSSM_ENCRYPT_MODE> wrapMode;
171 } wrappedHeader;
172
173 uint8 blobSignature[20]; // HMAC/SHA1 of entire blob except itself
174
175 // variable length fields:
176 void *publicAclBlob() { return at(sizeof(KeyBlob)); }
177 size_t publicAclBlobLength() const
178 { return startCryptoBlob - sizeof(KeyBlob); }
179
180 void *cryptoBlob() { return at(startCryptoBlob); }
181 size_t cryptoBlobLength() const { return totalLength - startCryptoBlob; }
182
183 uint32 length() const { return totalLength; }
184
185 // these bits are managed internally by the SecurityServer (and not passed to the CSPs)
186 static const uint32 managedAttributes =
187 CSSM_KEYATTR_ALWAYS_SENSITIVE |
188 CSSM_KEYATTR_NEVER_EXTRACTABLE |
189 CSSM_KEYATTR_PERMANENT |
190 CSSM_KEYATTR_SENSITIVE |
191 CSSM_KEYATTR_EXTRACTABLE;
192 static const uint32 forcedAttributes =
193 CSSM_KEYATTR_EXTRACTABLE;
194
195 /*
196 * Public Key blobs can be stored unencrypted. A unique blobSignature
197 * is used to indicate this state.
198 */
199 bool isClearText();
200 void setClearTextSignature();
201
202 public:
203 KeyBlob *copy(Allocator &alloc) const
204 {
205 KeyBlob *blob = alloc.malloc<KeyBlob>(length());
206 memcpy(blob, this, length());
207 return blob;
208 }
209 };
210
211
212 //
213 // An auto-unlock record (database identity plus raw unlock key)
214 //
215 class UnlockBlob : public CommonBlob {
216 public:
217 typedef uint8 MasterKey[24];
218 MasterKey masterKey; // raw bits (triple-DES) - make your own CssmKey
219 DbBlob::Signature signature; // signature is index
220 };
221
222
223 } // end namespace SecurityServer
224 } // end namespace Security
225
226
227 #endif //_H_SSBLOB