/src/mosquitto/libcommon/base64_common.c
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2012-2021 Roger Light <roger@atchoo.org> |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #ifdef WITH_TLS |
22 | | # include <openssl/opensslv.h> |
23 | | # include <openssl/evp.h> |
24 | | # include <openssl/buffer.h> |
25 | | #endif |
26 | | #include <string.h> |
27 | | |
28 | | #include "mosquitto.h" |
29 | | |
30 | | #ifdef WITH_TLS |
31 | | |
32 | | |
33 | | int mosquitto_base64_encode(const unsigned char *in, size_t in_len, char **encoded) |
34 | 462 | { |
35 | 462 | BIO *bmem, *b64; |
36 | 462 | BUF_MEM *bptr = NULL; |
37 | 462 | int rc = 1; |
38 | | |
39 | 462 | b64 = BIO_new(BIO_f_base64()); |
40 | 462 | if(b64 == NULL){ |
41 | 0 | return 1; |
42 | 0 | } |
43 | | |
44 | 462 | BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); |
45 | 462 | bmem = BIO_new(BIO_s_mem()); |
46 | 462 | if(bmem){ |
47 | 462 | b64 = BIO_push(b64, bmem); |
48 | 462 | BIO_write(b64, in, (int)in_len); |
49 | | |
50 | 462 | if(BIO_flush(b64) == 1){ |
51 | 462 | BIO_get_mem_ptr(b64, &bptr); |
52 | 462 | *encoded = mosquitto_malloc(bptr->length+1); |
53 | 462 | if(*encoded){ |
54 | 462 | memcpy(*encoded, bptr->data, bptr->length); |
55 | 462 | (*encoded)[bptr->length] = '\0'; |
56 | 462 | rc = 0; |
57 | 462 | } |
58 | 462 | } |
59 | 462 | } |
60 | 462 | BIO_free_all(b64); |
61 | | |
62 | 462 | return rc; |
63 | 462 | } |
64 | | |
65 | | |
66 | | int mosquitto_base64_decode(const char *in, unsigned char **decoded, unsigned int *decoded_len) |
67 | 29.1k | { |
68 | 29.1k | BIO *bmem, *b64; |
69 | 29.1k | size_t slen; |
70 | 29.1k | int len; |
71 | 29.1k | int rc = 1; |
72 | | |
73 | 29.1k | slen = strlen(in); |
74 | 29.1k | *decoded = NULL; |
75 | 29.1k | *decoded_len = 0; |
76 | | |
77 | 29.1k | b64 = BIO_new(BIO_f_base64()); |
78 | 29.1k | if(!b64){ |
79 | 0 | return 1; |
80 | 0 | } |
81 | | |
82 | 29.1k | BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); |
83 | 29.1k | bmem = BIO_new(BIO_s_mem()); |
84 | 29.1k | if(bmem){ |
85 | 29.1k | b64 = BIO_push(b64, bmem); |
86 | 29.1k | BIO_write(bmem, in, (int)slen); |
87 | | |
88 | 29.1k | if(BIO_flush(bmem) == 1){ |
89 | 29.1k | *decoded = mosquitto_calloc(slen, 1); |
90 | | |
91 | 29.1k | if(*decoded){ |
92 | 29.1k | len = BIO_read(b64, *decoded, (int)slen); |
93 | 29.1k | if(len > 0){ |
94 | 28.8k | *decoded_len = (unsigned int)len; |
95 | 28.8k | rc = 0; |
96 | 28.8k | }else{ |
97 | 266 | mosquitto_free(*decoded); |
98 | 266 | *decoded = NULL; |
99 | 266 | } |
100 | 29.1k | } |
101 | 29.1k | } |
102 | 29.1k | } |
103 | 29.1k | BIO_free_all(b64); |
104 | | |
105 | 29.1k | return rc; |
106 | 29.1k | } |
107 | | #endif |