37
37
PRODUCTION_NODE_URL_REST = "https://on.dcl.csa-iot.org"
38
38
TEST_NODE_URL_REST = "https://on.test-net.dcl.csa-iot.org"
39
39
40
+ MATTER_CERT_CA_SUBJECT = "MFIxDDAKBgNVBAoMA0NTQTEsMCoGA1UEAwwjTWF0dGVyIENlcnRpZmljYXRpb24gYW5kIFRlc3RpbmcgQ0ExFDASBgorBgEEAYKifAIBDARDNUEw"
41
+ MATTER_CERT_CA_SUBJECT_KEY_ID = "97:E4:69:D0:C5:04:14:C2:6F:C7:01:F7:7E:94:77:39:09:8D:F6:A5"
42
+
40
43
41
44
def parse_paa_root_certs (cmdpipe , paa_list ):
42
45
"""
@@ -73,13 +76,14 @@ def parse_paa_root_certs(cmdpipe, paa_list):
73
76
else :
74
77
if b': ' in line :
75
78
key , value = line .split (b': ' )
76
- result [key .strip (b' -' ).decode ("utf-8" )] = value .strip ().decode ("utf-8" )
79
+ result [key .strip (b' -' ).decode ("utf-8" )
80
+ ] = value .strip ().decode ("utf-8" )
77
81
parse_paa_root_certs .counter += 1
78
82
if parse_paa_root_certs .counter % 2 == 0 :
79
83
paa_list .append (copy .deepcopy (result ))
80
84
81
85
82
- def write_paa_root_cert (certificate , subject ):
86
+ def write_cert (certificate , subject ):
83
87
filename = 'dcld_mirror_' + \
84
88
re .sub ('[^a-zA-Z0-9_-]' , '' , re .sub ('[=, ]' , '_' , subject ))
85
89
with open (filename + '.pem' , 'w+' ) as outfile :
@@ -93,7 +97,8 @@ def write_paa_root_cert(certificate, subject):
93
97
serialization .Encoding .DER )
94
98
outfile .write (der_certificate )
95
99
except (IOError , ValueError ) as e :
96
- print (f"ERROR: Failed to convert { filename + '.pem' } : { str (e )} . Skipping..." )
100
+ print (
101
+ f"ERROR: Failed to convert { filename + '.pem' } : { str (e )} . Skipping..." )
97
102
98
103
99
104
def parse_paa_root_cert_from_dcld (cmdpipe ):
@@ -133,7 +138,38 @@ def use_dcld(dcld, production, cmdlist):
133
138
@optgroup .option ('--paa-trust-store-path' , default = 'paa-root-certs' , type = str , metavar = 'PATH' , help = "PAA trust store path (default: paa-root-certs)" )
134
139
def main (use_main_net_dcld , use_test_net_dcld , use_main_net_http , use_test_net_http , paa_trust_store_path ):
135
140
"""DCL PAA mirroring tools"""
141
+ fetch_paa_certs (use_main_net_dcld , use_test_net_dcld , use_main_net_http , use_test_net_http , paa_trust_store_path )
142
+
143
+
144
+ def get_cert_from_rest (rest_node_url , subject , subject_key_id ):
145
+ response = requests .get (
146
+ f"{ rest_node_url } /dcl/pki/certificates/{ subject } /{ subject_key_id } " ).json ()["approvedCertificates" ]["certs" ][0 ]
147
+ certificate = response ["pemCert" ].rstrip ("\n " )
148
+ subject = response ["subjectAsText" ]
149
+ return certificate , subject
150
+
151
+
152
+ def fetch_cd_signing_certs (store_path ):
153
+ ''' Only supports using main net http currently.'''
154
+ rest_node_url = PRODUCTION_NODE_URL_REST
155
+ os .makedirs (store_path , exist_ok = True )
156
+ original_dir = os .getcwd ()
157
+ os .chdir (store_path )
136
158
159
+ cd_signer_ids = requests .get (
160
+ f"{ rest_node_url } /dcl/pki/child-certificates/{ MATTER_CERT_CA_SUBJECT } /{ MATTER_CERT_CA_SUBJECT_KEY_ID } " ).json ()['childCertificates' ]['certIds' ]
161
+ for signer in cd_signer_ids :
162
+ subject = signer ['subject' ]
163
+ subject_key_id = signer ['subjectKeyId' ]
164
+ certificate , subject = get_cert_from_rest (rest_node_url , subject , subject_key_id )
165
+
166
+ print (f"Downloaded CD signing cert with subject: { subject } " )
167
+ write_cert (certificate , subject )
168
+
169
+ os .chdir (original_dir )
170
+
171
+
172
+ def fetch_paa_certs (use_main_net_dcld , use_test_net_dcld , use_main_net_http , use_test_net_http , paa_trust_store_path ):
137
173
production = False
138
174
dcld = use_test_net_dcld
139
175
@@ -148,36 +184,43 @@ def main(use_main_net_dcld, use_test_net_dcld, use_main_net_http, use_test_net_h
148
184
rest_node_url = PRODUCTION_NODE_URL_REST if production else TEST_NODE_URL_REST
149
185
150
186
os .makedirs (paa_trust_store_path , exist_ok = True )
187
+ original_dir = os .getcwd ()
151
188
os .chdir (paa_trust_store_path )
152
189
153
190
if use_rest :
154
- paa_list = requests .get (f"{ rest_node_url } /dcl/pki/root-certificates" ).json ()["approvedRootCertificates" ]["certs" ]
191
+ paa_list = requests .get (
192
+ f"{ rest_node_url } /dcl/pki/root-certificates" ).json ()["approvedRootCertificates" ]["certs" ]
155
193
else :
156
194
cmdlist = ['query' , 'pki' , 'all-x509-root-certs' ]
157
195
158
- cmdpipe = subprocess .Popen (use_dcld (dcld , production , cmdlist ), stdout = subprocess .PIPE , stderr = subprocess .PIPE )
196
+ cmdpipe = subprocess .Popen (use_dcld (
197
+ dcld , production , cmdlist ), stdout = subprocess .PIPE , stderr = subprocess .PIPE )
159
198
160
199
paa_list = []
161
200
parse_paa_root_certs .counter = 0
162
201
parse_paa_root_certs (cmdpipe , paa_list )
163
202
164
203
for paa in paa_list :
204
+ if paa ['subject' ] == MATTER_CERT_CA_SUBJECT and paa ['subjectKeyId' ] == MATTER_CERT_CA_SUBJECT_KEY_ID :
205
+ # Don't include the CD signing cert as a PAA root.
206
+ continue
165
207
if use_rest :
166
- response = requests .get (
167
- f"{ rest_node_url } /dcl/pki/certificates/{ paa ['subject' ]} /{ paa ['subjectKeyId' ]} " ).json ()["approvedCertificates" ]["certs" ][0 ]
168
- certificate = response ["pemCert" ]
169
- subject = response ["subjectAsText" ]
208
+ certificate , subject = get_cert_from_rest (rest_node_url , paa ['subject' ], paa ['subjectKeyId' ])
170
209
else :
171
- cmdlist = ['query' , 'pki' , 'x509-cert' , '-u' , paa ['subject' ], '-k' , paa ['subjectKeyId' ]]
210
+ cmdlist = ['query' , 'pki' , 'x509-cert' , '-u' ,
211
+ paa ['subject' ], '-k' , paa ['subjectKeyId' ]]
172
212
173
- cmdpipe = subprocess .Popen (use_dcld (dcld , production , cmdlist ), stdout = subprocess .PIPE , stderr = subprocess .PIPE )
213
+ cmdpipe = subprocess .Popen (use_dcld (
214
+ dcld , production , cmdlist ), stdout = subprocess .PIPE , stderr = subprocess .PIPE )
174
215
175
216
(certificate , subject ) = parse_paa_root_cert_from_dcld (cmdpipe )
176
217
177
218
certificate = certificate .rstrip ('\n ' )
178
219
179
- print (f"Downloaded certificate with subject: { subject } " )
180
- write_paa_root_cert (certificate , subject )
220
+ print (f"Downloaded PAA certificate with subject: { subject } " )
221
+ write_cert (certificate , subject )
222
+
223
+ os .chdir (original_dir )
181
224
182
225
183
226
if __name__ == "__main__" :
0 commit comments