| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_BASE64_H |
| 5 |
#define OPENSSL_HEADER_BASE64_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#if defined(__cplusplus) |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
|
| 13 |
|
| 14 |
// base64 functions. |
| 15 |
// |
| 16 |
// For historical reasons, these functions have the EVP_ prefix but just do |
| 17 |
// base64 encoding and decoding. Note that BoringSSL is a cryptography library, |
| 18 |
// so these functions are implemented with side channel protections, at a |
| 19 |
// performance cost. For other base64 uses, use a general-purpose base64 |
| 20 |
// implementation. |
| 21 |
|
| 22 |
|
| 23 |
// Encoding |
| 24 |
|
| 25 |
// EVP_EncodeBlock encodes |src_len| bytes from |src| and writes the |
| 26 |
// result to |dst| with a trailing NUL. It returns the number of bytes |
| 27 |
// written, not including this trailing NUL. |
| 28 |
OPENSSL_EXPORT size_t EVP_EncodeBlock(uint8_t *dst, const uint8_t *src, |
| 29 |
size_t src_len); |
| 30 |
|
| 31 |
// EVP_EncodedLength sets |*out_len| to the number of bytes that will be needed |
| 32 |
// to call |EVP_EncodeBlock| on an input of length |len|. This includes the |
| 33 |
// final NUL that |EVP_EncodeBlock| writes. It returns one on success or zero |
| 34 |
// on error. |
| 35 |
OPENSSL_EXPORT int EVP_EncodedLength(size_t *out_len, size_t len); |
| 36 |
|
| 37 |
|
| 38 |
// Decoding |
| 39 |
|
| 40 |
// EVP_DecodedLength sets |*out_len| to the maximum number of bytes that will |
| 41 |
// be needed to call |EVP_DecodeBase64| on an input of length |len|. It returns |
| 42 |
// one on success or zero if |len| is not a valid length for a base64-encoded |
| 43 |
// string. |
| 44 |
OPENSSL_EXPORT int EVP_DecodedLength(size_t *out_len, size_t len); |
| 45 |
|
| 46 |
// EVP_DecodeBase64 decodes |in_len| bytes from base64 and writes |
| 47 |
// |*out_len| bytes to |out|. |max_out| is the size of the output |
| 48 |
// buffer. If it is not enough for the maximum output size, the |
| 49 |
// operation fails. It returns one on success or zero on error. |
| 50 |
OPENSSL_EXPORT int EVP_DecodeBase64(uint8_t *out, size_t *out_len, |
| 51 |
size_t max_out, const uint8_t *in, |
| 52 |
size_t in_len); |
| 53 |
|
| 54 |
|
| 55 |
// Deprecated functions. |
| 56 |
// |
| 57 |
// OpenSSL provides a streaming base64 implementation, however its behavior is |
| 58 |
// very specific to PEM. It is also very lenient of invalid input. Use of any of |
| 59 |
// these functions is thus deprecated. |
| 60 |
|
| 61 |
// EVP_ENCODE_CTX_new returns a newly-allocated |EVP_ENCODE_CTX| or NULL on |
| 62 |
// error. The caller must release the result with |EVP_ENCODE_CTX_free| when |
| 63 |
// done. |
| 64 |
OPENSSL_EXPORT EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void); |
| 65 |
|
| 66 |
// EVP_ENCODE_CTX_free releases memory associated with |ctx|. |
| 67 |
OPENSSL_EXPORT void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx); |
| 68 |
|
| 69 |
// EVP_EncodeInit initialises |*ctx|, which is typically stack |
| 70 |
// allocated, for an encoding operation. |
| 71 |
// |
| 72 |
// NOTE: The encoding operation breaks its output with newlines every |
| 73 |
// 64 characters of output (48 characters of input). Use |
| 74 |
// EVP_EncodeBlock to encode raw base64. |
| 75 |
OPENSSL_EXPORT void EVP_EncodeInit(EVP_ENCODE_CTX *ctx); |
| 76 |
|
| 77 |
// EVP_EncodeUpdate encodes |in_len| bytes from |in| and writes an encoded |
| 78 |
// version of them to |out| and sets |*out_len| to the number of bytes written. |
| 79 |
// Some state may be contained in |ctx| so |EVP_EncodeFinal| must be used to |
| 80 |
// flush it before using the encoded data. |
| 81 |
OPENSSL_EXPORT int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, |
| 82 |
int *out_len, const uint8_t *in, |
| 83 |
size_t in_len); |
| 84 |
|
| 85 |
// EVP_EncodeFinal flushes any remaining output bytes from |ctx| to |out| and |
| 86 |
// sets |*out_len| to the number of bytes written. |
| 87 |
OPENSSL_EXPORT void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, |
| 88 |
int *out_len); |
| 89 |
|
| 90 |
// EVP_DecodeInit initialises |*ctx|, which is typically stack allocated, for |
| 91 |
// a decoding operation. |
| 92 |
// |
| 93 |
// TODO(davidben): This isn't a straight-up base64 decode either. Document |
| 94 |
// and/or fix exactly what's going on here; maximum line length and such. |
| 95 |
OPENSSL_EXPORT void EVP_DecodeInit(EVP_ENCODE_CTX *ctx); |
| 96 |
|
| 97 |
// EVP_DecodeUpdate decodes |in_len| bytes from |in| and writes the decoded |
| 98 |
// data to |out| and sets |*out_len| to the number of bytes written. Some state |
| 99 |
// may be contained in |ctx| so |EVP_DecodeFinal| must be used to flush it |
| 100 |
// before using the encoded data. |
| 101 |
// |
| 102 |
// It returns -1 on error, one if a full line of input was processed and zero |
| 103 |
// if the line was short (i.e. it was the last line). |
| 104 |
OPENSSL_EXPORT int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, |
| 105 |
int *out_len, const uint8_t *in, |
| 106 |
size_t in_len); |
| 107 |
|
| 108 |
// EVP_DecodeFinal flushes any remaining output bytes from |ctx| to |out| and |
| 109 |
// sets |*out_len| to the number of bytes written. It returns one on success |
| 110 |
// and minus one on error. |
| 111 |
OPENSSL_EXPORT int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, |
| 112 |
int *out_len); |
| 113 |
|
| 114 |
// EVP_DecodeBlock encodes |src_len| bytes from |src| and writes the result to |
| 115 |
// |dst|. It returns the number of bytes written or -1 on error. |
| 116 |
// |
| 117 |
// WARNING: EVP_DecodeBlock's return value does not take padding into |
| 118 |
// account. It also strips leading whitespace and trailing |
| 119 |
// whitespace and minuses. |
| 120 |
OPENSSL_EXPORT int EVP_DecodeBlock(uint8_t *dst, const uint8_t *src, |
| 121 |
size_t src_len); |
| 122 |
|
| 123 |
|
| 124 |
struct evp_encode_ctx_st { |
| 125 |
// data_used indicates the number of bytes of |data| that are valid. When |
| 126 |
// encoding, |data| will be filled and encoded as a lump. When decoding, only |
| 127 |
// the first four bytes of |data| will be used. |
| 128 |
unsigned data_used; |
| 129 |
uint8_t data[48]; |
| 130 |
|
| 131 |
// eof_seen indicates that the end of the base64 data has been seen when |
| 132 |
// decoding. Only whitespace can follow. |
| 133 |
char eof_seen; |
| 134 |
|
| 135 |
// error_encountered indicates that invalid base64 data was found. This will |
| 136 |
// cause all future calls to fail. |
| 137 |
char error_encountered; |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
#if defined(__cplusplus) |
| 142 |
} // extern C |
| 143 |
#endif |
| 144 |
|
| 145 |
#endif // OPENSSL_HEADER_BASE64_H |