Skip to content

Commit bd1e3f9

Browse files
committed
支持国密
1 parent 311a076 commit bd1e3f9

2 files changed

Lines changed: 56 additions & 18 deletions

File tree

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ require (
3434
github.com/spf13/cobra v1.0.0
3535
github.com/spf13/viper v1.6.2
3636
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca
37+
github.com/tjfoc/gmsm v1.4.1
3738
github.com/xuperchain/crypto v0.0.0-20201028025054-4d560674bcd6
3839
github.com/xuperchain/log15 v0.0.0-20190620081506-bc88a9198230
3940
github.com/xuperchain/xvm v0.0.0-20210126142521-68fd016c56d7
40-
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de
41+
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee
4142
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
4243
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
4344
google.golang.org/grpc v1.35.0

kernel/network/p2p/util.go

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ package p2p
22

33
import (
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

Comments
 (0)