1 | #include <assert.h> |
---|
2 | #include <err.h> |
---|
3 | #include <termios.h> |
---|
4 | #include <time.h> |
---|
5 | |
---|
6 | #include "libcreddy_common.h" |
---|
7 | |
---|
8 | #define KEY_SUFFIX "_private.pem" |
---|
9 | #define CERT_SUFFIX "_ID.pem" |
---|
10 | /* Size of password memory allocation */ |
---|
11 | #define PWLEN 128 |
---|
12 | |
---|
13 | // |
---|
14 | // ID object |
---|
15 | // |
---|
16 | struct _creddy_id_t { |
---|
17 | char *keyid; |
---|
18 | char *cn; |
---|
19 | certificate_t *cert; |
---|
20 | private_key_t *key; |
---|
21 | }; |
---|
22 | |
---|
23 | /* Callback configuration */ |
---|
24 | struct cb_opts { |
---|
25 | bool use_prompt; /* Print a prompt to stderr */ |
---|
26 | bool use_echo; /* If true, turn off input echo on stdin */ |
---|
27 | unsigned int tries; /* Number of attempts allowed */ |
---|
28 | char prompt[20]; /* The prompt to display if use_echo is true */ |
---|
29 | }; |
---|
30 | |
---|
31 | static char *_get_keyid(certificate_t *cert); |
---|
32 | static chunk_t _passphrase_callback(void *user, int try); |
---|
33 | static private_key_t *_generate_key(void); |
---|
34 | static certificate_t *_generate_cert(private_key_t *private, char *cn, int validity); |
---|
35 | static void _encode_base64(FILE *out, chunk_t encoding); |
---|
36 | |
---|
37 | /** |
---|
38 | * Load an ID cert from a file. |
---|
39 | */ |
---|
40 | creddy_id_t *creddy_id_from_file(char *filename) { |
---|
41 | creddy_init(); |
---|
42 | |
---|
43 | certificate_t *cert = lib->creds->create(lib->creds, |
---|
44 | CRED_CERTIFICATE, CERT_X509, |
---|
45 | BUILD_FROM_FILE, filename, |
---|
46 | BUILD_X509_FLAG, X509_AA, |
---|
47 | BUILD_END |
---|
48 | ); |
---|
49 | |
---|
50 | if (cert == NULL) |
---|
51 | return NULL; |
---|
52 | |
---|
53 | creddy_id_t *id = creddy_xmalloc(sizeof(creddy_id_t)); |
---|
54 | id->keyid = NULL; |
---|
55 | id->cn = NULL; |
---|
56 | id->cert = cert; |
---|
57 | id->key = NULL; |
---|
58 | |
---|
59 | id->keyid = _get_keyid(id->cert); |
---|
60 | |
---|
61 | // TODO get CN |
---|
62 | |
---|
63 | return id; |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | /** |
---|
68 | * Load private key for a cert. |
---|
69 | */ |
---|
70 | int creddy_id_load_privkey(creddy_id_t *id, char *filename) { |
---|
71 | struct cb_opts c_opts = { 1, 0, 3, "Key password:" }; |
---|
72 | |
---|
73 | assert(id != NULL); |
---|
74 | |
---|
75 | creddy_init(); |
---|
76 | |
---|
77 | // load signer key |
---|
78 | private_key_t *key = lib->creds->create(lib->creds, |
---|
79 | CRED_PRIVATE_KEY, KEY_RSA, |
---|
80 | BUILD_FROM_FILE, filename, |
---|
81 | /* Ask for password if the key's encrypted */ |
---|
82 | BUILD_PASSPHRASE_CALLBACK, _passphrase_callback, &c_opts, |
---|
83 | BUILD_END |
---|
84 | ); |
---|
85 | if (key == NULL) |
---|
86 | return 0; |
---|
87 | |
---|
88 | id->key = key; |
---|
89 | return 1; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Generate an ID with the specified CN and validity. |
---|
94 | */ |
---|
95 | int creddy_id_generate(creddy_id_t **ret, char *cn, int validity) { |
---|
96 | if (cn == NULL || !creddy_clean_name(cn)) |
---|
97 | return CREDDY_GENERATE_INVALID_CN; |
---|
98 | |
---|
99 | if (validity < 0) |
---|
100 | return CREDDY_GENERATE_INVALID_VALIDITY; |
---|
101 | |
---|
102 | creddy_id_t *id = creddy_xmalloc(sizeof(creddy_id_t)); |
---|
103 | |
---|
104 | id->cn = creddy_xstrdup(cn); |
---|
105 | id->key = _generate_key(); |
---|
106 | id->cert = _generate_cert(id->key, cn, validity); |
---|
107 | id->keyid = _get_keyid(id->cert); |
---|
108 | |
---|
109 | *ret = id; |
---|
110 | return CREDDY_SUCCESS; |
---|
111 | } |
---|
112 | |
---|
113 | char *creddy_id_keyid(creddy_id_t *id) { |
---|
114 | assert(id != NULL); |
---|
115 | |
---|
116 | return id->keyid; |
---|
117 | } |
---|
118 | |
---|
119 | certificate_t *creddy_id_cert(creddy_id_t *id) { |
---|
120 | assert(id != NULL); |
---|
121 | |
---|
122 | return id->cert; |
---|
123 | } |
---|
124 | |
---|
125 | private_key_t *creddy_id_privkey(creddy_id_t *id) { |
---|
126 | assert(id != NULL); |
---|
127 | |
---|
128 | return id->key; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Get the default filename for the cert. Value must be freed by caller. |
---|
133 | */ |
---|
134 | char *creddy_id_cert_filename(creddy_id_t *id) { |
---|
135 | assert(id != NULL); |
---|
136 | assert(id->cn != NULL); |
---|
137 | |
---|
138 | // malloc the filename |
---|
139 | int len = strlen(id->cn) + strlen(CERT_SUFFIX) + 1; |
---|
140 | char *filename = creddy_xmalloc(len); |
---|
141 | sprintf(filename, "%s" CERT_SUFFIX, id->cn); |
---|
142 | |
---|
143 | return filename; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Write the ID cert to an open file pointer. |
---|
148 | */ |
---|
149 | void creddy_id_write_cert(creddy_id_t *id, FILE *out) { |
---|
150 | assert(id != NULL); |
---|
151 | |
---|
152 | chunk_t encoding = id->cert->get_encoding(id->cert); |
---|
153 | _encode_base64(out, encoding); |
---|
154 | free(encoding.ptr); |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Default private key filename. Value must be freed by caller. |
---|
159 | */ |
---|
160 | char *creddy_id_privkey_filename(creddy_id_t *id) { |
---|
161 | assert(id != NULL); |
---|
162 | assert(id->cn != NULL); |
---|
163 | |
---|
164 | // malloc the filename |
---|
165 | int len = strlen(id->cn) + strlen(KEY_SUFFIX) + 1; |
---|
166 | char *filename = creddy_xmalloc(len); |
---|
167 | sprintf(filename, "%s" KEY_SUFFIX, id->cn); |
---|
168 | |
---|
169 | return filename; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Write the private key to a file. |
---|
174 | */ |
---|
175 | void creddy_id_write_privkey(creddy_id_t *id, FILE *out) { |
---|
176 | int ret; |
---|
177 | chunk_t encoding; |
---|
178 | |
---|
179 | assert(id != NULL); |
---|
180 | |
---|
181 | ret = id->key->get_encoding(id->key, KEY_PRIV_PEM, &encoding); |
---|
182 | if (!ret) |
---|
183 | errx(1, "Couldn't encode private key"); |
---|
184 | |
---|
185 | fwrite(encoding.ptr, encoding.len, 1, out); |
---|
186 | |
---|
187 | free(encoding.ptr); |
---|
188 | } |
---|
189 | |
---|
190 | void creddy_id_free(creddy_id_t *id) { |
---|
191 | if (id == NULL) |
---|
192 | return; |
---|
193 | |
---|
194 | DESTROY_IF(id->cert); |
---|
195 | DESTROY_IF(id->key); |
---|
196 | |
---|
197 | free(id->keyid); |
---|
198 | free(id); |
---|
199 | } |
---|
200 | |
---|
201 | // |
---|
202 | // Helper functions below |
---|
203 | // |
---|
204 | |
---|
205 | static char *_get_keyid(certificate_t *cert) { |
---|
206 | // get the keyid |
---|
207 | x509_t *x509 = (x509_t *)cert; |
---|
208 | chunk_t keyid = x509->get_subjectKeyIdentifier(x509); |
---|
209 | chunk_t string = chunk_to_hex(keyid, NULL, 0); |
---|
210 | return (char *)string.ptr; |
---|
211 | } |
---|
212 | |
---|
213 | static chunk_t _passphrase_callback(void *user, int try) { |
---|
214 | /* Get a password from stdin and return it as a chunk_t. If too many tries |
---|
215 | * have occurred or there is any other problem, return an empty chunk_t, |
---|
216 | * which libstrongswan takes as giving up. The chunk is alloated here |
---|
217 | * (inside getline), and presumably freed by libstrongswan. User points to |
---|
218 | * a cb_opts struct, which affects this routine in the obvious ways. |
---|
219 | */ |
---|
220 | /* Configuration options */ |
---|
221 | struct cb_opts *opts = (struct cb_opts *) user; |
---|
222 | chunk_t rv = chunk_empty; /* Return value, starts empty */ |
---|
223 | |
---|
224 | if (try -1 < opts->tries ) { |
---|
225 | struct termios t; /* Terminal settings */ |
---|
226 | size_t len = 0; /* Length of string from getline */ |
---|
227 | tcflag_t orig = 0; /* Holds the original local flags (echo in here) */ |
---|
228 | |
---|
229 | if (!opts->use_echo) { |
---|
230 | /* Use tc{get,set}attr to turn echo off and restore the intial |
---|
231 | * echo settings */ |
---|
232 | if (!tcgetattr(0, &t)) { |
---|
233 | orig = t.c_lflag; |
---|
234 | |
---|
235 | t.c_lflag &= ~ECHO; |
---|
236 | if ( tcsetattr(0, TCSANOW, &t) ) { |
---|
237 | perror("Cannot turn off echo"); |
---|
238 | return rv; |
---|
239 | } |
---|
240 | } |
---|
241 | else { |
---|
242 | perror("Cannot turn get attributes to off echo"); |
---|
243 | return rv; |
---|
244 | } |
---|
245 | } |
---|
246 | if (opts->use_prompt) printf("%s", opts->prompt); |
---|
247 | |
---|
248 | /* Because rv.ptr starts as NULL, getline allocates memory. The size |
---|
249 | * of the allocation returns in rv.len and the size of the string |
---|
250 | * (including newline and NUL) is in len. */ |
---|
251 | if ((rv.ptr = (u_char *) malloc(rv.len = PWLEN))) { |
---|
252 | if ( fgets(rv.ptr, rv.len, stdin) ) { |
---|
253 | /* Readjust the chunk_t's len field to the size of the string |
---|
254 | * w/o the newline or NUL */ |
---|
255 | /* would prefer strnlen, but no such luck in FBSD7 or earlier*/ |
---|
256 | size_t len = strlen(rv.ptr); |
---|
257 | |
---|
258 | if (rv.ptr[len-2] == '\n') rv.len = len-2; |
---|
259 | else rv.len = len -1; |
---|
260 | } |
---|
261 | else { |
---|
262 | /* Read failed. Deallocate and clear rv */ |
---|
263 | free(rv.ptr); |
---|
264 | rv = chunk_empty; |
---|
265 | } |
---|
266 | } |
---|
267 | else { |
---|
268 | /* Failed malloc. Restore rv to empty and return it */ |
---|
269 | perror("malloc"); |
---|
270 | rv = chunk_empty; |
---|
271 | return rv; |
---|
272 | } |
---|
273 | |
---|
274 | if (!opts->use_echo ) { |
---|
275 | /* Pop echo beck to its original setting. */ |
---|
276 | t.c_lflag = orig; |
---|
277 | |
---|
278 | if ( tcsetattr(0, TCSANOW, &t) ) |
---|
279 | perror("Cannot restore echo setting?"); |
---|
280 | |
---|
281 | if (opts->use_prompt) printf("\n"); |
---|
282 | } |
---|
283 | } |
---|
284 | else fprintf(stderr, "Too many tries (%d)", try-1); |
---|
285 | return rv; |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * Generate a private key. |
---|
290 | */ |
---|
291 | static private_key_t *_generate_key(void) { |
---|
292 | private_key_t *key; |
---|
293 | |
---|
294 | // generate the key |
---|
295 | key = lib->creds->create( |
---|
296 | lib->creds, |
---|
297 | CRED_PRIVATE_KEY, KEY_RSA, |
---|
298 | BUILD_KEY_SIZE, 2048, |
---|
299 | BUILD_END |
---|
300 | ); |
---|
301 | if (key == NULL) |
---|
302 | errx(1, "Key generation failed"); |
---|
303 | |
---|
304 | return key; |
---|
305 | } |
---|
306 | |
---|
307 | static char *_create_dn(char *cn) { |
---|
308 | |
---|
309 | #define DN "cn=" |
---|
310 | |
---|
311 | char *dn = creddy_xmalloc(sizeof(DN) + strlen(cn)); |
---|
312 | memcpy(dn, DN, sizeof(DN)); |
---|
313 | strcat(dn, cn); |
---|
314 | |
---|
315 | return dn; |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | * Generate certificate. |
---|
320 | */ |
---|
321 | static certificate_t *_generate_cert(private_key_t *private, char *cn, int validity) { |
---|
322 | // build the DN |
---|
323 | char *dn_string = _create_dn(cn); |
---|
324 | identification_t *id = identification_create_from_string(dn_string); |
---|
325 | if (id == NULL) |
---|
326 | errx(1, "couldn't create ID from DN %s", dn_string); |
---|
327 | free(dn_string); |
---|
328 | |
---|
329 | // get the public key |
---|
330 | public_key_t *public = private->get_public_key(private); |
---|
331 | if (public == NULL) |
---|
332 | errx(1, "couldn't get public key from private key"); |
---|
333 | |
---|
334 | // create a serial (stolen from strongswan pki) |
---|
335 | rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK); |
---|
336 | if (!rng) |
---|
337 | errx(1, "no random number generator"); |
---|
338 | |
---|
339 | // random serial |
---|
340 | chunk_t serial = creddy_generate_serial(); |
---|
341 | |
---|
342 | // validity period |
---|
343 | time_t not_before = time(NULL); |
---|
344 | time_t not_after = not_before + validity * 24 * 60 * 60; |
---|
345 | |
---|
346 | // create! |
---|
347 | certificate_t *cert = lib->creds->create(lib->creds, |
---|
348 | CRED_CERTIFICATE, CERT_X509, |
---|
349 | BUILD_SIGNING_KEY, private, |
---|
350 | BUILD_PUBLIC_KEY, public, |
---|
351 | BUILD_SUBJECT, id, |
---|
352 | BUILD_NOT_BEFORE_TIME, not_before, |
---|
353 | BUILD_NOT_AFTER_TIME, not_after, |
---|
354 | BUILD_SERIAL, serial, |
---|
355 | BUILD_DIGEST_ALG, HASH_SHA1, |
---|
356 | BUILD_X509_FLAG, X509_CA, |
---|
357 | BUILD_PATHLEN, X509_NO_PATH_LEN_CONSTRAINT, |
---|
358 | BUILD_END |
---|
359 | ); |
---|
360 | if (cert == NULL) |
---|
361 | errx(1, "couldn't build cert :("); |
---|
362 | |
---|
363 | DESTROY_IF(id); |
---|
364 | DESTROY_IF(public); |
---|
365 | free(serial.ptr); |
---|
366 | |
---|
367 | return cert; |
---|
368 | } |
---|
369 | |
---|
370 | #define BYTES_PER_LINE 64 |
---|
371 | |
---|
372 | // thx libstrongswan |
---|
373 | static void _encode_base64(FILE *out, chunk_t encoding) { |
---|
374 | int start; |
---|
375 | |
---|
376 | chunk_t b64 = chunk_to_base64(encoding, NULL); |
---|
377 | |
---|
378 | fprintf(out, "-----BEGIN CERTIFICATE-----\n"); |
---|
379 | |
---|
380 | for (start = 0; start < b64.len; start += BYTES_PER_LINE) { |
---|
381 | int left = b64.len - start; |
---|
382 | int len = left < BYTES_PER_LINE ? left : BYTES_PER_LINE; |
---|
383 | fwrite(b64.ptr + start, len, 1, out); |
---|
384 | fprintf(out, "\n"); |
---|
385 | } |
---|
386 | |
---|
387 | fprintf(out, "-----END CERTIFICATE-----\n"); |
---|
388 | |
---|
389 | free(b64.ptr); |
---|
390 | } |
---|