strongswan/src/openac/openac.c

572 lines
12 KiB
C
Raw Normal View History

2007-09-25 20:13:06 +00:00
/**
* @file openac.c
*
* @brief Generation of X.509 attribute certificates.
*
*/
/*
* Copyright (C) 2002 Ueli Galizzi, Ariane Seiler
2007-09-25 20:13:06 +00:00
* Copyright (C) 2004,2007 Andreas Steffen
* Hochschule fuer Technik Rapperswil, Switzerland
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
2007-10-08 20:09:57 +00:00
*
* RCSID $Id$
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2007-09-25 20:13:06 +00:00
#include <syslog.h>
#include <unistd.h>
#include <getopt.h>
#include <ctype.h>
#include <time.h>
#include <gmp.h>
2008-03-21 10:42:05 +00:00
#include <library.h>
2007-09-18 11:23:52 +00:00
#include <debug.h>
#include <asn1/asn1.h>
#include <asn1/pem.h>
2007-09-18 11:23:52 +00:00
#include <asn1/ttodata.h>
#include <credentials/certificates/x509.h>
#include <credentials/certificates/ac.h>
2007-09-18 11:23:52 +00:00
#include <utils/optionsfrom.h>
2007-09-25 20:13:06 +00:00
#ifdef INTEGRITY_TEST
#include <fips/fips.h>
#include <fips_signature.h>
#endif /* INTEGRITY_TEST */
#define OPENAC_PATH IPSEC_CONFDIR "/openac"
#define OPENAC_SERIAL IPSEC_CONFDIR "/openac/serial"
#define DEFAULT_VALIDITY 24*3600 /* seconds */
2007-10-12 18:39:40 +00:00
/**
* @brief prints the usage of the program to the stderr
*/
static void usage(const char *message)
{
2007-10-12 18:39:40 +00:00
if (message != NULL && *message != '\0')
2007-04-10 19:31:42 +00:00
{
2007-10-12 18:39:40 +00:00
fprintf(stderr, "%s\n", message);
2007-04-10 19:31:42 +00:00
}
fprintf(stderr, "Usage: openac"
" [--help]"
" [--version]"
" [--optionsfrom <filename>]"
" [--quiet]"
" \\\n\t"
2007-09-25 20:13:06 +00:00
" [--debug <level 0..4>]"
2007-04-10 19:31:42 +00:00
" \\\n\t"
" [--days <days>]"
" [--hours <hours>]"
" \\\n\t"
" [--startdate <YYYYMMDDHHMMSSZ>]"
" [--enddate <YYYYMMDDHHMMSSZ>]"
" \\\n\t"
" --cert <certfile>"
" --key <keyfile>"
" [--password <password>]"
" \\\n\t"
" --usercert <certfile>"
" --groups <attr1,attr2,..>"
" --out <filename>"
"\n"
);
}
2007-09-25 20:13:06 +00:00
2007-09-18 11:23:52 +00:00
/**
* convert a chunk into a multi-precision integer
*/
static void chunk_to_mpz(chunk_t chunk, mpz_t number)
{
mpz_import(number, chunk.len, 1, 1, 1, 0, chunk.ptr);
}
/**
* convert a multi-precision integer into a chunk
*/
static chunk_t mpz_to_chunk(mpz_t number)
{
chunk_t chunk;
chunk.len = 1 + mpz_sizeinbase(number, 2)/BITS_PER_BYTE;
chunk.ptr = mpz_export(NULL, NULL, 1, chunk.len, 1, 0, number);
return chunk;
}
2007-04-10 19:31:42 +00:00
/**
* read the last serial number from file
*/
2007-04-10 19:31:42 +00:00
static chunk_t read_serial(void)
{
2007-09-18 11:23:52 +00:00
mpz_t number;
2007-09-18 11:23:52 +00:00
char buf[BUF_LEN], buf1[BUF_LEN];
chunk_t last_serial = { buf1, BUF_LEN};
chunk_t serial;
2007-04-10 19:31:42 +00:00
FILE *fd = fopen(OPENAC_SERIAL, "r");
2007-09-18 11:23:52 +00:00
/* last serial number defaults to 0 */
*last_serial.ptr = 0x00;
last_serial.len = 1;
2007-04-10 19:31:42 +00:00
if (fd)
{
2007-04-10 19:31:42 +00:00
if (fscanf(fd, "%s", buf))
{
2007-09-18 11:23:52 +00:00
err_t ugh = ttodata(buf, 0, 16, last_serial.ptr, BUF_LEN, &last_serial.len);
2007-04-10 19:31:42 +00:00
if (ugh != NULL)
{
2007-09-18 11:23:52 +00:00
DBG1(" error reading serial number from %s: %s",
OPENAC_SERIAL, ugh);
2007-04-10 19:31:42 +00:00
}
}
fclose(fd);
}
2007-04-10 19:31:42 +00:00
else
{
2007-09-18 20:49:05 +00:00
DBG1(" file '%s' does not exist yet - serial number set to 01", OPENAC_SERIAL);
2007-04-10 19:31:42 +00:00
}
2007-04-10 19:31:42 +00:00
/**
* conversion of read serial number to a multiprecision integer
* and incrementing it by one
* and representing it as a two's complement octet string
*/
2007-09-18 11:23:52 +00:00
mpz_init(number);
chunk_to_mpz(last_serial, number);
mpz_add_ui(number, number, 0x01);
serial = mpz_to_chunk(number);
mpz_clear(number);
2007-04-10 19:31:42 +00:00
return serial;
}
2007-04-10 19:31:42 +00:00
/**
* write back the last serial number to file
*/
2007-04-10 19:31:42 +00:00
static void write_serial(chunk_t serial)
{
2007-04-10 19:31:42 +00:00
FILE *fd = fopen(OPENAC_SERIAL, "w");
if (fd)
{
2007-09-18 11:23:52 +00:00
DBG1(" serial number is %#B", &serial);
fprintf(fd, "%#B\n", &serial);
2007-04-10 19:31:42 +00:00
fclose(fd);
}
else
{
2007-09-18 11:23:52 +00:00
DBG1(" could not open file '%s' for writing", OPENAC_SERIAL);
2007-04-10 19:31:42 +00:00
}
}
2007-04-10 19:31:42 +00:00
/**
* Load and parse a private key file
*/
static private_key_t* private_key_create_from_file(char *path, chunk_t *secret)
{
bool pgp = FALSE;
chunk_t chunk = chunk_empty;
private_key_t *key = NULL;
if (!pem_asn1_load_file(path, secret, &chunk, &pgp))
{
DBG1(" could not load private key file '%s'", path);
return NULL;
}
key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
BUILD_BLOB_ASN1_DER, chunk, BUILD_END);
if (key == NULL)
{
DBG1(" could not parse loaded private key file '%s'", path);
return NULL;
}
DBG1(" loaded private key file '%s'", path);
return key;
}
/**
* global variables accessible by both main() and build.c
*/
2007-09-25 20:13:06 +00:00
static int debug_level = 1;
static bool stderr_quiet = FALSE;
2007-10-08 21:21:21 +00:00
2007-09-25 20:13:06 +00:00
/**
* openac dbg function
*/
static void openac_dbg(int level, char *fmt, ...)
{
int priority = LOG_INFO;
va_list args;
if (level <= debug_level)
{
va_start(args, fmt);
if (!stderr_quiet)
{
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
}
vsyslog(priority, fmt, args);
va_end(args);
}
}
/**
2007-10-17 02:55:17 +00:00
* @brief openac main program
2007-10-12 18:39:40 +00:00
*
* @param argc number of arguments
* @param argv pointer to the argument values
2007-09-25 20:13:06 +00:00
*/
2007-04-10 19:31:42 +00:00
int main(int argc, char **argv)
{
certificate_t *attr_cert = NULL;
certificate_t *userCert = NULL;
certificate_t *signerCert = NULL;
private_key_t *signerKey = NULL;
time_t notBefore = UNDEFINED_TIME;
time_t notAfter = UNDEFINED_TIME;
time_t validity = 0;
2007-04-10 19:31:42 +00:00
char *keyfile = NULL;
char *certfile = NULL;
char *usercertfile = NULL;
char *outfile = NULL;
char *groups = "";
2007-09-18 11:23:52 +00:00
char buf[BUF_LEN];
2007-09-18 11:23:52 +00:00
chunk_t passphrase = { buf, 0 };
2008-03-21 10:42:05 +00:00
chunk_t serial = chunk_empty;
chunk_t attr_chunk = chunk_empty;
2007-09-25 20:13:06 +00:00
int status = 1;
/* enable openac debugging hook */
dbg = openac_dbg;
2007-09-18 11:23:52 +00:00
passphrase.ptr[0] = '\0';
2007-09-25 20:13:06 +00:00
openlog("openac", 0, LOG_AUTHPRIV);
2008-03-21 10:42:05 +00:00
/* initialize library */
library_init(STRONGSWAN_CONF);
2008-03-21 10:42:05 +00:00
lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR, "libstrongswan-");
/* initialize optionsfrom */
options_t *options = options_create();
2007-04-10 19:31:42 +00:00
/* handle arguments */
for (;;)
{
static const struct option long_opts[] = {
/* name, has_arg, flag, val */
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{ "optionsfrom", required_argument, NULL, '+' },
{ "quiet", no_argument, NULL, 'q' },
{ "cert", required_argument, NULL, 'c' },
2007-09-25 20:13:06 +00:00
{ "key", required_argument, NULL, 'k' },
2007-04-10 19:31:42 +00:00
{ "password", required_argument, NULL, 'p' },
{ "usercert", required_argument, NULL, 'u' },
{ "groups", required_argument, NULL, 'g' },
{ "days", required_argument, NULL, 'D' },
{ "hours", required_argument, NULL, 'H' },
{ "startdate", required_argument, NULL, 'S' },
{ "enddate", required_argument, NULL, 'E' },
{ "out", required_argument, NULL, 'o' },
2007-09-25 20:13:06 +00:00
{ "debug", required_argument, NULL, 'd' },
2007-04-10 19:31:42 +00:00
{ 0,0,0,0 }
};
2007-09-25 20:13:06 +00:00
int c = getopt_long(argc, argv, "hv+:qc:k:p;u:g:D:H:S:E:o:d:", long_opts, NULL);
2007-04-10 19:31:42 +00:00
/* Note: "breaking" from case terminates loop */
switch (c)
{
case EOF: /* end of flags */
break;
case 0: /* long option already handled */
continue;
case ':': /* diagnostic already printed by getopt_long */
case '?': /* diagnostic already printed by getopt_long */
case 'h': /* --help */
usage(NULL);
2007-09-25 20:13:06 +00:00
status = 1;
goto end;
2007-04-10 19:31:42 +00:00
case 'v': /* --version */
2007-09-25 20:13:06 +00:00
printf("openac (strongSwan %s)\n", VERSION);
status = 0;
goto end;
2007-04-10 19:31:42 +00:00
case '+': /* --optionsfrom <filename> */
{
char path[BUF_LEN];
if (*optarg == '/') /* absolute pathname */
2007-09-18 11:23:52 +00:00
{
2007-04-10 19:31:42 +00:00
strncpy(path, optarg, BUF_LEN);
2007-09-18 11:23:52 +00:00
}
2007-04-10 19:31:42 +00:00
else /* relative pathname */
2007-09-18 11:23:52 +00:00
{
2007-04-10 19:31:42 +00:00
snprintf(path, BUF_LEN, "%s/%s", OPENAC_PATH, optarg);
2007-09-18 11:23:52 +00:00
}
2008-02-04 14:46:43 +00:00
if (!options->from(options, path, &argc, &argv, optind))
{
2007-10-03 15:07:46 +00:00
status = 1;
goto end;
}
2007-04-10 19:31:42 +00:00
}
continue;
case 'q': /* --quiet */
2007-09-25 20:13:06 +00:00
stderr_quiet = TRUE;
2007-04-10 19:31:42 +00:00
continue;
case 'c': /* --cert */
certfile = optarg;
continue;
case 'k': /* --key */
keyfile = optarg;
continue;
case 'p': /* --key */
2007-09-18 11:23:52 +00:00
if (strlen(optarg) > BUF_LEN)
{
usage("passphrase too long");
2007-09-25 20:13:06 +00:00
goto end;
2007-09-18 11:23:52 +00:00
}
strncpy(passphrase.ptr, optarg, BUF_LEN);
passphrase.len = min(strlen(optarg), BUF_LEN);
2007-04-10 19:31:42 +00:00
continue;
case 'u': /* --usercert */
usercertfile = optarg;
continue;
case 'g': /* --groups */
groups = optarg;
2007-04-10 19:31:42 +00:00
continue;
case 'D': /* --days */
if (optarg == NULL || !isdigit(optarg[0]))
2007-09-18 11:23:52 +00:00
{
2007-04-10 19:31:42 +00:00
usage("missing number of days");
2007-09-25 20:13:06 +00:00
goto end;
2007-09-18 11:23:52 +00:00
}
else
2007-04-10 19:31:42 +00:00
{
char *endptr;
long days = strtol(optarg, &endptr, 0);
if (*endptr != '\0' || endptr == optarg || days <= 0)
2007-09-18 11:23:52 +00:00
{
2007-04-10 19:31:42 +00:00
usage("<days> must be a positive number");
2007-09-25 20:13:06 +00:00
goto end;
2007-09-18 11:23:52 +00:00
}
2007-04-10 19:31:42 +00:00
validity += 24*3600*days;
}
continue;
case 'H': /* --hours */
if (optarg == NULL || !isdigit(optarg[0]))
2007-09-18 11:23:52 +00:00
{
2007-04-10 19:31:42 +00:00
usage("missing number of hours");
2007-09-25 20:13:06 +00:00
goto end;
2007-09-18 11:23:52 +00:00
}
else
2007-04-10 19:31:42 +00:00
{
char *endptr;
long hours = strtol(optarg, &endptr, 0);
if (*endptr != '\0' || endptr == optarg || hours <= 0)
2007-09-18 11:23:52 +00:00
{
2007-04-10 19:31:42 +00:00
usage("<hours> must be a positive number");
2007-09-25 20:13:06 +00:00
goto end;
2007-09-18 11:23:52 +00:00
}
2007-04-10 19:31:42 +00:00
validity += 3600*hours;
}
continue;
case 'S': /* --startdate */
if (optarg == NULL || strlen(optarg) != 15 || optarg[14] != 'Z')
2007-09-18 11:23:52 +00:00
{
2007-04-10 19:31:42 +00:00
usage("date format must be YYYYMMDDHHMMSSZ");
2007-09-25 20:13:06 +00:00
goto end;
2007-09-18 11:23:52 +00:00
}
else
2007-04-10 19:31:42 +00:00
{
chunk_t date = { optarg, 15 };
2007-09-18 11:23:52 +00:00
2007-04-10 19:31:42 +00:00
notBefore = asn1totime(&date, ASN1_GENERALIZEDTIME);
}
continue;
case 'E': /* --enddate */
if (optarg == NULL || strlen(optarg) != 15 || optarg[14] != 'Z')
2007-09-18 11:23:52 +00:00
{
2007-04-10 19:31:42 +00:00
usage("date format must be YYYYMMDDHHMMSSZ");
2007-09-25 20:13:06 +00:00
goto end;
2007-09-18 11:23:52 +00:00
}
else
2007-04-10 19:31:42 +00:00
{
chunk_t date = { optarg, 15 };
notAfter = asn1totime(&date, ASN1_GENERALIZEDTIME);
}
continue;
2007-09-25 20:13:06 +00:00
case 'o': /* --out */
2007-04-10 19:31:42 +00:00
outfile = optarg;
continue;
2007-09-25 20:13:06 +00:00
case 'd': /* --debug */
debug_level = atoi(optarg);
2007-04-10 19:31:42 +00:00
continue;
2007-09-25 20:13:06 +00:00
2007-04-10 19:31:42 +00:00
default:
2007-09-18 11:23:52 +00:00
usage("");
2007-09-25 20:13:06 +00:00
status = 0;
goto end;
2007-04-10 19:31:42 +00:00
}
2007-10-12 18:39:40 +00:00
/* break from loop */
2007-04-10 19:31:42 +00:00
break;
}
2007-04-10 19:31:42 +00:00
if (optind != argc)
2007-09-18 11:23:52 +00:00
{
2007-04-10 19:31:42 +00:00
usage("unexpected argument");
2007-09-25 20:13:06 +00:00
goto end;
2007-09-18 11:23:52 +00:00
}
2007-09-25 20:13:06 +00:00
DBG1("starting openac (strongSwan Version %s)", VERSION);
#ifdef INTEGRITY_TEST
DBG1("integrity test of libstrongswan code");
if (fips_verify_hmac_signature(hmac_key, hmac_signature))
{
DBG1(" integrity test passed");
}
else
{
DBG1(" integrity test failed");
2007-10-03 15:07:46 +00:00
status = 3;
goto end;
2007-09-25 20:13:06 +00:00
}
#endif /* INTEGRITY_TEST */
2007-04-10 19:31:42 +00:00
/* load the signer's RSA private key */
if (keyfile != NULL)
{
signerKey = private_key_create_from_file(keyfile, &passphrase);
2007-04-10 19:31:42 +00:00
if (signerKey == NULL)
2007-04-10 19:31:42 +00:00
{
2007-09-25 20:13:06 +00:00
goto end;
2007-04-10 19:31:42 +00:00
}
}
2007-04-10 19:31:42 +00:00
/* load the signer's X.509 certificate */
if (certfile != NULL)
{
signerCert = lib->creds->create(lib->creds,
CRED_CERTIFICATE, CERT_X509,
BUILD_FROM_FILE, certfile,
BUILD_X509_FLAG, 0,
BUILD_END);
if (signerCert == NULL)
2007-09-18 11:23:52 +00:00
{
2007-09-25 20:13:06 +00:00
goto end;
2007-09-18 11:23:52 +00:00
}
2007-04-10 19:31:42 +00:00
}
2007-04-10 19:31:42 +00:00
/* load the users's X.509 certificate */
if (usercertfile != NULL)
{
userCert = lib->creds->create(lib->creds,
CRED_CERTIFICATE, CERT_X509,
BUILD_FROM_FILE, usercertfile,
BUILD_X509_FLAG, 0,
BUILD_END);
if (userCert == NULL)
2007-09-18 11:23:52 +00:00
{
2007-09-25 20:13:06 +00:00
goto end;
2007-09-18 11:23:52 +00:00
}
}
2007-04-10 19:31:42 +00:00
/* compute validity interval */
validity = (validity)? validity : DEFAULT_VALIDITY;
2007-09-25 20:13:06 +00:00
notBefore = (notBefore == UNDEFINED_TIME) ? time(NULL) : notBefore;
notAfter = (notAfter == UNDEFINED_TIME) ? time(NULL) + validity : notAfter;
2007-04-10 19:31:42 +00:00
/* build and parse attribute certificate */
if (userCert != NULL && signerCert != NULL && signerKey != NULL)
2007-04-10 19:31:42 +00:00
{
/* read the serial number and increment it by one */
serial = read_serial();
attr_cert = lib->creds->create(lib->creds,
CRED_CERTIFICATE, CERT_X509_AC,
BUILD_CERT, userCert->get_ref(userCert),
BUILD_NOT_BEFORE_TIME, notBefore,
BUILD_NOT_AFTER_TIME, notAfter,
BUILD_SERIAL, serial,
BUILD_IETF_GROUP_ATTR, groups,
BUILD_SIGNING_CERT, signerCert->get_ref(signerCert),
BUILD_SIGNING_KEY, signerKey->get_ref(signerKey),
BUILD_END);
2008-03-21 10:42:05 +00:00
if (!attr_cert)
{
goto end;
}
2007-04-10 19:31:42 +00:00
/* write the attribute certificate to file */
2008-03-21 10:42:05 +00:00
attr_chunk = attr_cert->get_encoding(attr_cert);
2008-04-17 18:56:55 +00:00
if (chunk_write(attr_chunk, outfile, 0022, TRUE))
2007-09-18 11:23:52 +00:00
{
2008-04-17 18:56:55 +00:00
DBG1(" wrote attribute cert file '%s' (%u bytes)", outfile, attr_chunk.len);
2007-09-18 11:23:52 +00:00
write_serial(serial);
2007-09-25 20:13:06 +00:00
status = 0;
2007-09-18 11:23:52 +00:00
}
2007-04-10 19:31:42 +00:00
}
2008-03-21 10:42:05 +00:00
else
{
usage("some of the mandatory parameters --usercert --cert --key "
"are missing");
}
2007-04-10 19:31:42 +00:00
2007-09-25 20:13:06 +00:00
end:
2007-09-18 11:23:52 +00:00
/* delete all dynamically allocated objects */
DESTROY_IF(signerKey);
DESTROY_IF(signerCert);
DESTROY_IF(userCert);
DESTROY_IF(attr_cert);
free(attr_chunk.ptr);
2007-09-18 11:23:52 +00:00
free(serial.ptr);
2007-09-25 20:13:06 +00:00
closelog();
dbg = dbg_default;
2008-02-04 14:46:43 +00:00
options->destroy(options);
2008-03-21 10:42:05 +00:00
library_deinit();
2007-09-25 20:13:06 +00:00
exit(status);
}