Skip to content

Commit

Permalink
allow setting signature algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Love Hörnquist Åstrand committed Feb 12, 2014
1 parent e37d664 commit c69c463
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
43 changes: 41 additions & 2 deletions lib/hx509/ca.c
Expand Up @@ -61,7 +61,7 @@ struct hx509_ca_tbs {
CRLDistributionPoints crldp;
heim_bit_string subjectUniqueID;
heim_bit_string issuerUniqueID;

AlgorithmIdentifier *sigalg;
};

/**
Expand Down Expand Up @@ -109,6 +109,10 @@ hx509_ca_tbs_free(hx509_ca_tbs *tbs)
der_free_bit_string(&(*tbs)->subjectUniqueID);
der_free_bit_string(&(*tbs)->issuerUniqueID);
hx509_name_free(&(*tbs)->subject);
if ((*tbs)->sigalg) {
free_AlgorithmIdentifier((*tbs)->sigalg);
free((*tbs)->sigalg);
}

memset(*tbs, 0, sizeof(**tbs));
free(*tbs);
Expand Down Expand Up @@ -904,6 +908,39 @@ hx509_ca_tbs_subject_expand(hx509_context context,
return hx509_name_expand(context, tbs->subject, env);
}

/**
* Set signature algorithm on the to be signed certificate
*
* @param context A hx509 context.
* @param tbs object to be signed.
* @param sigalg signature algorithm to use
*
* @return An hx509 error code, see hx509_get_error_string().
*
* @ingroup hx509_ca
*/

int
hx509_ca_tbs_set_signature_algorithm(hx509_context context,
hx509_ca_tbs tbs,
const AlgorithmIdentifier *sigalg)
{
int ret;

tbs->sigalg = calloc(1, sizeof(*tbs->sigalg));
if (tbs->sigalg == NULL) {
hx509_set_error_string(context, 0, ENOMEM, "Out of memory");
return ENOMEM;
}
ret = copy_AlgorithmIdentifier(sigalg, tbs->sigalg);
if (ret) {
free(tbs->sigalg);
tbs->sigalg = NULL;
return ret;
}
return 0;
}

/*
*
*/
Expand Down Expand Up @@ -998,7 +1035,9 @@ ca_sign(hx509_context context,
time_t notAfter;
unsigned key_usage;

sigalg = _hx509_crypto_default_sig_alg;
sigalg = tbs->sigalg;
if (sigalg == NULL)
sigalg = _hx509_crypto_default_sig_alg;

memset(&c, 0, sizeof(c));

Expand Down
5 changes: 5 additions & 0 deletions lib/hx509/hxtool-commands.in
Expand Up @@ -634,6 +634,11 @@ command = {
type = "string"
help = "Lifetime of certificate"
}
option = {
long = "signature-algorithm"
type = "string"
help = "Signature algorithm to use"
}
option = {
long = "serial-number"
type = "string"
Expand Down
11 changes: 11 additions & 0 deletions lib/hx509/hxtool.c
Expand Up @@ -1928,6 +1928,17 @@ hxtool_ca(struct certificate_sign_options *opt, int argc, char **argv)
if (ret)
hx509_err(context, 1, ret, "hx509_ca_tbs_init");

if (opt->signature_algorithm_string) {
const AlgorithmIdentifier *sigalg;
if (strcasecmp(opt->signature_algorithm_string, "rsa-with-sha1") == 0)
sigalg = hx509_signature_rsa_with_sha1();
else if (strcasecmp(opt->signature_algorithm_string, "rsa-with-sha256") == 0)
sigalg = hx509_signature_rsa_with_sha256();
else
errx(1, "unsupported sigature algorith");
hx509_ca_tbs_set_signature_algorithm(context, tbs, sigalg);
}

if (opt->template_certificate_string) {
hx509_cert template;
hx509_certs tcerts;
Expand Down
28 changes: 28 additions & 0 deletions lib/hx509/test_ca.in
Expand Up @@ -421,4 +421,32 @@ ${hxtool} verify --missing-revoke \
cert:FILE:cert-ee.pem \
anchor:FILE:cert-ca.pem > /dev/null || exit 1

echo "+++++++++++ test sigalg"

echo "issue cert with sha256"
${hxtool} issue-certificate \
--ca-certificate=FILE:cert-ca.pem \
--signature-algorithm=rsa-with-sha256 \
--subject="cn=foo" \
--req="PKCS10:pkcs10-request.der" \
--certificate="FILE:cert-ee.pem" || exit 1

echo "verify certificate"
${hxtool} verify --missing-revoke \
cert:FILE:cert-ee.pem \
anchor:FILE:cert-ca.pem > /dev/null || exit 1

echo "issue cert with sha1"
${hxtool} issue-certificate \
--ca-certificate=FILE:cert-ca.pem \
--signature-algorithm=rsa-with-sha1 \
--subject="cn=foo" \
--req="PKCS10:pkcs10-request.der" \
--certificate="FILE:cert-ee.pem" || exit 1

echo "verify certificate"
${hxtool} verify --missing-revoke \
cert:FILE:cert-ee.pem \
anchor:FILE:cert-ca.pem > /dev/null || exit 1

exit 0

0 comments on commit c69c463

Please sign in to comment.