@@ -16,6 +16,7 @@ import (
1616 "crypto/rsa"
1717 "crypto/sha512"
1818 "crypto/x509"
19+ "crypto/x509/pkix"
1920 "encoding/binary"
2021 "errors"
2122 "fmt"
@@ -26,6 +27,7 @@ import (
2627 "sort"
2728 "time"
2829
30+ "github.com/cloudflare/circl/sign/mldsa/mldsa65"
2931 "github.com/xtls/reality/fips140tls"
3032 "github.com/xtls/reality/hpke"
3133 "github.com/xtls/reality/tls13"
@@ -70,19 +72,26 @@ type serverHandshakeStateTLS13 struct {
7072}
7173
7274var (
73- ed25519Priv ed25519.PrivateKey
74- signedCert []byte
75+ ed25519Priv ed25519.PrivateKey
76+ signedCert []byte
77+ signedCertMldsa65 []byte
7578)
7679
7780func init () {
7881 certificate := x509.Certificate {SerialNumber : & big.Int {}}
82+ certificateMldsa65 := x509.Certificate {SerialNumber : & big.Int {}, ExtraExtensions : []pkix.Extension {{Id : []int {0 , 0 }, Value : empty [:3309 ]}}}
7983 _ , ed25519Priv , _ = ed25519 .GenerateKey (rand .Reader )
8084 signedCert , _ = x509 .CreateCertificate (rand .Reader , & certificate , & certificate , ed25519 .PublicKey (ed25519Priv [32 :]), ed25519Priv )
85+ signedCertMldsa65 , _ = x509 .CreateCertificate (rand .Reader , & certificateMldsa65 , & certificateMldsa65 , ed25519 .PublicKey (ed25519Priv [32 :]), ed25519Priv )
8186}
8287
8388func (hs * serverHandshakeStateTLS13 ) handshake () error {
8489 c := hs .c
85-
90+ if c .config .Show {
91+ remoteAddr := c .RemoteAddr ().String ()
92+ fmt .Printf ("REALITY remoteAddr: %v\t is using X25519MLKEM768 for TLS' communication: %v\n " , remoteAddr , hs .hello .serverShare .group == X25519MLKEM768 )
93+ fmt .Printf ("REALITY remoteAddr: %v\t is using ML-DSA-65 for cert's extra signature: %v\n " , remoteAddr , len (c .config .Mldsa65Key ) > 0 )
94+ }
8695 // For an overview of the TLS 1.3 handshake, see RFC 8446, Section 2.
8796 /*
8897 if err := hs.processClientHello(); err != nil {
@@ -130,14 +139,26 @@ func (hs *serverHandshakeStateTLS13) handshake() error {
130139 }
131140 */
132141 {
133- signedCert := append ([]byte {}, signedCert ... )
142+ var cert []byte
143+ if len (c .config .Mldsa65Key ) > 0 {
144+ cert = bytes .Clone (signedCertMldsa65 )
145+ } else {
146+ cert = bytes .Clone (signedCert )
147+ }
134148
135149 h := hmac .New (sha512 .New , c .AuthKey )
136150 h .Write (ed25519Priv [32 :])
137- h .Sum (signedCert [:len (signedCert )- 64 ])
151+ h .Sum (cert [:len (cert )- 64 ])
152+
153+ if len (c .config .Mldsa65Key ) > 0 {
154+ h .Write (hs .clientHello .original )
155+ h .Write (hs .hello .original )
156+ key , _ := mldsa65 .Scheme ().UnmarshalBinaryPrivateKey (c .config .Mldsa65Key )
157+ mldsa65 .SignTo (key .(* mldsa65.PrivateKey ), h .Sum (nil ), nil , false , cert [126 :]) // fixed location
158+ }
138159
139160 hs .cert = & Certificate {
140- Certificate : [][]byte {signedCert },
161+ Certificate : [][]byte {cert },
141162 PrivateKey : ed25519Priv ,
142163 }
143164 hs .sigAlg = Ed25519
0 commit comments