-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpkicore_openssl_csr.c
339 lines (296 loc) · 7.66 KB
/
pkicore_openssl_csr.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* CSR Generation routines - OpenSSL specific
*
* Copyright (c) 2011 Nick Kossifidis <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <stdio.h>
#include "pkicore.h"
#include "pkicore_openssl.h"
#include "pkicore_openssl_extensions.h"
/**
* pki_ossl_generate_pkey - Generates a private key
*
* Tries to generate a private key based on the given
* configuration.
*
* @struct pki_config *conf - The configuration from above
*
* returns: An EVP_PKEY structure or NULL + ret code
*/
static EVP_PKEY*
pki_ossl_generate_pkey(struct pki_config *conf)
{
EVP_PKEY *pkey = NULL;
RSA *rsa = NULL;
DSA *dsa = NULL;
int ret = PKI_OK;
#if defined(OPENSSL_NO_RSA) && defined(OPENSSL_NO_DSA)
#error OpenSSL compiled without RSA or DSA support !!!
#endif
/* TODO: Handle /dev/random properly */
RAND_load_file("/dev/urandom", 2048);
pkey = EVP_PKEY_new();
if (!pkey) {
pki_msg(0,"CSR",
"Unable to allocate a new private key !\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
switch (conf->akey_type){
case PKI_KEY_RSA:
#ifndef OPENSSL_NO_RSA
rsa = RSA_generate_key(conf->key_bits, RSA_F4,
NULL, NULL);
if(!rsa) {
pki_msg(0,"CSR",
"Unable to generate RSA key !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
ret = EVP_PKEY_assign(pkey, EVP_PKEY_RSA, rsa);
if(!ret) {
pki_msg(0,"CSR",
"Unable to initialize private key !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* Set to NULL so that we don't free it
* on cleanup */
rsa = NULL;
/* Done */
pki_msg(2,"CSR",
"Generated RSA key (%i bits)\n",
conf->key_bits);
#endif
break;
case PKI_KEY_DSA:
#ifndef OPENSSL_NO_DSA
dsa = DSA_new();
if(!dsa) {
pki_msg(0,"CSR",
"Unable to allocate a DSA struct !\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
ret = DSA_generate_parameters_ex(dsa,conf->key_bits,
NULL,0,NULL,NULL, NULL);
if(!ret) {
pki_msg(0,"CSR",
"Unable to generate DSA parameters !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
ret = DSA_generate_key(dsa);
if(!ret) {
pki_msg(0,"CSR",
"Unable to generate DSA key !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
ret = EVP_PKEY_assign(pkey, EVP_PKEY_DSA, dsa);
if(!ret) {
pki_msg(0,"CSR",
"Unable to initialize private key !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* Set to NULL so that we don't free it
* on cleanup */
dsa = NULL;
/* Done */
pki_msg(2,"CSR",
"Generated DSA key (%i bits)\n",
conf->key_bits);
#endif
break;
}
ret = PKI_OK;
cleanup:
if(rsa)
RSA_free(rsa);
if(dsa)
DSA_free(dsa);
if(ret) {
if(pkey)
EVP_PKEY_free(pkey);
pki_set_ret_code(ret);
}
return pkey;
}
/**
* pki_ossl_create_csr - Generates a Certificate Signing Request
*
* Tries to generate a Certificate Signing Request (CSR) based
* on the given config from above.
*
* @struct pki_cmd *cmd - The command structure from above
*
* returns: One of pki_error_codes
*/
int
pki_ossl_create_csr(struct pki_cmd *cmd)
{
X509_REQ *csr = NULL;
EVP_PKEY *pkey = NULL;
const EVP_MD *digest = NULL;
X509_NAME *dn = NULL;
struct pki_config *conf = cmd->conf;
struct pki_dn *dn_conf = conf->dn;
int ret = PKI_OK;
openssl_init();
/* Allocate a new structure */
csr = X509_REQ_new();
if (!csr) {
pki_msg(0,"CSR",
"Unable to allocate a new CSR structure!\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
/* Generate public/private key pair */
pkey = pki_ossl_generate_pkey(conf);
if(!pkey) {
pki_msg(0,"CSR",
"Unable to generate pkey for CSR !\n");
ret = pki_get_ret_code();
goto cleanup;
}
/* Put public key on CSR */
ret = X509_REQ_set_pubkey(csr, pkey);
if(!ret) {
pki_msg(0,"CSR",
"Unable to initialize public key !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* Create subject DN */
dn = X509_REQ_get_subject_name(csr);
/* An extra check to be safe */
ret = pki_check_dn(dn_conf);
if (ret) {
pki_msg(0,"CSR",
"Invalid input on dn field(s)\n");
goto cleanup;
}
if(!X509_REQ_set_version(csr, 0L)) {
pki_msg(0,"CSR",
"Unable to set CSR version !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
if(dn_conf->country)
X509_NAME_add_entry_by_txt(dn,"C",
MBSTRING_ASC, dn_conf->country,
-1, -1, 0);
if(dn_conf->state_province)
X509_NAME_add_entry_by_txt(dn,"ST",
MBSTRING_ASC, dn_conf->state_province,
-1, -1, 0);
if(dn_conf->locality)
X509_NAME_add_entry_by_txt(dn,"L",
MBSTRING_ASC, dn_conf->locality,
-1, -1, 0);
if(dn_conf->organization)
X509_NAME_add_entry_by_txt(dn,"O",
MBSTRING_ASC, dn_conf->organization,
-1, -1, 0);
if(dn_conf->organizational_unit)
X509_NAME_add_entry_by_txt(dn,"OU",
MBSTRING_ASC, dn_conf->organizational_unit,
-1, -1, 0);
X509_NAME_add_entry_by_txt(dn,"CN",
MBSTRING_ASC, dn_conf->common_name,
-1, -1, 0);
X509_NAME_add_entry_by_txt(dn,"emailAddress",
MBSTRING_ASC, dn_conf->email,
-1, -1, 0);
/* Add challenge pass if provided */
if(conf->challenge_pass)
X509_REQ_add1_attr_by_NID(csr, NID_pkcs9_challengePassword,
V_ASN1_UTF8STRING,
conf->challenge_pass,
conf->challenge_pass_len);
/* Add extensions */
ret = pki_ossl_add_csr_extensions(cmd, csr);
if (ret) {
pki_msg(0,"CSR",
"Unable to add extensions !\n");
goto cleanup;
}
if (EVP_PKEY_type(pkey->type) == EVP_PKEY_RSA)
digest = EVP_sha1();
else if (EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA)
digest = EVP_dss1();
ret = X509_REQ_sign(csr, pkey, digest);
if (!ret) {
pki_msg(0,"CSR",
"Unable to sign request\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
if(pki_get_debug_mask() & PKI_DBG_PRINT_DATA)
X509_REQ_print_fp(pki_get_debug_fp(), csr);
/* If this CSR is used to generate a self-signed
* certificate, just pass the raw structures to
* cmd->result and cmd->result_key so that we
* don't convert them again and waste resources */
if(cmd->flags & PKI_CMD_GEN_SELFSIGNED) {
cmd->result = (unsigned char *) csr;
cmd->result_key = (unsigned char *) pkey;
} else {
/* We are done, store private key to a
* PKCS#8 structure and DER encode the CSR */
ret = i2d_X509_REQ(csr, &cmd->result);
if (!ret) {
pki_msg(0,"CSR",
"Unable to export csr !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
cmd->result_len = ret;
ret = pki_ossl_pkey2pkcs8(cmd, pkey);
if(ret) {
pki_msg(0, "CSR",
"Unable to save PKCS#8 structure !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* It's freed so set it to NULL or it'll get
* freed again on cleanup */
pkey = NULL;
}
ret = PKI_OK;
/* Done */
pki_msg(2,"CSR",
"Generated CSR for %s\n",
X509_NAME_oneline(csr->req_info->subject,
NULL, PKI_MAX_DN_FIELD_LEN));
cleanup:
if(!(cmd->flags & PKI_CMD_GEN_SELFSIGNED)) {
if(pkey)
EVP_PKEY_free(pkey);
if(csr)
X509_REQ_free(csr);
/*
* Don't call openssl_exit() here
* when generating a self-signed
* certificate because we 'll keep
* using OpenSSL stuff later.
* We 'll call it on pki_ossl_sign_csr
*/
openssl_exit();
}
return ret;
}