2 * Copyright (c) 2000-2004,2006,2011,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 // PODWrapper for CssmKey and related types
31 #include <security_utilities/utilities.h>
32 #include <security_cdsa_utilities/cssmpods.h>
33 #include <security_cdsa_utilities/cssmerrors.h>
34 #include <Security/cssm.h>
41 // User-friendlier CSSM_KEY objects
43 class CssmKey
: public PodWrapper
<CssmKey
, CSSM_KEY
> {
45 CssmKey() { clearPod(); KeyHeader
.HeaderVersion
= CSSM_KEYHEADER_VERSION
; }
46 // all of the following constructors take over ownership of the key data
47 CssmKey(const CSSM_KEY
&key
);
48 CssmKey(const CSSM_DATA
&keyData
);
49 CssmKey(uint32 length
, void *data
);
52 class Header
: public PodWrapper
<Header
, CSSM_KEYHEADER
> {
54 // access to components of the key header
55 CSSM_KEYBLOB_TYPE
blobType() const { return BlobType
; }
56 void blobType(CSSM_KEYBLOB_TYPE blobType
) { BlobType
= blobType
; }
58 CSSM_KEYBLOB_FORMAT
blobFormat() const { return Format
; }
59 void blobFormat(CSSM_KEYBLOB_FORMAT blobFormat
) { Format
= blobFormat
; }
61 CSSM_KEYCLASS
keyClass() const { return KeyClass
; }
62 void keyClass(CSSM_KEYCLASS keyClass
) { KeyClass
= keyClass
; }
64 CSSM_KEY_TYPE
algorithm() const { return AlgorithmId
; }
65 void algorithm(CSSM_KEY_TYPE algorithm
) { AlgorithmId
= algorithm
; }
67 CSSM_KEY_TYPE
wrapAlgorithm() const { return WrapAlgorithmId
; }
68 void wrapAlgorithm(CSSM_KEY_TYPE wrapAlgorithm
) { WrapAlgorithmId
= wrapAlgorithm
; }
70 CSSM_ENCRYPT_MODE
wrapMode() const { return WrapMode
; }
71 void wrapMode(CSSM_ENCRYPT_MODE mode
) { WrapMode
= mode
; }
73 bool isWrapped() const { return WrapAlgorithmId
!= CSSM_ALGID_NONE
; }
75 const Guid
&cspGuid() const { return Guid::overlay(CspId
); }
76 void cspGuid(const Guid
&guid
) { Guid::overlay(CspId
) = guid
; }
78 uint32
attributes() const { return KeyAttr
; }
79 bool attribute(uint32 attr
) const { return KeyAttr
& attr
; }
80 void setAttribute(uint32 attr
) { KeyAttr
|= attr
; }
81 void clearAttribute(uint32 attr
) { KeyAttr
&= ~attr
; }
83 uint32
usage() const { return KeyUsage
; }
84 bool useFor(uint32 u
) const { return KeyUsage
& u
; }
86 void usage(uint32 u
) { KeyUsage
|= u
; }
87 void clearUsage(uint32 u
) { KeyUsage
&= ~u
; }
91 // access to the key header
92 Header
&header() { return Header::overlay(KeyHeader
); }
93 const Header
&header() const { return Header::overlay(KeyHeader
); }
95 CSSM_KEYBLOB_TYPE
blobType() const { return header().blobType(); }
96 void blobType(CSSM_KEYBLOB_TYPE blobType
) { header().blobType(blobType
); }
98 CSSM_KEYBLOB_FORMAT
blobFormat() const { return header().blobFormat(); }
99 void blobFormat(CSSM_KEYBLOB_FORMAT blobFormat
) { header().blobFormat(blobFormat
); }
101 CSSM_KEYCLASS
keyClass() const { return header().keyClass(); }
102 void keyClass(CSSM_KEYCLASS keyClass
) { header().keyClass(keyClass
); }
104 CSSM_KEY_TYPE
algorithm() const { return header().algorithm(); }
105 void algorithm(CSSM_KEY_TYPE algorithm
) { header().algorithm(algorithm
); }
107 CSSM_KEY_TYPE
wrapAlgorithm() const { return header().wrapAlgorithm(); }
108 void wrapAlgorithm(CSSM_KEY_TYPE wrapAlgorithm
) { header().wrapAlgorithm(wrapAlgorithm
); }
110 CSSM_ENCRYPT_MODE
wrapMode() const { return header().wrapMode(); }
111 void wrapMode(CSSM_ENCRYPT_MODE mode
) { header().wrapMode(mode
); }
113 bool isWrapped() const { return header().isWrapped(); }
114 const Guid
&cspGuid() const { return header().cspGuid(); }
116 uint32
attributes() const { return header().attributes(); }
117 bool attribute(uint32 a
) const { return header().attribute(a
); }
118 void setAttribute(uint32 attr
) { header().setAttribute(attr
); }
119 void clearAttribute(uint32 attr
) { header().clearAttribute(attr
); }
121 uint32
usage() const { return header().usage(); }
122 bool useFor(uint32 u
) const { return header().useFor(u
); }
124 void usage(uint32 u
) { header().usage(u
); }
125 void clearUsage(uint32 u
) { header().clearUsage(u
); }
128 // access to the key data
129 size_t length() const { return KeyData
.Length
; }
130 void *data() const { return KeyData
.Data
; }
131 operator void * () const { return data(); }
132 CssmData
&keyData() { return CssmData::overlay(KeyData
); }
133 const CssmData
&keyData() const { return CssmData::overlay(KeyData
); }
134 operator CssmData
& () { return keyData(); }
135 operator const CssmData
& () const { return keyData(); }
136 operator bool () const { return KeyData
.Data
!= NULL
; }
137 void operator = (const CssmData
&data
) { KeyData
= data
; }
142 // Wrapped keys are currently identically structured to normal keys.
143 // But perhaps in the future...
145 typedef CssmKey CssmWrappedKey
;
148 } // end namespace Security
151 #endif //_H_CSSMUTILITIES