2 * Copyright (c) 2000-2001 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.
20 // ssblob - objects to represent persistent blobs used by SecurityServer
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>
35 namespace SecurityServer
{
37 using LowLevelMemoryUtilities::increment
;
42 // Note that Blob and its subclasses are meant to be Byte Order Corrected.
43 // Make sure all non-byte fields are Endian<> qualified.
47 typedef Endian
<uint32
> uint32e
;
48 typedef Endian
<sint32
> sint32e
;
52 T
*at(off_t offset
) { return LowLevelMemoryUtilities::increment
<T
>(this, offset
); }
53 void *at(off_t offset
) { return LowLevelMemoryUtilities::increment(this, offset
); }
58 // The common features of our blobs
60 class CommonBlob
: public Blob
{
62 // initial fixed fields for versioning
63 uint32e magic
; // magic number
64 uint32e blobVersion
; // version code
65 uint32
version() const { return blobVersion
; }
67 static const uint32 magicNumber
= 0xfade0711;
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
;
74 void initialize(uint32 version
= currentVersion
);
76 void validate(CSSM_RETURN failureCode
) const;
78 void *data() { return at(0); }
85 class DbBlob
: public CommonBlob
{
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; }
96 struct PrivateBlob
: public Blob
{
97 typedef uint8 EncryptionKey
[24];
98 typedef uint8 SigningKey
[20];
100 EncryptionKey encryptionKey
; // master encryption key
101 SigningKey signingKey
; // master signing key
103 // private ACL blob follows, to the end
104 void *privateAclBlob() { return at(sizeof(PrivateBlob
)); }
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
112 Signature randomSignature
; // randomizing database signature
113 uint32e sequence
; // database sequence number
114 DBParameters params
; // database settable parameters
116 uint8 salt
[20]; // derivation salt
117 uint8 iv
[8]; // encryption iv
119 uint8 blobSignature
[20]; // HMAC/SHA1 of entire blob except itself
121 // variable length fields:
122 void *publicAclBlob() { return at(sizeof(DbBlob
)); }
123 size_t publicAclBlobLength() const
124 { return startCryptoBlob
- sizeof(DbBlob
); }
126 void *cryptoBlob() { return at(startCryptoBlob
); }
127 size_t cryptoBlobLength() const { return totalLength
- startCryptoBlob
; }
129 uint32
length() const { return totalLength
; }
131 DbBlob
*copy(CssmAllocator
&alloc
= CssmAllocator::standard()) const
133 DbBlob
*blob
= alloc
.malloc
<DbBlob
>(length());
134 memcpy(blob
, this, length());
143 class KeyBlob
: public CommonBlob
{
145 uint32e startCryptoBlob
; // end of public ACL; start of crypto blob
146 uint32e totalLength
; // end of crypto blob; end of entire blob
148 uint8 iv
[8]; // encryption iv
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
;
158 uint8 blobSignature
[20]; // HMAC/SHA1 of entire blob except itself
160 // variable length fields:
161 void *publicAclBlob() { return at(sizeof(KeyBlob
)); }
162 size_t publicAclBlobLength() const
163 { return startCryptoBlob
- sizeof(KeyBlob
); }
165 void *cryptoBlob() { return at(startCryptoBlob
); }
166 size_t cryptoBlobLength() const { return totalLength
- startCryptoBlob
; }
168 uint32
length() const { return totalLength
; }
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
;
180 KeyBlob
*copy(CssmAllocator
&alloc
) const
182 KeyBlob
*blob
= alloc
.malloc
<KeyBlob
>(length());
183 memcpy(blob
, this, length());
190 // An auto-unlock record (database identity plus raw unlock key)
192 class UnlockBlob
: public CommonBlob
{
194 typedef uint8 MasterKey
[24];
195 MasterKey masterKey
; // raw bits (triple-DES) - make your own CssmKey
196 DbBlob::Signature signature
; // signature is index
200 } // end namespace SecurityServer
201 } // end namespace Security