Lines Matching +full:close +full:- +full:range
31 \ -- In the preamble, a macro called "CTX" that evaluates to the current
34 \ -- In the preamble, a macro called "CONTEXT_NAME" that evaluates to the
37 \ -- A word called "read8-low" ( -- x ) that reads the next byte, or -1
40 \ -- A word called "read-blob-inner" ( addr len -- addr len ) that is
41 \ the multi-byte version of read8-low.
43 \ -- A word called "skip-remaining-inner" ( lim -- lim ) which reads but
53 : skip-blanks begin char dup 32 > if ret then drop again ;
55 : fail-oid
60 : parse-number ( -- val nextchar )
69 : encode7E ( val -- )
70 0 encode7E-inner ;
72 : encode7E-inner ( val eb -- )
74 dup 7 u>> 0x80 encode7E-inner 0x7F and
76 or data-add8 ;
83 current-data 0 data-add8
85 skip-blanks decval dup 2 > if fail-oid then
88 char `. <> if fail-oid then
90 parse-number { nextchar }
91 dup 40 >= if fail-oid then
95 parse-number >nextchar
99 dup current-data swap - 1- swap data-set8
105 new-data-block next-word define-data-word postpone OID ;
110 next-word { field }
111 "addr-" field + 0 1 define-word
112 0 8191 "offsetof(CONTEXT_NAME, " field + ")" + make-CX
119 next-word { name }
120 name 0 1 define-word
121 0 63 "BR_" name + make-CX postpone literal postpone ; ;
154 cc: fail ( err -- ! ) {
155 CTX->err = T0_POPi();
160 : read8-nc ( -- x )
162 read8-low dup 0 >= if ret then
167 : read8 ( lim -- lim x )
169 1- read8-nc ;
171 \ Read a 16-bit value, big-endian encoding.
172 : read16be ( lim -- lim x )
175 \ Read a 16-bit value, little-endian encoding.
176 : read16le ( lim -- lim x )
179 \ Read all bytes from the current element, then close it (i.e. drop the
181 : read-blob ( lim addr -- )
183 begin dup while read-blob-inner dup if co then repeat
186 \ Skip remaining bytes in the current structure, but do not close it
188 : skip-remaining ( lim -- lim )
189 begin dup while skip-remaining-inner dup if co then repeat ;
191 : skip-remaining-inner ( lim -- lim )
192 0 over read-blob-inner -rot 2drop ;
194 cc: set8 ( val addr -- ) {
199 cc: set16 ( val addr -- ) {
204 cc: set32 ( val addr -- ) {
209 cc: get8 ( addr -- val ) {
214 cc: get16 ( addr -- val ) {
219 cc: get32 ( addr -- val ) {
225 \ and the tag value. The constructed status is a boolean (-1 for
232 : read-tag ( lim -- lim constructed value )
252 \ synthetic "no tag" value (primitive, with value -1).
253 : read-tag-or-end ( lim -- lim constructed value )
254 dup ifnot 0 -1 ret then
255 read-tag ;
259 : iftag-skip ( lim constructed value ref -- lim constructed value )
262 read-length-open-elt skip-close-elt
263 read-tag-or-end
271 : read-length ( lim -- lim length )
278 0x80 - dup ifnot ERR_X509_INDEFINITE_LENGTH fail then
286 begin n 0 > while n 1- >n
291 \ Open a sub-structure. This subtracts the length from the limit, and
293 : open-elt ( lim length -- lim_outer lim_inner )
295 dup { len } - len ;
297 \ Read a length and open the value as a sub-structure.
298 : read-length-open-elt ( lim -- lim_outer lim_inner )
299 read-length open-elt ;
301 \ Close a sub-structure. This verifies that there is no remaining
303 : close-elt ( lim -- )
306 \ Skip remaining bytes in the current structure, then close it.
307 : skip-close-elt ( lim -- )
308 skip-remaining drop ;
311 : read-length-skip ( lim -- lim )
312 read-length-open-elt skip-close-elt ;
315 : check-tag-constructed ( constructed value refvalue -- )
317 check-constructed ;
321 : check-constructed ( constructed -- )
325 : check-tag-primitive ( constructed value refvalue -- )
327 check-primitive ;
331 : check-primitive ( constructed -- )
335 : check-sequence ( constructed value -- )
336 0x10 check-tag-constructed ;
339 \ it as a sub-element.
340 : read-sequence-open ( lim -- lim_outer lim_inner )
341 read-tag check-sequence read-length-open-elt ;
344 \ it as a sub-element.
345 : read-bits-open ( lim -- lim_outer lim_inner )
346 read-tag 0x03 check-tag-primitive
347 read-length-open-elt
358 OID: id-sha1 1.3.14.3.2.26
359 OID: id-sha224 2.16.840.1.101.3.4.2.4
360 OID: id-sha256 2.16.840.1.101.3.4.2.1
361 OID: id-sha384 2.16.840.1.101.3.4.2.2
362 OID: id-sha512 2.16.840.1.101.3.4.2.3
364 OID: id-ecPublicKey 1.2.840.10045.2.1
370 OID: ecdsa-with-SHA1 1.2.840.10045.4.1
371 OID: ecdsa-with-SHA224 1.2.840.10045.4.3.1
372 OID: ecdsa-with-SHA256 1.2.840.10045.4.3.2
373 OID: ecdsa-with-SHA384 1.2.840.10045.4.3.3
374 OID: ecdsa-with-SHA512 1.2.840.10045.4.3.4
376 OID: id-at-commonName 2.5.4.3
382 \ method returns false (0). Otherwise, it returns true (-1).
384 : read-small-value ( lim -- lim bool )
385 read-length-open-elt
386 dup 255 > if skip-close-elt 0 addr-pad set8 0 ret then
387 dup addr-pad set8
388 addr-pad 1+ read-blob
389 -1 ;
392 \ value is returned, which is true (-1) if the OID value fits on the pad,
394 : read-OID ( lim -- lim bool )
395 read-tag 0x06 check-tag-primitive read-small-value ;
397 \ Read a UTF-8 code point. On error, return 0. Reading a code point of
399 : read-UTF8 ( lim -- lim val )
404 dup 0xE0 < uf 0x1F and 1 read-UTF8-next 0x80 0x7FF enduf
405 dup 0xF0 < uf 0x0F and 2 read-UTF8-next 0x800 0xFFFF enduf
406 dup 0xF8 < uf 0x07 and 3 read-UTF8-next 0x10000 0x10FFFF enduf
413 \ value is -1 on error, or the code point numerical value. The final
415 : read-UTF8-next ( lim val n -- lim val val )
417 -rot
418 read-UTF8-chunk
419 rot 1-
423 \ Read one byte, that should be a trailing UTF-8 byte, and complement the
424 \ current value. On error, value is set to -1.
425 : read-UTF8-chunk ( lim val -- lim val )
428 dup ifnot 2drop 0 -1 ret then
431 swap dup 6 >> 2 <> if 2drop -1 ret then
434 : high-surrogate? ( x -- x bool )
437 : low-surrogate? ( x -- x bool )
440 : assemble-surrogate-pair ( hi lim lo -- lim val )
441 low-surrogate? ifnot rot 2drop 0 ret then
442 rot 10 << + 0x35FDC00 - ;
444 \ Read a UTF-16 code point (big-endian). Returned value is 0 on error.
445 : read-UTF16BE ( lim -- lim val )
448 high-surrogate? uf
450 read16be assemble-surrogate-pair
452 low-surrogate? uf
457 \ Read a UTF-16 code point (little-endian). Returned value is 0 on error.
458 : read-UTF16LE ( lim -- lim val )
461 high-surrogate? uf
463 read16le assemble-surrogate-pair
465 low-surrogate? uf
471 : pad-append ( off val -- off )
473 over addr-pad + set8 1+ ;
475 \ Add UTF-8 chunk byte to the pad. The 'nn' parameter is the shift count.
476 : pad-append-UTF8-chunk ( off val nn -- off )
477 >> 0x3F and 0x80 or pad-append ;
480 \ 66 noncharacters, and also the surrogate range; this function does NOT
481 \ check that the value is in the 0..10FFFF range.
482 : valid-unicode? ( val -- bool )
487 \ Encode a code point in UTF-8. Offset is in the pad; it is updated, or
489 : encode-UTF8 ( val off -- off )
494 dup valid-unicode? ifnot 2drop 0 ret then
496 dup 0x80 < uf pad-append enduf
498 6 >> 0xC0 or pad-append
499 val 0 pad-append-UTF8-chunk
502 12 >> 0xE0 or pad-append
503 val 6 pad-append-UTF8-chunk
504 val 0 pad-append-UTF8-chunk
506 18 >> 0xF0 or pad-append
507 val 12 pad-append-UTF8-chunk
508 val 6 pad-append-UTF8-chunk
509 val 0 pad-append-UTF8-chunk
513 \ characters are UTF-8 and non-zero. The string length (in bytes) is
514 \ written in the first pad byte. Returned value is true (-1) on success,
516 : read-value-UTF8 ( lim -- lim bool )
517 read-length-open-elt
520 read-UTF8 dup ifnot drop skip-close-elt 0 ret then
521 off encode-UTF8 >off
523 drop off dup ifnot ret then 1- addr-pad set8 -1 ;
525 \ Decode a UTF-16 string into the pad. The string is converted to UTF-8,
527 \ honoured (big-endian is assumed if there is no BOM). A code point of
528 \ value 0 is an error. Returned value is true (-1) on success, false (0)
530 : read-value-UTF16 ( lim -- lim bool )
531 read-length-open-elt
532 dup ifnot addr-pad set8 -1 ret then
534 read-UTF16BE dup 0xFFFE = if
535 \ Leading BOM, and indicates little-endian.
538 read-UTF16LE dup ifnot drop skip-close-elt 0 ret then
539 off encode-UTF8 >off
542 dup ifnot drop skip-close-elt 0 ret then
543 \ Big-endian BOM, or no BOM.
545 off encode-UTF8 >off
547 read-UTF16BE dup ifnot drop skip-close-elt 0 ret then
550 drop off dup ifnot ret then 1- addr-pad set8 -1 ;
552 \ Decode a latin-1 string into the pad. The string is converted to UTF-8,
554 \ value 0 is an error. Returned value is true (-1) on success, false (0)
556 : read-value-latin1 ( lim -- lim bool )
557 read-length-open-elt
560 read8 dup ifnot drop skip-close-elt 0 ret then
561 off encode-UTF8 >off
563 drop off dup ifnot ret then 1- addr-pad set8 -1 ;
566 \ the integer value does not fit on an unsigned 32-bit value, an error
569 : read-small-int-value ( lim -- lim x )
570 read-length-open-elt
581 \ Returned value is -1 on equality, 0 otherwise.
582 cc: eqOID ( addrConst -- bool ) {
584 const unsigned char *a1 = &CTX->pad[0];
588 x = -(memcmp(a1 + 1, a2 + 1, len) == 0);
595 \ Compare two blobs in the context. Returned value is -1 on equality, 0
597 cc: eqblob ( addr1 addr2 len -- bool ) {
601 T0_PUSHi(-(memcmp(a1, a2, len) == 0));
604 \ Check that a value is in a given range (inclusive).
605 : between? ( x min max -- bool )
608 \ Convert the provided byte value into a number in the 0..9 range,
609 \ assuming that it is an ASCII digit. A non-digit triggers an error
611 : digit-dec ( char -- value )
612 `0 - dup 0 9 between? ifnot ERR_X509_BAD_TIME fail then ;
614 \ Read two ASCII digits and return the value in the 0..99 range. An
616 : read-dec2 ( lim -- lim x )
617 read8 digit-dec 10 * { x } read8 digit-dec x + ;
620 \ range (inclusive).
621 : read-dec2-range ( lim min max -- lim x )
623 read-dec2 dup min max between? ifnot ERR_X509_BAD_TIME fail then ;
626 \ 16-bit value contains the month day count in its lower 5 bits. The first
628 data: month-to-days
638 : read-date ( lim -- lim days seconds )
641 read-tag
644 check-primitive
645 read-length-open-elt
651 \ Year is 4-digit with GeneralizedTime. With UTCTime, the year
652 \ is in the 1950..2049 range, and only the last two digits are
654 read-dec2
656 100 * >x read-dec2 x +
661 x 365 * x 3 + 4 / + x 99 + 100 / - x 399 + 400 / + >days
665 1 12 read-dec2-range
666 1- 1 <<
668 month-to-days + data-get16
674 1 swap read-dec2-range
675 days + 1- >days
681 \ implies a one-second shift that we can ignore).
682 0 23 read-dec2-range 3600 * >seconds
683 0 59 read-dec2-range 60 * seconds + >seconds
684 0 60 read-dec2-range seconds + >seconds
698 close-elt
702 \ positive; its unsigned big-endian encoding is stored in the provided
703 \ in-context buffer. Returned value is the decoded length. If the integer
705 : read-integer ( lim addr len -- lim dlen )
706 rot read-tag 0x02 check-tag-primitive -rot
707 read-integer-next ;
709 \ Identical to read-integer, but the tag has already been read and checked.
710 : read-integer-next ( lim addr len -- lim dlen )
712 read-length-open-elt
721 \ At that point, we have the first non-zero byte on the stack.
723 len dup ifnot ERR_X509_LIMIT_EXCEEDED fail then 1- >len
727 drop origlen len - ;
731 : read-boolean ( lim constructed value -- lim bool )
732 0x01 check-tag-primitive
733 read-length 1 <> if ERR_X509_BAD_BOOLEAN fail then
738 : read-curve-ID ( lim -- lim curve )
739 read-OID ifnot ERR_X509_UNSUPPORTED fail then
748 cc: DEBUG ( -- ) {
753 for (p = &CTX->dp_stack[0]; p != dp; p ++) {