2016-02-25 21:33:48 +08:00
|
|
|
/*
|
2021-06-17 20:24:59 +08:00
|
|
|
* Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2016-02-25 21:33:48 +08:00
|
|
|
*
|
2018-12-06 20:34:58 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:26 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2016-02-25 21:33:48 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/ct.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "ct_local.h"
|
2016-02-25 21:33:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Decodes the base64 string |in| into |out|.
|
|
|
|
* A new string will be malloc'd and assigned to |out|. This will be owned by
|
|
|
|
* the caller. Do not provide a pre-allocated string in |out|.
|
|
|
|
*/
|
2016-03-01 04:25:08 +08:00
|
|
|
static int ct_base64_decode(const char *in, unsigned char **out)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
2016-03-01 04:25:08 +08:00
|
|
|
size_t inlen = strlen(in);
|
2017-06-10 01:31:11 +08:00
|
|
|
int outlen, i;
|
2016-02-25 21:33:48 +08:00
|
|
|
unsigned char *outbuf = NULL;
|
|
|
|
|
|
|
|
if (inlen == 0) {
|
|
|
|
*out = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
outlen = (inlen / 4) * 3;
|
|
|
|
outbuf = OPENSSL_malloc(outlen);
|
|
|
|
if (outbuf == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
|
2016-02-25 21:33:48 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
outlen = EVP_DecodeBlock(outbuf, (unsigned char *)in, inlen);
|
|
|
|
if (outlen < 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, CT_R_BASE64_DECODE_ERROR);
|
2016-02-25 21:33:48 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-06-10 01:31:11 +08:00
|
|
|
/* Subtract padding bytes from |outlen|. Any more than 2 is malformed. */
|
|
|
|
i = 0;
|
2016-10-19 22:11:04 +08:00
|
|
|
while (in[--inlen] == '=') {
|
|
|
|
--outlen;
|
2017-06-10 01:31:11 +08:00
|
|
|
if (++i > 2)
|
|
|
|
goto err;
|
2016-10-19 22:11:04 +08:00
|
|
|
}
|
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
*out = outbuf;
|
|
|
|
return outlen;
|
|
|
|
err:
|
|
|
|
OPENSSL_free(outbuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCT *SCT_new_from_base64(unsigned char version, const char *logid_base64,
|
|
|
|
ct_log_entry_type_t entry_type, uint64_t timestamp,
|
|
|
|
const char *extensions_base64,
|
|
|
|
const char *signature_base64)
|
|
|
|
{
|
2016-03-01 04:25:08 +08:00
|
|
|
SCT *sct = SCT_new();
|
2016-02-25 21:33:48 +08:00
|
|
|
unsigned char *dec = NULL;
|
2016-10-19 22:38:20 +08:00
|
|
|
const unsigned char* p = NULL;
|
2016-02-25 21:33:48 +08:00
|
|
|
int declen;
|
|
|
|
|
|
|
|
if (sct == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
|
2016-02-25 21:33:48 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RFC6962 section 4.1 says we "MUST NOT expect this to be 0", but we
|
|
|
|
* can only construct SCT versions that have been defined.
|
|
|
|
*/
|
|
|
|
if (!SCT_set_version(sct, version)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, CT_R_SCT_UNSUPPORTED_VERSION);
|
2016-02-25 21:33:48 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-03-01 04:25:08 +08:00
|
|
|
declen = ct_base64_decode(logid_base64, &dec);
|
2016-02-25 21:33:48 +08:00
|
|
|
if (declen < 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, X509_R_BASE64_DECODE_ERROR);
|
2016-02-25 21:33:48 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!SCT_set0_log_id(sct, dec, declen))
|
|
|
|
goto err;
|
|
|
|
dec = NULL;
|
|
|
|
|
2016-03-01 04:25:08 +08:00
|
|
|
declen = ct_base64_decode(extensions_base64, &dec);
|
2016-02-25 21:33:48 +08:00
|
|
|
if (declen < 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, X509_R_BASE64_DECODE_ERROR);
|
2016-02-25 21:33:48 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
SCT_set0_extensions(sct, dec, declen);
|
|
|
|
dec = NULL;
|
|
|
|
|
2016-03-01 04:25:08 +08:00
|
|
|
declen = ct_base64_decode(signature_base64, &dec);
|
2016-02-25 21:33:48 +08:00
|
|
|
if (declen < 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, X509_R_BASE64_DECODE_ERROR);
|
2016-02-25 21:33:48 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2016-10-19 22:38:20 +08:00
|
|
|
|
|
|
|
p = dec;
|
|
|
|
if (o2i_SCT_signature(sct, &p, declen) <= 0)
|
2016-02-25 21:33:48 +08:00
|
|
|
goto err;
|
2016-02-22 22:42:09 +08:00
|
|
|
OPENSSL_free(dec);
|
|
|
|
dec = NULL;
|
2016-02-25 21:33:48 +08:00
|
|
|
|
|
|
|
SCT_set_timestamp(sct, timestamp);
|
|
|
|
|
|
|
|
if (!SCT_set_log_entry_type(sct, entry_type))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
return sct;
|
|
|
|
|
|
|
|
err:
|
|
|
|
OPENSSL_free(dec);
|
|
|
|
SCT_free(sct);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-28 04:42:25 +08:00
|
|
|
/*
|
2016-07-06 03:22:18 +08:00
|
|
|
* Allocate, build and returns a new |ct_log| from input |pkey_base64|
|
|
|
|
* It returns 1 on success,
|
|
|
|
* 0 on decoding failure, or invalid parameter if any
|
|
|
|
* -1 on internal (malloc) failure
|
2016-06-28 04:42:25 +08:00
|
|
|
*/
|
2020-09-24 17:42:23 +08:00
|
|
|
int CTLOG_new_from_base64_ex(CTLOG **ct_log, const char *pkey_base64,
|
2020-10-15 17:55:50 +08:00
|
|
|
const char *name, OSSL_LIB_CTX *libctx,
|
2020-09-24 17:42:23 +08:00
|
|
|
const char *propq)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
2016-02-22 22:42:09 +08:00
|
|
|
unsigned char *pkey_der = NULL;
|
2017-06-10 01:31:11 +08:00
|
|
|
int pkey_der_len;
|
2016-02-22 22:42:09 +08:00
|
|
|
const unsigned char *p;
|
2016-02-25 21:33:48 +08:00
|
|
|
EVP_PKEY *pkey = NULL;
|
2016-06-28 04:42:25 +08:00
|
|
|
|
|
|
|
if (ct_log == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, ERR_R_PASSED_INVALID_ARGUMENT);
|
2016-06-28 04:42:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-02-25 21:33:48 +08:00
|
|
|
|
2017-06-10 01:31:11 +08:00
|
|
|
pkey_der_len = ct_base64_decode(pkey_base64, &pkey_der);
|
|
|
|
if (pkey_der_len < 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, CT_R_LOG_CONF_INVALID_KEY);
|
2016-06-28 04:42:25 +08:00
|
|
|
return 0;
|
2016-02-25 21:33:48 +08:00
|
|
|
}
|
|
|
|
|
2016-02-22 22:42:09 +08:00
|
|
|
p = pkey_der;
|
2021-05-25 22:19:56 +08:00
|
|
|
pkey = d2i_PUBKEY_ex(NULL, &p, pkey_der_len, libctx, propq);
|
2016-02-22 22:42:09 +08:00
|
|
|
OPENSSL_free(pkey_der);
|
2016-02-25 21:33:48 +08:00
|
|
|
if (pkey == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, CT_R_LOG_CONF_INVALID_KEY);
|
2016-06-28 04:42:25 +08:00
|
|
|
return 0;
|
2016-02-25 21:33:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
*ct_log = CTLOG_new_ex(pkey, name, libctx, propq);
|
2016-06-28 04:42:25 +08:00
|
|
|
if (*ct_log == NULL) {
|
2016-02-25 21:33:48 +08:00
|
|
|
EVP_PKEY_free(pkey);
|
2016-08-06 09:27:12 +08:00
|
|
|
return 0;
|
2016-02-25 21:33:48 +08:00
|
|
|
}
|
|
|
|
|
2016-06-28 04:42:25 +08:00
|
|
|
return 1;
|
2016-02-25 21:33:48 +08:00
|
|
|
}
|
2020-04-03 23:25:18 +08:00
|
|
|
|
|
|
|
int CTLOG_new_from_base64(CTLOG **ct_log, const char *pkey_base64,
|
|
|
|
const char *name)
|
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return CTLOG_new_from_base64_ex(ct_log, pkey_base64, name, NULL, NULL);
|
2020-04-03 23:25:18 +08:00
|
|
|
}
|