Skip to content

Commit 67e912c

Browse files
authored
Certificate comparison improvement (#600)
Revocation Distribution Point: Certificate comparison improvement
1 parent 6c14949 commit 67e912c

6 files changed

+169
-6
lines changed

integration_tests/constants/constants.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,25 @@ HSMEGDAWgBRq/SJ3H1Ef7L8WQZdnENzcMaFxfjAKBggqhkjOPQQDAgNHADBEAiBQ
280280
qoAC9NkyqaAFOPZTaK0P/8jvu8m+t9pWmDXPmqdRDgIgI7rI/g8j51RFtlM5CBpH
281281
mUkpxyqvChVI1A0DTVFLJd4=
282282
-----END CERTIFICATE-----`
283-
PAACertWithNumericVidSubject = "MDAxGDAWBgNVBAMMD01hdHRlciBUZXN0IFBBQTEUMBIGCisGAQQBgqJ8AgEMBEZGRjE="
284-
PAACertWithNumericVidSubjectKeyID = "6A:FD:22:77:1F:51:1F:EC:BF:16:41:97:67:10:DC:DC:31:A1:71:7E"
285-
PAACertWithNumericVidVid = 65521
283+
PAACertWithNumericVidSubject = "MDAxGDAWBgNVBAMMD01hdHRlciBUZXN0IFBBQTEUMBIGCisGAQQBgqJ8AgEMBEZGRjE="
284+
PAACertWithNumericVidSubjectKeyID = "6A:FD:22:77:1F:51:1F:EC:BF:16:41:97:67:10:DC:DC:31:A1:71:7E"
285+
PAACertWithNumericVidVid = 65521
286+
PAACertWithNumericVidDifferentWhitespaces = `
287+
-----BEGIN CERTIFICATE-----
288+
MIIBvTCCAWSgAwIBAgIITqjoMY
289+
LUHBwwCgYIKoZIzj0EAwIwMDEYMBYGA1UEAwwP
290+
TWF0dGVyIFRlc3QgUEFBMRQ
291+
wEgYKKwYBBAGConwCAQwERkZGMTAgFw0yMTA2Mjgx
292+
ND IzNDNaGA85OTk5MTI
293+
zMTIzNTk1OVowMDEYMBYGA1UEAwwPTWF0dGVyIFRlc3Qg
294+
UEFBMRQwEgYKKwYBBAGConwCAQwERkZGMTBZMBMGByqGSM49AgEGCCqGSM49AwEH
295+
A0IABLbLY3KIfyko9brIGqnZOuJDHK2p154kL2UXfvnO2TKijs0Duq9qj8oYShpQ
296+
NUKWDUU/ MD8fGUIddR6Pjxqam3WjZjBkMBIGA1UdEwEB/wQIMAYBAf8CAQEwDgYD
297+
VR0PAQH/BAQDAgEGMB0GA1Ud DgQWBBRq/SJ3H1Ef7L8WQZdnENzcMaFxfjAfBgNV
298+
HSMEGDAWgBRq/SJ3H1Ef7L8WQZdnENzcMaFxfjAKBggqhkjOPQQDAgNHADBEAiBQ
299+
qoAC9NkyqaAFOPZTaK0P/8jvu8m+t9pWmDXPmqdRDgIgI7rI/g8j51RFtlM5CBpH
300+
mUkpxyqvChVI1A0DTVFLJd4=
301+
-----END CERTIFICATE-----`
286302

287303
PAACertNoVid = `
288304
-----BEGIN CERTIFICATE-----
@@ -314,6 +330,7 @@ NbKsuLiNm8I5idctQg3eaw==
314330
-----END CERTIFICATE-----`
315331
PAACertWithNumericVid1Subject = "MDAxGDAWBgNVBAMMD01hdHRlciBUZXN0IFBBQTEUMBIGCisGAQQBgqJ8AgEMBEZGRjI="
316332
PAACertWithNumericVid1SubjectKeyID = "7F:1D:AA:F2:44:98:B9:86:68:0E:A0:8F:C1:89:21:E8:48:48:9D:17"
333+
PAACertWithNumericVid1Vid = 65522
317334

318335
PAICertWithNumericPidVid = `
319336
-----BEGIN CERTIFICATE-----
@@ -550,7 +567,8 @@ al/8sTx3xx7fWpS+rJ3jviCpHgP+cGV/ANg8hOlyr68u0FE+x6pye00TmxcFzDuo
550567
2Vciq5eYOIi+PlP+HI5QzlZYxSqFjJrFcfzYCJ4=
551568
-----END CERTIFICATE-----`
552569

553-
RootCertWithVid = `-----BEGIN CERTIFICATE-----
570+
RootCertWithVid = `
571+
-----BEGIN CERTIFICATE-----
554572
MIICdDCCAhmgAwIBAgIBATAKBggqhkjOPQQDAjCBmDELMAkGA1UEBhMCVVMxETAP
555573
BgNVBAgMCE5ldyBZb3JrMREwDwYDVQQHDAhOZXcgWW9yazEYMBYGA1UECgwPRXhh
556574
bXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRgwFgYDVQQD

integration_tests/grpc_rest/pki/helpers.go

+42
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,48 @@ func Demo(suite *utils.TestSuite) {
16871687
revDistPoints, _ = GetAllPkiRevocationDistributionPoints(suite)
16881688
require.Equal(suite.T, 1, len(revDistPoints))
16891689

1690+
// Add revocation distribution point for PAA by Vendor with certificate with different whitespaces
1691+
label := "label-add-update"
1692+
dataURL := testconstants.DataURL + "add-update"
1693+
1694+
msgAddPkiRevDistPoints = pkitypes.MsgAddPkiRevocationDistributionPoint{
1695+
Signer: vendorAccount.Address,
1696+
Vid: vendorAccount.VendorID,
1697+
IssuerSubjectKeyID: testconstants.SubjectKeyIDWithoutColons,
1698+
IsPAA: true,
1699+
CrlSignerCertificate: testconstants.PAACertWithNumericVidDifferentWhitespaces,
1700+
Label: label,
1701+
DataURL: dataURL,
1702+
RevocationType: 1,
1703+
SchemaVersion: 0,
1704+
}
1705+
_, err = suite.BuildAndBroadcastTx([]sdk.Msg{&msgAddPkiRevDistPoints}, vendorName, vendorAccount)
1706+
require.NoError(suite.T, err)
1707+
1708+
revocationPointBySubjectKeyID, err := GetPkiRevocationDistributionPointsBySubject(suite, testconstants.SubjectKeyIDWithoutColons)
1709+
require.NoError(suite.T, err)
1710+
require.Equal(suite.T, 2, len(revocationPointBySubjectKeyID.Points))
1711+
require.Equal(suite.T, msgAddPkiRevDistPoints.CrlSignerCertificate, revocationPointBySubjectKeyID.Points[1].CrlSignerCertificate)
1712+
1713+
// Update revocation distribution point
1714+
msgUpdatePkiRevocationDistributionPoint := pkitypes.MsgUpdatePkiRevocationDistributionPoint{
1715+
Signer: vendorAccount.Address,
1716+
Vid: vendorAccount.VendorID,
1717+
IssuerSubjectKeyID: testconstants.SubjectKeyIDWithoutColons,
1718+
CrlSignerCertificate: testconstants.PAACertWithNumericVid,
1719+
Label: label,
1720+
DataURL: dataURL + "/new",
1721+
SchemaVersion: 0,
1722+
}
1723+
_, err = suite.BuildAndBroadcastTx([]sdk.Msg{&msgUpdatePkiRevocationDistributionPoint}, vendorName, vendorAccount)
1724+
require.NoError(suite.T, err)
1725+
1726+
revocationPointBySubjectKeyID, err = GetPkiRevocationDistributionPointsBySubject(suite, testconstants.SubjectKeyIDWithoutColons)
1727+
require.NoError(suite.T, err)
1728+
require.Equal(suite.T, 2, len(revocationPointBySubjectKeyID.Points))
1729+
require.Equal(suite.T, msgUpdatePkiRevocationDistributionPoint.CrlSignerCertificate, revocationPointBySubjectKeyID.Points[1].CrlSignerCertificate)
1730+
require.Equal(suite.T, msgUpdatePkiRevocationDistributionPoint.DataURL, revocationPointBySubjectKeyID.Points[1].DataURL)
1731+
16901732
// Revoke certificates by serialNumber
16911733

16921734
// Add root certificates

x/pki/handler_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,96 @@ func TestHandler_RevocationPointsByIssuerSubjectKeyID(t *testing.T) {
18241824
require.Equal(t, len(revocationPointBySubjectKeyID.Points), 1)
18251825
}
18261826

1827+
func TestHandler_AddRevocationPointForSameCertificateWithDifferentWhitespaces(t *testing.T) {
1828+
setup := Setup(t)
1829+
1830+
vendorAcc := GenerateAccAddress()
1831+
setup.AddAccount(vendorAcc, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, 65521)
1832+
1833+
// propose x509 root certificate by account Trustee1
1834+
proposeAddX509RootCert := types.NewMsgProposeAddX509RootCert(setup.Trustee1.String(), testconstants.PAACertWithNumericVid, testconstants.Info, testconstants.Vid, testconstants.CertSchemaVersion)
1835+
_, err := setup.Handler(setup.Ctx, proposeAddX509RootCert)
1836+
require.NoError(t, err)
1837+
1838+
// approve
1839+
approveAddX509RootCert := types.NewMsgApproveAddX509RootCert(
1840+
setup.Trustee2.String(), testconstants.PAACertWithNumericVidSubject, testconstants.PAACertWithNumericVidSubjectKeyID, testconstants.Info)
1841+
_, err = setup.Handler(setup.Ctx, approveAddX509RootCert)
1842+
require.NoError(t, err)
1843+
1844+
addPkiRevocationDistributionPoint := types.MsgAddPkiRevocationDistributionPoint{
1845+
Signer: vendorAcc.String(),
1846+
Vid: testconstants.PAACertWithNumericVidVid,
1847+
IsPAA: true,
1848+
Pid: 8,
1849+
CrlSignerCertificate: testconstants.PAACertWithNumericVidDifferentWhitespaces,
1850+
Label: "label",
1851+
DataURL: testconstants.DataURL + "/1",
1852+
IssuerSubjectKeyID: testconstants.SubjectKeyIDWithoutColons,
1853+
RevocationType: 1,
1854+
}
1855+
_, err = setup.Handler(setup.Ctx, &addPkiRevocationDistributionPoint)
1856+
require.NoError(t, err)
1857+
1858+
revocationPointBySubjectKeyID, isFound := setup.Keeper.GetPkiRevocationDistributionPointsByIssuerSubjectKeyID(setup.Ctx, testconstants.SubjectKeyIDWithoutColons)
1859+
require.True(t, isFound)
1860+
require.Equal(t, len(revocationPointBySubjectKeyID.Points), 1)
1861+
require.Equal(t, revocationPointBySubjectKeyID.Points[0].CrlSignerCertificate, addPkiRevocationDistributionPoint.CrlSignerCertificate)
1862+
}
1863+
1864+
func TestHandler_UpdateRevocationPointForSameCertificateWithDifferentWhitespaces(t *testing.T) {
1865+
setup := Setup(t)
1866+
1867+
vendorAcc := GenerateAccAddress()
1868+
setup.AddAccount(vendorAcc, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, 65521)
1869+
1870+
// propose x509 root certificate by account Trustee1
1871+
proposeAddX509RootCert := types.NewMsgProposeAddX509RootCert(setup.Trustee1.String(), testconstants.PAACertWithNumericVid, testconstants.Info, testconstants.Vid, testconstants.CertSchemaVersion)
1872+
_, err := setup.Handler(setup.Ctx, proposeAddX509RootCert)
1873+
require.NoError(t, err)
1874+
1875+
// approve
1876+
approveAddX509RootCert := types.NewMsgApproveAddX509RootCert(
1877+
setup.Trustee2.String(), testconstants.PAACertWithNumericVidSubject, testconstants.PAACertWithNumericVidSubjectKeyID, testconstants.Info)
1878+
_, err = setup.Handler(setup.Ctx, approveAddX509RootCert)
1879+
require.NoError(t, err)
1880+
1881+
addPkiRevocationDistributionPoint := types.MsgAddPkiRevocationDistributionPoint{
1882+
Signer: vendorAcc.String(),
1883+
Vid: testconstants.PAACertWithNumericVidVid,
1884+
IsPAA: true,
1885+
Pid: 8,
1886+
CrlSignerCertificate: testconstants.PAACertWithNumericVid,
1887+
Label: "label",
1888+
DataURL: testconstants.DataURL + "/1",
1889+
IssuerSubjectKeyID: testconstants.SubjectKeyIDWithoutColons,
1890+
RevocationType: 1,
1891+
}
1892+
_, err = setup.Handler(setup.Ctx, &addPkiRevocationDistributionPoint)
1893+
require.NoError(t, err)
1894+
1895+
revocationPointBySubjectKeyID, isFound := setup.Keeper.GetPkiRevocationDistributionPointsByIssuerSubjectKeyID(setup.Ctx, testconstants.SubjectKeyIDWithoutColons)
1896+
require.True(t, isFound)
1897+
require.Equal(t, len(revocationPointBySubjectKeyID.Points), 1)
1898+
1899+
dataURLNew := testconstants.DataURL + "/new"
1900+
updatePkiRevocationDistributionPoint := types.MsgUpdatePkiRevocationDistributionPoint{
1901+
Signer: vendorAcc.String(),
1902+
Vid: testconstants.PAACertWithNumericVidVid,
1903+
CrlSignerCertificate: testconstants.PAACertWithNumericVidDifferentWhitespaces,
1904+
Label: "label",
1905+
DataURL: dataURLNew,
1906+
IssuerSubjectKeyID: testconstants.SubjectKeyIDWithoutColons,
1907+
}
1908+
_, err = setup.Handler(setup.Ctx, &updatePkiRevocationDistributionPoint)
1909+
require.NoError(t, err)
1910+
1911+
revocationPointBySubjectKeyID, isFound = setup.Keeper.GetPkiRevocationDistributionPointsByIssuerSubjectKeyID(setup.Ctx, testconstants.SubjectKeyIDWithoutColons)
1912+
require.True(t, isFound)
1913+
require.Equal(t, revocationPointBySubjectKeyID.Points[0].CrlSignerCertificate, updatePkiRevocationDistributionPoint.CrlSignerCertificate)
1914+
require.Equal(t, revocationPointBySubjectKeyID.Points[0].DataURL, updatePkiRevocationDistributionPoint.DataURL)
1915+
}
1916+
18271917
func TestHandler_AssignVid_SenderNotVendorAdmin(t *testing.T) {
18281918
setup := Setup(t)
18291919

x/pki/keeper/msg_server_add_pki_revocation_distribution_point.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (k msgServer) checkRootCert(ctx sdk.Context, crlSignerCertificate *x509.Cer
9494
// check that it has the same PEM value
9595
var foundRootCert *types.Certificate
9696
for _, approvedCertificate := range approvedCertificates.Certs {
97-
if approvedCertificate.PemCert == msg.CrlSignerCertificate {
97+
if x509.RemoveWhitespaces(approvedCertificate.PemCert) == x509.RemoveWhitespaces(msg.CrlSignerCertificate) {
9898
foundRootCert = approvedCertificate
9999

100100
break

x/pki/keeper/msg_server_update_pki_revocation_distribution_point.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (k msgServer) verifyUpdatedPAA(ctx sdk.Context, newCertificatePem string, r
131131
// check that it has the same PEM value
132132
var foundRootCert *types.Certificate
133133
for _, approvedCertificate := range approvedCertificates.Certs {
134-
if approvedCertificate.PemCert == newCertificatePem {
134+
if x509.RemoveWhitespaces(approvedCertificate.PemCert) == x509.RemoveWhitespaces(newCertificatePem) {
135135
foundRootCert = approvedCertificate
136136

137137
break

x/pki/x509/x509.go

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strconv"
2525
"strings"
2626
"time"
27+
"unicode"
2728

2829
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/types/pki"
2930
)
@@ -163,6 +164,18 @@ func BytesToHex(bytes []byte) string {
163164
return strings.Join(bytesHex, ":")
164165
}
165166

167+
func RemoveWhitespaces(pem string) string {
168+
var builder strings.Builder
169+
170+
for _, r := range pem {
171+
if !unicode.IsSpace(r) {
172+
builder.WriteRune(r)
173+
}
174+
}
175+
176+
return builder.String()
177+
}
178+
166179
func (c Certificate) Verify(parent *Certificate, blockTime time.Time) error {
167180
roots := x509.NewCertPool()
168181
roots.AddCert(parent.Certificate)

0 commit comments

Comments
 (0)