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