1/*
2 * Copyright (c) 2000-2004,2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// PODWrapper for CssmKey and related types
27//
28#ifndef _H_CSSMKEY
29#define _H_CSSMKEY
30
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>
35
36
37namespace Security {
38
39
40//
41// User-friendlier CSSM_KEY objects
42//
43class CssmKey : public PodWrapper<CssmKey, CSSM_KEY> {
44public:
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);
50
51public:
52    class Header : public PodWrapper<Header, CSSM_KEYHEADER> {
53    public:
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; }
57
58		CSSM_KEYBLOB_FORMAT blobFormat() const { return Format; }
59		void blobFormat(CSSM_KEYBLOB_FORMAT blobFormat) { Format = blobFormat; }
60
61		CSSM_KEYCLASS keyClass() const { return KeyClass; }
62		void keyClass(CSSM_KEYCLASS keyClass) { KeyClass = keyClass; }
63
64		CSSM_KEY_TYPE algorithm() const { return AlgorithmId; }
65		void algorithm(CSSM_KEY_TYPE algorithm) { AlgorithmId = algorithm; }
66
67		CSSM_KEY_TYPE wrapAlgorithm() const { return WrapAlgorithmId; }
68		void wrapAlgorithm(CSSM_KEY_TYPE wrapAlgorithm) { WrapAlgorithmId = wrapAlgorithm; }
69
70        CSSM_ENCRYPT_MODE wrapMode() const { return WrapMode; }
71        void wrapMode(CSSM_ENCRYPT_MODE mode) { WrapMode = mode; }
72
73		bool isWrapped() const { return WrapAlgorithmId != CSSM_ALGID_NONE; }
74
75		const Guid &cspGuid() const { return Guid::overlay(CspId); }
76		void cspGuid(const Guid &guid) { Guid::overlay(CspId) = guid; }
77
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; }
82
83		uint32 usage() const { return KeyUsage; }
84		bool useFor(uint32 u) const { return KeyUsage & u; }
85
86		void usage(uint32 u) { KeyUsage |= u; }
87		void clearUsage(uint32 u) { KeyUsage &= ~u; }
88
89    };
90
91	// access to the key header
92	Header &header() { return Header::overlay(KeyHeader); }
93	const Header &header() const { return Header::overlay(KeyHeader); }
94
95	CSSM_KEYBLOB_TYPE blobType() const	{ return header().blobType(); }
96	void blobType(CSSM_KEYBLOB_TYPE blobType) { header().blobType(blobType); }
97
98	CSSM_KEYBLOB_FORMAT blobFormat() const { return header().blobFormat(); }
99	void blobFormat(CSSM_KEYBLOB_FORMAT blobFormat) { header().blobFormat(blobFormat); }
100
101	CSSM_KEYCLASS keyClass() const		{ return header().keyClass(); }
102	void keyClass(CSSM_KEYCLASS keyClass) { header().keyClass(keyClass); }
103
104	CSSM_KEY_TYPE algorithm() const		{ return header().algorithm(); }
105	void algorithm(CSSM_KEY_TYPE algorithm) { header().algorithm(algorithm); }
106
107	CSSM_KEY_TYPE wrapAlgorithm() const	{ return header().wrapAlgorithm(); }
108	void wrapAlgorithm(CSSM_KEY_TYPE wrapAlgorithm) { header().wrapAlgorithm(wrapAlgorithm); }
109
110    CSSM_ENCRYPT_MODE wrapMode() const	{ return header().wrapMode(); }
111    void wrapMode(CSSM_ENCRYPT_MODE mode) { header().wrapMode(mode); }
112
113	bool isWrapped() const				{ return header().isWrapped(); }
114	const Guid &cspGuid() const			{ return header().cspGuid(); }
115
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); }
120
121	uint32 usage() const				{ return header().usage(); }
122	bool useFor(uint32 u) const			{ return header().useFor(u); }
123
124	void usage(uint32 u) { header().usage(u); }
125	void clearUsage(uint32 u) { header().clearUsage(u); }
126
127public:
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; }
138};
139
140
141//
142// Wrapped keys are currently identically structured to normal keys.
143// But perhaps in the future...
144//
145typedef CssmKey CssmWrappedKey;
146
147
148} // end namespace Security
149
150
151#endif //_H_CSSMUTILITIES
152