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 key and database blobs to 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>
36 using LowLevelMemoryUtilities::increment
;
38 namespace SecurityServer
47 T
*at(off_t offset
) { return LowLevelMemoryUtilities::increment
<T
>(this, offset
); }
48 void *at(off_t offset
) { return LowLevelMemoryUtilities::increment(this, offset
); }
53 // The common features of our blobs
55 class CommonBlob
: public Blob
{
57 // initial fixed fields for versioning
58 uint32 magic
; // magic number
59 uint32 version
; // version code
61 static const uint32 magicNumber
= 0xfade0711;
63 static const uint32 version_MacOS_10_0
= 0x00000100; // MacOS 10.0.x
64 static const uint32 version_MacOS_10_1
= 0x00000101; // MacOS 10.1.x and on
65 static const uint32 currentVersion
= version_MacOS_10_0
;
68 void initialize(uint32 version
= currentVersion
);
69 void validate(CSSM_RETURN failureCode
) const;
71 void *data() { return at(0); }
78 class DbBlob
: public CommonBlob
{
83 bool operator < (const Signature
&sig
) const
84 { return memcmp(bytes
, sig
.bytes
, sizeof(bytes
)) < 0; }
85 bool operator == (const Signature
&sig
) const
86 { return memcmp(bytes
, sig
.bytes
, sizeof(bytes
)) == 0; }
89 struct PrivateBlob
: public Blob
{
90 uint8 encryptionKey
[24]; // master encryption key
91 uint8 signingKey
[20]; // master signing key
93 // private ACL blob follows, to the end
94 void *privateAclBlob() { return at(sizeof(PrivateBlob
)); }
98 // position separators between variable-length fields (see below)
99 uint32 startCryptoBlob
; // end of public ACL; start of crypto blob
100 uint32 totalLength
; // end of crypto blob; end of entire blob
102 Signature randomSignature
; // randomizing database signature
103 uint32 sequence
; // database sequence number
104 DBParameters params
; // database settable parameters
106 uint8 salt
[20]; // derivation salt
107 uint8 iv
[8]; // encryption iv
109 uint8 blobSignature
[20]; // HMAC/SHA1 of entire blob except itself
111 // variable length fields:
112 void *publicAclBlob() { return at(sizeof(DbBlob
)); }
113 size_t publicAclBlobLength() const
114 { return startCryptoBlob
- sizeof(DbBlob
); }
116 void *cryptoBlob() { return at(startCryptoBlob
); }
117 size_t cryptoBlobLength() const { return totalLength
- startCryptoBlob
; }
119 uint32
length() const { return totalLength
; }
121 DbBlob
*copy(CssmAllocator
&alloc
= CssmAllocator::standard()) const
123 DbBlob
*blob
= alloc
.malloc
<DbBlob
>(length());
124 memcpy(blob
, this, length());
133 class KeyBlob
: public CommonBlob
{
135 uint32 startCryptoBlob
; // end of public ACL; start of crypto blob
136 uint32 totalLength
; // end of crypto blob; end of entire blob
138 uint8 iv
[8]; // encryption iv
140 CssmKey::Header header
; // key header as-is
141 struct WrappedFields
{
142 CSSM_KEYBLOB_TYPE blobType
;
143 CSSM_KEYBLOB_FORMAT blobFormat
;
144 CSSM_ALGORITHMS wrapAlgorithm
;
145 CSSM_ENCRYPT_MODE wrapMode
;
148 uint8 blobSignature
[20]; // HMAC/SHA1 of entire blob except itself
150 // variable length fields:
151 void *publicAclBlob() { return at(sizeof(KeyBlob
)); }
152 size_t publicAclBlobLength() const
153 { return startCryptoBlob
- sizeof(KeyBlob
); }
155 void *cryptoBlob() { return at(startCryptoBlob
); }
156 size_t cryptoBlobLength() const { return totalLength
- startCryptoBlob
; }
158 uint32
length() const { return totalLength
; }
160 // these bits are managed internally by the SecurityServer (and not passed to the CSPs)
161 static const uint32 managedAttributes
=
162 CSSM_KEYATTR_ALWAYS_SENSITIVE
|
163 CSSM_KEYATTR_NEVER_EXTRACTABLE
|
164 CSSM_KEYATTR_PERMANENT
;
167 KeyBlob
*copy(CssmAllocator
&alloc
) const
169 KeyBlob
*blob
= alloc
.malloc
<KeyBlob
>(length());
170 memcpy(blob
, this, length());
176 } // end namespace SecurityServer
178 } // end namespace Security