| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use bytes; |
| 5 |
use io; |
| 6 |
use math::{bit_size}; |
| 7 |
use os; |
| 8 |
use strings; |
| 9 |
use time::date; |
| 10 |
use types; |
| 11 |
|
| 12 |
|
| 13 |
def TAGMASK: u8 = 0x1f; |
| 14 |
def MAX_CONS_DEPTH: size = 32; |
| 15 |
|
| 16 |
// Each DER entry starts with an header that describes the content. |
| 17 |
export type head = struct { |
| 18 |
|
| 19 |
// Tells whether the data is constructed and encapsulates multiple |
| 20 |
// other data fields; or primitive and the value follows. |
| 21 |
cons: bool, |
| 22 |
|
| 23 |
// Class info |
| 24 |
class: class, |
| 25 |
|
| 26 |
// Tag id of the data |
| 27 |
tagid: u32, |
| 28 |
|
| 29 |
// Start position in stream |
| 30 |
start: size, |
| 31 |
|
| 32 |
// Start position of data in stream |
| 33 |
data: size, |
| 34 |
|
| 35 |
// End position in stream |
| 36 |
end: size, |
| 37 |
|
| 38 |
implicit: bool, |
| 39 |
}; |
| 40 |
|
| 41 |
fn head_endpos(d: head) size = d.end; |
| 42 |
|
| 43 |
// Size of current element (header size + data size) |
| 44 |
export fn sz(d: head) size = d.end - d.start; |
| 45 |
|
| 46 |
// Size of the encoded data. |
| 47 |
export fn dsz(d: head) size = d.end - d.data; |
| 48 |
|
| 49 |
export type decoder = struct { |
| 50 |
src: io::handle, |
| 51 |
pos: size, |
| 52 |
cstack: [MAX_CONS_DEPTH]head, |
| 53 |
cstackp: size, |
| 54 |
next: (void | head), |
| 55 |
cur: (void | head), |
| 56 |
unbuf: [3]u8, |
| 57 |
unbufn: u8, |
| 58 |
implicit: bool, |
| 59 |
}; |
| 60 |
|
| 61 |
// Creates a new DER decoder that reads from 'src'. A buffered stream (see |
| 62 |
// [[bufio::]]) is recommended for efficiency, as the decoder performs mostly |
| 63 |
// short reads. |
| 64 |
// |
| 65 |
// Each entry must be read in its entirety before the next one is attended to. |
| 66 |
// The user must call [[finish]] when finished with the decoder to ensure that |
| 67 |
// the entire input was read correctly. |
| 68 |
export fn derdecoder(src: io::handle) decoder = { |
| 69 |
return decoder { |
| 70 |
src = src, |
| 71 |
pos = 0, |
| 72 |
cstackp = 0, |
| 73 |
cur = void, |
| 74 |
next = void, |
| 75 |
implicit = false, |
| 76 |
... |
| 77 |
}; |
| 78 |
}; |
| 79 |
|
| 80 |
// Verifies that the entire input to the decoder was read. |
| 81 |
export fn finish(d: *decoder) (void | error) = { |
| 82 |
if (d.cstackp != 0 || d.next is head) return invalid; |
| 83 |
match (d.cur) { |
| 84 |
case void => |
| 85 |
return; |
| 86 |
case let h: head => |
| 87 |
if (h.end != d.pos) return invalid; |
| 88 |
}; |
| 89 |
}; |
| 90 |
|
| 91 |
// Returns last opened cons or void if none is open. |
| 92 |
fn curcons(d: *decoder) (void | head) = { |
| 93 |
if (d.cstackp == 0) { |
| 94 |
return; |
| 95 |
}; |
| 96 |
return d.cstack[d.cstackp-1]; |
| 97 |
}; |
| 98 |
|
| 99 |
// Peeks the header of the next data field. Fails with [[badformat]] if no data |
| 100 |
// follows. |
| 101 |
export fn peek(d: *decoder) (head | error) = { |
| 102 |
match (trypeek(d)?) { |
| 103 |
case io::EOF => |
| 104 |
return badformat; |
| 105 |
case let h: head => |
| 106 |
return h; |
| 107 |
}; |
| 108 |
}; |
| 109 |
|
| 110 |
// Tries to peek the header of the next data field, or returns EOF if none |
| 111 |
// exists. |
| 112 |
export fn trypeek(d: *decoder) (head | error | io::EOF) = { |
| 113 |
if (!(d.next is void)) { |
| 114 |
return d.next: head; |
| 115 |
}; |
| 116 |
|
| 117 |
if (is_endofcons(d)) return io::EOF; |
| 118 |
|
| 119 |
match (parse_header(d)?) { |
| 120 |
case io::EOF => |
| 121 |
const unreaddata = d.unbufn > 0; |
| 122 |
if (d.cstackp != 0 || unreaddata) { |
| 123 |
return badformat; |
| 124 |
}; |
| 125 |
return io::EOF; |
| 126 |
case let dh: head => |
| 127 |
d.next = dh; |
| 128 |
return dh; |
| 129 |
}; |
| 130 |
}; |
| 131 |
|
| 132 |
// Cons is open and end is reached. |
| 133 |
fn is_endofcons(d: *decoder) bool = { |
| 134 |
match (curcons(d)) { |
| 135 |
case void => |
| 136 |
return false; |
| 137 |
case let cur: head => |
| 138 |
return d.pos == head_endpos(cur); |
| 139 |
}; |
| 140 |
}; |
| 141 |
|
| 142 |
// Returns the next data element, or [[badformat]] on EOF. |
| 143 |
fn next(d: *decoder) (head | error) = { |
| 144 |
match (trynext(d)?) { |
| 145 |
case io::EOF => |
| 146 |
return badformat; |
| 147 |
case let dh: head => |
| 148 |
return dh; |
| 149 |
}; |
| 150 |
}; |
| 151 |
|
| 152 |
fn trynext(d: *decoder) (head | error | io::EOF) = { |
| 153 |
if (d.next is head) { |
| 154 |
let dh = d.next: head; |
| 155 |
d.cur = dh; |
| 156 |
d.next = void; |
| 157 |
dh.implicit = d.implicit; |
| 158 |
d.implicit = false; |
| 159 |
return dh; |
| 160 |
}; |
| 161 |
|
| 162 |
if (is_endofcons(d)) return io::EOF; |
| 163 |
|
| 164 |
let dh = match (parse_header(d)?) { |
| 165 |
case io::EOF => |
| 166 |
return io::EOF; |
| 167 |
case let dh: head => |
| 168 |
yield dh; |
| 169 |
}; |
| 170 |
|
| 171 |
d.cur = dh; |
| 172 |
dh.implicit = d.implicit; |
| 173 |
d.implicit = false; |
| 174 |
return dh; |
| 175 |
}; |
| 176 |
|
| 177 |
fn parse_header(d: *decoder) (head | error | io::EOF) = { |
| 178 |
const consend = match (curcons(d)) { |
| 179 |
case void => |
| 180 |
yield types::SIZE_MAX; |
| 181 |
case let h: head => |
| 182 |
yield h.end; |
| 183 |
}; |
| 184 |
|
| 185 |
if (d.pos == consend) return invalid; |
| 186 |
|
| 187 |
const epos = d.pos; |
| 188 |
const id = match (tryscan_byte(d)?) { |
| 189 |
case io::EOF => |
| 190 |
d.cur = void; |
| 191 |
return io::EOF; |
| 192 |
case let id: u8 => |
| 193 |
yield id; |
| 194 |
}; |
| 195 |
|
| 196 |
const class = ((id & 0xc0) >> 6): class; |
| 197 |
|
| 198 |
let tagid: u32 = id & TAGMASK; |
| 199 |
if (tagid == TAGMASK) { |
| 200 |
tagid = parse_longtag(d, consend - d.pos)?; |
| 201 |
}; |
| 202 |
const l = parse_len(d, consend - d.pos)?; |
| 203 |
const hl = d.pos - epos; |
| 204 |
|
| 205 |
const end = epos + hl + l; |
| 206 |
if (end > consend) return invalid; |
| 207 |
|
| 208 |
return head { |
| 209 |
class = class, |
| 210 |
cons = ((id >> 5) & 1) == 1, |
| 211 |
tagid = tagid, |
| 212 |
start = epos, |
| 213 |
data = epos + hl, |
| 214 |
end = end, |
| 215 |
implicit = d.implicit, |
| 216 |
... |
| 217 |
}; |
| 218 |
}; |
| 219 |
|
| 220 |
fn tryscan_byte(d: *decoder) (u8 | io::EOF | error) = { |
| 221 |
let buf: [1]u8 = [0...]; |
| 222 |
match (io::readall(d.src, buf)?) { |
| 223 |
case io::EOF => |
| 224 |
return io::EOF; |
| 225 |
case size => |
| 226 |
d.pos += 1; |
| 227 |
return buf[0]; |
| 228 |
}; |
| 229 |
}; |
| 230 |
|
| 231 |
fn scan_byte(d: *decoder) (u8 | error) = { |
| 232 |
match (tryscan_byte(d)?) { |
| 233 |
case io::EOF => |
| 234 |
return truncated; |
| 235 |
case let b: u8 => |
| 236 |
return b; |
| 237 |
}; |
| 238 |
}; |
| 239 |
|
| 240 |
// Reads data of current entry and advances pointer. Data must have been opened |
| 241 |
// using [[next]] or [[trynext]]. [[io::EOF]] is returned on end of data. |
| 242 |
fn dataread(d: *decoder, buf: []u8) (size | io::EOF | io::error) = { |
| 243 |
let cur = match (d.cur) { |
| 244 |
case void => |
| 245 |
abort("primitive must be opened with [[next]] or [[trynext]]"); |
| 246 |
case let dh: head => |
| 247 |
yield dh; |
| 248 |
}; |
| 249 |
|
| 250 |
const dataleft = head_endpos(cur) - d.pos + d.unbufn; |
| 251 |
if (dataleft == 0) { |
| 252 |
return io::EOF; |
| 253 |
}; |
| 254 |
|
| 255 |
let n = 0z; |
| 256 |
if (d.unbufn > 0) { |
| 257 |
const max = if (d.unbufn > len(buf)) len(buf): u8 else d.unbufn; |
| 258 |
buf[..max] = d.unbuf[..max]; |
| 259 |
d.unbufn -= max; |
| 260 |
n += max; |
| 261 |
}; |
| 262 |
|
| 263 |
const max = if (dataleft < len(buf) - n) dataleft else len(buf) - n; |
| 264 |
|
| 265 |
match (io::read(d.src, buf[n..n + max])?) { |
| 266 |
case io::EOF => |
| 267 |
// there should be data left |
| 268 |
return wrap_err(truncated); |
| 269 |
case let sz: size => |
| 270 |
d.pos += sz; |
| 271 |
return n + sz; |
| 272 |
}; |
| 273 |
}; |
| 274 |
|
| 275 |
// unread incomplete utf8 runes. |
| 276 |
fn dataunread(d: *decoder, buf: []u8) void = { |
| 277 |
assert(len(buf) + d.unbufn <= len(d.unbuf)); |
| 278 |
|
| 279 |
d.unbuf[d.unbufn..d.unbufn + len(buf)] = buf; |
| 280 |
d.unbufn += len(buf): u8; |
| 281 |
}; |
| 282 |
|
| 283 |
fn dataeof(d: *decoder) bool = { |
| 284 |
match (d.cur) { |
| 285 |
case void => |
| 286 |
return true; |
| 287 |
case let h: head => |
| 288 |
return d.pos + d.unbufn == head_endpos(h); |
| 289 |
}; |
| 290 |
}; |
| 291 |
|
| 292 |
fn parse_longtag(p: *decoder, max: size) (u32 | error) = { |
| 293 |
// XXX: u32 too much? |
| 294 |
let tag: u32 = 0; |
| 295 |
let maxbits = size(u32) * 8; |
| 296 |
let nbits = 0z; |
| 297 |
|
| 298 |
for (let i = 0z; i < max; i += 1) { |
| 299 |
let b = scan_byte(p)?; |
| 300 |
const part = b & 0x7f; |
| 301 |
|
| 302 |
nbits += if (tag == 0) bit_size(part) else 7; |
| 303 |
if (nbits > maxbits) { |
| 304 |
// overflows u32 |
| 305 |
return invalid; |
| 306 |
}; |
| 307 |
|
| 308 |
tag = (tag << 7) + part; |
| 309 |
if (tag == 0) { |
| 310 |
// first tag part must not be 0 |
| 311 |
return invalid; |
| 312 |
}; |
| 313 |
|
| 314 |
if ((b >> 7) == 0) { |
| 315 |
return tag; |
| 316 |
}; |
| 317 |
}; |
| 318 |
return invalid; // max has been reached |
| 319 |
}; |
| 320 |
|
| 321 |
fn parse_len(p: *decoder, max: size) (size | error) = { |
| 322 |
if (max == 0) return invalid; |
| 323 |
|
| 324 |
const b = scan_byte(p)?; |
| 325 |
if (b == 0xff) { |
| 326 |
return invalid; |
| 327 |
}; |
| 328 |
if (b >> 7 == 0) { |
| 329 |
// short form |
| 330 |
return b: size; |
| 331 |
}; |
| 332 |
|
| 333 |
let l = 0z; |
| 334 |
const n = b & 0x7f; |
| 335 |
if (n == 0) { |
| 336 |
// Indefinite encoding is not supported in DER. |
| 337 |
return invalid; |
| 338 |
}; |
| 339 |
|
| 340 |
if (n > size(size)) { |
| 341 |
// would cause a size overflow |
| 342 |
return invalid; |
| 343 |
}; |
| 344 |
|
| 345 |
if (n + 1 > max) return invalid; |
| 346 |
|
| 347 |
for (let i = 0z; i < n; i += 1) { |
| 348 |
const b = scan_byte(p)?; |
| 349 |
l = (l << 8) + b; |
| 350 |
if (l == 0) { |
| 351 |
// Leading zeroes means minimum number of bytes for |
| 352 |
// length encoding has not been used. |
| 353 |
return invalid; |
| 354 |
}; |
| 355 |
}; |
| 356 |
|
| 357 |
if (l <= 0x7f) { |
| 358 |
// Could've used short form. |
| 359 |
return invalid; |
| 360 |
}; |
| 361 |
|
| 362 |
return l; |
| 363 |
}; |
| 364 |
|
| 365 |
// Expects an IMPLICIT defined data field having class 'c' and tag 'tag'. |
| 366 |
// If the requirements are met, a read call (i.e. one of the "read_" or "reader" |
| 367 |
// functions) must follow to read the actual data as stored. |
| 368 |
export fn expect_implicit(d: *decoder, c: class, tag: u32) (void | error) = { |
| 369 |
let h = peek(d)?; |
| 370 |
expect_tag(h, c, tag)?; |
| 371 |
d.implicit = true; |
| 372 |
}; |
| 373 |
|
| 374 |
// Opens an EXPLICIT encoded field of given class 'c' and 'tag'. The user must |
| 375 |
// call [[close_explicit]] after containing data has been read. |
| 376 |
export fn open_explicit(d: *decoder, c: class, tag: u32) (void | error) = |
| 377 |
open_cons(d, c, tag); |
| 378 |
|
| 379 |
// Closes an EXPLICIT encoded field. |
| 380 |
export fn close_explicit(d: *decoder) (void | badformat) = close_cons(d); |
| 381 |
|
| 382 |
|
| 383 |
// Opens a constructed value of given 'class' and 'tagid'. Fails if not a |
| 384 |
// constructed value or the encoded value has an unexpected tag. |
| 385 |
fn open_cons(d: *decoder, class: class, tagid: u32) (void | error) = { |
| 386 |
let dh = next(d)?; |
| 387 |
if (!dh.cons) { |
| 388 |
return invalid; |
| 389 |
}; |
| 390 |
|
| 391 |
expect_tag(dh, class, tagid)?; |
| 392 |
|
| 393 |
if (d.cstackp == len(d.cstack)) { |
| 394 |
return badformat; |
| 395 |
}; |
| 396 |
|
| 397 |
d.cstack[d.cstackp] = dh; |
| 398 |
d.cstackp += 1; |
| 399 |
}; |
| 400 |
|
| 401 |
// Closes current constructed value. badformat is returend, if not all data has |
| 402 |
// been read. |
| 403 |
fn close_cons(d: *decoder) (void | badformat) = { |
| 404 |
if (d.implicit) { |
| 405 |
// a datafield marked implicit has not been read |
| 406 |
return badformat; |
| 407 |
}; |
| 408 |
|
| 409 |
match (curcons(d)) { |
| 410 |
case void => |
| 411 |
abort("No constructed value open"); |
| 412 |
case let h: head => |
| 413 |
if (d.pos != head_endpos(h) || d.unbufn > 0) { |
| 414 |
// All data must have been read before closing the seq |
| 415 |
return badformat; |
| 416 |
}; |
| 417 |
}; |
| 418 |
|
| 419 |
d.cstackp -= 1; |
| 420 |
}; |
| 421 |
|
| 422 |
// Opens a sequence. Call [[close_seq]] after reading. |
| 423 |
export fn open_seq(d: *decoder) (void | error) = |
| 424 |
open_cons(d, class::UNIVERSAL, utag::SEQUENCE: u32)?; |
| 425 |
|
| 426 |
// Closes the current sequence. If the caller has not read all of the data |
| 427 |
// present in the encoded seqeunce, [[badformat]] is returned. |
| 428 |
export fn close_seq(d: *decoder) (void | badformat) = close_cons(d); |
| 429 |
|
| 430 |
// Opens a set. Note that sets must be ordered according to DER, but this module |
| 431 |
// does not validate this constraint. Call [[close_set]] after reading. |
| 432 |
export fn open_set(d: *decoder) (void | error) = |
| 433 |
open_cons(d, class::UNIVERSAL, utag::SET: u32)?; |
| 434 |
|
| 435 |
// Closes the current set. If the caller has not read all of the data present in |
| 436 |
// the encoded seqeunce, [[badformat]] is returned. |
| 437 |
export fn close_set(d: *decoder) (void | badformat) = close_cons(d); |
| 438 |
|
| 439 |
fn expect_tag(h: head, class: class, tagid: u32) (void | invalid | badformat) = { |
| 440 |
if (class == class::UNIVERSAL && (tagid == utag::SEQUENCE |
| 441 |
|| tagid == utag::SET) && !h.cons) { |
| 442 |
return invalid; |
| 443 |
}; |
| 444 |
|
| 445 |
if (h.implicit) { |
| 446 |
return; |
| 447 |
}; |
| 448 |
|
| 449 |
if (h.class != class || h.tagid != tagid) { |
| 450 |
return badformat; |
| 451 |
}; |
| 452 |
}; |
| 453 |
|
| 454 |
fn expect_utag(dh: head, tag: utag) (void | invalid | badformat) = |
| 455 |
expect_tag(dh, class::UNIVERSAL, tag: u32); |
| 456 |
|
| 457 |
fn read_bytes(d: *decoder, buf: []u8) (size | error) = { |
| 458 |
match (dataread(d, buf)) { |
| 459 |
case io::EOF => |
| 460 |
return 0z; |
| 461 |
case let n: size => |
| 462 |
if (!dataeof(d)) { |
| 463 |
return badformat; |
| 464 |
}; |
| 465 |
return n; |
| 466 |
}; |
| 467 |
}; |
| 468 |
|
| 469 |
fn read_nbytes(d: *decoder, buf: []u8) (size | error) = { |
| 470 |
const n = read_bytes(d, buf)?; |
| 471 |
if (n != len(buf)) { |
| 472 |
return badformat; |
| 473 |
}; |
| 474 |
return n; |
| 475 |
}; |
| 476 |
|
| 477 |
// Reads a boolean value. |
| 478 |
export fn read_bool(d: *decoder) (bool | error) = { |
| 479 |
let dh = next(d)?; |
| 480 |
expect_utag(dh, utag::BOOLEAN)?; |
| 481 |
if (dsz(dh) != 1) { |
| 482 |
return invalid; |
| 483 |
}; |
| 484 |
|
| 485 |
let b = scan_byte(d)?; |
| 486 |
|
| 487 |
if (b != 0x00 && b != 0xff) { |
| 488 |
return invalid; |
| 489 |
}; |
| 490 |
|
| 491 |
return b == 0xff; |
| 492 |
}; |
| 493 |
|
| 494 |
fn validate_intprefix(i: []u8) (void | error) = { |
| 495 |
switch (len(i)) { |
| 496 |
case 0 => |
| 497 |
return invalid; |
| 498 |
case 1 => |
| 499 |
return; |
| 500 |
case => |
| 501 |
// An int must be encoded using the minimal number of bytes |
| 502 |
// possible as defined in X.690 s8.3.2 |
| 503 |
if ((i[0] == 0x00 && i[1] >> 7 == 0) |
| 504 |
|| (i[0] == 0xff && i[1] >> 7 == 1)) { |
| 505 |
return invalid; |
| 506 |
}; |
| 507 |
}; |
| 508 |
}; |
| 509 |
|
| 510 |
// Reads an arbitrary-length integer into 'buf' and returns its length in bytes. |
| 511 |
// Fails if the encoded integer size exceeds the buffer size. The integer is |
| 512 |
// stored in big endian, and negative values are stored with two's compliment. |
| 513 |
// The minimum integer size is one byte. |
| 514 |
export fn read_int(d: *decoder, buf: []u8) (size | error) = { |
| 515 |
assert(len(buf) > 0); |
| 516 |
|
| 517 |
let dh = next(d)?; |
| 518 |
expect_utag(dh, utag::INTEGER)?; |
| 519 |
const n = read_bytes(d, buf)?; |
| 520 |
validate_intprefix(buf[..n])?; |
| 521 |
return n; |
| 522 |
}; |
| 523 |
|
| 524 |
// Similar to [[read_int]], but returns [[badformat]] if the encoded value is |
| 525 |
// signed. Discards the most significant zero bytes. |
| 526 |
export fn read_uint(d: *decoder, buf: []u8) (size | error) = { |
| 527 |
let s = read_int(d, buf)?; |
| 528 |
if (buf[0] & 0x80 == 0x80) { |
| 529 |
return badformat; |
| 530 |
}; |
| 531 |
if (buf[0] == 0) { |
| 532 |
buf[..s-1] = buf[1..s]; |
| 533 |
s -= 1; |
| 534 |
}; |
| 535 |
return s; |
| 536 |
}; |
| 537 |
|
| 538 |
fn read_ux(d: *decoder, x: u8) (u64 | error) = { |
| 539 |
assert(x <= 8); |
| 540 |
let b: [9]u8 = [0...]; |
| 541 |
const n = read_int(d, b[..x+1])?; |
| 542 |
|
| 543 |
if (b[0] & 0x80 != 0) { |
| 544 |
// sign bit is set |
| 545 |
return invalid; |
| 546 |
}; |
| 547 |
|
| 548 |
const s = if (b[0] == 0x00) 1u8 else 0u8; |
| 549 |
if (n - s > x) { |
| 550 |
return invalid; |
| 551 |
}; |
| 552 |
|
| 553 |
let r = 0u64; |
| 554 |
for (let i = s; i < n; i += 1) { |
| 555 |
r <<= 8; |
| 556 |
r += b[i]; |
| 557 |
}; |
| 558 |
return r; |
| 559 |
}; |
| 560 |
|
| 561 |
// Reads an integer that is expected to fit into u8. |
| 562 |
export fn read_u8(d: *decoder) (u8 | error) = read_ux(d, 1)?: u8; |
| 563 |
|
| 564 |
// Reads an integer that is expected to fit into u16. |
| 565 |
export fn read_u16(d: *decoder) (u16 | error) = read_ux(d, 2)?: u16; |
| 566 |
|
| 567 |
// Reads an integer that is expected to fit into u32. |
| 568 |
export fn read_u32(d: *decoder) (u32 | error) = read_ux(d, 4)?: u32; |
| 569 |
|
| 570 |
// Reads an integer that is expected to fit into u64. |
| 571 |
export fn read_u64(d: *decoder) (u64 | error) = read_ux(d, 8)?; |
| 572 |
|
| 573 |
// Reads a bitstring value. The result tuple contains the bitstring and the |
| 574 |
// number of unused bits in the last byte. The [[bitstr_isset]] function may be |
| 575 |
// used to check for set bits. |
| 576 |
export fn read_bitstr(d: *decoder, buf: []u8) (([]u8, u8) | error) = { |
| 577 |
let dh = next(d)?; |
| 578 |
expect_utag(dh, utag::BITSTRING)?; |
| 579 |
|
| 580 |
let unused: [1]u8 = [0...]; |
| 581 |
match (dataread(d, unused)?) { |
| 582 |
case io::EOF => |
| 583 |
return invalid; |
| 584 |
case let n: size => |
| 585 |
if (n != 1) { |
| 586 |
return invalid; |
| 587 |
}; |
| 588 |
}; |
| 589 |
const unused = unused[0]; |
| 590 |
if (unused > 7) { |
| 591 |
return invalid; |
| 592 |
}; |
| 593 |
|
| 594 |
const n = read_bytes(d, buf)?; |
| 595 |
const mask = (1 << unused) - 1; |
| 596 |
if (n > 0 && buf[n-1] & mask != 0) { |
| 597 |
// unused bits must be zero |
| 598 |
return invalid; |
| 599 |
}; |
| 600 |
return (buf[..n], unused); |
| 601 |
}; |
| 602 |
|
| 603 |
// Checks whether bit at 'pos' is set in given bitstring. 'pos' starts from 0, |
| 604 |
// which is the highest order bit in the first byte. |
| 605 |
export fn bitstr_isset(bitstr: ([]u8, u8), pos: size) (bool | invalid) = { |
| 606 |
const i = pos / 8; |
| 607 |
if (i >= len(bitstr.0)) { |
| 608 |
return false; |
| 609 |
}; |
| 610 |
let b = bitstr.0[i]; |
| 611 |
|
| 612 |
const j = pos - i * 8; |
| 613 |
if (i == len(bitstr.0) - 1 && j >= (8 - bitstr.1)) { |
| 614 |
return invalid; |
| 615 |
}; |
| 616 |
const mask = (1 << (7 - j)); |
| 617 |
return mask & b == mask; |
| 618 |
}; |
| 619 |
|
| 620 |
// Returns an [[io::reader]] for octet string data. |
| 621 |
export fn octetstrreader(d: *decoder) (bytestream | error) = { |
| 622 |
// TODO add limit? |
| 623 |
let dh = next(d)?; |
| 624 |
expect_utag(dh, utag::OCTET_STRING)?; |
| 625 |
return newbytereader(d); |
| 626 |
}; |
| 627 |
|
| 628 |
// Read an octet string into 'buf', returning its length. Returns [[badformat]] |
| 629 |
// if 'buf' is too small. |
| 630 |
export fn read_octetstr(d: *decoder, buf: []u8) (size | error) = { |
| 631 |
assert(len(buf) > 0); |
| 632 |
|
| 633 |
let dh = next(d)?; |
| 634 |
expect_utag(dh, utag::OCTET_STRING)?; |
| 635 |
return read_bytes(d, buf); |
| 636 |
}; |
| 637 |
|
| 638 |
// Reads a null entry. |
| 639 |
export fn read_null(d: *decoder) (void | error) = { |
| 640 |
let dh = next(d)?; |
| 641 |
expect_utag(dh, utag::NULL)?; |
| 642 |
if (dsz(dh) != 0) { |
| 643 |
return invalid; |
| 644 |
}; |
| 645 |
}; |
| 646 |
|
| 647 |
export type bytestream = struct { |
| 648 |
stream: io::stream, |
| 649 |
d: *decoder, |
| 650 |
}; |
| 651 |
|
| 652 |
fn newbytereader(d: *decoder) bytestream = { |
| 653 |
return bytestream { |
| 654 |
stream = &bytestream_vtable, |
| 655 |
d = d, |
| 656 |
... |
| 657 |
}; |
| 658 |
}; |
| 659 |
|
| 660 |
const bytestream_vtable: io::vtable = io::vtable { |
| 661 |
reader = &bytestream_reader, |
| 662 |
... |
| 663 |
}; |
| 664 |
|
| 665 |
fn bytestream_reader(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = |
| 666 |
dataread((s: *bytestream).d, buf); |
| 667 |
|
| 668 |
// Returns an [[io::reader]] that reads raw data (in its ASN.1 encoded form) |
| 669 |
// from a [[decoder]]. Note that this reader will not perform any kind of |
| 670 |
// validation. |
| 671 |
export fn bytereader(d: *decoder, c: class, tagid: u32) (bytestream | error) = { |
| 672 |
let dh = next(d)?; |
| 673 |
expect_tag(dh, c, tagid)?; |
| 674 |
return newbytereader(d); |
| 675 |
}; |
| 676 |
|
| 677 |
// Reads an UTC time. Since the stored date only has a two digit year, 'maxyear' |
| 678 |
// is required to define the epoch. For example 'maxyear' = 2046 causes all |
| 679 |
// encoded years <= 46 to be after 2000 and all values > 46 will have 1900 as |
| 680 |
// the century. |
| 681 |
export fn read_utctime(d: *decoder, maxyear: u16) (date::date | error) = { |
| 682 |
assert(maxyear > 100); |
| 683 |
|
| 684 |
let dh = next(d)?; |
| 685 |
expect_utag(dh, utag::UTC_TIME)?; |
| 686 |
|
| 687 |
let time: [13]u8 = [0...]; |
| 688 |
read_nbytes(d, time[..])?; |
| 689 |
|
| 690 |
if (time[len(time)-1] != 'Z') { |
| 691 |
return invalid; |
| 692 |
}; |
| 693 |
|
| 694 |
let year: u16 = (time[0] - 0x30): u16 * 10 + (time[1] - 0x30): u16; |
| 695 |
let cent = maxyear - (maxyear % 100); |
| 696 |
if (year > maxyear % 100) { |
| 697 |
cent -= 100; |
| 698 |
}; |
| 699 |
|
| 700 |
let v = date::newvirtual(); |
| 701 |
v.vloc = date::UTC; |
| 702 |
v.year = (year + cent): int; |
| 703 |
v.zoff = 0; |
| 704 |
v.nanosecond = 0; |
| 705 |
|
| 706 |
let datestr = strings::fromutf8(time[2..])!; |
| 707 |
if (!(date::parse(&v, "%m%d%H%M%S%Z", datestr) is void)) { |
| 708 |
return invalid; |
| 709 |
}; |
| 710 |
|
| 711 |
let dt = match (date::realize(v)) { |
| 712 |
case let dt: date::date => |
| 713 |
yield dt; |
| 714 |
case let e: (date::insufficient | date::invalid) => |
| 715 |
return invalid; |
| 716 |
}; |
| 717 |
|
| 718 |
return dt; |
| 719 |
}; |
| 720 |
|
| 721 |
// Reads a generalized datetime value. |
| 722 |
export fn read_gtime(d: *decoder) (date::date | error) = { |
| 723 |
let dh = next(d)?; |
| 724 |
expect_utag(dh, utag::GENERALIZED_TIME)?; |
| 725 |
|
| 726 |
// The date begins with the encoded datetime |
| 727 |
def DATESZ = 14z; |
| 728 |
// followed by optional fractional seconds separated by '.' |
| 729 |
def NANOSZ = 10z; |
| 730 |
def NANOSEPPOS = 14; |
| 731 |
// and ends with the zone info 'Z' |
| 732 |
def ZONESZ = 1z; |
| 733 |
|
| 734 |
let time: [DATESZ + NANOSZ + ZONESZ]u8 = [0...]; |
| 735 |
let n = read_bytes(d, time[..])?; |
| 736 |
|
| 737 |
// zone info and seconds must always be present |
| 738 |
if (time[n-1] != 'Z' || n < DATESZ + ZONESZ) { |
| 739 |
return invalid; |
| 740 |
}; |
| 741 |
|
| 742 |
// validate fractional seconds |
| 743 |
if (n > DATESZ + ZONESZ) { |
| 744 |
// fractional seconds must not be empty |
| 745 |
if (time[NANOSEPPOS] != '.' || n == DATESZ + ZONESZ + 1) { |
| 746 |
return invalid; |
| 747 |
}; |
| 748 |
// fractional seconds must not end with 0 and must be > 0 |
| 749 |
if (time[n-2] == '0') return invalid; |
| 750 |
}; |
| 751 |
|
| 752 |
// right pad fractional seconds to make them valid nanoseconds |
| 753 |
time[n-1..] = ['0'...]; |
| 754 |
time[NANOSEPPOS] = '.'; |
| 755 |
|
| 756 |
match (date::from_str("%Y%m%d%H%M%S.%N", strings::fromutf8(time)!)) { |
| 757 |
case let d: date::date => |
| 758 |
return d; |
| 759 |
case => |
| 760 |
return invalid; |
| 761 |
}; |
| 762 |
}; |
| 763 |
|
| 764 |
// Skips an element and returns the size of the data that has been skipped. |
| 765 |
// Returns an error if the skipped data is invalid. |
| 766 |
// |
| 767 |
// Presently only supports BOOLEAN, INTEGER, NULL, OCTET_STRING, and BITSTRING |
| 768 |
// utags, and will abort when attempting to skip anything else. |
| 769 |
export fn skip(d: *decoder, tag: utag, max: size) (size | error) = { |
| 770 |
static let buf: [os::BUFSZ]u8 = [0...]; |
| 771 |
let s = 0z; |
| 772 |
switch (tag) { |
| 773 |
case utag::BOOLEAN => |
| 774 |
read_bool(d)?; |
| 775 |
return 1z; |
| 776 |
case utag::INTEGER => |
| 777 |
let br = bytereader(d, class::UNIVERSAL, utag::INTEGER)?; |
| 778 |
let n = match (io::read(&br, buf)?) { |
| 779 |
case let n: size => |
| 780 |
yield n; |
| 781 |
case io::EOF => |
| 782 |
return invalid; |
| 783 |
}; |
| 784 |
validate_intprefix(buf[..n])?; |
| 785 |
n += streamskip(&br, max, buf)?; |
| 786 |
return n; |
| 787 |
case utag::NULL => |
| 788 |
read_null(d)?; |
| 789 |
return 0z; |
| 790 |
case utag::OCTET_STRING => |
| 791 |
let r = octetstrreader(d)?; |
| 792 |
return streamskip(&r, max, buf)?; |
| 793 |
case utag::BITSTRING => |
| 794 |
assert(max <= len(buf)); |
| 795 |
let buf = buf[..max]; |
| 796 |
let p = read_bitstr(d, buf)?; |
| 797 |
bytes::zero(p.0); |
| 798 |
return len(p.0) + 1; |
| 799 |
case => |
| 800 |
abort("skip for given utag not implemented"); |
| 801 |
}; |
| 802 |
}; |
| 803 |
|
| 804 |
fn streamskip(r: io::handle, max: size, buf: []u8) (size | error) = { |
| 805 |
defer bytes::zero(buf); |
| 806 |
let buf = if (max < len(buf)) buf[..max] else buf[..]; |
| 807 |
let s = 0z; |
| 808 |
for (true) { |
| 809 |
match (io::read(r, buf)?) { |
| 810 |
case let n: size => |
| 811 |
s += n; |
| 812 |
case io::EOF => |
| 813 |
return s; |
| 814 |
}; |
| 815 |
|
| 816 |
if (s > max) { |
| 817 |
return badformat; |
| 818 |
}; |
| 819 |
}; |
| 820 |
}; |