Skip to content

Commit 52ee03e

Browse files
committed
condense all the errors
1 parent 7cb96be commit 52ee03e

16 files changed

+320
-630
lines changed

lib/core.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ let split_str ?(start = 0) str off =
1111
String.sub str start off,
1212
String.sub str (start + off) (String.length str - off - start)
1313

14-
let map_reader_error r = Result.map_error (fun re -> `Fatal (`ReaderError re)) r
14+
let map_reader_error r =
15+
Result.map_error (fun e -> `Fatal e) r
1516

1617
type tls13 = [ `TLS_1_3 ]
1718

lib/engine.ml

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ open State
33

44
type state = State.state
55

6-
type client_hello_errors = State.client_hello_errors
76
type error = State.error
87
type fatal = State.fatal
98
type failure = State.failure
@@ -21,44 +20,17 @@ let alert_of_error = function
2120
| `CouldntSelectCertificate -> Packet.HANDSHAKE_FAILURE
2221

2322
let alert_of_fatal = function
24-
| `NoSecureRenegotiation -> Packet.HANDSHAKE_FAILURE
25-
| `NoSupportedGroup -> Packet.HANDSHAKE_FAILURE
26-
| `MACUnderflow -> Packet.BAD_RECORD_MAC
27-
| `MACMismatch -> Packet.BAD_RECORD_MAC
28-
| `RecordOverflow _ -> Packet.RECORD_OVERFLOW
29-
| `UnknownRecordVersion _ -> Packet.PROTOCOL_VERSION
30-
| `UnknownContentType _ -> Packet.UNEXPECTED_MESSAGE
31-
| `ReaderError (Reader.TrailingBytes _ | Reader.WrongLength _) -> Packet.UNEXPECTED_MESSAGE
32-
| `ReaderError (Reader.Unknown _) -> Packet.DECODE_ERROR
33-
| `CannotHandleApplicationDataYet -> Packet.UNEXPECTED_MESSAGE
34-
| `BadRecordVersion _ -> Packet.PROTOCOL_VERSION
35-
| `InvalidRenegotiation -> Packet.HANDSHAKE_FAILURE
36-
| `InvalidServerHello -> Packet.UNSUPPORTED_EXTENSION
37-
| `InvalidRenegotiationVersion _ -> Packet.HANDSHAKE_FAILURE
38-
| `BadCertificate _ -> Packet.BAD_CERTIFICATE
39-
| `NoVersions _ -> Packet.PROTOCOL_VERSION
40-
| `BadDH _ -> Packet.ILLEGAL_PARAMETER
41-
| `BadFinished -> Packet.DECRYPT_ERROR
42-
| `HandshakeFragmentsNotEmpty -> Packet.HANDSHAKE_FAILURE
43-
| `InvalidSession -> Packet.HANDSHAKE_FAILURE
44-
| `UnexpectedCCS -> Packet.UNEXPECTED_MESSAGE
45-
| `UnexpectedHandshake _ -> Packet.UNEXPECTED_MESSAGE
46-
| `SignatureVerificationFailed _ -> Packet.HANDSHAKE_FAILURE
47-
| `SigningFailed _ -> Packet.HANDSHAKE_FAILURE
48-
| `InvalidClientHello `NoSignatureAlgorithmsExtension
49-
| `InvalidClientHello `NoKeyShareExtension
50-
| `InvalidClientHello `NoSupportedGroupExtension -> Packet.MISSING_EXTENSION
51-
| `InvalidClientHello (`NotSetSupportedGroup _)
52-
| `InvalidClientHello (`NotSetKeyShare _)
53-
| `InvalidClientHello (`NotSubsetKeyShareSupportedGroup _) -> Packet.ILLEGAL_PARAMETER
54-
| `InvalidClientHello _ -> Packet.HANDSHAKE_FAILURE
55-
| `InappropriateFallback -> Packet.INAPPROPRIATE_FALLBACK
56-
| `NoApplicationProtocol -> Packet.NO_APPLICATION_PROTOCOL
57-
| `InvalidMessage -> Packet.HANDSHAKE_FAILURE
58-
| `Toomany0rttbytes -> Packet.UNEXPECTED_MESSAGE
59-
| `MissingContentType -> Packet.UNEXPECTED_MESSAGE
60-
| `Downgrade12 | `Downgrade11 -> Packet.ILLEGAL_PARAMETER
61-
| `WriteHalfClosed -> Packet.UNEXPECTED_MESSAGE
23+
| `Protocol_version _ -> Packet.PROTOCOL_VERSION
24+
| `Unexpected _ -> Packet.UNEXPECTED_MESSAGE
25+
| `Decode _ -> Packet.DECODE_ERROR
26+
| `Handshake _ -> Packet.HANDSHAKE_FAILURE
27+
| `Bad_mac -> Packet.BAD_RECORD_MAC
28+
| `Record_overflow _ -> Packet.RECORD_OVERFLOW
29+
| `Unsupported_extension -> Packet.UNSUPPORTED_EXTENSION
30+
| `Bad_certificate _ -> Packet.BAD_CERTIFICATE
31+
| `Missing_extension _ -> Packet.MISSING_EXTENSION
32+
| `Inappropriate_fallback -> Packet.INAPPROPRIATE_FALLBACK
33+
| `No_application_protocol -> Packet.NO_APPLICATION_PROTOCOL
6234

6335
let alert_of_failure = function
6436
| `Error x -> Packet.FATAL, alert_of_error x
@@ -178,13 +150,13 @@ let verify_mac sequence mac mac_k ty ver decrypted =
178150
let module H = (val Digestif.module_of_hash' mac) in
179151
String.length decrypted - H.digest_size
180152
in
181-
let* () = guard (macstart >= 0) (`Fatal `MACUnderflow) in
153+
let* () = guard (macstart >= 0) (`Fatal (`Decode "MAC underflow")) in
182154
let (body, mmac) = split_str decrypted macstart in
183155
let cmac =
184156
let ver = pair_of_tls_version ver in
185157
let hdr = Crypto.pseudo_header sequence ty ver (String.length body) in
186158
Crypto.mac mac mac_k hdr body in
187-
let* () = guard (String.equal cmac mmac) (`Fatal `MACMismatch) in
159+
let* () = guard (String.equal cmac mmac) (`Fatal `Bad_mac) in
188160
Ok body
189161

190162

@@ -202,7 +174,7 @@ let decrypt ?(trial = false) (version : tls_version) (st : crypto_state) ty buf
202174
https://www.openssl.org/~bodo/tls-cbc.txt *)
203175
let mask_decrypt_failure seq mac mac_k =
204176
let* _ = compute_mac seq mac mac_k buf in
205-
Error (`Fatal `MACMismatch)
177+
Error (`Fatal `Bad_mac)
206178
in
207179

208180
let dec ctx =
@@ -223,7 +195,7 @@ let decrypt ?(trial = false) (version : tls_version) (st : crypto_state) ty buf
223195
Ok (CBC { c with iv_mode = Iv iv' }, msg)
224196
| Random_iv ->
225197
if String.length buf < Crypto.cbc_block c.cipher then
226-
Error (`Fatal `MACUnderflow)
198+
Error (`Fatal (`Decode "MAC underflow"))
227199
else
228200
let iv, buf = split_str buf (Crypto.cbc_block c.cipher) in
229201
let* msg, _ = dec iv buf in
@@ -233,7 +205,7 @@ let decrypt ?(trial = false) (version : tls_version) (st : crypto_state) ty buf
233205
if c.explicit_nonce then
234206
let explicit_nonce_len = 8 in
235207
if String.length buf < explicit_nonce_len then
236-
Error (`Fatal `MACUnderflow)
208+
Error (`Fatal (`Decode "MAC underflow"))
237209
else
238210
let explicit_nonce, buf = split_str buf explicit_nonce_len in
239211
let adata =
@@ -242,7 +214,7 @@ let decrypt ?(trial = false) (version : tls_version) (st : crypto_state) ty buf
242214
and nonce = c.nonce ^ explicit_nonce
243215
in
244216
match Crypto.decrypt_aead ~cipher:c.cipher ~key:c.cipher_secret ~nonce ~adata buf with
245-
| None -> Error (`Fatal `MACMismatch)
217+
| None -> Error (`Fatal `Bad_mac)
246218
| Some x -> Ok (AEAD c, x)
247219
else
248220
(* RFC 7905: no explicit nonce, instead TLS 1.3 construction is adapted *)
@@ -252,7 +224,7 @@ let decrypt ?(trial = false) (version : tls_version) (st : crypto_state) ty buf
252224
and nonce = Crypto.aead_nonce c.nonce seq
253225
in
254226
(match Crypto.decrypt_aead ~adata ~cipher:c.cipher ~key:c.cipher_secret ~nonce buf with
255-
| None -> Error (`Fatal `MACMismatch)
227+
| None -> Error (`Fatal `Bad_mac)
256228
| Some x -> Ok (AEAD c, x))
257229
in
258230
match st, version with
@@ -275,12 +247,12 @@ let decrypt ?(trial = false) (version : tls_version) (st : crypto_state) ty buf
275247
let nonce = Crypto.aead_nonce c.nonce ctx.sequence in
276248
let unpad x =
277249
let rec eat = function
278-
| -1 -> Error (`Fatal `MissingContentType)
250+
| -1 -> Error (`Fatal (`Unexpected (`Message "missing content type")))
279251
| idx -> match String.get_uint8 x idx with
280252
| 0 -> eat (pred idx)
281253
| n -> match Packet.int_to_content_type n with
282254
| Some ct -> Ok (String.sub x 0 idx, ct)
283-
| None -> Error (`Fatal (`UnknownContentType n))
255+
| None -> Error (`Fatal (`Unexpected (`Content_type n)))
284256
in
285257
eat (pred (String.length x))
286258
in
@@ -290,12 +262,13 @@ let decrypt ?(trial = false) (version : tls_version) (st : crypto_state) ty buf
290262
if trial then
291263
Ok (Some ctx, "", Packet.APPLICATION_DATA)
292264
else
293-
Error (`Fatal `MACMismatch)
265+
Error (`Fatal `Bad_mac)
294266
| Some x ->
295267
let* data, ty = unpad x in
296268
Ok (Some { ctx with sequence = Int64.succ ctx.sequence }, data, ty))
297-
| _ -> Error (`Fatal `InvalidMessage))
298-
| _ -> Error (`Fatal `InvalidMessage))
269+
| _ -> Error (`Fatal (`Handshake (`Message "unexpected cipher state (must be AEAD)"))))
270+
| _ ->
271+
Error (`Fatal (`Handshake (`Message "unexpected content type (TLS 1.3)"))))
299272
| Some ctx, _ ->
300273
let* st', msg = dec ctx in
301274
let ctx' = { cipher_st = st' ; sequence = Int64.succ ctx.sequence } in
@@ -392,7 +365,7 @@ let handle_change_cipher_spec = function
392365
| Server13 AwaitClientHelloHRR13
393366
| Server13 (AwaitClientCertificate13 _)
394367
| Server13 (AwaitClientFinished13 _) -> (fun s _ -> Ok (s, []))
395-
| _ -> (fun _ _ -> Error (`Fatal `UnexpectedCCS))
368+
| _ -> (fun _ _ -> Error (`Fatal (`Unexpected (`Message "change cipher spec"))))
396369

397370
and handle_handshake = function
398371
| Client cs -> Handshake_client.handle_handshake cs
@@ -420,7 +393,7 @@ let handle_packet hs buf = function
420393
(Tracing.cs ~tag:"application-data-in" buf;
421394
Ok (hs, [], non_empty buf, false))
422395
else
423-
Error (`Fatal `CannotHandleApplicationDataYet)
396+
Error (`Fatal (`Unexpected (`Message "application data")))
424397

425398
| Packet.CHANGE_CIPHER_SPEC ->
426399
let* hs, items = handle_change_cipher_spec hs.machina hs buf in
@@ -442,7 +415,10 @@ let decrement_early_data hs ty buf =
442415
let bytes left cipher =
443416
let count = String.length buf - fst (Ciphersuite.kn_13 (Ciphersuite.privprot13 cipher)) in
444417
let left' = Int32.sub left (Int32.of_int count) in
445-
if left' < 0l then Error (`Fatal `Toomany0rttbytes) else Ok left'
418+
if left' < 0l then
419+
Error (`Fatal (`Unexpected (`Message "too many 0RTT bytes")))
420+
else
421+
Ok left'
446422
in
447423
if ty = Packet.APPLICATION_DATA && early_data hs then
448424
let cipher = match hs.session with
@@ -466,8 +442,12 @@ let handle_raw_record state (hdr, buf as record : raw_record) =
466442
| Client (AwaitServerHello _), _ -> Ok ()
467443
| Server AwaitClientHello, _ -> Ok ()
468444
| Server13 AwaitClientHelloHRR13, _ -> Ok ()
469-
| _, `TLS_1_3 -> guard (hdr.version = `TLS_1_2) (`Fatal (`BadRecordVersion hdr.version))
470-
| _, v -> guard (version_eq hdr.version v) (`Fatal (`BadRecordVersion hdr.version))
445+
| _, `TLS_1_3 ->
446+
guard (hdr.version = `TLS_1_2)
447+
(`Fatal (`Protocol_version (`Bad_record hdr.version)))
448+
| _, v ->
449+
guard (version_eq hdr.version v)
450+
(`Fatal (`Protocol_version (`Bad_record hdr.version)))
471451
in
472452
let trial = match hs.machina with
473453
| Server13 (AwaitEndOfEarlyData13 _) | Server13 Established13 -> false
@@ -607,7 +587,7 @@ let reneg ?authenticator ?acceptable_cas ?cert st =
607587

608588
let key_update ?(request = true) state =
609589
if state.write_closed then
610-
Error (`Fatal `WriteHalfClosed)
590+
Error (`Fatal (`Unexpected (`Message "write half already closed")))
611591
else
612592
let* state', out = Handshake_common.output_key_update ~request state in
613593
let _, outbuf = send_records state [out] in

lib/engine.mli

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -62,55 +62,31 @@ type error = [
6262
| `CouldntSelectCertificate
6363
]
6464

65-
type client_hello_errors = [
66-
| `EmptyCiphersuites
67-
| `NoSupportedCiphersuite of Packet.any_ciphersuite list
68-
| `NotSetExtension of Core.client_extension list
69-
| `NoSignatureAlgorithmsExtension
70-
| `NoGoodSignatureAlgorithms of Core.signature_algorithm list
71-
| `NoKeyShareExtension
72-
| `NoSupportedGroupExtension
73-
| `NotSetSupportedGroup of Packet.named_group list
74-
| `NotSetKeyShare of (Packet.named_group * string) list
75-
| `NotSubsetKeyShareSupportedGroup of (Packet.named_group list * (Packet.named_group * string) list)
76-
| `Has0rttAfterHRR
77-
| `NoCookie
78-
]
79-
8065
(** failures from received garbage or lack of features *)
8166
type fatal = [
82-
| `NoSecureRenegotiation
83-
| `NoSupportedGroup
84-
| `NoVersions of Core.tls_any_version list
85-
| `ReaderError of Reader.error
86-
| `BadCertificate of string
87-
| `SignatureVerificationFailed of string
88-
| `SigningFailed of string
89-
| `MACMismatch
90-
| `MACUnderflow
91-
| `RecordOverflow of int
92-
| `UnknownRecordVersion of int * int
93-
| `UnknownContentType of int
94-
| `CannotHandleApplicationDataYet
95-
| `BadRecordVersion of Core.tls_any_version
96-
| `BadFinished
97-
| `HandshakeFragmentsNotEmpty
98-
| `BadDH of string
99-
| `InvalidRenegotiation
100-
| `InvalidClientHello of client_hello_errors
101-
| `InvalidServerHello
102-
| `InvalidRenegotiationVersion of Core.tls_version
103-
| `InappropriateFallback
104-
| `UnexpectedCCS
105-
| `UnexpectedHandshake of Core.tls_handshake
106-
| `InvalidSession
107-
| `NoApplicationProtocol
108-
| `InvalidMessage
109-
| `Toomany0rttbytes
110-
| `MissingContentType
111-
| `Downgrade12
112-
| `Downgrade11
113-
| `WriteHalfClosed
67+
| `Protocol_version of [
68+
| `None_supported of Core.tls_any_version list
69+
| `Unknown_record of int * int
70+
| `Bad_record of Core.tls_any_version
71+
]
72+
| `Unexpected of [
73+
| `Content_type of int
74+
| `Message of string
75+
| `Handshake of Core.tls_handshake
76+
]
77+
| `Decode of string
78+
| `Handshake of [
79+
| `Message of string
80+
| `Fragments
81+
| `BadDH of string
82+
]
83+
| `Bad_certificate of string
84+
| `Missing_extension of string
85+
| `Bad_mac
86+
| `Record_overflow of int
87+
| `Unsupported_extension
88+
| `Inappropriate_fallback
89+
| `No_application_protocol
11490
]
11591

11692
(** type of failures *)

0 commit comments

Comments
 (0)