Skip to content

Commit 01fdfc3

Browse files
authored
Merge pull request #616 from zigbee-alliance/refactoring
Refactored PKI Keeper to reduce code duplication
2 parents c76de5c + f06d87f commit 01fdfc3

File tree

44 files changed

+1999
-1032
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1999
-1032
lines changed

integration_tests/constants/noc_constants.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ BAMCA0kAMEYCIQDzsjB569j1SsltNIP8CMTD4kRsTulqSp+O7JbQdWyzPAIhAODV
129129
zodhpBXZfzhHDvINejK8wzwWgf7Ds8wk3oENlmAj
130130
-----END CERTIFICATE-----`
131131

132+
NocRootCert1Issuer = "MHoxCzAJBgNVBAYTAlVaMRMwEQYDVQQIDApTb21lIFN0YXRlMREwDwYDVQQHDAhUYXNoa2VudDEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMQ4wDAYDVQQDDAVOT0MtMQ=="
132133
NocRootCert1Subject = "MHoxCzAJBgNVBAYTAlVaMRMwEQYDVQQIDApTb21lIFN0YXRlMREwDwYDVQQHDAhUYXNoa2VudDEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMQ4wDAYDVQQDDAVOT0MtMQ=="
133134
NocRootCert1SubjectKeyID = "44:EB:4C:62:6B:25:48:CD:A2:B3:1C:87:41:5A:08:E7:2B:B9:83:26"
134135
NocRootCert1SerialNumber = "47211865327720222621302679792296833381734533449"
@@ -149,11 +150,12 @@ zodhpBXZfzhHDvINejK8wzwWgf7Ds8wk3oENlmAj
149150
NocRootCert3SerialNumber = "38457288443253426021793906708335409501754677187"
150151
NocRootCert3SubjectAsText = "CN=NOC-3,O=Internet Widgits Pty Ltd,ST=Some-State,C=AU"
151152

152-
NocCert1Subject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMQ=="
153-
NocCert1Issuer = NocRootCert1Subject
154-
NocCert1SubjectKeyID = "02:72:6E:BC:BB:EF:D6:BD:8D:9B:42:AE:D4:3C:C0:55:5F:66:3A:B3"
155-
NocCert1SerialNumber = "631388393741945881054190991612463928825155142122"
156-
NocCert1SubjectAsText = "CN=NOC-child-1,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
153+
NocCert1Subject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMQ=="
154+
NocCert1Issuer = NocRootCert1Subject
155+
NocCert1AuthorityKeyID = NocRootCert1SubjectKeyID
156+
NocCert1SubjectKeyID = "02:72:6E:BC:BB:EF:D6:BD:8D:9B:42:AE:D4:3C:C0:55:5F:66:3A:B3"
157+
NocCert1SerialNumber = "631388393741945881054190991612463928825155142122"
158+
NocCert1SubjectAsText = "CN=NOC-child-1,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
157159

158160
NocCert1CopySubject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMQ=="
159161
NocCert1CopyIssuer = NocRootCert1Subject
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* eslint-disable */
2+
import _m0 from "protobufjs/minimal";
3+
import { Certificate } from "./certificate";
4+
5+
export const protobufPackage = "zigbeealliance.distributedcomplianceledger.pki";
6+
7+
export interface AllCertificatesBySubjectKeyId {
8+
subjectKeyId: string;
9+
certs: Certificate[];
10+
schemaVersion: number;
11+
}
12+
13+
function createBaseAllCertificatesBySubjectKeyId(): AllCertificatesBySubjectKeyId {
14+
return { subjectKeyId: "", certs: [], schemaVersion: 0 };
15+
}
16+
17+
export const AllCertificatesBySubjectKeyId = {
18+
encode(message: AllCertificatesBySubjectKeyId, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
19+
if (message.subjectKeyId !== "") {
20+
writer.uint32(10).string(message.subjectKeyId);
21+
}
22+
for (const v of message.certs) {
23+
Certificate.encode(v!, writer.uint32(18).fork()).ldelim();
24+
}
25+
if (message.schemaVersion !== 0) {
26+
writer.uint32(24).uint32(message.schemaVersion);
27+
}
28+
return writer;
29+
},
30+
31+
decode(input: _m0.Reader | Uint8Array, length?: number): AllCertificatesBySubjectKeyId {
32+
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
33+
let end = length === undefined ? reader.len : reader.pos + length;
34+
const message = createBaseAllCertificatesBySubjectKeyId();
35+
while (reader.pos < end) {
36+
const tag = reader.uint32();
37+
switch (tag >>> 3) {
38+
case 1:
39+
message.subjectKeyId = reader.string();
40+
break;
41+
case 2:
42+
message.certs.push(Certificate.decode(reader, reader.uint32()));
43+
break;
44+
case 3:
45+
message.schemaVersion = reader.uint32();
46+
break;
47+
default:
48+
reader.skipType(tag & 7);
49+
break;
50+
}
51+
}
52+
return message;
53+
},
54+
55+
fromJSON(object: any): AllCertificatesBySubjectKeyId {
56+
return {
57+
subjectKeyId: isSet(object.subjectKeyId) ? String(object.subjectKeyId) : "",
58+
certs: Array.isArray(object?.certs) ? object.certs.map((e: any) => Certificate.fromJSON(e)) : [],
59+
schemaVersion: isSet(object.schemaVersion) ? Number(object.schemaVersion) : 0,
60+
};
61+
},
62+
63+
toJSON(message: AllCertificatesBySubjectKeyId): unknown {
64+
const obj: any = {};
65+
message.subjectKeyId !== undefined && (obj.subjectKeyId = message.subjectKeyId);
66+
if (message.certs) {
67+
obj.certs = message.certs.map((e) => e ? Certificate.toJSON(e) : undefined);
68+
} else {
69+
obj.certs = [];
70+
}
71+
message.schemaVersion !== undefined && (obj.schemaVersion = Math.round(message.schemaVersion));
72+
return obj;
73+
},
74+
75+
fromPartial<I extends Exact<DeepPartial<AllCertificatesBySubjectKeyId>, I>>(
76+
object: I,
77+
): AllCertificatesBySubjectKeyId {
78+
const message = createBaseAllCertificatesBySubjectKeyId();
79+
message.subjectKeyId = object.subjectKeyId ?? "";
80+
message.certs = object.certs?.map((e) => Certificate.fromPartial(e)) || [];
81+
message.schemaVersion = object.schemaVersion ?? 0;
82+
return message;
83+
},
84+
};
85+
86+
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
87+
88+
export type DeepPartial<T> = T extends Builtin ? T
89+
: T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
90+
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
91+
: Partial<T>;
92+
93+
type KeysOfUnion<T> = T extends T ? keyof T : never;
94+
export type Exact<P, I extends P> = P extends Builtin ? P
95+
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
96+
97+
function isSet(value: any): boolean {
98+
return value !== null && value !== undefined;
99+
}

x/pki/keeper/approved_root_certificates.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ func (k Keeper) RemoveApprovedRootCertificates(ctx sdk.Context) {
3535
}
3636

3737
// Add root certificate to the list.
38-
func (k Keeper) AddApprovedRootCertificate(ctx sdk.Context, certID types.CertificateIdentifier) {
38+
func (k Keeper) AddApprovedRootCertificate(ctx sdk.Context, certificate types.Certificate) {
3939
rootCertificates, _ := k.GetApprovedRootCertificates(ctx)
4040

41+
certID := types.CertificateIdentifier{
42+
Subject: certificate.Subject,
43+
SubjectKeyId: certificate.SubjectKeyId,
44+
}
45+
4146
// Check if the root cert is already there
4247
for _, existingCertID := range rootCertificates.Certs {
4348
if *existingCertID == certID {
@@ -51,7 +56,16 @@ func (k Keeper) AddApprovedRootCertificate(ctx sdk.Context, certID types.Certifi
5156
}
5257

5358
// Remove root certificate from the list.
54-
func (k Keeper) RemoveApprovedRootCertificate(ctx sdk.Context, certID types.CertificateIdentifier) {
59+
func (k Keeper) RemoveApprovedRootCertificate(
60+
ctx sdk.Context,
61+
subject string,
62+
subjectKeyID string,
63+
) {
64+
certID := types.CertificateIdentifier{
65+
Subject: subject,
66+
SubjectKeyId: subjectKeyID,
67+
}
68+
5569
rootCertificates, _ := k.GetApprovedRootCertificates(ctx)
5670

5771
certIDIndex := -1

0 commit comments

Comments
 (0)