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