999447d9a1d6d15afbb8d3c3dc4b3170afea8e8f
4 * @brief Generation of X.509 attribute certificates.
9 * Copyright (C) 2002 Ueli Galizzi, Ariane Seiler
10 * Copyright (C) 2004,2007 Andreas Steffen
11 * Hochschule fuer Technik Rapperswil, Switzerland
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
38 #include <asn1/asn1.h>
40 #include <credentials/certificates/x509.h>
41 #include <credentials/certificates/ac.h>
42 #include <utils/optionsfrom.h>
45 #include <fips/fips.h>
46 #include <fips_signature.h>
47 #endif /* INTEGRITY_TEST */
49 #define OPENAC_PATH IPSEC_CONFDIR "/openac"
50 #define OPENAC_SERIAL IPSEC_CONFDIR "/openac/serial"
52 #define DEFAULT_VALIDITY 24*3600 /* seconds */
55 * @brief prints the usage of the program to the stderr
57 static void usage(const char *message
)
59 if (message
!= NULL
&& *message
!= '\0')
61 fprintf(stderr
, "%s\n", message
);
63 fprintf(stderr
, "Usage: openac"
66 " [--optionsfrom <filename>]"
69 " [--debug <level 0..4>]"
74 " [--startdate <YYYYMMDDHHMMSSZ>]"
75 " [--enddate <YYYYMMDDHHMMSSZ>]"
79 " [--password <password>]"
81 " --usercert <certfile>"
82 " --groups <attr1,attr2,..>"
90 * convert a chunk into a multi-precision integer
92 static void chunk_to_mpz(chunk_t chunk
, mpz_t number
)
94 mpz_import(number
, chunk
.len
, 1, 1, 1, 0, chunk
.ptr
);
98 * convert a multi-precision integer into a chunk
100 static chunk_t
mpz_to_chunk(mpz_t number
)
104 chunk
.len
= 1 + mpz_sizeinbase(number
, 2)/BITS_PER_BYTE
;
105 chunk
.ptr
= mpz_export(NULL
, NULL
, 1, chunk
.len
, 1, 0, number
);
106 if (chunk
.ptr
== NULL
)
114 * read the last serial number from file
116 static chunk_t
read_serial(void)
120 char buf
[BUF_LEN
], buf1
[BUF_LEN
];
121 chunk_t hex_serial
= { buf
, BUF_LEN
};
122 chunk_t last_serial
= { buf1
, BUF_LEN
};
125 FILE *fd
= fopen(OPENAC_SERIAL
, "r");
127 /* last serial number defaults to 0 */
128 *last_serial
.ptr
= 0x00;
133 if (fscanf(fd
, "%s", hex_serial
.ptr
))
135 hex_serial
.len
= strlen(hex_serial
.ptr
);
136 last_serial
= chunk_from_hex(hex_serial
, last_serial
.ptr
);
142 DBG1(" file '%s' does not exist yet - serial number set to 01", OPENAC_SERIAL
);
146 * conversion of read serial number to a multiprecision integer
147 * and incrementing it by one
148 * and representing it as a two's complement octet string
151 chunk_to_mpz(last_serial
, number
);
152 mpz_add_ui(number
, number
, 0x01);
153 serial
= mpz_to_chunk(number
);
160 * write back the last serial number to file
162 static void write_serial(chunk_t serial
)
164 FILE *fd
= fopen(OPENAC_SERIAL
, "w");
170 DBG1(" serial number is %#B", &serial
);
171 hex_serial
= chunk_to_hex(serial
, NULL
, FALSE
);
172 fprintf(fd
, "%.*s\n", hex_serial
.len
, hex_serial
.ptr
);
174 free(hex_serial
.ptr
);
178 DBG1(" could not open file '%s' for writing", OPENAC_SERIAL
);
183 * Load and parse a private key file
185 static private_key_t
* private_key_create_from_file(char *path
, chunk_t
*secret
)
188 chunk_t chunk
= chunk_empty
;
189 private_key_t
*key
= NULL
;
191 if (!pem_asn1_load_file(path
, secret
, &chunk
, &pgp
))
193 DBG1(" could not load private key file '%s'", path
);
196 key
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_RSA
,
197 BUILD_BLOB_ASN1_DER
, chunk
, BUILD_END
);
201 DBG1(" could not parse loaded private key file '%s'", path
);
204 DBG1(" loaded private key file '%s'", path
);
209 * global variables accessible by both main() and build.c
212 static int debug_level
= 1;
213 static bool stderr_quiet
= FALSE
;
216 * openac dbg function
218 static void openac_dbg(int level
, char *fmt
, ...)
220 int priority
= LOG_INFO
;
223 if (level
<= debug_level
)
228 vfprintf(stderr
, fmt
, args
);
229 fprintf(stderr
, "\n");
231 vsyslog(priority
, fmt
, args
);
237 * @brief openac main program
239 * @param argc number of arguments
240 * @param argv pointer to the argument values
242 int main(int argc
, char **argv
)
244 certificate_t
*attr_cert
= NULL
;
245 certificate_t
*userCert
= NULL
;
246 certificate_t
*signerCert
= NULL
;
247 private_key_t
*signerKey
= NULL
;
249 time_t notBefore
= UNDEFINED_TIME
;
250 time_t notAfter
= UNDEFINED_TIME
;
253 char *keyfile
= NULL
;
254 char *certfile
= NULL
;
255 char *usercertfile
= NULL
;
256 char *outfile
= NULL
;
260 chunk_t passphrase
= { buf
, 0 };
261 chunk_t serial
= chunk_empty
;
262 chunk_t attr_chunk
= chunk_empty
;
266 /* enable openac debugging hook */
269 passphrase
.ptr
[0] = '\0';
271 openlog("openac", 0, LOG_AUTHPRIV
);
273 /* initialize library */
274 library_init(STRONGSWAN_CONF
);
275 lib
->plugins
->load(lib
->plugins
, IPSEC_PLUGINDIR
,
276 lib
->settings
->get_str(lib
->settings
, "openac.load", PLUGINS
));
278 /* initialize optionsfrom */
279 options_t
*options
= options_create();
281 /* handle arguments */
284 static const struct option long_opts
[] = {
285 /* name, has_arg, flag, val */
286 { "help", no_argument
, NULL
, 'h' },
287 { "version", no_argument
, NULL
, 'v' },
288 { "optionsfrom", required_argument
, NULL
, '+' },
289 { "quiet", no_argument
, NULL
, 'q' },
290 { "cert", required_argument
, NULL
, 'c' },
291 { "key", required_argument
, NULL
, 'k' },
292 { "password", required_argument
, NULL
, 'p' },
293 { "usercert", required_argument
, NULL
, 'u' },
294 { "groups", required_argument
, NULL
, 'g' },
295 { "days", required_argument
, NULL
, 'D' },
296 { "hours", required_argument
, NULL
, 'H' },
297 { "startdate", required_argument
, NULL
, 'S' },
298 { "enddate", required_argument
, NULL
, 'E' },
299 { "out", required_argument
, NULL
, 'o' },
300 { "debug", required_argument
, NULL
, 'd' },
304 int c
= getopt_long(argc
, argv
, "hv+:qc:k:p;u:g:D:H:S:E:o:d:", long_opts
, NULL
);
306 /* Note: "breaking" from case terminates loop */
309 case EOF
: /* end of flags */
312 case 0: /* long option already handled */
315 case ':': /* diagnostic already printed by getopt_long */
316 case '?': /* diagnostic already printed by getopt_long */
317 case 'h': /* --help */
322 case 'v': /* --version */
323 printf("openac (strongSwan %s)\n", VERSION
);
327 case '+': /* --optionsfrom <filename> */
331 if (*optarg
== '/') /* absolute pathname */
333 strncpy(path
, optarg
, BUF_LEN
);
335 else /* relative pathname */
337 snprintf(path
, BUF_LEN
, "%s/%s", OPENAC_PATH
, optarg
);
339 if (!options
->from(options
, path
, &argc
, &argv
, optind
))
347 case 'q': /* --quiet */
351 case 'c': /* --cert */
355 case 'k': /* --key */
359 case 'p': /* --key */
360 if (strlen(optarg
) > BUF_LEN
)
362 usage("passphrase too long");
365 strncpy(passphrase
.ptr
, optarg
, BUF_LEN
);
366 passphrase
.len
= min(strlen(optarg
), BUF_LEN
);
369 case 'u': /* --usercert */
370 usercertfile
= optarg
;
373 case 'g': /* --groups */
377 case 'D': /* --days */
378 if (optarg
== NULL
|| !isdigit(optarg
[0]))
380 usage("missing number of days");
386 long days
= strtol(optarg
, &endptr
, 0);
388 if (*endptr
!= '\0' || endptr
== optarg
|| days
<= 0)
390 usage("<days> must be a positive number");
393 validity
+= 24*3600*days
;
397 case 'H': /* --hours */
398 if (optarg
== NULL
|| !isdigit(optarg
[0]))
400 usage("missing number of hours");
406 long hours
= strtol(optarg
, &endptr
, 0);
408 if (*endptr
!= '\0' || endptr
== optarg
|| hours
<= 0)
410 usage("<hours> must be a positive number");
413 validity
+= 3600*hours
;
417 case 'S': /* --startdate */
418 if (optarg
== NULL
|| strlen(optarg
) != 15 || optarg
[14] != 'Z')
420 usage("date format must be YYYYMMDDHHMMSSZ");
425 chunk_t date
= { optarg
, 15 };
427 notBefore
= asn1_to_time(&date
, ASN1_GENERALIZEDTIME
);
431 case 'E': /* --enddate */
432 if (optarg
== NULL
|| strlen(optarg
) != 15 || optarg
[14] != 'Z')
434 usage("date format must be YYYYMMDDHHMMSSZ");
439 chunk_t date
= { optarg
, 15 };
440 notAfter
= asn1_to_time(&date
, ASN1_GENERALIZEDTIME
);
444 case 'o': /* --out */
448 case 'd': /* --debug */
449 debug_level
= atoi(optarg
);
457 /* break from loop */
463 usage("unexpected argument");
467 DBG1("starting openac (strongSwan Version %s)", VERSION
);
469 #ifdef INTEGRITY_TEST
470 DBG1("integrity test of libstrongswan code");
471 if (fips_verify_hmac_signature(hmac_key
, hmac_signature
))
473 DBG1(" integrity test passed");
477 DBG1(" integrity test failed");
481 #endif /* INTEGRITY_TEST */
483 /* load the signer's RSA private key */
486 signerKey
= private_key_create_from_file(keyfile
, &passphrase
);
488 if (signerKey
== NULL
)
494 /* load the signer's X.509 certificate */
495 if (certfile
!= NULL
)
497 signerCert
= lib
->creds
->create(lib
->creds
,
498 CRED_CERTIFICATE
, CERT_X509
,
499 BUILD_FROM_FILE
, certfile
,
502 if (signerCert
== NULL
)
508 /* load the users's X.509 certificate */
509 if (usercertfile
!= NULL
)
511 userCert
= lib
->creds
->create(lib
->creds
,
512 CRED_CERTIFICATE
, CERT_X509
,
513 BUILD_FROM_FILE
, usercertfile
,
516 if (userCert
== NULL
)
522 /* compute validity interval */
523 validity
= (validity
)? validity
: DEFAULT_VALIDITY
;
524 notBefore
= (notBefore
== UNDEFINED_TIME
) ?
time(NULL
) : notBefore
;
525 notAfter
= (notAfter
== UNDEFINED_TIME
) ?
time(NULL
) + validity
: notAfter
;
527 /* build and parse attribute certificate */
528 if (userCert
!= NULL
&& signerCert
!= NULL
&& signerKey
!= NULL
)
530 /* read the serial number and increment it by one */
531 serial
= read_serial();
533 attr_cert
= lib
->creds
->create(lib
->creds
,
534 CRED_CERTIFICATE
, CERT_X509_AC
,
535 BUILD_CERT
, userCert
,
536 BUILD_NOT_BEFORE_TIME
, notBefore
,
537 BUILD_NOT_AFTER_TIME
, notAfter
,
538 BUILD_SERIAL
, serial
,
539 BUILD_IETF_GROUP_ATTR
, groups
,
540 BUILD_SIGNING_CERT
, signerCert
,
541 BUILD_SIGNING_KEY
, signerKey
,
549 /* write the attribute certificate to file */
550 attr_chunk
= attr_cert
->get_encoding(attr_cert
);
551 if (chunk_write(attr_chunk
, outfile
, 0022, TRUE
))
553 DBG1(" wrote attribute cert file '%s' (%u bytes)", outfile
, attr_chunk
.len
);
554 write_serial(serial
);
560 usage("some of the mandatory parameters --usercert --cert --key "
565 /* delete all dynamically allocated objects */
566 DESTROY_IF(signerKey
);
567 DESTROY_IF(signerCert
);
568 DESTROY_IF(userCert
);
569 DESTROY_IF(attr_cert
);
570 free(attr_chunk
.ptr
);
574 options
->destroy(options
);