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
);
110 * read the last serial number from file
112 static chunk_t
read_serial(void)
116 char buf
[BUF_LEN
], buf1
[BUF_LEN
];
117 chunk_t hex_serial
= { buf
, BUF_LEN
};
118 chunk_t last_serial
= { buf1
, BUF_LEN
};
121 FILE *fd
= fopen(OPENAC_SERIAL
, "r");
123 /* last serial number defaults to 0 */
124 *last_serial
.ptr
= 0x00;
129 if (fscanf(fd
, "%s", hex_serial
.ptr
))
131 hex_serial
.len
= strlen(hex_serial
.ptr
);
132 last_serial
= chunk_from_hex(hex_serial
, last_serial
.ptr
);
138 DBG1(" file '%s' does not exist yet - serial number set to 01", OPENAC_SERIAL
);
142 * conversion of read serial number to a multiprecision integer
143 * and incrementing it by one
144 * and representing it as a two's complement octet string
147 chunk_to_mpz(last_serial
, number
);
148 mpz_add_ui(number
, number
, 0x01);
149 serial
= mpz_to_chunk(number
);
156 * write back the last serial number to file
158 static void write_serial(chunk_t serial
)
160 FILE *fd
= fopen(OPENAC_SERIAL
, "w");
166 DBG1(" serial number is %#B", &serial
);
167 hex_serial
= chunk_to_hex(serial
, NULL
, FALSE
);
168 fprintf(fd
, "%.*s\n", hex_serial
.len
, hex_serial
.ptr
);
170 free(hex_serial
.ptr
);
174 DBG1(" could not open file '%s' for writing", OPENAC_SERIAL
);
179 * Load and parse a private key file
181 static private_key_t
* private_key_create_from_file(char *path
, chunk_t
*secret
)
184 chunk_t chunk
= chunk_empty
;
185 private_key_t
*key
= NULL
;
187 if (!pem_asn1_load_file(path
, secret
, &chunk
, &pgp
))
189 DBG1(" could not load private key file '%s'", path
);
192 key
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_RSA
,
193 BUILD_BLOB_ASN1_DER
, chunk
, BUILD_END
);
196 DBG1(" could not parse loaded private key file '%s'", path
);
199 DBG1(" loaded private key file '%s'", path
);
204 * global variables accessible by both main() and build.c
207 static int debug_level
= 1;
208 static bool stderr_quiet
= FALSE
;
211 * openac dbg function
213 static void openac_dbg(int level
, char *fmt
, ...)
215 int priority
= LOG_INFO
;
218 if (level
<= debug_level
)
223 vfprintf(stderr
, fmt
, args
);
224 fprintf(stderr
, "\n");
226 vsyslog(priority
, fmt
, args
);
232 * @brief openac main program
234 * @param argc number of arguments
235 * @param argv pointer to the argument values
237 int main(int argc
, char **argv
)
239 certificate_t
*attr_cert
= NULL
;
240 certificate_t
*userCert
= NULL
;
241 certificate_t
*signerCert
= NULL
;
242 private_key_t
*signerKey
= NULL
;
244 time_t notBefore
= UNDEFINED_TIME
;
245 time_t notAfter
= UNDEFINED_TIME
;
248 char *keyfile
= NULL
;
249 char *certfile
= NULL
;
250 char *usercertfile
= NULL
;
251 char *outfile
= NULL
;
255 chunk_t passphrase
= { buf
, 0 };
256 chunk_t serial
= chunk_empty
;
257 chunk_t attr_chunk
= chunk_empty
;
261 /* enable openac debugging hook */
264 passphrase
.ptr
[0] = '\0';
266 openlog("openac", 0, LOG_AUTHPRIV
);
268 /* initialize library */
269 library_init(STRONGSWAN_CONF
);
270 lib
->plugins
->load(lib
->plugins
, IPSEC_PLUGINDIR
,
271 lib
->settings
->get_str(lib
->settings
, "openac.load", PLUGINS
));
273 /* initialize optionsfrom */
274 options_t
*options
= options_create();
276 /* handle arguments */
279 static const struct option long_opts
[] = {
280 /* name, has_arg, flag, val */
281 { "help", no_argument
, NULL
, 'h' },
282 { "version", no_argument
, NULL
, 'v' },
283 { "optionsfrom", required_argument
, NULL
, '+' },
284 { "quiet", no_argument
, NULL
, 'q' },
285 { "cert", required_argument
, NULL
, 'c' },
286 { "key", required_argument
, NULL
, 'k' },
287 { "password", required_argument
, NULL
, 'p' },
288 { "usercert", required_argument
, NULL
, 'u' },
289 { "groups", required_argument
, NULL
, 'g' },
290 { "days", required_argument
, NULL
, 'D' },
291 { "hours", required_argument
, NULL
, 'H' },
292 { "startdate", required_argument
, NULL
, 'S' },
293 { "enddate", required_argument
, NULL
, 'E' },
294 { "out", required_argument
, NULL
, 'o' },
295 { "debug", required_argument
, NULL
, 'd' },
299 int c
= getopt_long(argc
, argv
, "hv+:qc:k:p;u:g:D:H:S:E:o:d:", long_opts
, NULL
);
301 /* Note: "breaking" from case terminates loop */
304 case EOF
: /* end of flags */
307 case 0: /* long option already handled */
310 case ':': /* diagnostic already printed by getopt_long */
311 case '?': /* diagnostic already printed by getopt_long */
312 case 'h': /* --help */
317 case 'v': /* --version */
318 printf("openac (strongSwan %s)\n", VERSION
);
322 case '+': /* --optionsfrom <filename> */
326 if (*optarg
== '/') /* absolute pathname */
328 strncpy(path
, optarg
, BUF_LEN
);
330 else /* relative pathname */
332 snprintf(path
, BUF_LEN
, "%s/%s", OPENAC_PATH
, optarg
);
334 if (!options
->from(options
, path
, &argc
, &argv
, optind
))
342 case 'q': /* --quiet */
346 case 'c': /* --cert */
350 case 'k': /* --key */
354 case 'p': /* --key */
355 if (strlen(optarg
) > BUF_LEN
)
357 usage("passphrase too long");
360 strncpy(passphrase
.ptr
, optarg
, BUF_LEN
);
361 passphrase
.len
= min(strlen(optarg
), BUF_LEN
);
364 case 'u': /* --usercert */
365 usercertfile
= optarg
;
368 case 'g': /* --groups */
372 case 'D': /* --days */
373 if (optarg
== NULL
|| !isdigit(optarg
[0]))
375 usage("missing number of days");
381 long days
= strtol(optarg
, &endptr
, 0);
383 if (*endptr
!= '\0' || endptr
== optarg
|| days
<= 0)
385 usage("<days> must be a positive number");
388 validity
+= 24*3600*days
;
392 case 'H': /* --hours */
393 if (optarg
== NULL
|| !isdigit(optarg
[0]))
395 usage("missing number of hours");
401 long hours
= strtol(optarg
, &endptr
, 0);
403 if (*endptr
!= '\0' || endptr
== optarg
|| hours
<= 0)
405 usage("<hours> must be a positive number");
408 validity
+= 3600*hours
;
412 case 'S': /* --startdate */
413 if (optarg
== NULL
|| strlen(optarg
) != 15 || optarg
[14] != 'Z')
415 usage("date format must be YYYYMMDDHHMMSSZ");
420 chunk_t date
= { optarg
, 15 };
422 notBefore
= asn1_to_time(&date
, ASN1_GENERALIZEDTIME
);
426 case 'E': /* --enddate */
427 if (optarg
== NULL
|| strlen(optarg
) != 15 || optarg
[14] != 'Z')
429 usage("date format must be YYYYMMDDHHMMSSZ");
434 chunk_t date
= { optarg
, 15 };
435 notAfter
= asn1_to_time(&date
, ASN1_GENERALIZEDTIME
);
439 case 'o': /* --out */
443 case 'd': /* --debug */
444 debug_level
= atoi(optarg
);
452 /* break from loop */
458 usage("unexpected argument");
462 DBG1("starting openac (strongSwan Version %s)", VERSION
);
464 #ifdef INTEGRITY_TEST
465 DBG1("integrity test of libstrongswan code");
466 if (fips_verify_hmac_signature(hmac_key
, hmac_signature
))
468 DBG1(" integrity test passed");
472 DBG1(" integrity test failed");
476 #endif /* INTEGRITY_TEST */
478 /* load the signer's RSA private key */
481 signerKey
= private_key_create_from_file(keyfile
, &passphrase
);
483 if (signerKey
== NULL
)
489 /* load the signer's X.509 certificate */
490 if (certfile
!= NULL
)
492 signerCert
= lib
->creds
->create(lib
->creds
,
493 CRED_CERTIFICATE
, CERT_X509
,
494 BUILD_FROM_FILE
, certfile
,
497 if (signerCert
== NULL
)
503 /* load the users's X.509 certificate */
504 if (usercertfile
!= NULL
)
506 userCert
= lib
->creds
->create(lib
->creds
,
507 CRED_CERTIFICATE
, CERT_X509
,
508 BUILD_FROM_FILE
, usercertfile
,
511 if (userCert
== NULL
)
517 /* compute validity interval */
518 validity
= (validity
)? validity
: DEFAULT_VALIDITY
;
519 notBefore
= (notBefore
== UNDEFINED_TIME
) ?
time(NULL
) : notBefore
;
520 notAfter
= (notAfter
== UNDEFINED_TIME
) ?
time(NULL
) + validity
: notAfter
;
522 /* build and parse attribute certificate */
523 if (userCert
!= NULL
&& signerCert
!= NULL
&& signerKey
!= NULL
)
525 /* read the serial number and increment it by one */
526 serial
= read_serial();
528 attr_cert
= lib
->creds
->create(lib
->creds
,
529 CRED_CERTIFICATE
, CERT_X509_AC
,
530 BUILD_CERT
, userCert
->get_ref(userCert
),
531 BUILD_NOT_BEFORE_TIME
, notBefore
,
532 BUILD_NOT_AFTER_TIME
, notAfter
,
533 BUILD_SERIAL
, serial
,
534 BUILD_IETF_GROUP_ATTR
, groups
,
535 BUILD_SIGNING_CERT
, signerCert
->get_ref(signerCert
),
536 BUILD_SIGNING_KEY
, signerKey
->get_ref(signerKey
),
543 /* write the attribute certificate to file */
544 attr_chunk
= attr_cert
->get_encoding(attr_cert
);
545 if (chunk_write(attr_chunk
, outfile
, 0022, TRUE
))
547 DBG1(" wrote attribute cert file '%s' (%u bytes)", outfile
, attr_chunk
.len
);
548 write_serial(serial
);
554 usage("some of the mandatory parameters --usercert --cert --key "
559 /* delete all dynamically allocated objects */
560 DESTROY_IF(signerKey
);
561 DESTROY_IF(signerCert
);
562 DESTROY_IF(userCert
);
563 DESTROY_IF(attr_cert
);
564 free(attr_chunk
.ptr
);
568 options
->destroy(options
);