utils_base64.c 4.7 KB
Newer Older
W
WangFengTu 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
3 4 5 6
 * iSulad licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
W
WangFengTu 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
10
 * See the Mulan PSL v2 for more details.
W
WangFengTu 已提交
11 12 13 14 15 16 17 18
 * Author: wangfengtu
 * Create: 2020-03-26
 * Description: provide base64 functions
 *******************************************************************************/

#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
19 20 21
#include <openssl/buffer.h>
#include <openssl/evp.h>
#include <openssl/ossl_typ.h>
W
WangFengTu 已提交
22

L
lifeng68 已提交
23
#include "isula_libutils/log.h"
W
WangFengTu 已提交
24
#include "utils_base64.h"
W
WangFengTu 已提交
25
#include "openssl/bio.h"
W
WangFengTu 已提交
26

W
WangFengTu 已提交
27
size_t util_base64_encode(unsigned char *bytes, size_t len, char *out, size_t out_len)
W
WangFengTu 已提交
28
{
W
WangFengTu 已提交
29 30 31 32 33 34
    BIO *base64 = NULL;
    BIO *io = NULL;
    size_t result_len = 0;
    int ret = 0;
    int bio_ret = 0;
    BUF_MEM *pmem = NULL;
35 36
    size_t i = 0;
    size_t count = 0;
W
WangFengTu 已提交
37 38

    if (bytes == NULL || len == 0 || out == NULL || out_len < util_base64_encode_len(len)) {
W
WangFengTu 已提交
39
        ERROR("Invalid param for encoding base64, input length %zu, out length %zu", len, out_len);
W
WangFengTu 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        return -1;
    }

    base64 = BIO_new(BIO_f_base64());
    if (base64 == NULL) {
        ERROR("bio new of base64 failed for base64 encode");
        ret = -1;
        goto out;
    }
    io = BIO_new(BIO_s_mem());
    if (io == NULL) {
        ERROR("bio new of memory failed for base64 encode");
        ret = -1;
        goto out;
    }
    io = BIO_push(base64, io);

    bio_ret = BIO_write(io, bytes, len);
    if (bio_ret <= 0) {
        ERROR("bio write failed, result is %d", bio_ret);
        ret = -1;
        goto out;
    }

    bio_ret = BIO_flush(io);
    if (bio_ret <= 0) {
        ERROR("bio flush failed, result is %d", bio_ret);
        ret = -1;
        goto out;
    }

    (void)BIO_get_mem_ptr(io, &pmem);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    // BIO_write append '\n' if every 76 chars have be output, so we need to strip them.
    for (i = 0; i < pmem->length; i++) {
        if (pmem->data[i] == '\n') {
            continue;
        }
        if (count + 1 == out_len) {
            ERROR("result length larger than output length, result length %zu, input length %zu, output length %zu",
                  pmem->length, len, out_len);
            ret = -1;
            goto out;
        }
        out[count] = pmem->data[i];
        count++;
    }

    if (count == 0) {
        ERROR("Base64 encode failed, result count is zero");
W
WangFengTu 已提交
89 90 91 92
        ret = -1;
        goto out;
    }

93 94
    out[count] = 0;
    result_len = count + 1;
W
WangFengTu 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107

out:

    if (io != NULL) {
        BIO_free_all(io);
        io = NULL;
    }

    if (ret != 0) {
        return -1;
    } else {
        return result_len;
    }
W
WangFengTu 已提交
108 109 110 111 112
}

size_t util_base64_encode_len(size_t len)
{
    if (len % 3 == 0) {
W
WangFengTu 已提交
113
        return len / 3 * 4 + 1;
W
WangFengTu 已提交
114
    } else {
W
WangFengTu 已提交
115
        return (len / 3 + 1) * 4 + 1;
W
WangFengTu 已提交
116 117 118
    }
}

L
lifeng68 已提交
119
size_t util_base64_decode_len(const char *input, size_t len)
W
WangFengTu 已提交
120 121 122 123
{
    size_t padding_count = 0;

    if (input == NULL || len < 4 || len % 4 != 0) {
L
lifeng68 已提交
124
        ERROR("Invalid param for base64 decode length, length is %ld", len);
W
WangFengTu 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137
        return -1;
    }

    if (input[len - 1] == '=') {
        padding_count++;
        if (input[len - 2] == '=') {
            padding_count++;
        }
    }

    return (strlen(input) / 4 * 3) - padding_count;
}

L
lifeng68 已提交
138
size_t util_base64_decode(const char *input, size_t len, unsigned char *out, size_t out_len)
W
WangFengTu 已提交
139 140 141 142 143 144 145 146
{
    BIO *base64 = NULL;
    BIO *io = NULL;
    int ret = 0;
    size_t result_len = util_base64_decode_len(input, len);
    size_t size = 0;

    if (input == NULL || result_len < 0 || out == 0 || result_len > out_len) {
L
lifeng68 已提交
147 148
        ERROR("Invalid param for base64 decode, input length %zu, result length %zu, output length %zu", len,
              result_len, out_len);
W
WangFengTu 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        return -1;
    }

    base64 = BIO_new(BIO_f_base64());
    if (base64 == NULL) {
        ERROR("bio new of base64 failed for base64 encode");
        ret = -1;
        goto out;
    }

    BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL);

    io = BIO_new_mem_buf(input, len);
    io = BIO_push(base64, io);

    size = BIO_read(io, out, out_len);
    if (size != result_len) {
L
lifeng68 已提交
166 167
        ERROR("base64 decode failed, actual length not match calculated length, expected %zu, got %zu", result_len,
              size);
W
WangFengTu 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181
    }

out:
    if (io != NULL) {
        BIO_free_all(io);
        io = NULL;
    }

    if (ret != 0) {
        return -1;
    } else {
        return result_len;
    }
}