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 typedef uint8 EncryptionKey
[24];
91 typedef uint8 SigningKey
[20];
93 EncryptionKey encryptionKey
; // master encryption key
94 SigningKey signingKey
; // master signing key
96 // private ACL blob follows, to the end
97 void *privateAclBlob() { return at(sizeof(PrivateBlob
)); }
101 // position separators between variable-length fields (see below)
102 uint32 startCryptoBlob
; // end of public ACL; start of crypto blob
103 uint32 totalLength
; // end of crypto blob; end of entire blob
105 Signature randomSignature
; // randomizing database signature
106 uint32 sequence
; // database sequence number
107 DBParameters params
; // database settable parameters
109 uint8 salt
[20]; // derivation salt
110 uint8 iv
[8]; // encryption iv
112 uint8 blobSignature
[20]; // HMAC/SHA1 of entire blob except itself
114 // variable length fields:
115 void *publicAclBlob() { return at(sizeof(DbBlob
)); }
116 size_t publicAclBlobLength() const
117 { return startCryptoBlob
- sizeof(DbBlob
); }
119 void *cryptoBlob() { return at(startCryptoBlob
); }
120 size_t cryptoBlobLength() const { return totalLength
- startCryptoBlob
; }
122 uint32
length() const { return totalLength
; }
124 DbBlob
*copy(CssmAllocator
&alloc
= CssmAllocator::standard()) const
126 DbBlob
*blob
= alloc
.malloc
<DbBlob
>(length());
127 memcpy(blob
, this, length());
136 class KeyBlob
: public CommonBlob
{
138 uint32 startCryptoBlob
; // end of public ACL; start of crypto blob
139 uint32 totalLength
; // end of crypto blob; end of entire blob
141 uint8 iv
[8]; // encryption iv
143 CssmKey::Header header
; // key header as-is
144 struct WrappedFields
{
145 CSSM_KEYBLOB_TYPE blobType
;
146 CSSM_KEYBLOB_FORMAT blobFormat
;
147 CSSM_ALGORITHMS wrapAlgorithm
;
148 CSSM_ENCRYPT_MODE wrapMode
;
151 uint8 blobSignature
[20]; // HMAC/SHA1 of entire blob except itself
153 // variable length fields:
154 void *publicAclBlob() { return at(sizeof(KeyBlob
)); }
155 size_t publicAclBlobLength() const
156 { return startCryptoBlob
- sizeof(KeyBlob
); }
158 void *cryptoBlob() { return at(startCryptoBlob
); }
159 size_t cryptoBlobLength() const { return totalLength
- startCryptoBlob
; }
161 uint32
length() const { return totalLength
; }
163 // these bits are managed internally by the SecurityServer (and not passed to the CSPs)
164 static const uint32 managedAttributes
=
165 CSSM_KEYATTR_ALWAYS_SENSITIVE
|
166 CSSM_KEYATTR_NEVER_EXTRACTABLE
|
167 CSSM_KEYATTR_PERMANENT
;
170 KeyBlob
*copy(CssmAllocator
&alloc
) const
172 KeyBlob
*blob
= alloc
.malloc
<KeyBlob
>(length());
173 memcpy(blob
, this, length());
179 } // end namespace SecurityServer
181 } // end namespace Security