1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*-------------------------------------------------------------------------*/
/**
* \file KMSAgentPKICommon.h
*
* X.509 Certificate and Private Key Support Interface
*
* This module provides simple interfaces to support SSL communication
* for the KMS Agent enrollment protocol. Basic classes supporting
* X.509 certificates, private key management are provided and hide
* specific implementations from users of these classes.
*/
/*-------------------------------------------------------------------------*/
#ifndef K_KMSAgentPKICommon_h
#define K_KMSAgentPKICommon_h
#ifdef WIN32
#pragma warning(disable: 4786)
#endif
#define MAX_CERT_SIZE 4096
#define MAX_KEY_SIZE 4096
#define DEFAULT_KEY_SIZE 2048
#ifdef KMSUSERPKCS12
enum EnumPKIFileFormat { FILE_FORMAT_DER, FILE_FORMAT_PEM, FILE_FORMAT_PKCS12 };
#else
enum EnumPKIFileFormat { FILE_FORMAT_DER, FILE_FORMAT_PEM };
#endif
/**
* This class provides a simple interface for the management of
* public keys. Simple load and store operations are provided for
* storage and retrieval from memory buffers.
*/
class CPublicKey
{
public:
CPublicKey();
/**
* This method saves public key into a buffer,
* it also returns the actual used buffer length.
* @param i_pcBuffer Buffer to receive public key
* @param i_iBufferLength length of the buffer provided
* @param o_pActualLength actual length of the public key stored into the buffer
* @param i_iFormat key format, @see EnumPKIFileFormat
*/
bool Save(unsigned char * const i_pcBuffer,
int i_iBufferLength,
int * const o_pActualLength,
int i_iFormat);
/**
* This method loads the public key from a buffer
* @param i_pcBuffer
* @param i_iLength
* @param i_iFormat one of the enums from EnumPKIFileFormat,
* only FILE_FORMAT_PEM is supported.
* @return true for success, false otherwise
*/
bool Load (unsigned char * const i_pcBuffer,
int i_iLength,
int i_iFormat);
/**
* use this object's public key to encrypt plaintext buffer
*/
bool Encrypt (int i_iLength,
const unsigned char * const i_pcPlainText,
unsigned char * const o_pcCypherText,
int * const o_pActualLength);
~CPublicKey();
private:
void *m_pPublicKeyImpl;
};
/**
* This class provides a simple interface for the management of
* private keys. Simple load and store operations are provided for
* storage and retrieval from memory buffers.
*
*/
class CPrivateKey
{
public:
CPrivateKey();
/**
* Saves the private key to a memory buffer specified by
* i_pcBuffer. Currently just the PEM format is supported.
* Specification of a passphrase allows encryption of the private
* key subject to the choice of the implementation.
*
* @param[in] i_pcBuffer
* @param[in] i_iBufferLength
* @param[out] o_pActualLength
* @param[in] i_pPassphrase optional, if non-null the private key is
* wrapped using this passphrase
* @param[in] i_iFormat one of the enums from EnumPKIFileFormat,
* only FILE_FORMAT_PEM is supported.
* @return true for success, false otherwise
*/
bool Save( unsigned char * const i_pcBuffer,
int i_iBufferLength,
int * const o_pActualLength,
const char * const i_pPassphrase,
int i_iFormat );
/**
* This method loads the private key from a buffer
* @param i_pcBuffer
* @param i_iLength
* @param i_pPassphrase optional, if non-null the private key is
* unwrapped using this passphrase
* @param i_iFormat one of the enums from EnumPKIFileFormat,
* only FILE_FORMAT_PEM is supported.
* @return true for success, false otherwise
*/
bool Load(unsigned char * const i_pcBuffer,
int i_iLength,
const char * const i_pPassphrase,
int i_iFormat);
~CPrivateKey();
#ifdef KMSUSERPKCS12
void *GetNative();
void SetNative(void *);
#endif
private:
void *m_pPKeyImpl;
};
/**
* This class provides a simple interface for managing X.509
* certificates providing only simple load and save operations for
* storage and retrieval.
*
*/
class CCertificate
{
public:
CCertificate();
~CCertificate();
/**
* save the certificate to the specified file name. Currently,
* only FILE_FORMAT_PEM is supported.
*/
bool Save( const char * const i_pcFileName,
int i_iFormat);
/**
* save the certificate to the specified buffer. Currently, only
* FILE_FORMAT_PEM is supported.
*/
bool Save( unsigned char * const i_pcBuffer,
int i_iBufferLength,
int * const o_pActualLength,
int i_iFormat);
/**
* load a certificate from the specified filename. Currently,
* only FILE_FORMAT_PEM is supported.
*/
bool Load( const char * const i_pcFileName,
int i_iFormat );
/**
* load a certificate from the specified buffer. Currently, only
* FILE_FORMAT_PEM is supported.
*/
bool Load( unsigned char * const i_pcBuffer,
int i_iLength,
int i_iFormat );
/**
* prints the certificate to stdout
*/
bool Dump();
#ifdef KMSUSERPKCS12
bool LoadPKCS12CertAndKey(char *filename,
int i_iFormat,
CPrivateKey *i_pPrivateKey,
char *i_pPassphrase);
bool SavePKCS12(
unsigned char *i_pcBuffer,
int i_iBufferLength,
int *o_pActualLength,
CPrivateKey* i_pPrivateKey,
char* i_sPassphrase );
#endif
private:
/**
* an opague pointer to implementation specific resources to be
* freed by the Destructor.
*/
void *m_pCertImpl;
#ifdef KMSUSERPKCS12
/**
* saves certificate to PKCS#12 memory BIO
* @param i_pPrivateKey
* @param i_sPassphrase
* @return pointer to the Memory BIO
*/
void* SaveCertToPKCS12MemoryBIO(
CPrivateKey* i_pPrivateKey,
char *i_sPassphrase);
#endif
};
/**
* This class provides a method for storing an X.509 certificate and
* private key to a file. The private key is appended to the
* certificate and optionally encrypted with the specified passphrase
* for encoding and storage in PEM format.
*/
class CPKI
{
public:
CPKI();
~CPKI();
public:
/**
* exports a certificate and associated private key to the
* specified file.
* @param i_pCertificate a pointer to an instance of a certificate
* @param i_pPrivateKey a pointer to an instance of a private key
* @param i_pcFileName the name of the file to store the cert and private key
* @param i_sPassphrase optional but when provided supplies a
* pass phrase to use for encrypting the private key. The cipher
* used for encryption is determined by the underlying implementation
* which for the reference implementation uses triple DES by default.
* @param i_eFileFormat the encoding format to use for the certificate and private key
*/
bool ExportCertAndKeyToFile(
CCertificate* const i_pCertificate,
CPrivateKey* const i_pPrivateKey,
const char* const i_pcFileName,
const char* const i_sPassphrase,
EnumPKIFileFormat i_eFileFormat );
private:
int m_iKeyLength;
CCertificate *m_pCACertificate;
CPrivateKey *m_pCAPrivateKey;
};
#endif //K_KMSAgentPKICommon_h
|