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
37 #include <asn1/asn1.h>
38 #include <asn1/ttodata.h>
39 #include <crypto/ac.h>
40 #include <crypto/ietf_attr_list.h>
41 #include <utils/optionsfrom.h>
44 #include <fips/fips.h>
45 #include <fips_signature.h>
46 #endif /* INTEGRITY_TEST */
50 #define OPENAC_PATH IPSEC_CONFDIR "/openac"
51 #define OPENAC_SERIAL IPSEC_CONFDIR "/openac/serial"
53 static void usage(const char *mess
)
55 if (mess
!= NULL
&& *mess
!= '\0')
57 fprintf(stderr
, "%s\n", mess
);
59 fprintf(stderr
, "Usage: openac"
62 " [--optionsfrom <filename>]"
65 " [--debug <level 0..4>]"
70 " [--startdate <YYYYMMDDHHMMSSZ>]"
71 " [--enddate <YYYYMMDDHHMMSSZ>]"
75 " [--password <password>]"
77 " --usercert <certfile>"
78 " --groups <attr1,attr2,..>"
86 * convert a chunk into a multi-precision integer
88 static void chunk_to_mpz(chunk_t chunk
, mpz_t number
)
90 mpz_import(number
, chunk
.len
, 1, 1, 1, 0, chunk
.ptr
);
94 * convert a multi-precision integer into a chunk
96 static chunk_t
mpz_to_chunk(mpz_t number
)
100 chunk
.len
= 1 + mpz_sizeinbase(number
, 2)/BITS_PER_BYTE
;
101 chunk
.ptr
= mpz_export(NULL
, NULL
, 1, chunk
.len
, 1, 0, number
);
106 * read the last serial number from file
108 static chunk_t
read_serial(void)
112 char buf
[BUF_LEN
], buf1
[BUF_LEN
];
113 chunk_t last_serial
= { buf1
, BUF_LEN
};
116 FILE *fd
= fopen(OPENAC_SERIAL
, "r");
118 /* last serial number defaults to 0 */
119 *last_serial
.ptr
= 0x00;
124 if (fscanf(fd
, "%s", buf
))
126 err_t ugh
= ttodata(buf
, 0, 16, last_serial
.ptr
, BUF_LEN
, &last_serial
.len
);
130 DBG1(" error reading serial number from %s: %s",
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");
164 DBG1(" serial number is %#B", &serial
);
165 fprintf(fd
, "%#B\n", &serial
);
170 DBG1(" could not open file '%s' for writing", OPENAC_SERIAL
);
175 * global variables accessible by both main() and build.c
177 x509_t
*usercert
= NULL
;
178 x509_t
*signercert
= NULL
;
180 linked_list_t
*groups
= NULL
;
181 rsa_private_key_t
*signerkey
= NULL
;
183 time_t notBefore
= UNDEFINED_TIME
;
184 time_t notAfter
= UNDEFINED_TIME
;
188 static int debug_level
= 1;
189 static bool stderr_quiet
= FALSE
;
191 * openac dbg function
193 static void openac_dbg(int level
, char *fmt
, ...)
195 int priority
= LOG_INFO
;
198 if (level
<= debug_level
)
203 vfprintf(stderr
, fmt
, args
);
204 fprintf(stderr
, "\n");
206 vsyslog(priority
, fmt
, args
);
212 * openac main program
214 int main(int argc
, char **argv
)
216 char *keyfile
= NULL
;
217 char *certfile
= NULL
;
218 char *usercertfile
= NULL
;
219 char *outfile
= NULL
;
222 chunk_t passphrase
= { buf
, 0 };
223 chunk_t attr_cert
= chunk_empty
;
226 const time_t default_validity
= 24*3600; /* 24 hours */
230 /* enable openac debugging hook */
233 passphrase
.ptr
[0] = '\0';
234 groups
= linked_list_create();
236 openlog("openac", 0, LOG_AUTHPRIV
);
238 /* handle arguments */
241 static const struct option long_opts
[] = {
242 /* name, has_arg, flag, val */
243 { "help", no_argument
, NULL
, 'h' },
244 { "version", no_argument
, NULL
, 'v' },
245 { "optionsfrom", required_argument
, NULL
, '+' },
246 { "quiet", no_argument
, NULL
, 'q' },
247 { "cert", required_argument
, NULL
, 'c' },
248 { "key", required_argument
, NULL
, 'k' },
249 { "password", required_argument
, NULL
, 'p' },
250 { "usercert", required_argument
, NULL
, 'u' },
251 { "groups", required_argument
, NULL
, 'g' },
252 { "days", required_argument
, NULL
, 'D' },
253 { "hours", required_argument
, NULL
, 'H' },
254 { "startdate", required_argument
, NULL
, 'S' },
255 { "enddate", required_argument
, NULL
, 'E' },
256 { "out", required_argument
, NULL
, 'o' },
257 { "debug", required_argument
, NULL
, 'd' },
261 int c
= getopt_long(argc
, argv
, "hv+:qc:k:p;u:g:D:H:S:E:o:d:", long_opts
, NULL
);
263 /* Note: "breaking" from case terminates loop */
266 case EOF
: /* end of flags */
269 case 0: /* long option already handled */
272 case ':': /* diagnostic already printed by getopt_long */
273 case '?': /* diagnostic already printed by getopt_long */
274 case 'h': /* --help */
279 case 'v': /* --version */
280 printf("openac (strongSwan %s)\n", VERSION
);
284 case '+': /* --optionsfrom <filename> */
288 if (*optarg
== '/') /* absolute pathname */
290 strncpy(path
, optarg
, BUF_LEN
);
292 else /* relative pathname */
294 snprintf(path
, BUF_LEN
, "%s/%s", OPENAC_PATH
, optarg
);
296 if (!optionsfrom(path
, &argc
, &argv
, optind
))
304 case 'q': /* --quiet */
308 case 'c': /* --cert */
312 case 'k': /* --key */
316 case 'p': /* --key */
317 if (strlen(optarg
) > BUF_LEN
)
319 usage("passphrase too long");
322 strncpy(passphrase
.ptr
, optarg
, BUF_LEN
);
323 passphrase
.len
= min(strlen(optarg
), BUF_LEN
);
326 case 'u': /* --usercert */
327 usercertfile
= optarg
;
330 case 'g': /* --groups */
331 ietfAttr_list_create_from_string(optarg
, groups
);
334 case 'D': /* --days */
335 if (optarg
== NULL
|| !isdigit(optarg
[0]))
337 usage("missing number of days");
343 long days
= strtol(optarg
, &endptr
, 0);
345 if (*endptr
!= '\0' || endptr
== optarg
|| days
<= 0)
347 usage("<days> must be a positive number");
350 validity
+= 24*3600*days
;
354 case 'H': /* --hours */
355 if (optarg
== NULL
|| !isdigit(optarg
[0]))
357 usage("missing number of hours");
363 long hours
= strtol(optarg
, &endptr
, 0);
365 if (*endptr
!= '\0' || endptr
== optarg
|| hours
<= 0)
367 usage("<hours> must be a positive number");
370 validity
+= 3600*hours
;
374 case 'S': /* --startdate */
375 if (optarg
== NULL
|| strlen(optarg
) != 15 || optarg
[14] != 'Z')
377 usage("date format must be YYYYMMDDHHMMSSZ");
382 chunk_t date
= { optarg
, 15 };
384 notBefore
= asn1totime(&date
, ASN1_GENERALIZEDTIME
);
388 case 'E': /* --enddate */
389 if (optarg
== NULL
|| strlen(optarg
) != 15 || optarg
[14] != 'Z')
391 usage("date format must be YYYYMMDDHHMMSSZ");
396 chunk_t date
= { optarg
, 15 };
397 notAfter
= asn1totime(&date
, ASN1_GENERALIZEDTIME
);
401 case 'o': /* --out */
405 case 'd': /* --debug */
406 debug_level
= atoi(optarg
);
419 usage("unexpected argument");
423 DBG1("starting openac (strongSwan Version %s)", VERSION
);
425 #ifdef INTEGRITY_TEST
426 DBG1("integrity test of libstrongswan code");
427 if (fips_verify_hmac_signature(hmac_key
, hmac_signature
))
429 DBG1(" integrity test passed");
433 DBG1(" integrity test failed");
437 #endif /* INTEGRITY_TEST */
439 /* load the signer's RSA private key */
442 signerkey
= rsa_private_key_create_from_file(keyfile
, &passphrase
);
444 if (signerkey
== NULL
)
450 /* load the signer's X.509 certificate */
451 if (certfile
!= NULL
)
453 signercert
= x509_create_from_file(certfile
, "signer cert");
455 if (signercert
== NULL
)
461 /* load the users's X.509 certificate */
462 if (usercertfile
!= NULL
)
464 usercert
= x509_create_from_file(usercertfile
, "user cert");
466 if (usercert
== NULL
)
472 /* compute validity interval */
473 validity
= (validity
)? validity
: default_validity
;
474 notBefore
= (notBefore
== UNDEFINED_TIME
) ?
time(NULL
) : notBefore
;
475 notAfter
= (notAfter
== UNDEFINED_TIME
) ?
time(NULL
) + validity
: notAfter
;
477 /* build and parse attribute certificate */
478 if (usercert
!= NULL
&& signercert
!= NULL
&& signerkey
!= NULL
)
480 /* read the serial number and increment it by one */
481 serial
= read_serial();
483 attr_cert
= build_attr_cert();
484 ac
= x509ac_create_from_chunk(attr_cert
);
486 /* write the attribute certificate to file */
487 if (chunk_write(attr_cert
, outfile
, "attribute cert", 0022, TRUE
))
489 write_serial(serial
);
495 /* delete all dynamically allocated objects */
496 DESTROY_IF(signerkey
);
497 DESTROY_IF(signercert
);
498 DESTROY_IF(usercert
);
500 ietfAttr_list_destroy(groups
);