2024-02-21 21:26:06 +01:00
|
|
|
/* $OpenBSD: tak.c,v 1.19 2024/02/21 09:17:06 tb Exp $ */
|
2023-04-30 03:15:27 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022 Job Snijders <job@fastly.com>
|
|
|
|
* Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
|
|
|
|
* Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <openssl/asn1.h>
|
|
|
|
#include <openssl/asn1t.h>
|
|
|
|
#include <openssl/safestack.h>
|
|
|
|
#include <openssl/stack.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/x509v3.h>
|
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
extern ASN1_OBJECT *tak_oid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ASN.1 templates for Trust Anchor Keys (draft-ietf-sidrops-signed-tal-12)
|
|
|
|
*/
|
|
|
|
|
2024-02-15 22:52:17 +01:00
|
|
|
ASN1_ITEM_EXP TAKey_it;
|
|
|
|
ASN1_ITEM_EXP TAK_it;
|
|
|
|
|
2023-04-30 03:15:27 +02:00
|
|
|
DECLARE_STACK_OF(ASN1_IA5STRING);
|
|
|
|
|
|
|
|
#ifndef DEFINE_STACK_OF
|
|
|
|
#define sk_ASN1_IA5STRING_num(st) SKM_sk_num(ASN1_IA5STRING, (st))
|
|
|
|
#define sk_ASN1_IA5STRING_value(st, i) SKM_sk_value(ASN1_IA5STRING, (st), (i))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
STACK_OF(ASN1_UTF8STRING) *comments;
|
|
|
|
STACK_OF(ASN1_IA5STRING) *certificateURIs;
|
|
|
|
X509_PUBKEY *subjectPublicKeyInfo;
|
|
|
|
} TAKey;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ASN1_INTEGER *version;
|
|
|
|
TAKey *current;
|
|
|
|
TAKey *predecessor;
|
|
|
|
TAKey *successor;
|
|
|
|
} TAK;
|
|
|
|
|
|
|
|
ASN1_SEQUENCE(TAKey) = {
|
|
|
|
ASN1_SEQUENCE_OF(TAKey, comments, ASN1_UTF8STRING),
|
|
|
|
ASN1_SEQUENCE_OF(TAKey, certificateURIs, ASN1_IA5STRING),
|
|
|
|
ASN1_SIMPLE(TAKey, subjectPublicKeyInfo, X509_PUBKEY),
|
|
|
|
} ASN1_SEQUENCE_END(TAKey);
|
|
|
|
|
|
|
|
ASN1_SEQUENCE(TAK) = {
|
|
|
|
ASN1_EXP_OPT(TAK, version, ASN1_INTEGER, 0),
|
|
|
|
ASN1_SIMPLE(TAK, current, TAKey),
|
|
|
|
ASN1_EXP_OPT(TAK, predecessor, TAKey, 0),
|
|
|
|
ASN1_EXP_OPT(TAK, successor, TAKey, 1),
|
|
|
|
} ASN1_SEQUENCE_END(TAK);
|
|
|
|
|
|
|
|
DECLARE_ASN1_FUNCTIONS(TAK);
|
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(TAK);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On success return pointer to allocated & valid takey structure,
|
|
|
|
* on failure return NULL.
|
|
|
|
*/
|
|
|
|
static struct takey *
|
|
|
|
parse_takey(const char *fn, const TAKey *takey)
|
|
|
|
{
|
|
|
|
const ASN1_UTF8STRING *comment;
|
|
|
|
const ASN1_IA5STRING *certURI;
|
2024-02-16 08:19:11 +01:00
|
|
|
X509_PUBKEY *pubkey;
|
2023-04-30 03:15:27 +02:00
|
|
|
struct takey *res = NULL;
|
2024-02-16 08:19:11 +01:00
|
|
|
unsigned char *der = NULL;
|
2023-04-30 03:15:27 +02:00
|
|
|
size_t i;
|
2024-02-16 08:19:11 +01:00
|
|
|
int der_len;
|
2023-04-30 03:15:27 +02:00
|
|
|
|
|
|
|
if ((res = calloc(1, sizeof(struct takey))) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
|
|
|
|
res->commentsz = sk_ASN1_UTF8STRING_num(takey->comments);
|
|
|
|
if (res->commentsz > 0) {
|
|
|
|
res->comments = calloc(res->commentsz, sizeof(char *));
|
|
|
|
if (res->comments == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < res->commentsz; i++) {
|
|
|
|
comment = sk_ASN1_UTF8STRING_value(takey->comments, i);
|
|
|
|
res->comments[i] = strndup(comment->data, comment->length);
|
|
|
|
if (res->comments[i] == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res->urisz = sk_ASN1_IA5STRING_num(takey->certificateURIs);
|
|
|
|
if (res->urisz == 0) {
|
|
|
|
warnx("%s: Signed TAL requires at least 1 CertificateURI", fn);
|
2024-02-15 22:52:17 +01:00
|
|
|
goto err;
|
2023-04-30 03:15:27 +02:00
|
|
|
}
|
|
|
|
if ((res->uris = calloc(res->urisz, sizeof(char *))) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < res->urisz; i++) {
|
|
|
|
certURI = sk_ASN1_IA5STRING_value(takey->certificateURIs, i);
|
|
|
|
if (!valid_uri(certURI->data, certURI->length, NULL)) {
|
|
|
|
warnx("%s: invalid TA URI", fn);
|
2024-02-15 22:52:17 +01:00
|
|
|
goto err;
|
2023-04-30 03:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: enforce that protocol is rsync or https. */
|
|
|
|
|
|
|
|
res->uris[i] = strndup(certURI->data, certURI->length);
|
|
|
|
if (res->uris[i] == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
|
2024-02-16 08:19:11 +01:00
|
|
|
pubkey = takey->subjectPublicKeyInfo;
|
|
|
|
if ((res->ski = x509_pubkey_get_ski(pubkey, fn)) == NULL)
|
2024-02-15 22:52:17 +01:00
|
|
|
goto err;
|
2023-04-30 03:15:27 +02:00
|
|
|
|
2024-02-16 08:19:11 +01:00
|
|
|
if ((der_len = i2d_X509_PUBKEY(pubkey, &der)) <= 0) {
|
2024-02-15 22:52:17 +01:00
|
|
|
warnx("%s: i2d_X509_PUBKEY failed", fn);
|
|
|
|
goto err;
|
2023-04-30 03:15:27 +02:00
|
|
|
}
|
2024-02-16 08:19:11 +01:00
|
|
|
res->pubkey = der;
|
|
|
|
res->pubkeysz = der_len;
|
2023-04-30 03:15:27 +02:00
|
|
|
|
|
|
|
return res;
|
2024-02-15 22:52:17 +01:00
|
|
|
|
|
|
|
err:
|
|
|
|
takey_free(res);
|
|
|
|
return NULL;
|
2023-04-30 03:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parses the eContent segment of an TAK file
|
|
|
|
* Returns zero on failure, non-zero on success.
|
|
|
|
*/
|
|
|
|
static int
|
2024-02-21 21:26:06 +01:00
|
|
|
tak_parse_econtent(const char *fn, struct tak *tak, const unsigned char *d,
|
|
|
|
size_t dsz)
|
2023-04-30 03:15:27 +02:00
|
|
|
{
|
2024-02-06 08:46:25 +01:00
|
|
|
const unsigned char *oder;
|
2024-02-17 22:37:04 +01:00
|
|
|
TAK *tak_asn1;
|
2024-02-06 08:46:25 +01:00
|
|
|
int rc = 0;
|
2023-04-30 03:15:27 +02:00
|
|
|
|
2024-02-06 08:46:25 +01:00
|
|
|
oder = d;
|
2024-02-17 22:37:04 +01:00
|
|
|
if ((tak_asn1 = d2i_TAK(NULL, &d, dsz)) == NULL) {
|
2023-06-29 12:53:26 +02:00
|
|
|
warnx("%s: failed to parse Trust Anchor Key", fn);
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
|
|
|
}
|
2024-02-06 08:46:25 +01:00
|
|
|
if (d != oder + dsz) {
|
2024-02-21 21:26:06 +01:00
|
|
|
warnx("%s: %td bytes trailing garbage in eContent", fn,
|
2024-02-06 08:46:25 +01:00
|
|
|
oder + dsz - d);
|
|
|
|
goto out;
|
|
|
|
}
|
2023-04-30 03:15:27 +02:00
|
|
|
|
2024-02-17 22:37:04 +01:00
|
|
|
if (!valid_econtent_version(fn, tak_asn1->version, 0))
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
|
|
|
|
2024-02-21 21:26:06 +01:00
|
|
|
tak->current = parse_takey(fn, tak_asn1->current);
|
|
|
|
if (tak->current == NULL)
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
|
|
|
|
2024-02-17 22:37:04 +01:00
|
|
|
if (tak_asn1->predecessor != NULL) {
|
2024-02-21 21:26:06 +01:00
|
|
|
tak->predecessor = parse_takey(fn, tak_asn1->predecessor);
|
|
|
|
if (tak->predecessor == NULL)
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2024-02-17 22:37:04 +01:00
|
|
|
if (tak_asn1->successor != NULL) {
|
2024-02-21 21:26:06 +01:00
|
|
|
tak->successor = parse_takey(fn, tak_asn1->successor);
|
|
|
|
if (tak->successor == NULL)
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 1;
|
|
|
|
out:
|
2024-02-17 22:37:04 +01:00
|
|
|
TAK_free(tak_asn1);
|
2023-04-30 03:15:27 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a full draft-ietf-sidrops-signed-tal file.
|
|
|
|
* Returns the TAK or NULL if the object was malformed.
|
|
|
|
*/
|
|
|
|
struct tak *
|
2023-09-28 10:40:30 +02:00
|
|
|
tak_parse(X509 **x509, const char *fn, int talid, const unsigned char *der,
|
|
|
|
size_t len)
|
2023-04-30 03:15:27 +02:00
|
|
|
{
|
2024-02-21 21:26:06 +01:00
|
|
|
struct tak *tak;
|
2023-06-20 22:38:03 +02:00
|
|
|
struct cert *cert = NULL;
|
2023-04-30 03:15:27 +02:00
|
|
|
unsigned char *cms;
|
|
|
|
size_t cmsz;
|
|
|
|
time_t signtime = 0;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
cms = cms_parse_validate(x509, fn, der, len, tak_oid, &cmsz, &signtime);
|
|
|
|
if (cms == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2024-02-21 21:26:06 +01:00
|
|
|
if ((tak = calloc(1, sizeof(struct tak))) == NULL)
|
2023-04-30 03:15:27 +02:00
|
|
|
err(1, NULL);
|
2024-02-21 21:26:06 +01:00
|
|
|
tak->signtime = signtime;
|
2023-04-30 03:15:27 +02:00
|
|
|
|
2024-02-21 21:26:06 +01:00
|
|
|
if (!x509_get_aia(*x509, fn, &tak->aia))
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
2024-02-21 21:26:06 +01:00
|
|
|
if (!x509_get_aki(*x509, fn, &tak->aki))
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
2024-02-21 21:26:06 +01:00
|
|
|
if (!x509_get_sia(*x509, fn, &tak->sia))
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
2024-02-21 21:26:06 +01:00
|
|
|
if (!x509_get_ski(*x509, fn, &tak->ski))
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
2024-02-21 21:26:06 +01:00
|
|
|
if (tak->aia == NULL || tak->aki == NULL || tak->sia == NULL ||
|
|
|
|
tak->ski == NULL) {
|
2023-04-30 03:15:27 +02:00
|
|
|
warnx("%s: RFC 6487 section 4.8: "
|
|
|
|
"missing AIA, AKI, SIA, or SKI X509 extension", fn);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2024-02-21 21:26:06 +01:00
|
|
|
if (!x509_get_notbefore(*x509, fn, &tak->notbefore))
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
2024-02-21 21:26:06 +01:00
|
|
|
if (!x509_get_notafter(*x509, fn, &tak->notafter))
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!x509_inherits(*x509)) {
|
|
|
|
warnx("%s: RFC 3779 extension not set to inherit", fn);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2024-02-21 21:26:06 +01:00
|
|
|
if (!tak_parse_econtent(fn, tak, cms, cmsz))
|
2023-04-30 03:15:27 +02:00
|
|
|
goto out;
|
|
|
|
|
2023-10-13 21:11:38 +02:00
|
|
|
if ((cert = cert_parse_ee_cert(fn, talid, *x509)) == NULL)
|
2023-06-20 22:38:03 +02:00
|
|
|
goto out;
|
|
|
|
|
2024-02-21 21:26:06 +01:00
|
|
|
if (strcmp(tak->aki, tak->current->ski) != 0) {
|
2023-04-30 03:15:27 +02:00
|
|
|
warnx("%s: current TAKey's SKI does not match EE AKI", fn);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 1;
|
|
|
|
out:
|
|
|
|
if (rc == 0) {
|
2024-02-21 21:26:06 +01:00
|
|
|
tak_free(tak);
|
|
|
|
tak = NULL;
|
2023-04-30 03:15:27 +02:00
|
|
|
X509_free(*x509);
|
|
|
|
*x509 = NULL;
|
|
|
|
}
|
2023-06-20 22:38:03 +02:00
|
|
|
cert_free(cert);
|
2023-04-30 03:15:27 +02:00
|
|
|
free(cms);
|
2024-02-21 21:26:06 +01:00
|
|
|
return tak;
|
2023-04-30 03:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free TAKey pointer.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
takey_free(struct takey *t)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (t == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < t->commentsz; i++)
|
|
|
|
free(t->comments[i]);
|
|
|
|
|
|
|
|
for (i = 0; i < t->urisz; i++)
|
|
|
|
free(t->uris[i]);
|
|
|
|
|
|
|
|
free(t->comments);
|
|
|
|
free(t->uris);
|
|
|
|
free(t->ski);
|
|
|
|
free(t->pubkey);
|
|
|
|
free(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free an TAK pointer.
|
|
|
|
* Safe to call with NULL.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
tak_free(struct tak *t)
|
|
|
|
{
|
|
|
|
if (t == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
takey_free(t->current);
|
|
|
|
takey_free(t->predecessor);
|
|
|
|
takey_free(t->successor);
|
|
|
|
|
|
|
|
free(t->aia);
|
|
|
|
free(t->aki);
|
|
|
|
free(t->ski);
|
|
|
|
free(t);
|
|
|
|
}
|