Skip to content

Commit b2059c2

Browse files
committed
CHANGE: removed the rest of the old legacy TLS code
1 parent f4ed6b5 commit b2059c2

File tree

1 file changed

+45
-58
lines changed

1 file changed

+45
-58
lines changed

src/mezz/prot-tls.reb

+45-58
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,33 @@ TLS-context: context [
103103
hash-method: ; one of: [MD5 SHA1 SHA256 SHA384]
104104
crypt-method: none
105105
is-aead?: false ; crypt-method with "Authenticated Encryption with Additional Data" (not yet supported!)
106-
mac-size: ; Size of message authentication code
106+
IV-size: ; The amount of data needed to be generated for the initialization vector.
107+
mac-size: ; Size of message authentication code
107108
crypt-size: ; The number of bytes from the key_block that are used for generating the write keys.
108-
block-size: ; The amount of data a block cipher enciphers in one chunk; a block
109+
block-size: 0 ; The amount of data a block cipher enciphers in one chunk; a block
109110
; cipher running in CBC mode can only encrypt an even multiple of
110111
; its block size.
111-
IV-size: 0 ; The amount of data needed to be generated for the initialization vector.
112-
113112

114-
local-iv:
113+
local-IV:
115114
local-mac:
116115
local-key:
117116
local-random:
118117

119-
remote-iv:
118+
remote-IV:
120119
remote-mac:
121120
remote-key:
122121
remote-random:
123122

124123
aead: none ; used now for chacha20/poly1305 combo
125124

125+
server-session: none
126126
server-certs: copy []
127127
server-extensions: copy []
128128

129129
seq-read: 0 ; sequence counters
130130
seq-write: 0
131131

132-
server-session:
132+
133133
pre-master-secret:
134134
master-secret:
135135
certificate:
@@ -723,22 +723,19 @@ client-key-exchange: function [
723723
AT :pos-key (pick [UI8BYTES UI16BYTES] key-data-len-bytes) :key-data
724724
]
725725

726-
727726
;-- make all secure data
728-
if ctx/version >= *Protocol-version/TLS1.0 [
729-
; NOTE: key-expansion is used just to generate keys so it does not need to be stored in context!
730-
ctx/master-secret: prf "master secret" (append copy ctx/local-random ctx/remote-random) pre-master-secret 48
731-
key-expansion: prf "key expansion" (append copy ctx/remote-random ctx/local-random) master-secret
732-
(mac-size + crypt-size + iv-size) * 2
733-
734-
pre-master-secret: none ;-- not needed anymore
735-
736-
;?? master-secret
737-
;?? key-expansion
738-
;?? mac-size
739-
;?? crypt-size
740-
;?? iv-size
741-
]
727+
; NOTE: key-expansion is used just to generate keys so it does not need to be stored in context!
728+
ctx/master-secret: prf "master secret" (append copy ctx/local-random ctx/remote-random) pre-master-secret 48
729+
key-expansion: prf "key expansion" (append copy ctx/remote-random ctx/local-random) master-secret
730+
(mac-size + crypt-size + iv-size) * 2
731+
732+
pre-master-secret: none ;-- not needed anymore
733+
734+
;?? master-secret
735+
;?? key-expansion
736+
;?? mac-size
737+
;?? crypt-size
738+
;?? iv-size
742739

743740
unless is-aead? [
744741
local-mac: take/part key-expansion mac-size
@@ -753,25 +750,25 @@ client-key-exchange: function [
753750
log-more ["Client-key: ^[[32m" local-key]
754751
log-more ["Server-key: ^[[32m" remote-key]
755752

756-
local-iv: take/part key-expansion iv-size
757-
remote-iv: take/part key-expansion iv-size
753+
local-IV: take/part key-expansion iv-size
754+
remote-IV: take/part key-expansion iv-size
758755

759-
log-more ["Client-IV: ^[[32m" local-iv]
760-
log-more ["Server-IV: ^[[32m" remote-iv]
756+
log-more ["Client-IV: ^[[32m" local-IV]
757+
log-more ["Server-IV: ^[[32m" remote-IV]
761758

762759
key-expansion: none
763760

764761
encrypt-port: open [
765762
scheme: 'crypt
766763
algorithm: :crypt-method
767-
init-vector: :local-iv
764+
init-vector: :local-IV
768765
key: :local-key
769766
]
770767
decrypt-port: open [
771768
scheme: 'crypt
772769
direction: 'decrypt
773770
algorithm: :crypt-method
774-
init-vector: :remote-iv
771+
init-vector: :remote-IV
775772
key: :remote-key
776773
]
777774

@@ -896,15 +893,12 @@ decrypt-msg: function [
896893
]
897894
]
898895
][
899-
if all [
900-
block-size
901-
version > *Protocol-version/TLS1.0
902-
][
896+
if block-size [
903897
;server's initialization vector is new with each message
904-
remote-iv: take/part data block-size
898+
remote-IV: take/part data block-size
905899
]
906900
;?? data
907-
modify decrypt-port 'init-vector remote-iv
901+
modify decrypt-port 'init-vector remote-IV
908902
data: read update write decrypt-port :data
909903

910904
;change data decrypt-data ctx data
@@ -926,9 +920,7 @@ decrypt-msg: function [
926920

927921
if mac <> mac-check [ critical-error: *Alert/Bad_record_MAC ]
928922

929-
if version > *Protocol-version/TLS1.0 [
930-
unset 'remote-iv ;-- avoid reuse in TLS 1.1 and above
931-
]
923+
unset 'remote-IV ;-- avoid reuse in TLS 1.1 and above
932924
]
933925
]
934926
binary/init bin 0 ;clear the temp bin buffer
@@ -969,24 +961,21 @@ encrypt-data: function [
969961
][
970962

971963
;@@ GenericBlockCipher: https://tools.ietf.org/html/rfc5246#section-6.2.3.2
972-
if version > *Protocol-version/TLS1.0 [
973-
;
974-
; "The Initialization Vector (IV) SHOULD be chosen at random, and
975-
; MUST be unpredictable. Note that in versions of TLS prior to 1.1,
976-
; there was no IV field, and the last ciphertext block of the
977-
; previous record (the "CBC residue") was used as the IV. This was
978-
; changed to prevent the attacks described in [CBCATT]. For block
979-
; ciphers, the IV length is SecurityParameters.record_iv_length,
980-
; which is equal to the SecurityParameters.block_size."
981-
;
982-
binary/write clear local-iv [RANDOM-BYTES :block-size]
983-
modify encrypt-port 'init-vector local-iv
984-
]
964+
; "The Initialization Vector (IV) SHOULD be chosen at random, and
965+
; MUST be unpredictable. Note that in versions of TLS prior to 1.1,
966+
; there was no IV field, and the last ciphertext block of the
967+
; previous record (the "CBC residue") was used as the IV. This was
968+
; changed to prevent the attacks described in [CBCATT]. For block
969+
; ciphers, the IV length is SecurityParameters.record_iv_length,
970+
; which is equal to the SecurityParameters.block_size."
971+
;
972+
binary/write clear local-IV [RANDOM-BYTES :block-size]
973+
modify encrypt-port 'init-vector local-IV
985974

986975
;?? ctx/seq-write
987-
log-more ["Client-IV: ^[[32m" local-iv]
988-
log-more ["Client-mac: ^[[32m" local-mac]
989-
log-more ["Hash-method: ^[[32m" hash-method]
976+
log-more ["Client-IV: ^[[32m" local-IV]
977+
log-more ["Client-mac: ^[[32m" local-mac]
978+
log-more ["Hash-method: ^[[32m" hash-method]
990979

991980
; Message Authentication Code
992981
; https://tools.ietf.org/html/rfc5246#section-6.2.3.1
@@ -1008,11 +997,9 @@ encrypt-data: function [
1008997
; on next line are 3 ops.. encrypting content, padding and getting the result
1009998
encrypted: read update write encrypt-port content
1010999

1011-
;-- TLS versions 1.1 and above include the local-iv in plaintext.
1012-
if version > *Protocol-version/TLS1.0 [
1013-
insert encrypted local-iv
1014-
;clear local-iv ;-- avoid accidental reuse
1015-
]
1000+
;-- TLS versions 1.1 and above include the local-IV in plaintext.
1001+
insert encrypted local-IV
1002+
;clear local-IV ;-- avoid accidental reuse
10161003
]
10171004
binary/init bin 0 ;reset the bin buffer
10181005
]

0 commit comments

Comments
 (0)