mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-08 18:39:01 +00:00
add new file
This commit is contained in:
34
lib/cert/cert.go
Normal file
34
lib/cert/cert.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// GetCertSnFromConfig return SerialNumber by tls.Config
|
||||
func GetCertSnFromConfig(config *tls.Config) (string, error) {
|
||||
if len(config.Certificates) == 0 || len(config.Certificates[0].Certificate) == 0 {
|
||||
return "", errors.New("certificates is empty")
|
||||
}
|
||||
return GetCertSnFromBlock(config.Certificates[0].Certificate[0])
|
||||
}
|
||||
|
||||
// GetCertSnFromEncode return SerialNumber by encoded cert
|
||||
func GetCertSnFromEncode(b []byte) (string, error) {
|
||||
block, _ := pem.Decode(b)
|
||||
if block == nil {
|
||||
return "", errors.New("block is not a cert encoded")
|
||||
}
|
||||
return GetCertSnFromBlock(block.Bytes)
|
||||
}
|
||||
|
||||
// GetCertSnFromBlock return SerialNumber by decode block
|
||||
func GetCertSnFromBlock(block []byte) (string, error) {
|
||||
cert, err := x509.ParseCertificate(block)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "ParseCertificate")
|
||||
}
|
||||
return cert.SerialNumber.String(), nil
|
||||
}
|
42
lib/cert/cert_test.go
Normal file
42
lib/cert/cert_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509/pkix"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetCertSerialNumber(t *testing.T) {
|
||||
g := NewX509Generator(pkix.Name{
|
||||
Country: []string{"CN"},
|
||||
Organization: []string{"Ehang.io"},
|
||||
OrganizationalUnit: []string{"nps"},
|
||||
Province: []string{"Beijing"},
|
||||
CommonName: "nps",
|
||||
Locality: []string{"Beijing"},
|
||||
})
|
||||
cert, key, err := g.CreateRootCa()
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, os.WriteFile(filepath.Join(os.TempDir(), "cert.pem"), cert, 0600))
|
||||
assert.NoError(t, os.WriteFile(filepath.Join(os.TempDir(), "key.pem"), key, 0600))
|
||||
assert.NoError(t, err)
|
||||
|
||||
cliCrt, err := tls.LoadX509KeyPair(filepath.Join(os.TempDir(), "cert.pem"), filepath.Join(os.TempDir(), "key.pem"))
|
||||
assert.NoError(t, err)
|
||||
|
||||
config := &tls.Config{
|
||||
Certificates: []tls.Certificate{cliCrt},
|
||||
}
|
||||
sn1, err := GetCertSnFromConfig(config)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, sn1)
|
||||
|
||||
sn2, err := GetCertSnFromEncode(cert)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, sn2)
|
||||
|
||||
assert.Equal(t, sn1, sn2)
|
||||
}
|
253
lib/cert/client_hello.go
Normal file
253
lib/cert/client_hello.go
Normal file
@@ -0,0 +1,253 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CurveID uint16
|
||||
type SignatureScheme uint16
|
||||
|
||||
const (
|
||||
statusTypeOCSP uint8 = 1
|
||||
extensionServerName uint16 = 0
|
||||
extensionStatusRequest uint16 = 5
|
||||
extensionSupportedCurves uint16 = 10
|
||||
extensionSupportedPoints uint16 = 11
|
||||
extensionSignatureAlgorithms uint16 = 13
|
||||
extensionALPN uint16 = 16
|
||||
extensionSCT uint16 = 18 // https://tools.ietf.org/html/rfc6962#section-6
|
||||
extensionSessionTicket uint16 = 35
|
||||
extensionNextProtoNeg uint16 = 13172 // not IANA assigned
|
||||
extensionRenegotiationInfo uint16 = 0xff01
|
||||
scsvRenegotiation uint16 = 0x00ff
|
||||
)
|
||||
|
||||
type ClientHelloMsg struct {
|
||||
raw []byte
|
||||
vers uint16
|
||||
random []byte
|
||||
sessionId []byte
|
||||
cipherSuites []uint16
|
||||
compressionMethods []uint8
|
||||
nextProtoNeg bool
|
||||
serverName string
|
||||
ocspStapling bool
|
||||
scts bool
|
||||
supportedCurves []CurveID
|
||||
supportedPoints []uint8
|
||||
ticketSupported bool
|
||||
sessionTicket []uint8
|
||||
supportedSignatureAlgorithms []SignatureScheme
|
||||
secureRenegotiation []byte
|
||||
secureRenegotiationSupported bool
|
||||
alpnProtocols []string
|
||||
}
|
||||
|
||||
func (m *ClientHelloMsg) GetServerName() string {
|
||||
return m.serverName
|
||||
}
|
||||
|
||||
func (m *ClientHelloMsg) Unmarshal(data []byte) bool {
|
||||
if len(data) < 42 {
|
||||
return false
|
||||
}
|
||||
m.raw = data
|
||||
m.vers = uint16(data[4])<<8 | uint16(data[5])
|
||||
m.random = data[6:38]
|
||||
sessionIdLen := int(data[38])
|
||||
if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
|
||||
return false
|
||||
}
|
||||
m.sessionId = data[39 : 39+sessionIdLen]
|
||||
data = data[39+sessionIdLen:]
|
||||
if len(data) < 2 {
|
||||
return false
|
||||
}
|
||||
// cipherSuiteLen is the number of bytes of cipher suite numbers. Since
|
||||
// they are uint16s, the number must be even.
|
||||
cipherSuiteLen := int(data[0])<<8 | int(data[1])
|
||||
if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
|
||||
return false
|
||||
}
|
||||
numCipherSuites := cipherSuiteLen / 2
|
||||
m.cipherSuites = make([]uint16, numCipherSuites)
|
||||
for i := 0; i < numCipherSuites; i++ {
|
||||
m.cipherSuites[i] = uint16(data[2+2*i])<<8 | uint16(data[3+2*i])
|
||||
if m.cipherSuites[i] == scsvRenegotiation {
|
||||
m.secureRenegotiationSupported = true
|
||||
}
|
||||
}
|
||||
data = data[2+cipherSuiteLen:]
|
||||
if len(data) < 1 {
|
||||
return false
|
||||
}
|
||||
compressionMethodsLen := int(data[0])
|
||||
if len(data) < 1+compressionMethodsLen {
|
||||
return false
|
||||
}
|
||||
m.compressionMethods = data[1 : 1+compressionMethodsLen]
|
||||
data = data[1+compressionMethodsLen:]
|
||||
|
||||
m.nextProtoNeg = false
|
||||
m.serverName = ""
|
||||
m.ocspStapling = false
|
||||
m.ticketSupported = false
|
||||
m.sessionTicket = nil
|
||||
m.supportedSignatureAlgorithms = nil
|
||||
m.alpnProtocols = nil
|
||||
m.scts = false
|
||||
|
||||
if len(data) == 0 {
|
||||
// ClientHello is optionally followed by extension data
|
||||
return true
|
||||
}
|
||||
if len(data) < 2 {
|
||||
return false
|
||||
}
|
||||
|
||||
extensionsLength := int(data[0])<<8 | int(data[1])
|
||||
data = data[2:]
|
||||
if extensionsLength != len(data) {
|
||||
return false
|
||||
}
|
||||
|
||||
for len(data) != 0 {
|
||||
if len(data) < 4 {
|
||||
return false
|
||||
}
|
||||
extension := uint16(data[0])<<8 | uint16(data[1])
|
||||
length := int(data[2])<<8 | int(data[3])
|
||||
data = data[4:]
|
||||
if len(data) < length {
|
||||
return false
|
||||
}
|
||||
|
||||
switch extension {
|
||||
case extensionServerName:
|
||||
d := data[:length]
|
||||
if len(d) < 2 {
|
||||
return false
|
||||
}
|
||||
namesLen := int(d[0])<<8 | int(d[1])
|
||||
d = d[2:]
|
||||
if len(d) != namesLen {
|
||||
return false
|
||||
}
|
||||
for len(d) > 0 {
|
||||
if len(d) < 3 {
|
||||
return false
|
||||
}
|
||||
nameType := d[0]
|
||||
nameLen := int(d[1])<<8 | int(d[2])
|
||||
d = d[3:]
|
||||
if len(d) < nameLen {
|
||||
return false
|
||||
}
|
||||
if nameType == 0 {
|
||||
m.serverName = string(d[:nameLen])
|
||||
// An SNI value may not include a
|
||||
// trailing dot. See
|
||||
// https://tools.ietf.org/html/rfc6066#section-3.
|
||||
if strings.HasSuffix(m.serverName, ".") {
|
||||
return false
|
||||
}
|
||||
break
|
||||
}
|
||||
d = d[nameLen:]
|
||||
}
|
||||
case extensionNextProtoNeg:
|
||||
if length > 0 {
|
||||
return false
|
||||
}
|
||||
m.nextProtoNeg = true
|
||||
case extensionStatusRequest:
|
||||
m.ocspStapling = length > 0 && data[0] == statusTypeOCSP
|
||||
case extensionSupportedCurves:
|
||||
// https://tools.ietf.org/html/rfc4492#section-5.5.1
|
||||
if length < 2 {
|
||||
return false
|
||||
}
|
||||
l := int(data[0])<<8 | int(data[1])
|
||||
if l%2 == 1 || length != l+2 {
|
||||
return false
|
||||
}
|
||||
numCurves := l / 2
|
||||
m.supportedCurves = make([]CurveID, numCurves)
|
||||
d := data[2:]
|
||||
for i := 0; i < numCurves; i++ {
|
||||
m.supportedCurves[i] = CurveID(d[0])<<8 | CurveID(d[1])
|
||||
d = d[2:]
|
||||
}
|
||||
case extensionSupportedPoints:
|
||||
// https://tools.ietf.org/html/rfc4492#section-5.5.2
|
||||
if length < 1 {
|
||||
return false
|
||||
}
|
||||
l := int(data[0])
|
||||
if length != l+1 {
|
||||
return false
|
||||
}
|
||||
m.supportedPoints = make([]uint8, l)
|
||||
copy(m.supportedPoints, data[1:])
|
||||
case extensionSessionTicket:
|
||||
// https://tools.ietf.org/html/rfc5077#section-3.2
|
||||
m.ticketSupported = true
|
||||
m.sessionTicket = data[:length]
|
||||
case extensionSignatureAlgorithms:
|
||||
// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
|
||||
if length < 2 || length&1 != 0 {
|
||||
return false
|
||||
}
|
||||
l := int(data[0])<<8 | int(data[1])
|
||||
if l != length-2 {
|
||||
return false
|
||||
}
|
||||
n := l / 2
|
||||
d := data[2:]
|
||||
m.supportedSignatureAlgorithms = make([]SignatureScheme, n)
|
||||
for i := range m.supportedSignatureAlgorithms {
|
||||
m.supportedSignatureAlgorithms[i] = SignatureScheme(d[0])<<8 | SignatureScheme(d[1])
|
||||
d = d[2:]
|
||||
}
|
||||
case extensionRenegotiationInfo:
|
||||
if length == 0 {
|
||||
return false
|
||||
}
|
||||
d := data[:length]
|
||||
l := int(d[0])
|
||||
d = d[1:]
|
||||
if l != len(d) {
|
||||
return false
|
||||
}
|
||||
|
||||
m.secureRenegotiation = d
|
||||
m.secureRenegotiationSupported = true
|
||||
case extensionALPN:
|
||||
if length < 2 {
|
||||
return false
|
||||
}
|
||||
l := int(data[0])<<8 | int(data[1])
|
||||
if l != length-2 {
|
||||
return false
|
||||
}
|
||||
d := data[2:length]
|
||||
for len(d) != 0 {
|
||||
stringLen := int(d[0])
|
||||
d = d[1:]
|
||||
if stringLen == 0 || stringLen > len(d) {
|
||||
return false
|
||||
}
|
||||
m.alpnProtocols = append(m.alpnProtocols, string(d[:stringLen]))
|
||||
d = d[stringLen:]
|
||||
}
|
||||
case extensionSCT:
|
||||
m.scts = true
|
||||
if length != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
data = data[length:]
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
108
lib/cert/generate.go
Normal file
108
lib/cert/generate.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"math/big"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ Generator = (*X509Generator)(nil)
|
||||
|
||||
type Generator interface {
|
||||
CreateRootCa() ([]byte, []byte, error)
|
||||
CreateCert(dnsName string) ([]byte, []byte, error)
|
||||
InitRootCa(rootCa []byte, rootKey []byte) error
|
||||
}
|
||||
|
||||
type X509Generator struct {
|
||||
rootCert *x509.Certificate
|
||||
rootRsaPrivate *rsa.PrivateKey
|
||||
subject pkix.Name
|
||||
}
|
||||
|
||||
func NewX509Generator(subject pkix.Name) *X509Generator {
|
||||
return &X509Generator{
|
||||
subject: subject,
|
||||
}
|
||||
}
|
||||
|
||||
func (cg *X509Generator) InitRootCa(rootCa []byte, rootKey []byte) error {
|
||||
var err error
|
||||
caBlock, _ := pem.Decode(rootCa)
|
||||
cg.rootCert, err = x509.ParseCertificate(caBlock.Bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
keyBlock, _ := pem.Decode(rootKey)
|
||||
cg.rootRsaPrivate, err = x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cg *X509Generator) CreateCert(dnsName string) ([]byte, []byte, error) {
|
||||
return cg.create(false, dnsName)
|
||||
}
|
||||
|
||||
func (cg *X509Generator) CreateRootCa() ([]byte, []byte, error) {
|
||||
return cg.create(true, "")
|
||||
}
|
||||
|
||||
func (cg *X509Generator) create(isRootCa bool, dnsName string) ([]byte, []byte, error) {
|
||||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
||||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
||||
template := &x509.Certificate{
|
||||
SerialNumber: serialNumber,
|
||||
Subject: cg.subject,
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(3, 0, 0),
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: false,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
|
||||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageDataEncipherment,
|
||||
DNSNames: []string{dnsName},
|
||||
}
|
||||
|
||||
if isRootCa {
|
||||
template.IsCA = true
|
||||
template.KeyUsage |= x509.KeyUsageCertSign
|
||||
}
|
||||
|
||||
priKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var ca []byte
|
||||
if !isRootCa {
|
||||
if cg.rootCert == nil || cg.rootRsaPrivate == nil {
|
||||
return nil, nil, errors.New("root ca is not exist")
|
||||
}
|
||||
ca, err = x509.CreateCertificate(rand.Reader, template, cg.rootCert, &priKey.PublicKey, cg.rootRsaPrivate)
|
||||
} else {
|
||||
ca, err = x509.CreateCertificate(rand.Reader, template, template, &priKey.PublicKey, priKey)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
caPem := &pem.Block{
|
||||
Type: "CERTIFICATE",
|
||||
Bytes: ca,
|
||||
}
|
||||
ca = pem.EncodeToMemory(caPem)
|
||||
|
||||
buf := x509.MarshalPKCS1PrivateKey(priKey)
|
||||
keyPem := &pem.Block{
|
||||
Type: "PRIVATE KEY",
|
||||
Bytes: buf,
|
||||
}
|
||||
key := pem.EncodeToMemory(keyPem)
|
||||
return ca, key, nil
|
||||
}
|
62
lib/cert/generate_test.go
Normal file
62
lib/cert/generate_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateCert(t *testing.T) {
|
||||
dnsName := "ehang.io"
|
||||
g := NewX509Generator(pkix.Name{
|
||||
Country: []string{"CN"},
|
||||
Organization: []string{"ehang.io"},
|
||||
OrganizationalUnit: []string{"nps"},
|
||||
Province: []string{"Beijing"},
|
||||
CommonName: "nps",
|
||||
Locality: []string{"Beijing"},
|
||||
})
|
||||
// generate root ca
|
||||
rootCa, rootKey, err := g.CreateRootCa()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = g.InitRootCa(rootCa, rootKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// generate npc cert
|
||||
clientCa, _, err := g.CreateCert(dnsName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// verify npc cert by root cert
|
||||
roots := x509.NewCertPool()
|
||||
ok := roots.AppendCertsFromPEM(rootCa)
|
||||
if !ok {
|
||||
panic("failed to parse root certificate")
|
||||
}
|
||||
|
||||
block, _ := pem.Decode(clientCa)
|
||||
if block == nil {
|
||||
t.Fatal("failed to parse certificate PEM")
|
||||
}
|
||||
cert, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
t.Fatal("failed to parse certificate: " + err.Error())
|
||||
}
|
||||
|
||||
opts := x509.VerifyOptions{
|
||||
Roots: roots,
|
||||
DNSName: dnsName,
|
||||
Intermediates: x509.NewCertPool(),
|
||||
}
|
||||
|
||||
if _, err := cert.Verify(opts); err != nil {
|
||||
t.Fatal("failed to verify certificate: " + err.Error())
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user