2 * Copyright (c) 2000-2006,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@
26 // ssblob - objects to represent persistent blobs used by SecurityServer
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>
40 namespace SecurityServer
{
42 using LowLevelMemoryUtilities::increment
;
47 // Note that Blob and its subclasses are meant to be Byte Order Corrected.
48 // Make sure all non-byte fields are Endian<> qualified.
52 typedef Endian
<uint32
> uint32e
;
53 typedef Endian
<sint32
> sint32e
;
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
); }
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
); }
67 // The common features of our blobs
69 class CommonBlob
: public Blob
{
71 // initial fixed fields for versioning
72 uint32e magic
; // magic number
73 uint32e blobVersion
; // version code
74 uint32
version() const { return blobVersion
; }
76 static const uint32 magicNumber
= 0xfade0711;
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 currentVersion
= version_MacOS_10_0
;
83 void initialize(uint32 version
= currentVersion
);
85 void validate(CSSM_RETURN failureCode
) const;
87 void *data() { return at(0); }
88 const void *data() const { return at(0); }
95 class DbBlob
: public CommonBlob
{
100 bool operator < (const Signature
&sig
) const
101 { return memcmp(bytes
, sig
.bytes
, sizeof(bytes
)) < 0; }
102 bool operator == (const Signature
&sig
) const
103 { return memcmp(bytes
, sig
.bytes
, sizeof(bytes
)) == 0; }
106 struct PrivateBlob
: public Blob
{
107 typedef uint8 EncryptionKey
[24];
108 typedef uint8 SigningKey
[20];
110 EncryptionKey encryptionKey
; // master encryption key
111 SigningKey signingKey
; // master signing key
113 // private ACL blob follows, to the end
114 void *privateAclBlob() { return at(sizeof(PrivateBlob
)); }
118 // position separators between variable-length fields (see below)
119 uint32e startCryptoBlob
; // end of public ACL; start of crypto blob
120 uint32e totalLength
; // end of crypto blob; end of entire blob
122 Signature randomSignature
; // randomizing database signature
123 uint32e sequence
; // database sequence number
124 DBParameters params
; // database settable parameters
126 uint8 salt
[20]; // derivation salt
127 uint8 iv
[8]; // encryption iv
129 uint8 blobSignature
[20]; // HMAC/SHA1 of entire blob except itself
131 // variable length fields:
132 void *publicAclBlob() { return at(sizeof(DbBlob
)); }
133 const void *publicAclBlob() const { return at(sizeof(DbBlob
)); }
134 size_t publicAclBlobLength() const
135 { return startCryptoBlob
- sizeof(DbBlob
); }
137 void *cryptoBlob() { return at(startCryptoBlob
); }
138 const void *cryptoBlob() const { return at(startCryptoBlob
); }
139 size_t cryptoBlobLength() const { return totalLength
- startCryptoBlob
; }
141 uint32
length() const { return totalLength
; }
143 DbBlob
*copy(Allocator
&alloc
= Allocator::standard()) const
145 DbBlob
*blob
= alloc
.malloc
<DbBlob
>(length());
146 memcpy(blob
, this, length());
155 class KeyBlob
: public CommonBlob
{
157 uint32e startCryptoBlob
; // end of public ACL; start of crypto blob
158 uint32e totalLength
; // end of crypto blob; end of entire blob
160 uint8 iv
[8]; // encryption iv
162 CssmKey::Header header
; // key header as-is
163 struct WrappedFields
{
164 Endian
<CSSM_KEYBLOB_TYPE
> blobType
;
165 Endian
<CSSM_KEYBLOB_FORMAT
> blobFormat
;
166 Endian
<CSSM_ALGORITHMS
> wrapAlgorithm
;
167 Endian
<CSSM_ENCRYPT_MODE
> wrapMode
;
170 uint8 blobSignature
[20]; // HMAC/SHA1 of entire blob except itself
172 // variable length fields:
173 void *publicAclBlob() { return at(sizeof(KeyBlob
)); }
174 size_t publicAclBlobLength() const
175 { return startCryptoBlob
- sizeof(KeyBlob
); }
177 void *cryptoBlob() { return at(startCryptoBlob
); }
178 size_t cryptoBlobLength() const { return totalLength
- startCryptoBlob
; }
180 uint32
length() const { return totalLength
; }
182 // these bits are managed internally by the SecurityServer (and not passed to the CSPs)
183 static const uint32 managedAttributes
=
184 CSSM_KEYATTR_ALWAYS_SENSITIVE
|
185 CSSM_KEYATTR_NEVER_EXTRACTABLE
|
186 CSSM_KEYATTR_PERMANENT
|
187 CSSM_KEYATTR_SENSITIVE
|
188 CSSM_KEYATTR_EXTRACTABLE
;
189 static const uint32 forcedAttributes
=
190 CSSM_KEYATTR_EXTRACTABLE
;
193 * Public Key blobs can be stored unencrypted. A unique blobSignature
194 * is used to indicate this state.
197 void setClearTextSignature();
200 KeyBlob
*copy(Allocator
&alloc
) const
202 KeyBlob
*blob
= alloc
.malloc
<KeyBlob
>(length());
203 memcpy(blob
, this, length());
210 // An auto-unlock record (database identity plus raw unlock key)
212 class UnlockBlob
: public CommonBlob
{
214 typedef uint8 MasterKey
[24];
215 MasterKey masterKey
; // raw bits (triple-DES) - make your own CssmKey
216 DbBlob::Signature signature
; // signature is index
220 } // end namespace SecurityServer
221 } // end namespace Security