fast-client-cert 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +56 -0
- data/README.md +152 -0
- data/lib/fast/client_cert/cert.rb +160 -0
- data/lib/fast/client_cert/chain.rb +334 -0
- data/lib/fast/client_cert/errors.rb +54 -0
- data/lib/fast/client_cert/pem.rb +151 -0
- data/lib/fast/client_cert/version.rb +7 -0
- data/lib/fast/client_cert.rb +88 -0
- metadata +77 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8b0ecac09b82d236ef1668013e67dc565950c34ce0f161d4c956ce2ef436cf16
|
|
4
|
+
data.tar.gz: a6c8bc249c30f26b8717160127aeea6ec9101abed7586e7ad6636134a3d8f2f3
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d986d9ff2cfcbd13f30fd22c16a20005f1b2aea695c570a0b6b59751e2c104a689468212b852156811de2cea2d46b81be49acc740d0baf6e7e5c1d46bc69e610
|
|
7
|
+
data.tar.gz: fc427f95bc4330a555d0f8de05658035e5859aa009c7c8ab04554cfb0758a500de1a8c01b7d396b9d7377c994fe7136cde75bf39510403ca80daf47d21a9ef2e
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-11-26)
|
|
4
|
+
|
|
5
|
+
- Fields: `Fast::ClientCert::Cert` and `Fast::ClientCert::Chain` — the
|
|
6
|
+
two RFC 9440 request fields as frozen value objects over fast-sfv,
|
|
7
|
+
with the family mechanics: `parse` fail-closed (a malformed field,
|
|
8
|
+
or any member violating the field's Byte Sequence constraint, is the
|
|
9
|
+
entire field ignored per RFC 9651 §2.2 — Cert answers nil, "no
|
|
10
|
+
certificate, the unauthenticated connection"; Chain answers the
|
|
11
|
+
shared frozen empty value, the same answer an absent field gives; no
|
|
12
|
+
member-level filtering), `parse!` positioned strict truth (the
|
|
13
|
+
wrapped Item/List failure, then the constraint — Cert at the value's
|
|
14
|
+
first non-OWS byte, Chain at the first offending member's, through
|
|
15
|
+
the quote-aware positioning Scan that runs only on the error path),
|
|
16
|
+
`valid?` the boolean mirror, and strict construction with canonical
|
|
17
|
+
`to_s`, class-checked equality and the reader sets (`der`; `ders`,
|
|
18
|
+
`size`, `empty?`, `each` Enumerator-graceful without a block, `[]`,
|
|
19
|
+
`split`).
|
|
20
|
+
- The singleton rule rides the Item grammar (§2.2: Client-Cert "MUST
|
|
21
|
+
NOT have a list of values or occur multiple times in a request" —
|
|
22
|
+
the Item parse fails a second member by itself, no detection code);
|
|
23
|
+
split lines recombine per RFC 9651 §4.2 into the same failure, and
|
|
24
|
+
`split` cuts a held chain back into per-instance lines for §2.3's
|
|
25
|
+
MAY-split (§3.1's header compression note).
|
|
26
|
+
- PEM helpers: `pem_to_field`, `field_to_pem`, `pems_to_field` and
|
|
27
|
+
`field_to_pems` — the §2.1 string surgery both directions ("it will
|
|
28
|
+
be sufficient to replace ---(BEGIN|END) CERTIFICATE--- with ':' and
|
|
29
|
+
remove line breaks in order to generate an appropriate item"), the
|
|
30
|
+
inverse wrapped at 64 characters per line (RFC 7468's form), pure
|
|
31
|
+
Ruby — no OpenSSL, DER bytes opaque. The PEM side trusts the armor
|
|
32
|
+
and never validates base64 (a mangled payload fails the Structured
|
|
33
|
+
Fields parse downstream, fail-closed); the field-value side is
|
|
34
|
+
strict. Usage failures raise the base `Fast::ClientCert::Error`;
|
|
35
|
+
wrong input types raise TypeError.
|
|
36
|
+
- Errors: `Fast::ClientCert::Error` → `ParseError`/`SerializeError`,
|
|
37
|
+
both carrying `#offset` — the family shape; one rescue guards the
|
|
38
|
+
whole gem.
|
|
39
|
+
- Budgets: the allocation tests for the nineteen paths (the two
|
|
40
|
+
fields' parse/parse!/valid?/new/emit paths, the split-lines parse,
|
|
41
|
+
the fail paths, the split, and the four PEM helpers), measured and
|
|
42
|
+
frozen in the file's comment.
|
|
43
|
+
- Scaffold: the gemspec (one runtime dependency, fast-sfv), the README
|
|
44
|
+
front door, and this changelog. The documentation pass and this
|
|
45
|
+
0.1.0 release land together (docs are part of the product — the
|
|
46
|
+
family's release rule).
|
|
47
|
+
- Docs: the diátaxis docs tree under `docs/` (tutorial, three how-to
|
|
48
|
+
guides — the TTRP insertion with the sanitization rules, the origin
|
|
49
|
+
validation with OpenSSL on the caller's side, the PEM round-trips —
|
|
50
|
+
one reference page, four explanations), the README front door with
|
|
51
|
+
the quickstart and the documentation index, and the both-mode
|
|
52
|
+
performance page from one measurement session (interpreter and
|
|
53
|
+
YJIT side by side, the benchmark kit alongside, rubocop-clean).
|
|
54
|
+
Every fenced ruby example was executed as printed and every RFC
|
|
55
|
+
citation checked against the RFC text by the pipeline's independent
|
|
56
|
+
review.
|
data/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# fast-client-cert
|
|
2
|
+
|
|
3
|
+
fast-client-cert is the RFC 9440 implementation for Ruby: the Client-Cert and Client-Cert-Chain request fields a TLS-terminating reverse proxy (TTRP) sends to convey the client certificate of a mutually authenticated TLS connection, built on fast-sfv.
|
|
4
|
+
|
|
5
|
+
## What it is
|
|
6
|
+
|
|
7
|
+
`Fast::ClientCert::Cert` implements RFC 9440 §2.2, the Client-Cert field: "This field contains the end-entity certificate used by the client in the TLS handshake" — "Client-Cert is a Byte Sequence with the value of the header encoded as described in Section 2.1", the DER bytes base64-coded and delimited with colons. A singleton field — "It is a singleton header field value as defined in Section 5.5 of [HTTP], which MUST NOT have a list of values or occur multiple times in a request" — the Item grammar enforces that by itself; a list-shaped value is a malformed field.
|
|
8
|
+
|
|
9
|
+
`Fast::ClientCert::Chain` implements RFC 9440 §2.3, the Client-Cert-Chain field: "the certificate chain used for validation of the end-entity certificate" — a List whose every member is a Byte Sequence, "The order is the same as the ordering in TLS"; the field "MAY have a list of values or occur multiple times in a request", so an Array of field lines is its native split form and `split` cuts a held chain back into per-instance lines. The membership rules (the chain "does not itself include the end-entity certificate", "The root certificate MAY be omitted", "Client-Cert-Chain MUST NOT appear unless Client-Cert is also present") are request-level policies over opaque DER bytes — documented, never enforced here.
|
|
10
|
+
|
|
11
|
+
The four PEM helpers (`pem_to_field`, `field_to_pem`, `pems_to_field`, `field_to_pems`) are the RFC 9440 §2.1 string surgery both directions, pure Ruby — no OpenSSL, the DER bytes opaque: "it will be sufficient to replace '---(BEGIN|END) CERTIFICATE---' with ':' and remove line breaks in order to generate an appropriate item".
|
|
12
|
+
|
|
13
|
+
The mechanics: the lenient parse (`parse`) answers a recipient's view and never raises on field content — a malformed field is treated as absent, and for Client-Cert absent means no certificate, the unauthenticated connection; the strict parse (`parse!`) tells the truth with byte offsets; `valid?` mirrors it with a boolean; `to_s` and construction are always strict; results are frozen value objects; one runtime dependency (fast-sfv), no OpenSSL, no DER decoding.
|
|
14
|
+
|
|
15
|
+
Both fields are request-only: the RFC states of each that it "is only for use in HTTP requests and MUST NOT be used in HTTP responses". RFC 9440 is an Informational RFC — implementers should weigh it accordingly. And RFC 9440 §4's warning: "backend servers MUST only accept the Client-Cert and Client-Cert-Chain header fields from a trusted TTRP" — sanitization and trust are the deployment's duties, never this gem's.
|
|
16
|
+
|
|
17
|
+
MIT license; source at `https://github.com/jetpks/fast-http-fields`.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
gem install fast-client-cert
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
In a Gemfile:
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
gem "fast-client-cert"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Require path is `fast/client_cert`:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
require "fast/client_cert"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Requires Ruby >= 3.4 and RubyGems >= 3.5.3; the only runtime requirement is fast-sfv.
|
|
38
|
+
|
|
39
|
+
## Quickstart
|
|
40
|
+
|
|
41
|
+
Read a Client-Cert field and compare the DER bytes — the day-one idiom:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
require "fast/client_cert"
|
|
45
|
+
|
|
46
|
+
cert = Fast::ClientCert::Cert.parse(":aGVsbG8=:")
|
|
47
|
+
cert.der # => "hello"
|
|
48
|
+
cert.to_s # => ":aGVsbG8=:"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The fail-closed story: a field a recipient cannot read is the field treated as absent — no certificate, the unauthenticated connection. `parse` answers nil for malformed content, a list-shaped value (the singleton rule) and an Item that is not a Byte Sequence alike; `parse!` is the strict truth, positioned:
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
Fast::ClientCert::Cert.parse("not a field") # => nil
|
|
55
|
+
Fast::ClientCert::Cert.valid?("!!") # => false
|
|
56
|
+
begin
|
|
57
|
+
Fast::ClientCert::Cert.parse!("5")
|
|
58
|
+
rescue Fast::ClientCert::ParseError => e
|
|
59
|
+
e.message # => "Client-Cert: the value is a Byte Sequence (got Integer)"
|
|
60
|
+
e.offset # => 0
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
The chain, its split form and the recombine:
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
chain = Fast::ClientCert::Chain.parse(":aGVsbG8=:, :d29ybGQ=:")
|
|
68
|
+
chain.ders # => ["hello", "world"]
|
|
69
|
+
chain.to_s # => ":aGVsbG8=:, :d29ybGQ=:"
|
|
70
|
+
|
|
71
|
+
lines = chain.split(1) # one certificate per field instance
|
|
72
|
+
lines # => [":aGVsbG8=:", ":d29ybGQ=:"]
|
|
73
|
+
Fast::ClientCert::Chain.parse(lines) == chain # => true
|
|
74
|
+
|
|
75
|
+
received = [":aGVsbG8=:", " :d29ybGQ=:"]
|
|
76
|
+
Fast::ClientCert::Chain.parse(received).to_s # => ":aGVsbG8=:, :d29ybGQ=:"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
A malformed chain, or any member that is not a Byte Sequence, is the whole field ignored — a valid certificate beside a bad one drops with it:
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
Fast::ClientCert::Chain.parse(":aGVsbG8=:, 5").empty? # => true
|
|
83
|
+
empty = Fast::ClientCert::Chain.parse("!!")
|
|
84
|
+
empty.empty? # => true
|
|
85
|
+
Fast::ClientCert::Chain.parse("").empty? # => true
|
|
86
|
+
Fast::ClientCert::Chain.valid?("") # => true
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## The PEM surgery
|
|
90
|
+
|
|
91
|
+
Certificates usually live as the textual blocks RFC 9440 §2.1 points at; the conversion "will be sufficient" as pure string surgery. Both directions, at both levels:
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
block = <<~PEM
|
|
95
|
+
-----BEGIN CERTIFICATE-----
|
|
96
|
+
aGVsbG8=
|
|
97
|
+
-----END CERTIFICATE-----
|
|
98
|
+
PEM
|
|
99
|
+
|
|
100
|
+
Fast::ClientCert.pem_to_field(block) # => ":aGVsbG8=:"
|
|
101
|
+
Fast::ClientCert.field_to_pem(":aGVsbG8=:") == block # => true
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The chain pair:
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
first = <<~PEM
|
|
108
|
+
-----BEGIN CERTIFICATE-----
|
|
109
|
+
aGVsbG8=
|
|
110
|
+
-----END CERTIFICATE-----
|
|
111
|
+
PEM
|
|
112
|
+
|
|
113
|
+
second = <<~PEM
|
|
114
|
+
-----BEGIN CERTIFICATE-----
|
|
115
|
+
d29ybGQ=
|
|
116
|
+
-----END CERTIFICATE-----
|
|
117
|
+
PEM
|
|
118
|
+
|
|
119
|
+
Fast::ClientCert.pems_to_field([first, second]) # => ":aGVsbG8=:, :d29ybGQ=:"
|
|
120
|
+
Fast::ClientCert.field_to_pems(":aGVsbG8=:, :d29ybGQ=:") == [first, second] # => true
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The trust split: the PEM side trusts the armor and never validates base64 — a mangled payload becomes a field value that fails the Structured Fields parse downstream, fail-closed like any malformed field; the field-value side is strict: a sender never writes garbage, so a value without colons on both ends and a whole base64 interior raises `Fast::ClientCert::Error` before anything is written; wrong input types raise TypeError; the empty blocks Array is a usage failure ("nothing to convert"); the empty chain value answers no blocks.
|
|
124
|
+
|
|
125
|
+
## The guarantees
|
|
126
|
+
|
|
127
|
+
`parse` never raises on field content. `parse!` raises `Fast::ClientCert::ParseError` positioned at the offending byte — the wrapped substrate failure with its offset preserved, or the Byte Sequence constraint at the value's first non-OWS byte (for a chain, the first offending member's, quote-aware: a comma inside a quoted parameter does not split). `to_s` and construction are always strict: a DER that is not a String tagged ASCII-8BIT (the encoding IS the Byte Sequence tag — a UTF-8 String would emit as a quoted sf-string) or a Token raises `Fast::ClientCert::SerializeError` before anything is emitted. `Fast::ClientCert::Error` → `ParseError`/`SerializeError`, both carrying `#offset` — one rescue guards the whole gem; a wrong input type is a caller bug and raises plain TypeError. The value objects are frozen — the Cert, its DER, the Chain, its ders Array and every DER String; the DER bytes are frozen in place on construction, so pass a copy if the buffer is reused. Parameters ride the canonical re-emit and never fail either field.
|
|
128
|
+
|
|
129
|
+
## Documentation
|
|
130
|
+
|
|
131
|
+
Current release 0.1.0; changelog at [CHANGELOG.md](CHANGELOG.md).
|
|
132
|
+
|
|
133
|
+
### Tutorial
|
|
134
|
+
|
|
135
|
+
- [Your first Client-Cert field](docs/tutorials/client-cert-fields.md) — Parse a Client-Cert and check its DER bytes, parse a chain and split it, convert a PEM block, and meet the fail-closed nil and the strict error.
|
|
136
|
+
|
|
137
|
+
### How-to guides
|
|
138
|
+
|
|
139
|
+
- [Insert Client-Cert and Client-Cert-Chain into forwarded requests](docs/how-to-guides/insert-the-fields-into-forwarded-requests.md) — Build the two fields from the connection's certificate, sanitize the incoming request per RFC 9440 §2.4, split the chain for compression, and know the rules around the fields.
|
|
140
|
+
- [Validate a Client-Cert at the origin](docs/how-to-guides/validate-a-client-cert-at-the-origin.md) — Read the field, treat nil as unauthenticated, compare the DER against an OpenSSL digest and the certificate through OpenSSL, and read the chain.
|
|
141
|
+
- [Convert between PEM blocks and field values](docs/how-to-guides/convert-between-pem-and-field-values.md) — The RFC 9440 §2.1 string surgery both directions at both levels, the tolerances, and the strict sender side.
|
|
142
|
+
|
|
143
|
+
### Reference
|
|
144
|
+
|
|
145
|
+
- [API reference](docs/reference/api.md) — The complete surface: both field classes (parsing, the value objects, construction), the four PEM helpers, the errors, and how the surface is verified.
|
|
146
|
+
|
|
147
|
+
### Explanation
|
|
148
|
+
|
|
149
|
+
- [Fail-closed: the unauthenticated connection](docs/explanation/fail-closed-semantics.md) — Why a field a recipient cannot read is the field treated as absent — nil, the unauthenticated connection — and what the strict view is for.
|
|
150
|
+
- [Field injection and the trusted path](docs/explanation/field-injection-and-trusted-path.md) — RFC 9440 §4 and Appendix B.1/B.2: the injection attack, why it does not fail safe, sanitizing as the mechanism, and the trusted path.
|
|
151
|
+
- [The whole certificate as opaque DER](docs/explanation/the-whole-certificate.md) — RFC 9440 Appendix B.3: why the full certificate travels, why the DER bytes stay opaque, and why the gem is pure Ruby with no OpenSSL.
|
|
152
|
+
- [Performance and allocation budgets](docs/explanation/performance.md) — How the paths stay allocation-lean; the both-mode benchmark tables and the allocation budgets, with methodology and caveats.
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module ClientCert
|
|
5
|
+
# RFC 9440 §2.2, the Client-Cert field: "This field contains the
|
|
6
|
+
# end-entity certificate used by the client in the TLS handshake"
|
|
7
|
+
# — "Client-Cert is a Byte Sequence with the value of the header
|
|
8
|
+
# encoded as described in Section 2.1", the DER bytes base64-coded
|
|
9
|
+
# and delimited with colons. A singleton field: "It is a singleton
|
|
10
|
+
# header field value as defined in Section 5.5 of [HTTP], which
|
|
11
|
+
# MUST NOT have a list of values or occur multiple times in a
|
|
12
|
+
# request" — the Item grammar enforces that by itself (it consumes
|
|
13
|
+
# exactly one value and fails at the byte after it), so this class
|
|
14
|
+
# carries no list-detection code; an Array of field lines
|
|
15
|
+
# recombines per RFC 9651 §4.2 into exactly that list-shaped
|
|
16
|
+
# failure.
|
|
17
|
+
class Cert
|
|
18
|
+
# Items this class builds carry no parameters, so they all share
|
|
19
|
+
# the one frozen empty Hash — the parse path's own shape.
|
|
20
|
+
EMPTY_PARAMS = {}.freeze
|
|
21
|
+
private_constant :EMPTY_PARAMS
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
# §2.2 read as RFC 9651 §2.2 recipient semantics — never
|
|
25
|
+
# raises on field content. A field whose syntax is not a
|
|
26
|
+
# Structured Field Value, a list-shaped value (the singleton
|
|
27
|
+
# rule), the empty field, or an Item whose value is not a Byte
|
|
28
|
+
# Sequence is the field treated as ABSENT: nil — no
|
|
29
|
+
# certificate, the unauthenticated connection (RFC 9440 §4:
|
|
30
|
+
# the access-control decision on absence is the caller's).
|
|
31
|
+
# There is no empty-certificate value object: the wire never
|
|
32
|
+
# carries one. Parameters on the Item never fail the field and
|
|
33
|
+
# ride the canonical re-emit. A String is one field value; an
|
|
34
|
+
# Array is the field's lines, combined with ", " (RFC 9651
|
|
35
|
+
# §4.2). A wrong input type is a caller bug and raises
|
|
36
|
+
# TypeError. The substrate failure is rescued raw rather than
|
|
37
|
+
# wrapped: the wrapped error is the strict caller's truth, and
|
|
38
|
+
# the answer is nil either way.
|
|
39
|
+
def parse(input)
|
|
40
|
+
item = SFV.parse_item(input)
|
|
41
|
+
from_item(item) if Checks.byte_sequence?(item.value)
|
|
42
|
+
rescue SFV::ParseError
|
|
43
|
+
nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Strict truth. The RFC 9651 Item failure is wrapped with its
|
|
47
|
+
# message and byte offset preserved; then the §2.1 constraint
|
|
48
|
+
# — the Item's value must be a Byte Sequence — raises
|
|
49
|
+
# ParseError at the value's first non-OWS byte in the combined
|
|
50
|
+
# field value (the §4.2 parse discards leading OWS, so " 5"
|
|
51
|
+
# fails at 1, not 0), the message naming the type §2.1 states.
|
|
52
|
+
# TypeError for a wrong input type, as parse.
|
|
53
|
+
def parse!(input)
|
|
54
|
+
item = SFV.parse_item(input)
|
|
55
|
+
return from_item(item) if Checks.byte_sequence?(item.value)
|
|
56
|
+
|
|
57
|
+
raise ParseError.new(
|
|
58
|
+
"Client-Cert: the value is a Byte Sequence (got #{Checks.shown(item)})", value_offset(input)
|
|
59
|
+
)
|
|
60
|
+
rescue SFV::ParseError => e
|
|
61
|
+
raise ParseError.new(e.message, e.offset)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# The boolean mirror: true exactly when the field carries a
|
|
65
|
+
# certificate — false for malformed content, non-Byte-Sequence
|
|
66
|
+
# items, list-shaped values, the empty field and a wrong input
|
|
67
|
+
# type alike (a caller bug is not field content either).
|
|
68
|
+
def valid?(input)
|
|
69
|
+
!parse(input).nil?
|
|
70
|
+
rescue TypeError
|
|
71
|
+
false
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
# The byte the value starts at in the combined field value:
|
|
77
|
+
# the leading OWS skipped — where the constraint's ParseError
|
|
78
|
+
# points. The RFC 9651 §4.2 item parse discards exactly these
|
|
79
|
+
# bytes before the value.
|
|
80
|
+
def value_offset(input)
|
|
81
|
+
field = input.is_a?(Array) ? input.join(", ") : input
|
|
82
|
+
index = 0
|
|
83
|
+
while index < field.bytesize
|
|
84
|
+
case field.getbyte(index)
|
|
85
|
+
when 0x20, 0x09 then index += 1
|
|
86
|
+
else break
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
index
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# The value object behind an Item that already passed the
|
|
93
|
+
# check: held as parsed, no re-validation, the Item's
|
|
94
|
+
# parameters preserved for the canonical re-emit.
|
|
95
|
+
def from_item(item)
|
|
96
|
+
cert = allocate
|
|
97
|
+
cert.send(:hold, item)
|
|
98
|
+
cert
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The DER bytes of the end-entity certificate — frozen, tagged
|
|
103
|
+
# ASCII-8BIT, bytewise-comparable from day one:
|
|
104
|
+
# Fast::ClientCert::Cert.parse(value).der == File.binread("client.der").
|
|
105
|
+
attr_reader :der
|
|
106
|
+
|
|
107
|
+
# Strict construction — a sender never puts garbage on the wire.
|
|
108
|
+
# +der+ is the DER-encoded certificate as a String tagged
|
|
109
|
+
# ASCII-8BIT and not a Token: the encoding IS the Byte Sequence
|
|
110
|
+
# tag (a UTF-8 String would emit as a quoted sf-string, a Token
|
|
111
|
+
# as a token), and the bytes themselves are NEVER inspected —
|
|
112
|
+
# DER structure is the caller's, nothing in this gem depends on
|
|
113
|
+
# it. The String is frozen IN PLACE as part of the value object
|
|
114
|
+
# — pass a copy if the buffer is reused. Anything else raises
|
|
115
|
+
# SerializeError before anything is emitted.
|
|
116
|
+
def initialize(der)
|
|
117
|
+
unless Checks.byte_sequence?(der)
|
|
118
|
+
raise SerializeError.new("Client-Cert: the value is a Byte Sequence (got #{Checks.shown(der)})", 0)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
hold(SFV::Item.new(der.freeze, EMPTY_PARAMS))
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# The canonical wire form: the RFC 9651 §4.1.3 serialization of
|
|
125
|
+
# the held Item, ":<base64>:" — a parsed value re-emits the
|
|
126
|
+
# parameters it carried, a constructed one carries none. Never
|
|
127
|
+
# raises: the parse and the construction validated everything.
|
|
128
|
+
def to_s
|
|
129
|
+
SFV.serialize_item(@item)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Canonical-wire-form equality WITHIN the class: a Cert equals a
|
|
133
|
+
# Cert — never a bare Item — whose serialization matches.
|
|
134
|
+
def ==(other)
|
|
135
|
+
other.instance_of?(self.class) && to_s == other.to_s
|
|
136
|
+
end
|
|
137
|
+
alias eql? ==
|
|
138
|
+
|
|
139
|
+
# Consistent with eql? — the canonical form's hash.
|
|
140
|
+
def hash
|
|
141
|
+
to_s.hash
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def inspect
|
|
145
|
+
wire = to_s
|
|
146
|
+
"#<#{self.class.name} #{wire}>"
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
private
|
|
150
|
+
|
|
151
|
+
# The one place the held state is written, then frozen: the Item
|
|
152
|
+
# (frozen already on the parse path) and the DER it carries.
|
|
153
|
+
def hold(item)
|
|
154
|
+
@item = item
|
|
155
|
+
@der = item.value
|
|
156
|
+
freeze
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module ClientCert
|
|
5
|
+
# RFC 9440 §2.3, the Client-Cert-Chain field: "the certificate
|
|
6
|
+
# chain used for validation of the end-entity certificate" — a
|
|
7
|
+
# List whose every member is a Byte Sequence (§2.1), "The order is
|
|
8
|
+
# the same as the ordering in TLS". The field "MAY have a list of
|
|
9
|
+
# values or occur multiple times in a request" — an Array of field
|
|
10
|
+
# lines is its native split form (combined per RFC 9651 §4.2, a
|
|
11
|
+
# List concatenates with no last-wins) and #split cuts a held
|
|
12
|
+
# chain back into per-instance lines, the §3.1 note that "for
|
|
13
|
+
# header compression purposes, it might be advantageous to split
|
|
14
|
+
# lists into multiple instances". TLS order is the List's own
|
|
15
|
+
# order — data, preserved exactly by the parse and the emit; the
|
|
16
|
+
# membership rules (the chain "does not itself include the
|
|
17
|
+
# end-entity certificate", "The root certificate MAY be omitted",
|
|
18
|
+
# "Client-Cert-Chain MUST NOT appear unless Client-Cert is also
|
|
19
|
+
# present") are request-level policies over opaque DER bytes —
|
|
20
|
+
# documented, never enforced here.
|
|
21
|
+
class Chain
|
|
22
|
+
# The RFC 9651 value shape a member holds, aliased so the
|
|
23
|
+
# checks and the construction read it by the short name.
|
|
24
|
+
Item = SFV::Item
|
|
25
|
+
private_constant :Item
|
|
26
|
+
|
|
27
|
+
# Items this class builds carry no parameters, so they all share
|
|
28
|
+
# the one frozen empty Hash — the parse path's own shape.
|
|
29
|
+
EMPTY_PARAMS = {}.freeze
|
|
30
|
+
private_constant :EMPTY_PARAMS
|
|
31
|
+
|
|
32
|
+
class << self
|
|
33
|
+
# §2.3 read as RFC 9651 §2.2 recipient semantics — never
|
|
34
|
+
# raises on field content. A field whose syntax is not a
|
|
35
|
+
# Structured Field Value, or a List carrying ANY member that is
|
|
36
|
+
# not a Byte Sequence, is the ENTIRE field ignored: the shared
|
|
37
|
+
# frozen empty value, the same answer an absent field gives.
|
|
38
|
+
# There is NO member-level filtering — a valid certificate
|
|
39
|
+
# beside a bad one drops with the field, the fail-closed
|
|
40
|
+
# posture RFC 9440 §4's security story demands. "" is the
|
|
41
|
+
# valid empty List and parses to the empty value too.
|
|
42
|
+
# Parameters on the members never fail the field and ride the
|
|
43
|
+
# canonical re-emit. A String is one field value; an Array is
|
|
44
|
+
# the field's lines, combined with ", " (RFC 9651 §4.2). A
|
|
45
|
+
# wrong input type is a caller bug and raises TypeError. The
|
|
46
|
+
# substrate failure is rescued raw rather than wrapped: the
|
|
47
|
+
# wrapped error is the strict caller's truth, and the answer
|
|
48
|
+
# is the empty value either way.
|
|
49
|
+
def parse(input)
|
|
50
|
+
members = SFV.parse_list(input)
|
|
51
|
+
# "" is the valid empty List (RFC 9651 4.2.1): it parses to
|
|
52
|
+
# the shared empty value, not a fresh one.
|
|
53
|
+
return EMPTY if members.empty?
|
|
54
|
+
return EMPTY unless members.all? { |member| member_ok?(member) }
|
|
55
|
+
|
|
56
|
+
from_members(members)
|
|
57
|
+
rescue SFV::ParseError
|
|
58
|
+
EMPTY
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Strict truth. The RFC 9651 List failure is wrapped with its
|
|
62
|
+
# message and byte offset preserved; then the §2.3 member
|
|
63
|
+
# constraint — every member an Item whose value is a Byte
|
|
64
|
+
# Sequence — the FIRST offender in List order raised at that
|
|
65
|
+
# member's first non-OWS byte in the combined field value, the
|
|
66
|
+
# message naming the field and the type §2.3 states. The
|
|
67
|
+
# positioning Scan runs only on this error path. TypeError for
|
|
68
|
+
# a wrong input type, as parse.
|
|
69
|
+
def parse!(input)
|
|
70
|
+
members = SFV.parse_list(input)
|
|
71
|
+
index = 0
|
|
72
|
+
while index < members.size
|
|
73
|
+
member = members[index]
|
|
74
|
+
unless member_ok?(member)
|
|
75
|
+
offset = Scan.new(combined(input)).member_offset(index)
|
|
76
|
+
raise ParseError.new(
|
|
77
|
+
"a Client-Cert-Chain member is a Byte Sequence (got #{Checks.shown(member)})", offset
|
|
78
|
+
)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
index += 1
|
|
82
|
+
end
|
|
83
|
+
from_members(members)
|
|
84
|
+
rescue SFV::ParseError => e
|
|
85
|
+
raise ParseError.new(e.message, e.offset)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The boolean mirror: true exactly when the strict parse
|
|
89
|
+
# returns — false for malformed content, member violations and
|
|
90
|
+
# a wrong input type alike (a caller bug is not field content
|
|
91
|
+
# either). The empty List is a Structured Field Value: "" is
|
|
92
|
+
# true.
|
|
93
|
+
def valid?(input)
|
|
94
|
+
parse!(input)
|
|
95
|
+
true
|
|
96
|
+
rescue TypeError, ParseError
|
|
97
|
+
false
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
# The §2.3 member constraint: an Item whose value is a Byte
|
|
103
|
+
# Sequence — the same check Cert reads, through the shared
|
|
104
|
+
# Checks seam, so the two fields' boundaries cannot drift.
|
|
105
|
+
def member_ok?(member)
|
|
106
|
+
member.is_a?(Item) && Checks.byte_sequence?(member.value)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The field value the Scan positions into: the lines joined
|
|
110
|
+
# with ", " — the same RFC 9651 §4.2 combining
|
|
111
|
+
# Fast::SFV.parse_list applies — a String as given. Computed
|
|
112
|
+
# only on the error path, where the positioning needs it.
|
|
113
|
+
def combined(input)
|
|
114
|
+
input.is_a?(Array) ? input.join(", ") : input
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# The value object behind members that already passed the
|
|
118
|
+
# checks: held as parsed, no re-validation, the members'
|
|
119
|
+
# parameters preserved for the canonical re-emit.
|
|
120
|
+
def from_members(members)
|
|
121
|
+
chain = allocate
|
|
122
|
+
chain.send(:hold, members)
|
|
123
|
+
chain
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# The DER bytes of the chain's certificates in TLS order — a
|
|
128
|
+
# frozen Array of frozen ASCII-8BIT Strings, bytewise-comparable
|
|
129
|
+
# from day one.
|
|
130
|
+
attr_reader :ders
|
|
131
|
+
|
|
132
|
+
# Strict construction — a sender never puts garbage on the wire.
|
|
133
|
+
# +ders+ is an Array of DER-encoded certificates, each a String
|
|
134
|
+
# tagged ASCII-8BIT and not a Token — the same constraint the
|
|
135
|
+
# parse reads, through the shared Checks seam, so the two
|
|
136
|
+
# boundaries cannot drift; each String is frozen IN PLACE as
|
|
137
|
+
# part of the value object (pass a copy if the buffer is
|
|
138
|
+
# reused), and the bytes themselves are never inspected. The
|
|
139
|
+
# empty Array builds the empty value: a sender omits the field.
|
|
140
|
+
# Anything else raises SerializeError before anything is
|
|
141
|
+
# emitted.
|
|
142
|
+
def initialize(ders)
|
|
143
|
+
unless ders.is_a?(Array)
|
|
144
|
+
raise SerializeError.new("Client-Cert-Chain members are an Array (got #{ders.class})", 0)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
items = []
|
|
148
|
+
ders.each do |der|
|
|
149
|
+
unless Checks.byte_sequence?(der)
|
|
150
|
+
raise SerializeError.new("a Client-Cert-Chain member is a Byte Sequence (got #{Checks.shown(der)})", 0)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
items << Item.new(der.freeze, EMPTY_PARAMS)
|
|
154
|
+
end
|
|
155
|
+
hold(items)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# How many certificates the chain carries.
|
|
159
|
+
def size
|
|
160
|
+
@ders.size
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# No certificates held — an absent field, or one treated as
|
|
164
|
+
# absent.
|
|
165
|
+
def empty?
|
|
166
|
+
@ders.empty?
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Yields each certificate's DER bytes in TLS order; without a
|
|
170
|
+
# block, the Enumerator over them.
|
|
171
|
+
def each(&block)
|
|
172
|
+
@ders.each(&block)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# The DER at +index+ — Array semantics (negative indices, nil
|
|
176
|
+
# beyond the ends).
|
|
177
|
+
def [](index)
|
|
178
|
+
@ders[index]
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# The canonical wire form: the RFC 9651 §4.1.1 List
|
|
182
|
+
# serialization of the held members — the Byte Sequences joined
|
|
183
|
+
# with ", " — "" when none are held (a sender omits the field
|
|
184
|
+
# entirely, the same reason Fast::SFV.serialize_list answers nil
|
|
185
|
+
# for the empty List). Never raises: the parse and the
|
|
186
|
+
# construction validated every member.
|
|
187
|
+
def to_s
|
|
188
|
+
SFV.serialize_list(@items) || ""
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# The sender-side cut (§2.3: "it might be advantageous to split
|
|
192
|
+
# lists into multiple instances"): a frozen Array of canonical
|
|
193
|
+
# field values, each carrying +per_line+ certificates — one per
|
|
194
|
+
# instance at 1, the whole chain in one value at the size. The
|
|
195
|
+
# lines recombine through the Array entry point:
|
|
196
|
+
# Fast::ClientCert::Chain.parse(chain.split(1)) answers the same
|
|
197
|
+
# chain. +per_line+ is a caller choice, so a non-positive one is
|
|
198
|
+
# a caller bug — ArgumentError, never a field error. The empty
|
|
199
|
+
# chain answers no lines.
|
|
200
|
+
def split(per_line)
|
|
201
|
+
unless per_line.is_a?(Integer) && per_line.positive?
|
|
202
|
+
raise ArgumentError, "per_line is a positive Integer (got #{per_line.inspect})"
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
lines = []
|
|
206
|
+
index = 0
|
|
207
|
+
size = @items.size
|
|
208
|
+
while index < size
|
|
209
|
+
lines << SFV.serialize_list(@items[index, per_line])
|
|
210
|
+
index += per_line
|
|
211
|
+
end
|
|
212
|
+
lines.freeze
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# Canonical-wire-form equality WITHIN the class: a Chain equals a
|
|
216
|
+
# Chain — never a Cert, never a bare Array — whose serialization
|
|
217
|
+
# matches.
|
|
218
|
+
def ==(other)
|
|
219
|
+
other.instance_of?(self.class) && to_s == other.to_s
|
|
220
|
+
end
|
|
221
|
+
alias eql? ==
|
|
222
|
+
|
|
223
|
+
# Consistent with eql? — the canonical form's hash.
|
|
224
|
+
def hash
|
|
225
|
+
to_s.hash
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def inspect
|
|
229
|
+
wire = to_s
|
|
230
|
+
wire.empty? ? "#<#{self.class.name}>" : "#<#{self.class.name} #{wire}>"
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
private
|
|
234
|
+
|
|
235
|
+
# The one place the held state is written, then frozen: the
|
|
236
|
+
# members as parsed (or as built), and the DERs view derived
|
|
237
|
+
# from them.
|
|
238
|
+
def hold(items)
|
|
239
|
+
@items = items.freeze
|
|
240
|
+
@ders = items.map(&:value).freeze
|
|
241
|
+
freeze
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# The strict parse's positioning walk: where member +index+'s
|
|
245
|
+
# first non-OWS byte sits in the combined field value — the
|
|
246
|
+
# offset a member ParseError points at. The field value already
|
|
247
|
+
# parsed when this runs, so the walk only tracks the member
|
|
248
|
+
# boundaries: members split on commas at inner-list depth zero
|
|
249
|
+
# outside quoted strings (RFC 9651 §4.2's separators), so a
|
|
250
|
+
# comma inside a quoted string (§4.2.5, a backslash escapes the
|
|
251
|
+
# next byte) or inside an inner list's parens (§4.2.1.1) does
|
|
252
|
+
# not split.
|
|
253
|
+
class Scan
|
|
254
|
+
# A 256-entry byte class: true for every byte of +spec+ — the
|
|
255
|
+
# byte-table shape the family's scanners walk with.
|
|
256
|
+
def self.byte_class(spec)
|
|
257
|
+
table = Array.new(256, false)
|
|
258
|
+
spec.each_byte { |byte| table[byte] = true }
|
|
259
|
+
table.freeze
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# The optional whitespace around a member separator (RFC 9651
|
|
263
|
+
# §4.2.1).
|
|
264
|
+
OWS = byte_class(" \t")
|
|
265
|
+
|
|
266
|
+
COMMA = 0x2C
|
|
267
|
+
LPAREN = 0x28
|
|
268
|
+
RPAREN = 0x29
|
|
269
|
+
DQUOTE = 0x22
|
|
270
|
+
BACKSLASH = 0x5C
|
|
271
|
+
|
|
272
|
+
def initialize(combined)
|
|
273
|
+
@combined = combined
|
|
274
|
+
@size = combined.bytesize
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# The byte offset of member +index+'s first non-OWS byte —
|
|
278
|
+
# where a member ParseError points.
|
|
279
|
+
def member_offset(index)
|
|
280
|
+
pos = skip_ows(0)
|
|
281
|
+
member = 0
|
|
282
|
+
while member < index
|
|
283
|
+
pos = boundary(pos) + 1
|
|
284
|
+
pos = skip_ows(pos)
|
|
285
|
+
member += 1
|
|
286
|
+
end
|
|
287
|
+
pos
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
private
|
|
291
|
+
|
|
292
|
+
# Discards leading OWS.
|
|
293
|
+
def skip_ows(index)
|
|
294
|
+
index += 1 while index < @size && OWS[@combined.getbyte(index)]
|
|
295
|
+
index
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# The index of the next member boundary at inner-list depth
|
|
299
|
+
# zero outside a quoted string, starting at +index+: the ","
|
|
300
|
+
# that ends a member. A quoted string's commas are inside its
|
|
301
|
+
# quotes (§4.2.5, a backslash escapes the next byte), an inner
|
|
302
|
+
# list's are inside its parens (§4.2.1.1).
|
|
303
|
+
def boundary(index)
|
|
304
|
+
depth = 0
|
|
305
|
+
quoted = false
|
|
306
|
+
while index < @size
|
|
307
|
+
byte = @combined.getbyte(index)
|
|
308
|
+
if quoted
|
|
309
|
+
index += byte == BACKSLASH ? 2 : 1
|
|
310
|
+
quoted = false if byte == DQUOTE
|
|
311
|
+
else
|
|
312
|
+
case byte
|
|
313
|
+
when DQUOTE then quoted = true
|
|
314
|
+
when LPAREN then depth += 1
|
|
315
|
+
when RPAREN then depth -= 1
|
|
316
|
+
when COMMA then return index if depth.zero?
|
|
317
|
+
end
|
|
318
|
+
index += 1
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
index
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
private_constant :Scan
|
|
325
|
+
|
|
326
|
+
# The members a fail-closed parse holds: none — the same answer
|
|
327
|
+
# an absent field gives (RFC 9651 §2.2; §2.3 defines no other
|
|
328
|
+
# handling for members). The shared singleton: every fall and
|
|
329
|
+
# every absent field answer this one frozen value.
|
|
330
|
+
EMPTY = new([])
|
|
331
|
+
private_constant :EMPTY
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module ClientCert
|
|
5
|
+
# The family root: every Fast::ClientCert failure descends from
|
|
6
|
+
# Error, so a caller can guard the whole surface with one rescue.
|
|
7
|
+
# Error is also raised in its own right, by the PEM helpers: an
|
|
8
|
+
# armor-shape failure (no BEGIN line, the END missing, no payload,
|
|
9
|
+
# a second block) or a field value that is not a whole base64 item
|
|
10
|
+
# is a USAGE failure — neither a parse defect (the field never
|
|
11
|
+
# parsed) nor a serialization defect (nothing was built) — so it
|
|
12
|
+
# answers the base class, and the message names the reason. A
|
|
13
|
+
# wrong input type is a caller bug, not field content: it raises
|
|
14
|
+
# plain TypeError and never this class.
|
|
15
|
+
class Error < StandardError; end
|
|
16
|
+
|
|
17
|
+
# A field this gem cannot read. Two shapes meet here: the
|
|
18
|
+
# underlying RFC 9651 parse failed — the failure is wrapped with
|
|
19
|
+
# its message and byte offset preserved — or, under the strict
|
|
20
|
+
# parse, a value broke the field's RFC 9440 constraint (§2.2's
|
|
21
|
+
# Byte Sequence Item, §2.3's Byte Sequence members), and #offset is
|
|
22
|
+
# the byte position of the offending value in the combined field
|
|
23
|
+
# value — for a chain, the first offender's first non-OWS byte.
|
|
24
|
+
# The lenient parse never raises it: a recipient that cannot read
|
|
25
|
+
# the field treats it as absent (RFC 9651 §2.2).
|
|
26
|
+
class ParseError < Error
|
|
27
|
+
attr_reader :offset
|
|
28
|
+
|
|
29
|
+
def initialize(message, offset)
|
|
30
|
+
super(message)
|
|
31
|
+
@offset = offset
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# A value cannot go on the wire: the field under construction
|
|
36
|
+
# breaks a bound RFC 9440 states — a DER that is not a String
|
|
37
|
+
# tagged ASCII-8BIT and not a Token (a UTF-8 String would emit as
|
|
38
|
+
# a quoted sf-string, a Token as a token — neither is the Byte
|
|
39
|
+
# Sequence §2.1 demands), or a chain members argument that is not
|
|
40
|
+
# an Array of them. #offset is the byte position in the output
|
|
41
|
+
# built so far — 0, because the check runs before anything is
|
|
42
|
+
# emitted; the whole emit path raises this class and never
|
|
43
|
+
# TypeError: on emit the wrongness is in the value being sent,
|
|
44
|
+
# which is field content, not a caller protocol bug.
|
|
45
|
+
class SerializeError < Error
|
|
46
|
+
attr_reader :offset
|
|
47
|
+
|
|
48
|
+
def initialize(message, offset)
|
|
49
|
+
super(message)
|
|
50
|
+
@offset = offset
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module ClientCert
|
|
5
|
+
# The RFC 9440 §2.1 armor lines, the text a PEM certificate block
|
|
6
|
+
# is wrapped in.
|
|
7
|
+
BEGIN_ARMOR = "-----BEGIN CERTIFICATE-----"
|
|
8
|
+
END_ARMOR = "-----END CERTIFICATE-----"
|
|
9
|
+
# The colon that delimits a Byte Sequence on both ends (§2.1:
|
|
10
|
+
# "delimited with colons on either side").
|
|
11
|
+
COLON = 0x3A
|
|
12
|
+
private_constant :BEGIN_ARMOR, :END_ARMOR, :COLON
|
|
13
|
+
|
|
14
|
+
# The base64 interior a ":<base64>:" value must carry: alphabet
|
|
15
|
+
# characters with padding only at the end (RFC 4648 §4).
|
|
16
|
+
BASE64 = %r{\A[A-Za-z0-9+/]*={0,2}\z}
|
|
17
|
+
private_constant :BASE64
|
|
18
|
+
|
|
19
|
+
# The RFC 9440 §2.1 string surgery, both directions — pure text
|
|
20
|
+
# manipulation, no OpenSSL, no DER decoding: "it will be
|
|
21
|
+
# sufficient to replace ---(BEGIN|END) CERTIFICATE--- with ':' and
|
|
22
|
+
# remove line breaks in order to generate an appropriate item".
|
|
23
|
+
# The PEM side trusts the armor and never validates the base64 (a
|
|
24
|
+
# mangled payload becomes a field value that fails the Structured
|
|
25
|
+
# Fields parse downstream — fail-closed like any malformed field);
|
|
26
|
+
# the field-value side is strict: a sender never writes garbage.
|
|
27
|
+
# Usage failures raise the base Error — neither a parse defect nor
|
|
28
|
+
# a serialization defect; wrong input types raise TypeError.
|
|
29
|
+
module_function
|
|
30
|
+
|
|
31
|
+
# One RFC 7468 certificate block → the item's field value
|
|
32
|
+
# ":<base64>:" — the §2.1 surgery: the BEGIN and END armor lines
|
|
33
|
+
# exchanged for colons, the payload's line breaks removed, each
|
|
34
|
+
# payload line stripped of the whitespace around it, generous the
|
|
35
|
+
# way textual encodings need. Blank lines may lead and follow the
|
|
36
|
+
# block; CRLF line endings are tolerated. Armor-shape failures
|
|
37
|
+
# raise Error: no BEGIN line, the END missing, no payload, or
|
|
38
|
+
# content other than blank lines after the END (a second block —
|
|
39
|
+
# one PEM block at a time; pass an Array of blocks to
|
|
40
|
+
# pems_to_field for a chain). TypeError for a non-String.
|
|
41
|
+
def pem_to_field(pem)
|
|
42
|
+
raise TypeError, "a PEM block is a String (got #{pem.class})" unless pem.is_a?(String)
|
|
43
|
+
|
|
44
|
+
lines = pem.split("\n", -1)
|
|
45
|
+
first = 0
|
|
46
|
+
first += 1 while first < lines.size && lines[first] != BEGIN_ARMOR && blank?(lines[first])
|
|
47
|
+
begins = first < lines.size && armor_line?(lines[first], BEGIN_ARMOR)
|
|
48
|
+
raise Error, "the PEM block must begin with #{BEGIN_ARMOR}" unless begins
|
|
49
|
+
|
|
50
|
+
last = lines.size - 1
|
|
51
|
+
last -= 1 while last > first && lines[last] != END_ARMOR && blank?(lines[last])
|
|
52
|
+
ends = last > first && armor_line?(lines[last], END_ARMOR)
|
|
53
|
+
raise Error, "the PEM block must end with #{END_ARMOR}" unless ends
|
|
54
|
+
unless first_armor_line(lines, first, last) == last
|
|
55
|
+
raise Error, "one PEM block at a time — pass an Array of blocks for a chain"
|
|
56
|
+
end
|
|
57
|
+
raise Error, "the PEM block carries no payload" unless last - first > 1
|
|
58
|
+
|
|
59
|
+
":#{lines[first + 1...last].map(&:strip).join}:"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The inverse: ":<base64>:" → one RFC 7468 block — the base64
|
|
63
|
+
# wrapped at 64 characters per line, every line (the END line
|
|
64
|
+
# included) newline-terminated. The sender side is strict: the
|
|
65
|
+
# value must carry colons on both ends and a whole, valid base64
|
|
66
|
+
# interior, or Error is raised before anything is written.
|
|
67
|
+
# TypeError for a non-String.
|
|
68
|
+
def field_to_pem(field)
|
|
69
|
+
raise TypeError, "a field value is a String (got #{field.class})" unless field.is_a?(String)
|
|
70
|
+
|
|
71
|
+
base64 = base64_interior(field)
|
|
72
|
+
block = "#{BEGIN_ARMOR}\n"
|
|
73
|
+
index = 0
|
|
74
|
+
size = base64.bytesize
|
|
75
|
+
while index < size
|
|
76
|
+
block << base64.byteslice(index, 64) << "\n"
|
|
77
|
+
index += 64
|
|
78
|
+
end
|
|
79
|
+
block << END_ARMOR << "\n"
|
|
80
|
+
block
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# An Array of RFC 7468 blocks → the Client-Cert-Chain field value:
|
|
84
|
+
# the per-block §2.1 surgery (each block through pem_to_field, its
|
|
85
|
+
# armor-shape rules intact), the members joined with the canonical
|
|
86
|
+
# ", " separator. The empty Array raises Error — nothing to
|
|
87
|
+
# convert. TypeError for a non-Array.
|
|
88
|
+
def pems_to_field(blocks)
|
|
89
|
+
raise TypeError, "blocks are an Array of PEM Strings (got #{blocks.class})" unless blocks.is_a?(Array)
|
|
90
|
+
raise Error, "nothing to convert — the blocks Array is empty" if blocks.empty?
|
|
91
|
+
|
|
92
|
+
blocks.map { |block| pem_to_field(block) }.join(", ")
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The Client-Cert-Chain field value → the Array of RFC 7468 blocks
|
|
96
|
+
# it carries, in order: the members split on the separator commas
|
|
97
|
+
# (base64 cannot carry one — the alphabet has no comma), each
|
|
98
|
+
# stripped of the OWS around it and converted exactly as
|
|
99
|
+
# field_to_pem demands. The empty value answers no blocks.
|
|
100
|
+
# TypeError for a non-String.
|
|
101
|
+
def field_to_pems(field)
|
|
102
|
+
raise TypeError, "a field value is a String (got #{field.class})" unless field.is_a?(String)
|
|
103
|
+
return [].freeze if field.empty?
|
|
104
|
+
|
|
105
|
+
field.split(",", -1).map { |member| field_to_pem(member.strip) }
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# A line that carries nothing but whitespace: the blank lines a
|
|
109
|
+
# textual encoding may pad a block with.
|
|
110
|
+
def blank?(line)
|
|
111
|
+
line.empty? || line.strip.empty?
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Is the line one of the armor lines? Exact when the text is
|
|
115
|
+
# clean — the allocation-free compare — with the stripped compare
|
|
116
|
+
# behind it for a trailing CR (CRLF text) or padding whitespace.
|
|
117
|
+
def armor_line?(line, armor)
|
|
118
|
+
line == armor || line.strip == armor
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# The first armor-shaped line ("-----") at or after +first+ and
|
|
122
|
+
# before +last+: the END when the block is one. Anything earlier —
|
|
123
|
+
# a second block's armor inside the payload region — is content
|
|
124
|
+
# the one block does not carry. Base64 has no "-", so a payload
|
|
125
|
+
# line never answers here.
|
|
126
|
+
def first_armor_line(lines, first, last)
|
|
127
|
+
index = first + 1
|
|
128
|
+
index += 1 while index < last && !lines[index].start_with?("-----")
|
|
129
|
+
index
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# The whole base64 interior of one ":<base64>:" field value,
|
|
133
|
+
# validated before anything is written: colons on both ends, a
|
|
134
|
+
# non-empty interior of alphabet characters with padding only at
|
|
135
|
+
# the end, and a length that is a multiple of four — the shape
|
|
136
|
+
# §2.1 states ("the binary DER certificate is encoded using
|
|
137
|
+
# base64 ... and delimited with colons on either side"). A value
|
|
138
|
+
# that is not one raises Error.
|
|
139
|
+
def base64_interior(field)
|
|
140
|
+
colons = field.bytesize > 2 && field.getbyte(0) == COLON && field.getbyte(-1) == COLON
|
|
141
|
+
base64 = colons ? field.byteslice(1, field.bytesize - 2) : nil
|
|
142
|
+
unless base64 && !base64.empty? && BASE64.match?(base64) && (base64.bytesize % 4).zero?
|
|
143
|
+
raise Error, "the value is not a :base64: item (got #{field.inspect})"
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
base64
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
private_class_method :blank?, :armor_line?, :first_armor_line, :base64_interior
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The substrate: RFC 9440's two fields are an RFC 9651 Item and an
|
|
4
|
+
# RFC 9651 List, so this gem is a fast-sfv consumer, never a parser
|
|
5
|
+
# of its own. The DER certificate bytes are opaque throughout: no
|
|
6
|
+
# OpenSSL, no DER decoding — the Structured Fields grammar and the
|
|
7
|
+
# §2.1 string surgery carry everything.
|
|
8
|
+
require "fast/sfv"
|
|
9
|
+
|
|
10
|
+
require_relative "client_cert/version"
|
|
11
|
+
require_relative "client_cert/errors"
|
|
12
|
+
require_relative "client_cert/cert"
|
|
13
|
+
require_relative "client_cert/chain"
|
|
14
|
+
require_relative "client_cert/pem"
|
|
15
|
+
|
|
16
|
+
module Fast
|
|
17
|
+
# RFC 9440 Client-Cert: the two request fields a TLS-terminating
|
|
18
|
+
# reverse proxy sends to convey the client certificate of a mutually
|
|
19
|
+
# authenticated TLS connection, and the pure-Ruby PEM surgery of
|
|
20
|
+
# §2.1 in both directions.
|
|
21
|
+
#
|
|
22
|
+
# Fast::ClientCert::Cert is the Client-Cert field (§2.2): "This
|
|
23
|
+
# field contains the end-entity certificate used by the client in
|
|
24
|
+
# the TLS handshake" — one Item carrying the certificate's DER bytes
|
|
25
|
+
# as a Byte Sequence (§2.1: "the binary DER certificate is encoded
|
|
26
|
+
# using base64 ... and delimited with colons on either side"). A
|
|
27
|
+
# recipient that cannot read the field has no certificate: parse
|
|
28
|
+
# answers nil, the unauthenticated answer the §4 security story
|
|
29
|
+
# demands ("Access control decisions based on the client certificate
|
|
30
|
+
# (or lack thereof)" are the origin server's).
|
|
31
|
+
#
|
|
32
|
+
# Fast::ClientCert::Chain is the Client-Cert-Chain field (§2.3): the
|
|
33
|
+
# validation chain as a List of Byte Sequences, "The order is the
|
|
34
|
+
# same as the ordering in TLS" — and it "MAY have a list of values
|
|
35
|
+
# or occur multiple times in a request", so an Array of field lines
|
|
36
|
+
# is its native split form and Chain#split cuts a held chain back
|
|
37
|
+
# into per-instance lines. A malformed field, or any member that is
|
|
38
|
+
# not a Byte Sequence, is the ENTIRE field ignored: the shared
|
|
39
|
+
# frozen empty value, the same answer an absent field gives.
|
|
40
|
+
#
|
|
41
|
+
# The four PEM helpers (pem_to_field, field_to_pem, pems_to_field,
|
|
42
|
+
# field_to_pems) are the §2.1 string surgery both directions: "it
|
|
43
|
+
# will be sufficient to replace ---(BEGIN|END) CERTIFICATE--- with
|
|
44
|
+
# ':' and remove line breaks in order to generate an appropriate
|
|
45
|
+
# item", and the inverse — the base64 wrapped at 64 characters per
|
|
46
|
+
# line, every line newline-terminated (RFC 7468's form).
|
|
47
|
+
#
|
|
48
|
+
# parse is the recipient's view and never raises on field content;
|
|
49
|
+
# parse! is strict truth with byte offsets; valid? mirrors it with a
|
|
50
|
+
# boolean; to_s and construction are always strict; results are
|
|
51
|
+
# frozen value objects; one runtime dependency, fast-sfv. RFC 9440
|
|
52
|
+
# is Informational — the README carries the note.
|
|
53
|
+
module ClientCert
|
|
54
|
+
# The Byte Sequence tag: a String tagged ASCII-8BIT IS the Byte
|
|
55
|
+
# Sequence on the wire (RFC 9651 §3.3.5 — the encoding is the
|
|
56
|
+
# wire-type tag), so the constraint below reads the same fact a
|
|
57
|
+
# serialization would, and a UTF-8 String would emit as a quoted
|
|
58
|
+
# sf-string, not ":base64:".
|
|
59
|
+
BINARY = Encoding::ASCII_8BIT
|
|
60
|
+
private_constant :BINARY
|
|
61
|
+
|
|
62
|
+
# The one constraint §2.1 and §2.3 state — "The headers in this
|
|
63
|
+
# document encode certificates as Byte Sequences" — read by both
|
|
64
|
+
# fields' strict parses AND both constructions, so the four
|
|
65
|
+
# boundaries cannot drift. A Token is a String whose class is the
|
|
66
|
+
# token's wire-type tag, so it is refused by class: a
|
|
67
|
+
# binary-tagged Token would slip through the encoding check alone
|
|
68
|
+
# and emit as a token, not a Byte Sequence.
|
|
69
|
+
module Checks
|
|
70
|
+
module_function
|
|
71
|
+
|
|
72
|
+
# Is +value+ the Byte Sequence shape §2.1/§2.3 demand?
|
|
73
|
+
def byte_sequence?(value)
|
|
74
|
+
value.is_a?(String) && !value.is_a?(SFV::Token) && value.encoding == BINARY
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# What a constraint error shows of the offender: the value's
|
|
78
|
+
# inspect when it is String-shaped, its class otherwise — an
|
|
79
|
+
# Item unwrapped first (an inner list shows its own class,
|
|
80
|
+
# Fast::SFV::InnerList).
|
|
81
|
+
def shown(shape)
|
|
82
|
+
value = shape.is_a?(SFV::Item) ? shape.value : shape
|
|
83
|
+
value.is_a?(String) ? value.inspect : value.class
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
private_constant :Checks
|
|
87
|
+
end
|
|
88
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: fast-client-cert
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Eric Jacobs
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: fast-sfv
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
description: |
|
|
27
|
+
Fast::ClientCert implements RFC 9440 — the two request fields a
|
|
28
|
+
TLS-terminating reverse proxy sends to convey the client
|
|
29
|
+
certificate of a mutually authenticated TLS connection, as frozen
|
|
30
|
+
value objects over fast-sfv: Cert (the Client-Cert field, a
|
|
31
|
+
singleton Byte Sequence Item carrying the end-entity certificate
|
|
32
|
+
with fail-closed recipient semantics — a malformed field answers
|
|
33
|
+
nil, the unauthenticated connection) and Chain (the
|
|
34
|
+
Client-Cert-Chain field, a List of Byte Sequences in TLS order
|
|
35
|
+
that MAY split across field instances), plus the four pure-Ruby
|
|
36
|
+
PEM conversion helpers — the RFC 9440 2.1 string surgery in both
|
|
37
|
+
directions, no OpenSSL, DER bytes opaque. One runtime dependency,
|
|
38
|
+
fast-sfv.
|
|
39
|
+
executables: []
|
|
40
|
+
extensions: []
|
|
41
|
+
extra_rdoc_files: []
|
|
42
|
+
files:
|
|
43
|
+
- CHANGELOG.md
|
|
44
|
+
- README.md
|
|
45
|
+
- lib/fast/client_cert.rb
|
|
46
|
+
- lib/fast/client_cert/cert.rb
|
|
47
|
+
- lib/fast/client_cert/chain.rb
|
|
48
|
+
- lib/fast/client_cert/errors.rb
|
|
49
|
+
- lib/fast/client_cert/pem.rb
|
|
50
|
+
- lib/fast/client_cert/version.rb
|
|
51
|
+
homepage: https://github.com/jetpks/fast-http-fields
|
|
52
|
+
licenses:
|
|
53
|
+
- MIT
|
|
54
|
+
metadata:
|
|
55
|
+
homepage_uri: https://github.com/jetpks/fast-http-fields
|
|
56
|
+
source_code_uri: https://github.com/jetpks/fast-http-fields
|
|
57
|
+
changelog_uri: https://github.com/jetpks/fast-http-fields/blob/main/gems/client_cert/CHANGELOG.md
|
|
58
|
+
documentation_uri: https://github.com/jetpks/fast-http-fields/tree/main/gems/client_cert/docs
|
|
59
|
+
rubygems_mfa_required: 'true'
|
|
60
|
+
rdoc_options: []
|
|
61
|
+
require_paths:
|
|
62
|
+
- lib
|
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '3.4'
|
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: 3.5.3
|
|
73
|
+
requirements: []
|
|
74
|
+
rubygems_version: 4.0.20
|
|
75
|
+
specification_version: 4
|
|
76
|
+
summary: 'RFC 9440 request fields: Client-Cert and Client-Cert-Chain.'
|
|
77
|
+
test_files: []
|