210e1f67639cf425311429b6385b31f50c1c7f10
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
, "libstrongswan-");
272 /* initialize optionsfrom */
273 options_t
*options
= options_create();
275 /* handle arguments */
278 static const struct option long_opts
[] = {
279 /* name, has_arg, flag, val */
280 { "help", no_argument
, NULL
, 'h' },
281 { "version", no_argument
, NULL
, 'v' },
282 { "optionsfrom", required_argument
, NULL
, '+' },
283 { "quiet", no_argument
, NULL
, 'q' },
284 { "cert", required_argument
, NULL
, 'c' },
285 { "key", required_argument
, NULL
, 'k' },
286 { "password", required_argument
, NULL
, 'p' },
287 { "usercert", required_argument
, NULL
, 'u' },
288 { "groups", required_argument
, NULL
, 'g' },
289 { "days", required_argument
, NULL
, 'D' },
290 { "hours", required_argument
, NULL
, 'H' },
291 { "startdate", required_argument
, NULL
, 'S' },
292 { "enddate", required_argument
, NULL
, 'E' },
293 { "out", required_argument
, NULL
, 'o' },
294 { "debug", required_argument
, NULL
, 'd' },
298 int c
= getopt_long(argc
, argv
, "hv+:qc:k:p;u:g:D:H:S:E:o:d:", long_opts
, NULL
);
300 /* Note: "breaking" from case terminates loop */
303 case EOF
: /* end of flags */
306 case 0: /* long option already handled */
309 case ':': /* diagnostic already printed by getopt_long */
310 case '?': /* diagnostic already printed by getopt_long */
311 case 'h': /* --help */
316 case 'v': /* --version */
317 printf("openac (strongSwan %s)\n", VERSION
);
321 case '+': /* --optionsfrom <filename> */
325 if (*optarg
== '/') /* absolute pathname */
327 strncpy(path
, optarg
, BUF_LEN
);
329 else /* relative pathname */
331 snprintf(path
, BUF_LEN
, "%s/%s", OPENAC_PATH
, optarg
);
333 if (!options
->from(options
, path
, &argc
, &argv
, optind
))
341 case 'q': /* --quiet */
345 case 'c': /* --cert */
349 case 'k': /* --key */
353 case 'p': /* --key */
354 if (strlen(optarg
) > BUF_LEN
)
356 usage("passphrase too long");
359 strncpy(passphrase
.ptr
, optarg
, BUF_LEN
);
360 passphrase
.len
= min(strlen(optarg
), BUF_LEN
);
363 case 'u': /* --usercert */
364 usercertfile
= optarg
;
367 case 'g': /* --groups */
371 case 'D': /* --days */
372 if (optarg
== NULL
|| !isdigit(optarg
[0]))
374 usage("missing number of days");
380 long days
= strtol(optarg
, &endptr
, 0);
382 if (*endptr
!= '\0' || endptr
== optarg
|| days
<= 0)
384 usage("<days> must be a positive number");
387 validity
+= 24*3600*days
;
391 case 'H': /* --hours */
392 if (optarg
== NULL
|| !isdigit(optarg
[0]))
394 usage("missing number of hours");
400 long hours
= strtol(optarg
, &endptr
, 0);
402 if (*endptr
!= '\0' || endptr
== optarg
|| hours
<= 0)
404 usage("<hours> must be a positive number");
407 validity
+= 3600*hours
;
411 case 'S': /* --startdate */
412 if (optarg
== NULL
|| strlen(optarg
) != 15 || optarg
[14] != 'Z')
414 usage("date format must be YYYYMMDDHHMMSSZ");
419 chunk_t date
= { optarg
, 15 };
421 notBefore
= asn1_to_time(&date
, ASN1_GENERALIZEDTIME
);
425 case 'E': /* --enddate */
426 if (optarg
== NULL
|| strlen(optarg
) != 15 || optarg
[14] != 'Z')
428 usage("date format must be YYYYMMDDHHMMSSZ");
433 chunk_t date
= { optarg
, 15 };
434 notAfter
= asn1_to_time(&date
, ASN1_GENERALIZEDTIME
);
438 case 'o': /* --out */
442 case 'd': /* --debug */
443 debug_level
= atoi(optarg
);
451 /* break from loop */
457 usage("unexpected argument");
461 DBG1("starting openac (strongSwan Version %s)", VERSION
);
463 #ifdef INTEGRITY_TEST
464 DBG1("integrity test of libstrongswan code");
465 if (fips_verify_hmac_signature(hmac_key
, hmac_signature
))
467 DBG1(" integrity test passed");
471 DBG1(" integrity test failed");
475 #endif /* INTEGRITY_TEST */
477 /* load the signer's RSA private key */
480 signerKey
= private_key_create_from_file(keyfile
, &passphrase
);
482 if (signerKey
== NULL
)
488 /* load the signer's X.509 certificate */
489 if (certfile
!= NULL
)
491 signerCert
= lib
->creds
->create(lib
->creds
,
492 CRED_CERTIFICATE
, CERT_X509
,
493 BUILD_FROM_FILE
, certfile
,
496 if (signerCert
== NULL
)
502 /* load the users's X.509 certificate */
503 if (usercertfile
!= NULL
)
505 userCert
= lib
->creds
->create(lib
->creds
,
506 CRED_CERTIFICATE
, CERT_X509
,
507 BUILD_FROM_FILE
, usercertfile
,
510 if (userCert
== NULL
)
516 /* compute validity interval */
517 validity
= (validity
)? validity
: DEFAULT_VALIDITY
;
518 notBefore
= (notBefore
== UNDEFINED_TIME
) ?
time(NULL
) : notBefore
;
519 notAfter
= (notAfter
== UNDEFINED_TIME
) ?
time(NULL
) + validity
: notAfter
;
521 /* build and parse attribute certificate */
522 if (userCert
!= NULL
&& signerCert
!= NULL
&& signerKey
!= NULL
)
524 /* read the serial number and increment it by one */
525 serial
= read_serial();
527 attr_cert
= lib
->creds
->create(lib
->creds
,
528 CRED_CERTIFICATE
, CERT_X509_AC
,
529 BUILD_CERT
, userCert
->get_ref(userCert
),
530 BUILD_NOT_BEFORE_TIME
, notBefore
,
531 BUILD_NOT_AFTER_TIME
, notAfter
,
532 BUILD_SERIAL
, serial
,
533 BUILD_IETF_GROUP_ATTR
, groups
,
534 BUILD_SIGNING_CERT
, signerCert
->get_ref(signerCert
),
535 BUILD_SIGNING_KEY
, signerKey
->get_ref(signerKey
),
542 /* write the attribute certificate to file */
543 attr_chunk
= attr_cert
->get_encoding(attr_cert
);
544 if (chunk_write(attr_chunk
, outfile
, 0022, TRUE
))
546 DBG1(" wrote attribute cert file '%s' (%u bytes)", outfile
, attr_chunk
.len
);
547 write_serial(serial
);
553 usage("some of the mandatory parameters --usercert --cert --key "
558 /* delete all dynamically allocated objects */
559 DESTROY_IF(signerKey
);
560 DESTROY_IF(signerCert
);
561 DESTROY_IF(userCert
);
562 DESTROY_IF(attr_cert
);
563 free(attr_chunk
.ptr
);
567 options
->destroy(options
);