X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=crypto%2Fct%2Fct_b64.c;h=f0bf3aff29d2686d2e0945149b85c35baa072e96;hb=eea5f335b81d89554272a1fab8de9e40656509da;hp=a257b8f0d52cf4a79fc494d9c504cf1e6da8b244;hpb=8c6afbc55cc8e2d036c0af5adbaff82d8117c6b2;p=oweals%2Fopenssl.git diff --git a/crypto/ct/ct_b64.c b/crypto/ct/ct_b64.c index a257b8f0d5..f0bf3aff29 100644 --- a/crypto/ct/ct_b64.c +++ b/crypto/ct/ct_b64.c @@ -1,59 +1,10 @@ /* - * Written by Rob Stradling (rob@comodo.com) and Stephen Henson - * (steve@openssl.org) for the OpenSSL project 2014. - */ -/* ==================================================================== - * Copyright (c) 2014 The OpenSSL Project. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. All advertising materials mentioning features or use of this - * software must display the following acknowledgment: - * "This product includes software developed by the OpenSSL Project - * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" - * - * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to - * endorse or promote products derived from this software without - * prior written permission. For written permission, please contact - * licensing@OpenSSL.org. - * - * 5. Products derived from this software may not be called "OpenSSL" - * nor may "OpenSSL" appear in their names without prior written - * permission of the OpenSSL Project. - * - * 6. Redistributions of any form whatsoever must retain the following - * acknowledgment: - * "This product includes software developed by the OpenSSL Project - * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" - * - * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY - * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * ==================================================================== - * - * This product includes cryptographic software written by Eric Young - * (eay@cryptsoft.com). This product includes software written by Tim - * Hudson (tjh@cryptsoft.com). + * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. * + * 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 */ #include @@ -63,32 +14,19 @@ #include #include -/* - * 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; @@ -103,11 +41,15 @@ static int CT_base64_decode(const char *in, unsigned char **out) 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; } + /* Subtract padding bytes from |outlen| */ + while (in[--inlen] == '=') { + --outlen; + } + *out = outbuf; return outlen; err: @@ -120,18 +62,11 @@ SCT *SCT_new_from_base64(unsigned char version, const char *logid_base64, const char *extensions_base64, const char *signature_base64) { - SCT *sct; + SCT *sct = SCT_new(); unsigned char *dec = NULL; + const unsigned char* p = 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; @@ -146,7 +81,7 @@ SCT *SCT_new_from_base64(unsigned char version, const char *logid_base64, 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; @@ -155,7 +90,7 @@ SCT *SCT_new_from_base64(unsigned char version, const char *logid_base64, 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; @@ -163,13 +98,17 @@ SCT *SCT_new_from_base64(unsigned char version, const char *logid_base64, 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) + + p = dec; + if (o2i_SCT_signature(sct, &p, declen) <= 0) goto err; + OPENSSL_free(dec); + dec = NULL; SCT_set_timestamp(sct, timestamp); @@ -184,30 +123,42 @@ SCT *SCT_new_from_base64(unsigned char version, const char *logid_base64, return NULL; } -CTLOG *CTLOG_new_from_base64(const char *pkey_base64, const char *name) +/* + * 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 + */ +int CTLOG_new_from_base64(CTLOG **ct_log, 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 (ct_log == NULL) { + CTerr(CT_F_CTLOG_NEW_FROM_BASE64, ERR_R_PASSED_INVALID_ARGUMENT); + return 0; + } + if (pkey_der_len <= 0) { CTerr(CT_F_CTLOG_NEW_FROM_BASE64, CT_R_LOG_CONF_INVALID_KEY); - return NULL; + return 0; } - 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; + return 0; } - log = CTLOG_new(pkey, name); - if (log == NULL) { + *ct_log = CTLOG_new(pkey, name); + if (*ct_log == NULL) { EVP_PKEY_free(pkey); - return NULL; + return 0; } - return log; + return 1; }