boring_imp/
base64.rs

1//! Base64 encoding support.
2use crate::cvt_n;
3use crate::error::ErrorStack;
4use crate::ffi;
5use libc::c_int;
6
7/// Encodes a slice of bytes to a base64 string.
8///
9/// This corresponds to [`EVP_EncodeBlock`].
10///
11/// # Panics
12///
13/// Panics if the input length or computed output length overflow a signed C integer.
14///
15/// [`EVP_EncodeBlock`]: https://www.openssl.org/docs/man1.1.1/man3/EVP_DecodeBlock.html
16pub fn encode_block(src: &[u8]) -> String {
17    assert!(src.len() <= c_int::MAX as usize);
18    let src_len = src.len();
19
20    let len = encoded_len(src_len).unwrap();
21    let mut out = Vec::with_capacity(len);
22
23    // SAFETY: `encoded_len` ensures space for 4 output characters
24    // for every 3 input bytes including padding and nul terminator.
25    // `EVP_EncodeBlock` will write only single byte ASCII characters.
26    // `EVP_EncodeBlock` will only write to not read from `out`.
27    unsafe {
28        let out_len = ffi::EVP_EncodeBlock(out.as_mut_ptr(), src.as_ptr(), src_len);
29        out.set_len(out_len);
30        String::from_utf8_unchecked(out)
31    }
32}
33
34/// Decodes a base64-encoded string to bytes.
35///
36/// This corresponds to [`EVP_DecodeBlock`].
37///
38/// # Panics
39///
40/// Panics if the input length or computed output length overflow a signed C integer.
41///
42/// [`EVP_DecodeBlock`]: https://www.openssl.org/docs/man1.1.1/man3/EVP_DecodeBlock.html
43pub fn decode_block(src: &str) -> Result<Vec<u8>, ErrorStack> {
44    let src = src.trim();
45
46    // https://github.com/openssl/openssl/issues/12143
47    if src.is_empty() {
48        return Ok(vec![]);
49    }
50
51    assert!(src.len() <= c_int::MAX as usize);
52    let src_len = src.len();
53
54    let len = decoded_len(src_len).unwrap();
55    let mut out = Vec::with_capacity(len);
56
57    // SAFETY: `decoded_len` ensures space for 3 output bytes
58    // for every 4 input characters including padding.
59    // `EVP_DecodeBlock` can write fewer bytes after stripping
60    // leading and trailing whitespace, but never more.
61    // `EVP_DecodeBlock` will only write to not read from `out`.
62    unsafe {
63        let out_len = cvt_n(ffi::EVP_DecodeBlock(
64            out.as_mut_ptr(),
65            src.as_ptr(),
66            src_len,
67        ))?;
68        out.set_len(out_len as usize);
69    }
70
71    if src.ends_with('=') {
72        out.pop();
73        if src.ends_with("==") {
74            out.pop();
75        }
76    }
77
78    Ok(out)
79}
80
81fn encoded_len(src_len: usize) -> Option<usize> {
82    let mut len = (src_len / 3).checked_mul(4)?;
83
84    if src_len % 3 != 0 {
85        len = len.checked_add(4)?;
86    }
87
88    len = len.checked_add(1)?;
89
90    Some(len)
91}
92
93fn decoded_len(src_len: usize) -> Option<usize> {
94    let mut len = (src_len / 4).checked_mul(3)?;
95
96    if src_len % 4 != 0 {
97        len = len.checked_add(3)?;
98    }
99
100    Some(len)
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn test_encode_block() {
109        assert_eq!("".to_string(), encode_block(b""));
110        assert_eq!("Zg==".to_string(), encode_block(b"f"));
111        assert_eq!("Zm8=".to_string(), encode_block(b"fo"));
112        assert_eq!("Zm9v".to_string(), encode_block(b"foo"));
113        assert_eq!("Zm9vYg==".to_string(), encode_block(b"foob"));
114        assert_eq!("Zm9vYmE=".to_string(), encode_block(b"fooba"));
115        assert_eq!("Zm9vYmFy".to_string(), encode_block(b"foobar"));
116    }
117
118    #[test]
119    fn test_decode_block() {
120        assert_eq!(b"".to_vec(), decode_block("").unwrap());
121        assert_eq!(b"f".to_vec(), decode_block("Zg==").unwrap());
122        assert_eq!(b"fo".to_vec(), decode_block("Zm8=").unwrap());
123        assert_eq!(b"foo".to_vec(), decode_block("Zm9v").unwrap());
124        assert_eq!(b"foob".to_vec(), decode_block("Zm9vYg==").unwrap());
125        assert_eq!(b"fooba".to_vec(), decode_block("Zm9vYmE=").unwrap());
126        assert_eq!(b"foobar".to_vec(), decode_block("Zm9vYmFy").unwrap());
127    }
128
129    #[test]
130    fn test_strip_whitespace() {
131        assert_eq!(b"foobar".to_vec(), decode_block(" Zm9vYmFy\n").unwrap());
132        assert_eq!(b"foob".to_vec(), decode_block(" Zm9vYg==\n").unwrap());
133    }
134}