| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use ascii; |
| 5 |
use bufio; |
| 6 |
use encoding::base64; |
| 7 |
use encoding::utf8; |
| 8 |
use errors; |
| 9 |
use fmt; |
| 10 |
use io; |
| 11 |
use memio; |
| 12 |
use strings; |
| 13 |
|
| 14 |
|
| 15 |
def BEGIN: str = "-----BEGIN "; |
| 16 |
def END: str = "-----END "; |
| 17 |
def SUFFIX: str = "-----"; |
| 18 |
|
| 19 |
export type decoder = struct { |
| 20 |
in: b64stream, |
| 21 |
label: memio::stream, |
| 22 |
}; |
| 23 |
|
| 24 |
export type b64stream = struct { |
| 25 |
stream: io::stream, |
| 26 |
in: bufio::scanner, |
| 27 |
}; |
| 28 |
|
| 29 |
export type pemdecoder = struct { |
| 30 |
stream: io::stream, |
| 31 |
b64: base64::decoder, |
| 32 |
}; |
| 33 |
|
| 34 |
const pemdecoder_vt: io::vtable = io::vtable { |
| 35 |
reader = &pem_read, |
| 36 |
... |
| 37 |
}; |
| 38 |
|
| 39 |
const b64stream_r_vt: io::vtable = io::vtable { |
| 40 |
reader = &b64_read, |
| 41 |
... |
| 42 |
}; |
| 43 |
|
| 44 |
// Creates a new PEM decoder. The caller must either read it until it returns |
| 45 |
// [[io::EOF]], or call [[finish]] to free state associated with the parser. |
| 46 |
export fn newdecoder(in: io::handle) decoder = { |
| 47 |
return decoder { |
| 48 |
in = b64stream { |
| 49 |
stream = &b64stream_r_vt, |
| 50 |
in = bufio::newscanner(in), |
| 51 |
}, |
| 52 |
label = memio::dynamic(), |
| 53 |
}; |
| 54 |
}; |
| 55 |
|
| 56 |
// Frees state associated with this [[decoder]]. |
| 57 |
export fn finish(dec: *decoder) void = { |
| 58 |
io::close(&dec.label)!; |
| 59 |
bufio::finish(&dec.in.in); |
| 60 |
}; |
| 61 |
|
| 62 |
// Converts an I/O error returned from a PEM decoder into a human-friendly |
| 63 |
// string. |
| 64 |
export fn strerror(err: io::error) const str = { |
| 65 |
match (err) { |
| 66 |
case errors::invalid => |
| 67 |
return "Invalid PEM data"; |
| 68 |
case => |
| 69 |
return io::strerror(err); |
| 70 |
}; |
| 71 |
}; |
| 72 |
|
| 73 |
// Finds the next PEM boundary in the stream, ignoring any non-PEM data, and |
| 74 |
// returns the label and a [[pemdecoder]] from which the encoded data may be |
| 75 |
// read, or [[io::EOF]] if no further PEM boundaries are found. The user must |
| 76 |
// completely read the pemdecoder until it returns [[io::EOF]] before calling |
| 77 |
// [[next]] again. |
| 78 |
// |
| 79 |
// The label returned by this function is borrowed from the decoder state and |
| 80 |
// does not contain "-----BEGIN " or "-----". |
| 81 |
export fn next(dec: *decoder) ((str, pemdecoder) | io::EOF | io::error) = { |
| 82 |
for (let line => bufio::scan_line(&dec.in.in)) { |
| 83 |
const line = match (line) { |
| 84 |
case let line: const str => |
| 85 |
yield line; |
| 86 |
case utf8::invalid => |
| 87 |
return errors::invalid; |
| 88 |
case let e: io::error => |
| 89 |
return e; |
| 90 |
}; |
| 91 |
const line = strings::rtrim(line, '\r'); |
| 92 |
|
| 93 |
if (!strings::hasprefix(line, BEGIN) |
| 94 |
|| !strings::hassuffix(line, SUFFIX)) { |
| 95 |
continue; |
| 96 |
}; |
| 97 |
|
| 98 |
memio::reset(&dec.label); |
| 99 |
const label = strings::sub(line, |
| 100 |
len(BEGIN), len(line) - len(SUFFIX)); |
| 101 |
memio::concat(&dec.label, label)!; |
| 102 |
|
| 103 |
return (memio::string(&dec.label)!, pemdecoder { |
| 104 |
stream = &pemdecoder_vt, |
| 105 |
b64 = base64::newdecoder(&base64::std_encoding, &dec.in), |
| 106 |
}); |
| 107 |
}; |
| 108 |
return io::EOF; |
| 109 |
}; |
| 110 |
|
| 111 |
fn pem_read(st: *io::stream, buf: []u8) (size | io::EOF | io::error) = { |
| 112 |
// We need to set up two streams. This is the stream which is actually |
| 113 |
// returned to the caller, which calls the base64 decoder against a |
| 114 |
// special stream (b64stream) which trims out whitespace and EOF's on |
| 115 |
// -----END. |
| 116 |
const st = st: *pemdecoder; |
| 117 |
assert(st.stream.reader == &pem_read); |
| 118 |
|
| 119 |
match (io::read(&st.b64, buf)?) { |
| 120 |
case let z: size => |
| 121 |
return z; |
| 122 |
case io::EOF => void; |
| 123 |
}; |
| 124 |
|
| 125 |
const line = match (bufio::scan_line(&(st.b64.in: *b64stream).in)) { |
| 126 |
case io::EOF => |
| 127 |
return io::EOF; |
| 128 |
case utf8::invalid => |
| 129 |
return errors::invalid; |
| 130 |
case let line: const str => |
| 131 |
yield line; |
| 132 |
}; |
| 133 |
const line = strings::rtrim(line, '\r'); |
| 134 |
|
| 135 |
if (!strings::hasprefix(line, END) |
| 136 |
|| !strings::hassuffix(line, SUFFIX)) { |
| 137 |
return errors::invalid; |
| 138 |
}; |
| 139 |
|
| 140 |
// XXX: We could verify the trailer matches but the RFC says it's |
| 141 |
// optional. |
| 142 |
return io::EOF; |
| 143 |
}; |
| 144 |
|
| 145 |
fn b64_read(st: *io::stream, buf: []u8) (size | io::EOF | io::error) = { |
| 146 |
const st = st: *b64stream; |
| 147 |
assert(st.stream.reader == &b64_read); |
| 148 |
|
| 149 |
const z = match (io::read(&st.in, buf)?) { |
| 150 |
case let z: size => |
| 151 |
yield z; |
| 152 |
case io::EOF => |
| 153 |
return errors::invalid; // Missing -----END |
| 154 |
}; |
| 155 |
|
| 156 |
// Trim off whitespace and look for -----END |
| 157 |
let sub = buf[..z]; |
| 158 |
for (let i = 0z; i < len(sub); i += 1) { |
| 159 |
if (sub[i] == '-') { |
| 160 |
bufio::unread(&st.in, sub[i..]); |
| 161 |
sub = sub[..i]; |
| 162 |
break; |
| 163 |
}; |
| 164 |
if (ascii::isspace(sub[i]: rune)) { |
| 165 |
static delete(sub[i]); |
| 166 |
i -= 1; |
| 167 |
continue; |
| 168 |
}; |
| 169 |
}; |
| 170 |
|
| 171 |
if (len(sub) == 0) { |
| 172 |
return io::EOF; |
| 173 |
}; |
| 174 |
|
| 175 |
return len(sub); |
| 176 |
}; |
| 177 |
|
| 178 |
export type pemencoder = struct { |
| 179 |
stream: io::stream, |
| 180 |
out: io::handle, |
| 181 |
b64: base64::encoder, |
| 182 |
label: str, |
| 183 |
buf: [48]u8, |
| 184 |
ln: u8, |
| 185 |
}; |
| 186 |
|
| 187 |
const pemencoder_vt: io::vtable = io::vtable { |
| 188 |
writer = &pem_write, |
| 189 |
closer = &pem_wclose, |
| 190 |
... |
| 191 |
}; |
| 192 |
|
| 193 |
// Creates a new PEM encoder stream. The stream has to be closed to write the |
| 194 |
// trailer. |
| 195 |
export fn newencoder(label: str, s: io::handle) (pemencoder | io::error) = { |
| 196 |
fmt::fprintf(s, "{}{}{}\n", BEGIN, label, SUFFIX)?; |
| 197 |
return pemencoder { |
| 198 |
stream = &pemencoder_vt, |
| 199 |
out = s, |
| 200 |
b64 = base64::newencoder(&base64::std_encoding, s), |
| 201 |
label = label, |
| 202 |
... |
| 203 |
}; |
| 204 |
}; |
| 205 |
|
| 206 |
fn pem_write(s: *io::stream, buf: const []u8) (size | io::error) = { |
| 207 |
let s = s: *pemencoder; |
| 208 |
let buf = buf: []u8; |
| 209 |
if (len(buf) < len(s.buf) - s.ln) { |
| 210 |
s.buf[s.ln..s.ln+len(buf)] = buf[..]; |
| 211 |
s.ln += len(buf): u8; |
| 212 |
return len(buf); |
| 213 |
}; |
| 214 |
let z = 0z; |
| 215 |
s.buf[s.ln..] = buf[..len(s.buf) - s.ln]; |
| 216 |
z += io::writeall(&s.b64, s.buf)?; |
| 217 |
z += io::write(s.out, ['\n'])?; |
| 218 |
buf = buf[len(s.buf) - s.ln..]; |
| 219 |
for (len(buf) >= 48; buf = buf[48..]) { |
| 220 |
z += io::writeall(&s.b64, buf[..48])?; |
| 221 |
z += io::write(s.out, ['\n'])?; |
| 222 |
}; |
| 223 |
s.ln = len(buf): u8; |
| 224 |
s.buf[..s.ln] = buf; |
| 225 |
return z + s.ln; |
| 226 |
}; |
| 227 |
|
| 228 |
fn pem_wclose(s: *io::stream) (void | io::error) = { |
| 229 |
let s = s: *pemencoder; |
| 230 |
io::writeall(&s.b64, s.buf[..s.ln])?; |
| 231 |
io::close(&s.b64)?; |
| 232 |
fmt::fprintf(s.out, "\n{}{}{}\n", END, s.label, SUFFIX)?; |
| 233 |
}; |