@@ -2,16 +2,21 @@ package p2p
22
33import (
44 "crypto/rand"
5- "crypto/tls"
6- "crypto/x509"
5+ defaulttls "crypto/tls"
6+ defaultx509 "crypto/x509"
77 "encoding/base64"
88 "encoding/pem"
99 "io/ioutil"
1010 math_rand "math/rand"
1111 "os"
1212 "path/filepath"
13+ "strings"
1314 "time"
1415
16+ tls "github.com/tjfoc/gmsm/gmtls"
17+ "github.com/tjfoc/gmsm/gmtls/gmcredentials"
18+ "github.com/tjfoc/gmsm/x509"
19+
1520 iaddr "github.com/ipfs/go-ipfs-addr"
1621 "github.com/libp2p/go-libp2p-core/crypto"
1722 "github.com/libp2p/go-libp2p-core/peer"
@@ -25,27 +30,59 @@ func NewTLS(path, serviceName string) (credentials.TransportCredentials, error)
2530 if err != nil {
2631 return nil , err
2732 }
28-
29- certPool := x509 .NewCertPool ()
30- ok := certPool .AppendCertsFromPEM (bs )
31- if ! ok {
33+ cacert , err := ioutil .ReadFile (filepath .Join (path , "cacert.pem" ))
34+ if err != nil {
3235 return nil , err
3336 }
34-
35- certificate , err := tls . LoadX509KeyPair ( filepath . Join ( path , "cert.pem" ), filepath . Join ( path , "private.key" ) )
37+ pb , _ := pem . Decode ( cacert )
38+ x509cert , err := x509 . ParseCertificate ( pb . Bytes )
3639 if err != nil {
3740 return nil , err
3841 }
42+ if strings .Contains (strings .ToLower (x509cert .SignatureAlgorithm .String ()), "sm" ) {
43+ certPool := x509 .NewCertPool ()
44+ ok := certPool .AppendCertsFromPEM (bs )
45+ if ! ok {
46+ return nil , err
47+ }
48+ certificate , err := tls .LoadX509KeyPair (filepath .Join (path , "cert.pem" ), filepath .Join (path , "private.key" ))
49+ if err != nil {
50+ return nil , err
51+ }
52+ creds := gmcredentials .NewTLS (
53+ & tls.Config {
54+ GMSupport : & tls.GMSupport {},
55+ ServerName : serviceName ,
56+ Certificates : []tls.Certificate {certificate , certificate },
57+ RootCAs : certPool ,
58+ ClientCAs : certPool ,
59+ ClientAuth : tls .RequireAndVerifyClientCert ,
60+ })
61+ return creds , nil
62+ } else {
63+
64+ certPool := defaultx509 .NewCertPool ()
65+ ok := certPool .AppendCertsFromPEM (bs )
66+ if ! ok {
67+ return nil , err
68+ }
69+
70+ certificate , err := defaulttls .LoadX509KeyPair (filepath .Join (path , "cert.pem" ), filepath .Join (path , "private.key" ))
71+ if err != nil {
72+ return nil , err
73+ }
74+
75+ creds := credentials .NewTLS (
76+ & defaulttls.Config {
77+ ServerName : serviceName ,
78+ Certificates : []defaulttls.Certificate {certificate },
79+ RootCAs : certPool ,
80+ ClientCAs : certPool ,
81+ ClientAuth : defaulttls .RequireAndVerifyClientCert ,
82+ })
83+ return creds , nil
84+ }
3985
40- creds := credentials .NewTLS (
41- & tls.Config {
42- ServerName : serviceName ,
43- Certificates : []tls.Certificate {certificate },
44- RootCAs : certPool ,
45- ClientCAs : certPool ,
46- ClientAuth : tls .RequireAndVerifyClientCert ,
47- })
48- return creds , nil
4986}
5087
5188// GenerateKeyPairWithPath generate xuper net key pair
0 commit comments