Coverage Report

Created: 2023-06-07 06:10

/src/mosquitto/common/base64_mosq.c
Line
Count
Source (jump to first uncovered line)
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
#include <errno.h>
22
#ifdef WITH_TLS
23
#  include <openssl/opensslv.h>
24
#  include <openssl/evp.h>
25
#  include <openssl/rand.h>
26
#  include <openssl/buffer.h>
27
#endif
28
#include <signal.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
33
#include "mosquitto.h"
34
#include "mosquitto_broker.h"
35
#include "base64_mosq.h"
36
#include "memory_mosq.h"
37
38
#ifdef WITH_TLS
39
int base64__encode(const unsigned char *in, size_t in_len, char **encoded)
40
0
{
41
0
  BIO *bmem, *b64;
42
0
  BUF_MEM *bptr = NULL;
43
44
0
  b64 = BIO_new(BIO_f_base64());
45
0
  if(b64 == NULL) return 1;
46
47
0
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
48
0
  bmem = BIO_new(BIO_s_mem());
49
0
  if(bmem == NULL){
50
0
    BIO_free_all(b64);
51
0
    return 1;
52
0
  }
53
0
  b64 = BIO_push(b64, bmem);
54
0
  BIO_write(b64, in, (int)in_len);
55
0
  if(BIO_flush(b64) != 1){
56
0
    BIO_free_all(b64);
57
0
    return 1;
58
0
  }
59
0
  BIO_get_mem_ptr(b64, &bptr);
60
0
  *encoded = malloc(bptr->length+1);
61
0
  if(!(*encoded)){
62
0
    BIO_free_all(b64);
63
0
    return 1;
64
0
  }
65
0
  memcpy(*encoded, bptr->data, bptr->length);
66
0
  (*encoded)[bptr->length] = '\0';
67
0
  BIO_free_all(b64);
68
69
0
  return 0;
70
0
}
71
72
73
int base64__decode(const char *in, unsigned char **decoded, unsigned int *decoded_len)
74
2.69k
{
75
2.69k
  BIO *bmem, *b64;
76
2.69k
  size_t slen;
77
2.69k
  int len;
78
79
2.69k
  slen = strlen(in);
80
81
2.69k
  b64 = BIO_new(BIO_f_base64());
82
2.69k
  if(!b64){
83
0
    return 1;
84
0
  }
85
2.69k
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
86
87
2.69k
  bmem = BIO_new(BIO_s_mem());
88
2.69k
  if(!bmem){
89
0
    BIO_free_all(b64);
90
0
    return 1;
91
0
  }
92
2.69k
  b64 = BIO_push(b64, bmem);
93
2.69k
  BIO_write(bmem, in, (int)slen);
94
95
2.69k
  if(BIO_flush(bmem) != 1){
96
0
    BIO_free_all(b64);
97
0
    return 1;
98
0
  }
99
2.69k
  *decoded = calloc(slen, 1);
100
2.69k
  if(!(*decoded)){
101
0
    BIO_free_all(b64);
102
0
    return 1;
103
0
  }
104
2.69k
  len = BIO_read(b64, *decoded, (int)slen);
105
2.69k
  BIO_free_all(b64);
106
107
2.69k
  if(len <= 0){
108
919
    SAFE_FREE(*decoded);
109
919
    *decoded_len = 0;
110
919
    return 1;
111
919
  }
112
1.77k
  *decoded_len = (unsigned int)len;
113
114
1.77k
  return 0;
115
2.69k
}
116
#endif