Skip to content

Commit b250aca

Browse files
author
Tony Yang
committed
[FAB-4364] [FAB-5352] Support IP SANs in cryptogen
No matter what is provided as SANS in crypto-config.yaml, current code logic of cryptogen takes them as host names, resulting in failed communication if fabric network is configured with IP addresses. The change proposes to examine the content of SANS, setting them as IP SANs in the generated certificates if they are IP addresses. Change-Id: Ie9cbc341ab21ec5966fdabcd48d79a9d05d7b961 Signed-off-by: Tony Yang <tony@arxanfintech.com>
1 parent e5b46d1 commit b250aca

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

common/tools/cryptogen/ca/ca_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package ca_test
1818
import (
1919
"crypto/ecdsa"
2020
"crypto/x509"
21+
"net"
2122
"os"
2223
"path/filepath"
2324
"testing"
@@ -31,6 +32,8 @@ const (
3132
testCAName = "root0"
3233
testCA2Name = "root1"
3334
testName = "cert0"
35+
testName2 = "cert1"
36+
testIP = "172.16.10.31"
3437
)
3538

3639
var testDir = filepath.Join(os.TempDir(), "ca-test")
@@ -85,6 +88,13 @@ func TestGenerateSignCertificate(t *testing.T) {
8588
assert.NoError(t, err, "Failed to generate signed certificate")
8689
assert.Equal(t, 0, len(cert.ExtKeyUsage))
8790

91+
// make sure sans are correctly set
92+
sans := []string{testName2, testIP}
93+
cert, err = rootCA.SignCertificate(certDir, testName, sans, ecPubKey,
94+
x509.KeyUsageDigitalSignature, []x509.ExtKeyUsage{})
95+
assert.Contains(t, cert.DNSNames, testName2)
96+
assert.Contains(t, cert.IPAddresses, net.ParseIP(testIP).To4())
97+
8898
// check to make sure the signed public key was stored
8999
pemFile := filepath.Join(certDir, testName+"-cert.pem")
90100
assert.Equal(t, true, checkForFile(pemFile),

common/tools/cryptogen/ca/generator.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"crypto/x509/pkix"
2424
"encoding/pem"
2525
"math/big"
26+
"net"
2627
"os"
2728
"time"
2829

@@ -100,7 +101,15 @@ func (ca *CA) SignCertificate(baseDir, name string, sans []string, pub *ecdsa.Pu
100101
subject.CommonName = name
101102

102103
template.Subject = subject
103-
template.DNSNames = sans
104+
for _, san := range sans {
105+
// try to parse as an IP address first
106+
ip := net.ParseIP(san)
107+
if ip != nil {
108+
template.IPAddresses = append(template.IPAddresses, ip)
109+
} else {
110+
template.DNSNames = append(template.DNSNames, san)
111+
}
112+
}
104113

105114
cert, err := genCertificateECDSA(baseDir, name, &template, ca.SignCert,
106115
pub, ca.Signer)

common/tools/cryptogen/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ PeerOrgs:
136136
# which obtains its values from the Spec.Hostname and
137137
# Org.Domain, respectively.
138138
# - SANS: (Optional) Specifies one or more Subject Alternative Names
139-
# the be set in the resulting x509. Accepts template
140-
# variables {{.Hostname}}, {{.Domain}}, {{.CommonName}}
139+
# to be set in the resulting x509. Accepts template
140+
# variables {{.Hostname}}, {{.Domain}}, {{.CommonName}}. IP
141+
# addresses provided here will be properly recognized. Other
142+
# values will be taken as DNS names.
141143
# NOTE: Two implicit entries are created for you:
142144
# - {{ .CommonName }}
143145
# - {{ .Hostname }}
@@ -149,6 +151,7 @@ PeerOrgs:
149151
# - "bar.{{.Domain}}"
150152
# - "altfoo.{{.Domain}}"
151153
# - "{{.Hostname}}.org6.net"
154+
# - 172.16.10.31
152155
# - Hostname: bar
153156
# - Hostname: baz
154157

0 commit comments

Comments
 (0)