mint_utils.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package quic
  2. import (
  3. gocrypto "crypto"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "errors"
  7. "github.com/bifurcation/mint"
  8. "github.com/lucas-clemente/quic-go/internal/protocol"
  9. )
  10. func tlsToMintConfig(tlsConf *tls.Config, pers protocol.Perspective) (*mint.Config, error) {
  11. mconf := &mint.Config{
  12. NonBlocking: true,
  13. CipherSuites: []mint.CipherSuite{
  14. mint.TLS_AES_128_GCM_SHA256,
  15. mint.TLS_AES_256_GCM_SHA384,
  16. },
  17. }
  18. if tlsConf != nil {
  19. mconf.ServerName = tlsConf.ServerName
  20. mconf.InsecureSkipVerify = tlsConf.InsecureSkipVerify
  21. mconf.Certificates = make([]*mint.Certificate, len(tlsConf.Certificates))
  22. mconf.RootCAs = tlsConf.RootCAs
  23. mconf.VerifyPeerCertificate = tlsConf.VerifyPeerCertificate
  24. for i, certChain := range tlsConf.Certificates {
  25. mconf.Certificates[i] = &mint.Certificate{
  26. Chain: make([]*x509.Certificate, len(certChain.Certificate)),
  27. PrivateKey: certChain.PrivateKey.(gocrypto.Signer),
  28. }
  29. for j, cert := range certChain.Certificate {
  30. c, err := x509.ParseCertificate(cert)
  31. if err != nil {
  32. return nil, err
  33. }
  34. mconf.Certificates[i].Chain[j] = c
  35. }
  36. }
  37. switch tlsConf.ClientAuth {
  38. case tls.NoClientCert:
  39. case tls.RequireAnyClientCert:
  40. mconf.RequireClientAuth = true
  41. default:
  42. return nil, errors.New("mint currently only support ClientAuthType RequireAnyClientCert")
  43. }
  44. }
  45. if err := mconf.Init(pers == protocol.PerspectiveClient); err != nil {
  46. return nil, err
  47. }
  48. return mconf, nil
  49. }