2023-09-28 08:40:30 +00:00
|
|
|
/* $OpenBSD: gbr.c,v 1.28 2023/09/25 11:08:45 tb Exp $ */
|
2023-04-30 01:15:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Claudio Jeker <claudio@openbsd.org>
|
|
|
|
*
|
|
|
|
* 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/x509.h>
|
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse results and data of the manifest file.
|
|
|
|
*/
|
|
|
|
struct parse {
|
|
|
|
const char *fn; /* manifest file name */
|
|
|
|
struct gbr *res; /* results */
|
|
|
|
};
|
|
|
|
|
|
|
|
extern ASN1_OBJECT *gbr_oid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a full RFC 6493 file and signed by the certificate "cacert"
|
|
|
|
* (the latter is optional and may be passed as NULL to disable).
|
|
|
|
* Returns the payload or NULL if the document was malformed.
|
|
|
|
*/
|
|
|
|
struct gbr *
|
2023-09-28 08:40:30 +00:00
|
|
|
gbr_parse(X509 **x509, const char *fn, int talid, const unsigned char *der,
|
|
|
|
size_t len)
|
2023-04-30 01:15:27 +00:00
|
|
|
{
|
|
|
|
struct parse p;
|
2023-06-20 20:38:03 +00:00
|
|
|
struct cert *cert = NULL;
|
2023-04-30 01:15:27 +00:00
|
|
|
size_t cmsz;
|
|
|
|
unsigned char *cms;
|
|
|
|
time_t signtime = 0;
|
|
|
|
|
|
|
|
memset(&p, 0, sizeof(struct parse));
|
|
|
|
p.fn = fn;
|
|
|
|
|
|
|
|
cms = cms_parse_validate(x509, fn, der, len, gbr_oid, &cmsz, &signtime);
|
|
|
|
if (cms == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((p.res = calloc(1, sizeof(*p.res))) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
p.res->signtime = signtime;
|
|
|
|
if ((p.res->vcard = strndup(cms, cmsz)) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
free(cms);
|
|
|
|
|
|
|
|
if (!x509_get_aia(*x509, fn, &p.res->aia))
|
|
|
|
goto out;
|
|
|
|
if (!x509_get_aki(*x509, fn, &p.res->aki))
|
|
|
|
goto out;
|
|
|
|
if (!x509_get_sia(*x509, fn, &p.res->sia))
|
|
|
|
goto out;
|
|
|
|
if (!x509_get_ski(*x509, fn, &p.res->ski))
|
|
|
|
goto out;
|
|
|
|
if (p.res->aia == NULL || p.res->aki == NULL || p.res->sia == NULL ||
|
|
|
|
p.res->ski == NULL) {
|
|
|
|
warnx("%s: RFC 6487 section 4.8: "
|
|
|
|
"missing AIA, AKI, SIA or SKI X509 extension", fn);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!x509_get_notbefore(*x509, fn, &p.res->notbefore))
|
|
|
|
goto out;
|
|
|
|
if (!x509_get_notafter(*x509, fn, &p.res->notafter))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!x509_inherits(*x509)) {
|
|
|
|
warnx("%s: RFC 3779 extension not set to inherit", fn);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-06-20 20:38:03 +00:00
|
|
|
if ((cert = cert_parse_ee_cert(fn, *x509)) == NULL)
|
|
|
|
goto out;
|
|
|
|
|
2023-04-30 01:15:27 +00:00
|
|
|
return p.res;
|
|
|
|
|
|
|
|
out:
|
|
|
|
gbr_free(p.res);
|
|
|
|
X509_free(*x509);
|
|
|
|
*x509 = NULL;
|
2023-06-20 20:38:03 +00:00
|
|
|
cert_free(cert);
|
2023-04-30 01:15:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a GBR pointer.
|
|
|
|
* Safe to call with NULL.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gbr_free(struct gbr *p)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
return;
|
|
|
|
free(p->aia);
|
|
|
|
free(p->aki);
|
|
|
|
free(p->sia);
|
|
|
|
free(p->ski);
|
|
|
|
free(p->vcard);
|
|
|
|
free(p);
|
|
|
|
}
|