2016-02-25 13:33:48 +00:00
|
|
|
/*
|
2016-05-17 18:51:26 +00:00
|
|
|
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
|
2016-02-25 13:33:48 +00:00
|
|
|
*
|
2016-05-17 18:51:26 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* 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 13:33:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/ct.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
2016-02-29 20:26:36 +00:00
|
|
|
#include "ct_locl.h"
|
2016-02-25 13:33:48 +00: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-02-29 20:25:08 +00:00
|
|
|
static int ct_base64_decode(const char *in, unsigned char **out)
|
2016-02-25 13:33:48 +00:00
|
|
|
{
|
2016-02-29 20:25:08 +00:00
|
|
|
size_t inlen = strlen(in);
|
2016-02-25 13:33:48 +00:00
|
|
|
int outlen;
|
|
|
|
unsigned char *outbuf = NULL;
|
|
|
|
|
|
|
|
if (inlen == 0) {
|
|
|
|
*out = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
outlen = (inlen / 4) * 3;
|
|
|
|
outbuf = OPENSSL_malloc(outlen);
|
|
|
|
if (outbuf == NULL) {
|
|
|
|
CTerr(CT_F_CT_BASE64_DECODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
outlen = EVP_DecodeBlock(outbuf, (unsigned char *)in, inlen);
|
|
|
|
if (outlen < 0) {
|
|
|
|
CTerr(CT_F_CT_BASE64_DECODE, CT_R_BASE64_DECODE_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
*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-02-29 20:25:08 +00:00
|
|
|
SCT *sct = SCT_new();
|
2016-02-25 13:33:48 +00:00
|
|
|
unsigned char *dec = NULL;
|
|
|
|
int declen;
|
|
|
|
|
|
|
|
if (sct == NULL) {
|
|
|
|
CTerr(CT_F_SCT_NEW_FROM_BASE64, ERR_R_MALLOC_FAILURE);
|
|
|
|
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)) {
|
|
|
|
CTerr(CT_F_SCT_NEW_FROM_BASE64, CT_R_SCT_UNSUPPORTED_VERSION);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-02-29 20:25:08 +00:00
|
|
|
declen = ct_base64_decode(logid_base64, &dec);
|
2016-02-25 13:33:48 +00:00
|
|
|
if (declen < 0) {
|
|
|
|
CTerr(CT_F_SCT_NEW_FROM_BASE64, X509_R_BASE64_DECODE_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!SCT_set0_log_id(sct, dec, declen))
|
|
|
|
goto err;
|
|
|
|
dec = NULL;
|
|
|
|
|
2016-02-29 20:25:08 +00:00
|
|
|
declen = ct_base64_decode(extensions_base64, &dec);
|
2016-02-25 13:33:48 +00:00
|
|
|
if (declen < 0) {
|
|
|
|
CTerr(CT_F_SCT_NEW_FROM_BASE64, X509_R_BASE64_DECODE_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
SCT_set0_extensions(sct, dec, declen);
|
|
|
|
dec = NULL;
|
|
|
|
|
2016-02-29 20:25:08 +00:00
|
|
|
declen = ct_base64_decode(signature_base64, &dec);
|
2016-02-25 13:33:48 +00:00
|
|
|
if (declen < 0) {
|
|
|
|
CTerr(CT_F_SCT_NEW_FROM_BASE64, X509_R_BASE64_DECODE_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (o2i_SCT_signature(sct, (const unsigned char **)&dec, declen) <= 0)
|
|
|
|
goto err;
|
2016-02-22 14:42:09 +00:00
|
|
|
OPENSSL_free(dec);
|
|
|
|
dec = NULL;
|
2016-02-25 13:33:48 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTLOG *CTLOG_new_from_base64(const char *pkey_base64, const char *name)
|
|
|
|
{
|
2016-02-22 14:42:09 +00:00
|
|
|
unsigned char *pkey_der = NULL;
|
2016-02-29 20:25:08 +00:00
|
|
|
int pkey_der_len = ct_base64_decode(pkey_base64, &pkey_der);
|
2016-02-22 14:42:09 +00:00
|
|
|
const unsigned char *p;
|
2016-02-25 13:33:48 +00:00
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
CTLOG *log = NULL;
|
|
|
|
|
|
|
|
if (pkey_der_len <= 0) {
|
|
|
|
CTerr(CT_F_CTLOG_NEW_FROM_BASE64, CT_R_LOG_CONF_INVALID_KEY);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-22 14:42:09 +00:00
|
|
|
p = pkey_der;
|
|
|
|
pkey = d2i_PUBKEY(NULL, &p, pkey_der_len);
|
|
|
|
OPENSSL_free(pkey_der);
|
2016-02-25 13:33:48 +00:00
|
|
|
if (pkey == NULL) {
|
|
|
|
CTerr(CT_F_CTLOG_NEW_FROM_BASE64, CT_R_LOG_CONF_INVALID_KEY);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
log = CTLOG_new(pkey, name);
|
|
|
|
if (log == NULL) {
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return log;
|
|
|
|
}
|