Subtract padding from outlen in ct_base64_decode
[openssl.git] / crypto / ct / ct_b64.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <limits.h>
11 #include <string.h>
12
13 #include <openssl/ct.h>
14 #include <openssl/err.h>
15 #include <openssl/evp.h>
16
17 #include "ct_locl.h"
18
19 /*
20  * Decodes the base64 string |in| into |out|.
21  * A new string will be malloc'd and assigned to |out|. This will be owned by
22  * the caller. Do not provide a pre-allocated string in |out|.
23  */
24 static int ct_base64_decode(const char *in, unsigned char **out)
25 {
26     size_t inlen = strlen(in);
27     int outlen;
28     unsigned char *outbuf = NULL;
29
30     if (inlen == 0) {
31         *out = NULL;
32         return 0;
33     }
34
35     outlen = (inlen / 4) * 3;
36     outbuf = OPENSSL_malloc(outlen);
37     if (outbuf == NULL) {
38         CTerr(CT_F_CT_BASE64_DECODE, ERR_R_MALLOC_FAILURE);
39         goto err;
40     }
41
42     outlen = EVP_DecodeBlock(outbuf, (unsigned char *)in, inlen);
43     if (outlen < 0) {
44         CTerr(CT_F_CT_BASE64_DECODE, CT_R_BASE64_DECODE_ERROR);
45         goto err;
46     }
47
48     /* Subtract padding bytes from |outlen| */
49     while (in[--inlen] == '=') {
50         --outlen;
51     }
52
53     *out = outbuf;
54     return outlen;
55 err:
56     OPENSSL_free(outbuf);
57     return -1;
58 }
59
60 SCT *SCT_new_from_base64(unsigned char version, const char *logid_base64,
61                          ct_log_entry_type_t entry_type, uint64_t timestamp,
62                          const char *extensions_base64,
63                          const char *signature_base64)
64 {
65     SCT *sct = SCT_new();
66     unsigned char *dec = NULL;
67     int declen;
68
69     if (sct == NULL) {
70         CTerr(CT_F_SCT_NEW_FROM_BASE64, ERR_R_MALLOC_FAILURE);
71         return NULL;
72     }
73
74     /*
75      * RFC6962 section 4.1 says we "MUST NOT expect this to be 0", but we
76      * can only construct SCT versions that have been defined.
77      */
78     if (!SCT_set_version(sct, version)) {
79         CTerr(CT_F_SCT_NEW_FROM_BASE64, CT_R_SCT_UNSUPPORTED_VERSION);
80         goto err;
81     }
82
83     declen = ct_base64_decode(logid_base64, &dec);
84     if (declen < 0) {
85         CTerr(CT_F_SCT_NEW_FROM_BASE64, X509_R_BASE64_DECODE_ERROR);
86         goto err;
87     }
88     if (!SCT_set0_log_id(sct, dec, declen))
89         goto err;
90     dec = NULL;
91
92     declen = ct_base64_decode(extensions_base64, &dec);
93     if (declen < 0) {
94         CTerr(CT_F_SCT_NEW_FROM_BASE64, X509_R_BASE64_DECODE_ERROR);
95         goto err;
96     }
97     SCT_set0_extensions(sct, dec, declen);
98     dec = NULL;
99
100     declen = ct_base64_decode(signature_base64, &dec);
101     if (declen < 0) {
102         CTerr(CT_F_SCT_NEW_FROM_BASE64, X509_R_BASE64_DECODE_ERROR);
103         goto err;
104     }
105     if (o2i_SCT_signature(sct, (const unsigned char **)&dec, declen) <= 0)
106         goto err;
107     OPENSSL_free(dec);
108     dec = NULL;
109
110     SCT_set_timestamp(sct, timestamp);
111
112     if (!SCT_set_log_entry_type(sct, entry_type))
113         goto err;
114
115     return sct;
116
117  err:
118     OPENSSL_free(dec);
119     SCT_free(sct);
120     return NULL;
121 }
122
123 /*
124  * Allocate, build and returns a new |ct_log| from input |pkey_base64|
125  * It returns 1 on success,
126  * 0 on decoding failure, or invalid parameter if any
127  * -1 on internal (malloc) failure
128  */
129 int CTLOG_new_from_base64(CTLOG **ct_log, const char *pkey_base64, const char *name)
130 {
131     unsigned char *pkey_der = NULL;
132     int pkey_der_len = ct_base64_decode(pkey_base64, &pkey_der);
133     const unsigned char *p;
134     EVP_PKEY *pkey = NULL;
135
136     if (ct_log == NULL) {
137         CTerr(CT_F_CTLOG_NEW_FROM_BASE64, ERR_R_PASSED_INVALID_ARGUMENT);
138         return 0;
139     }
140
141     if (pkey_der_len <= 0) {
142         CTerr(CT_F_CTLOG_NEW_FROM_BASE64, CT_R_LOG_CONF_INVALID_KEY);
143         return 0;
144     }
145
146     p = pkey_der;
147     pkey = d2i_PUBKEY(NULL, &p, pkey_der_len);
148     OPENSSL_free(pkey_der);
149     if (pkey == NULL) {
150         CTerr(CT_F_CTLOG_NEW_FROM_BASE64, CT_R_LOG_CONF_INVALID_KEY);
151         return 0;
152     }
153
154     *ct_log = CTLOG_new(pkey, name);
155     if (*ct_log == NULL) {
156         EVP_PKEY_free(pkey);
157         return 0;
158     }
159
160     return 1;
161 }