2
* Copyright (C) 2014 Cisco and/or its affiliates. All rights reserved.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License version 2 as
8
* published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
#include "clamav-config.h"
35
#include <sys/types.h>
37
#include <openssl/bio.h>
38
#include <openssl/evp.h>
40
#include "libclamav/conv.h"
41
#include "libclamav/crypto.h"
43
/** Get the expected decoded length of a base64-encoded string
44
* @param[in] data Base64-encoded string
45
* @param[in] len length of the string
46
* @return The expected decoded length of the base64-encoded string
48
static size_t base64_len(const char *data, size_t len)
56
for (i=len-1; i > 0 && data[i] == '='; i--)
59
return (size_t)((3*len)/4 - padding);
62
/** Decode a base64-encoded string
63
* @param[in] data The base64-encoded string
64
* @param[in] len Length of the base64-encoded string
65
* @param[out] obuf If obuf is not set to NULL, store the decoded data in obuf. Otherwise, the decoded data is stored in a dynamically-allocated buffer.
66
* @param[out] olen The length of the decoded data
67
* @return The base64-decoded data
69
void *cl_base64_decode(char *data, size_t len, void *obuf, size_t *olen)
74
buf = (obuf) ? obuf : malloc(base64_len(data, len)+1);
78
b64 = BIO_new(BIO_f_base64());
86
bio = BIO_new_mem_buf(data, len);
95
bio = BIO_push(b64, bio);
96
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
98
*olen = BIO_read(bio, buf, base64_len(data, len));
105
/** Base64-encode data
106
* @param[in] data The data to be encoded
107
* @param[in] len The length of the data
108
* @return A pointer to the base64-encoded data. The data is stored in a dynamically-allocated buffer.
110
char *cl_base64_encode(void *data, size_t len)
116
b64 = BIO_new(BIO_f_base64());
117
bio = BIO_new(BIO_s_mem());
119
bio = BIO_push(b64, bio);
120
BIO_write(bio, data, len);
123
elen = (size_t)BIO_get_mem_data(bio, &buf);
125
/* Ensure we're dealing with a NULL-terminated string */
126
p = (char *)malloc(elen+1);
127
memcpy((void *)p, (void *)buf, elen);
136
#if defined(CONV_SELF_TEST)
138
int main(int argc, char *argv[])
140
char *plaintext, *encoded, *decoded;
141
unsigned char *sha_plaintext, *sha_decoded;
148
plaintext = (argv[1]) ? argv[1] : "Hello. This is dog";
149
sha_plaintext = sha256(plaintext, strlen(plaintext), NULL, NULL);
150
if (!(sha_plaintext)) {
151
fprintf(stderr, "Could not generate sha256 of plaintext\n");
155
encoded = base64_encode(plaintext, strlen(plaintext));
157
fprintf(stderr, "Could not base64 encode plaintest\n");
160
fprintf(stderr, "Base64 encoded: %s\n", encoded);
162
decoded = base64_decode(encoded, strlen(encoded), NULL, &len);
164
fprintf(stderr, "Could not base64 decoded string\n");
168
sha_decoded = sha256(decoded, len, NULL, &shalen);
169
if (!(sha_decoded)) {
170
fprintf(stderr, "Could not generate sha256 of decoded data\n");
174
if (memcmp(sha_plaintext, sha_decoded, shalen)) {
175
fprintf(stderr, "Decoded does not match plaintext: %s\n", decoded);