#include <openssl/err.h>
#include <openssl/evp.h>
-/*
- * TODO(robpercival): These macros are getting duplicated all over the place.
- * Is there a single place they should be defined for re-use?
- * Also, is there a good reason they aren't functions?
- */
-#define n2s(c,s) ((s=(((unsigned int)((c)[0]))<<8) | \
- (((unsigned int)((c)[1])))), \
- c+=2)
+#include "ct_locl.h"
/*
* 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|.
*/
-static int CT_base64_decode(const char *in, unsigned char **out)
+static int ct_base64_decode(const char *in, unsigned char **out)
{
- size_t inlen;
+ size_t inlen = strlen(in);
int outlen;
unsigned char *outbuf = NULL;
- if (in == NULL || out == NULL) {
- CTerr(CT_F_CT_BASE64_DECODE, ERR_R_PASSED_NULL_PARAMETER);
- goto err;
- }
-
- inlen = strlen(in);
if (inlen == 0) {
*out = NULL;
return 0;
outlen = EVP_DecodeBlock(outbuf, (unsigned char *)in, inlen);
if (outlen < 0) {
- OPENSSL_free(outbuf);
CTerr(CT_F_CT_BASE64_DECODE, CT_R_BASE64_DECODE_ERROR);
goto err;
}
const char *extensions_base64,
const char *signature_base64)
{
- SCT *sct;
+ SCT *sct = SCT_new();
unsigned char *dec = NULL;
int declen;
- if (logid_base64 == NULL ||
- extensions_base64 == NULL ||
- signature_base64 == NULL) {
- CTerr(CT_F_SCT_NEW_FROM_BASE64, ERR_R_PASSED_NULL_PARAMETER);
- return NULL;
- }
-
- sct = SCT_new();
if (sct == NULL) {
CTerr(CT_F_SCT_NEW_FROM_BASE64, ERR_R_MALLOC_FAILURE);
return NULL;
goto err;
}
- declen = CT_base64_decode(logid_base64, &dec);
+ declen = ct_base64_decode(logid_base64, &dec);
if (declen < 0) {
CTerr(CT_F_SCT_NEW_FROM_BASE64, X509_R_BASE64_DECODE_ERROR);
goto err;
goto err;
dec = NULL;
- declen = CT_base64_decode(extensions_base64, &dec);
+ declen = ct_base64_decode(extensions_base64, &dec);
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;
- declen = CT_base64_decode(signature_base64, &dec);
+ declen = ct_base64_decode(signature_base64, &dec);
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;
+ OPENSSL_free(dec);
+ dec = NULL;
SCT_set_timestamp(sct, timestamp);
CTLOG *CTLOG_new_from_base64(const char *pkey_base64, const char *name)
{
- unsigned char *pkey_der;
- int pkey_der_len;
+ unsigned char *pkey_der = NULL;
+ int pkey_der_len = ct_base64_decode(pkey_base64, &pkey_der);
+ const unsigned char *p;
EVP_PKEY *pkey = NULL;
CTLOG *log = NULL;
- pkey_der_len = CT_base64_decode(pkey_base64, &pkey_der);
if (pkey_der_len <= 0) {
CTerr(CT_F_CTLOG_NEW_FROM_BASE64, CT_R_LOG_CONF_INVALID_KEY);
return NULL;
}
- pkey = d2i_PUBKEY(NULL, (const unsigned char **)&pkey_der, pkey_der_len);
+ p = pkey_der;
+ pkey = d2i_PUBKEY(NULL, &p, pkey_der_len);
+ OPENSSL_free(pkey_der);
if (pkey == NULL) {
CTerr(CT_F_CTLOG_NEW_FROM_BASE64, CT_R_LOG_CONF_INVALID_KEY);
return NULL;