#include "extension.h"
#include "json_util.h"
#include "libcrypto_util.h"
+#include "log.h"
static json_t *
validity2json(X509 *x)
json_t *
Certificate_any2json(ANY_t *ber)
{
- const unsigned char *tmp;
+ unsigned char const *origin, *cursor;
X509 *cert;
json_t *json;
* (https://www.openssl.org/docs/man1.0.2/crypto/d2i_X509_fp.html)
* We don't want @ber->buf modified, so use a dummy pointer.
*/
- tmp = (const unsigned char *) ber->buf;
+ origin = (unsigned char const *) ber->buf;
+ cursor = origin;
- cert = d2i_X509(NULL, &tmp, ber->size);
+ cert = d2i_X509(NULL, &cursor, ber->size);
if (cert == NULL)
return NULL;
+ if (cursor != origin + ber->size)
+ pr_op_warn("There's trailing garbage after one of the certificates.");
json = x509_to_json(cert);
return error;
}
+static X509_PUBKEY *
+decode_spki(struct tal *tal)
+{
+ X509_PUBKEY *spki = NULL;
+ unsigned char const *origin, *cursor;
+ size_t len;
+
+ fnstack_push(tal_get_file_name(tal));
+ tal_get_spki(tal, &origin, &len);
+ cursor = origin;
+ spki = d2i_X509_PUBKEY(NULL, &cursor, len);
+
+ if (spki == NULL) {
+ op_crypto_err("The public key cannot be decoded.");
+ goto fail;
+ }
+ if (cursor != origin + len) {
+ X509_PUBKEY_free(spki);
+ op_crypto_err("The public key contains trailing garbage.");
+ goto fail;
+ }
+
+ fnstack_pop();
+ return spki;
+
+fail: fnstack_pop();
+ return NULL;
+}
+
static int
root_different_alg_err(void)
{
{
struct validation *state;
struct tal *tal;
-
X509_PUBKEY *tal_spki;
- unsigned char const *_tal_spki;
- size_t _tal_spki_len;
state = state_retrieve();
* Reminder: "X509_PUBKEY" and "Subject Public Key Info" are synonyms.
*/
- fnstack_push(tal_get_file_name(tal));
- tal_get_spki(tal, &_tal_spki, &_tal_spki_len);
- tal_spki = d2i_X509_PUBKEY(NULL, &_tal_spki, _tal_spki_len);
- fnstack_pop();
-
- if (tal_spki == NULL) {
- op_crypto_err("The TAL's public key cannot be decoded");
- goto fail1;
- }
+ tal_spki = decode_spki(tal);
+ if (tal_spki == NULL)
+ return -EINVAL;
if (spki_cmp(tal_spki, cert_spki, root_different_alg_err,
- root_different_pk_err) != 0)
- goto fail2;
+ root_different_pk_err) != 0) {
+ X509_PUBKEY_free(tal_spki);
+ validation_pubkey_invalid(state);
+ return -EINVAL;
+ }
X509_PUBKEY_free(tal_spki);
validation_pubkey_valid(state);
return 0;
-
-fail2:
- X509_PUBKEY_free(tal_spki);
-fail1:
- validation_pubkey_invalid(state);
- return -EINVAL;
}
/*
static int
validate_router_spki(unsigned char *data, size_t len)
{
- unsigned char const *tmp;
+ unsigned char const *origin, *cursor;
X509_PUBKEY *spki;
X509_ALGOR *pa;
ASN1_OBJECT *alg;
int ok;
int error;
- tmp = data;
- spki = d2i_X509_PUBKEY(NULL, &tmp, len);
+ origin = data;
+ cursor = origin;
+
+ spki = d2i_X509_PUBKEY(NULL, &cursor, len);
if (spki == NULL)
return op_crypto_err("Not a valid router public key");
+ if (cursor != origin + len) {
+ X509_PUBKEY_free(spki);
+ return op_crypto_err("Router public key contains trailing garbage.");
+ }
ok = X509_PUBKEY_get0_param(&alg, NULL, NULL, &pa, spki);
if (!ok) {