1 // -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 do_get_profile(); // must be called before getting nsIX509CertDB
9 const certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
13 function load_cert(cert_name, trust_string) {
14 let cert_filename = cert_name + ".pem";
15 return addCertFromFile(
17 "test_cert_trust/" + cert_filename,
22 function setup_basic_trusts(ca_cert, int_cert) {
25 Ci.nsIX509Cert.CA_CERT,
26 Ci.nsIX509CertDB.TRUSTED_SSL | Ci.nsIX509CertDB.TRUSTED_EMAIL
29 certdb.setCertTrust(int_cert, Ci.nsIX509Cert.CA_CERT, 0);
32 async function test_ca_distrust(ee_cert, cert_to_modify_trust, isRootCA) {
33 // On reset most usages are successful
34 await checkCertErrorGeneric(
38 certificateUsageSSLServer
40 await checkCertErrorGeneric(
44 certificateUsageSSLClient
46 await checkCertErrorGeneric(
49 SEC_ERROR_CA_CERT_INVALID,
52 await checkCertErrorGeneric(
56 certificateUsageEmailSigner
58 await checkCertErrorGeneric(
62 certificateUsageEmailRecipient
65 // Test of active distrust. No usage should pass.
66 setCertTrust(cert_to_modify_trust, "p,p,p");
67 await checkCertErrorGeneric(
70 SEC_ERROR_UNTRUSTED_ISSUER,
71 certificateUsageSSLServer
73 await checkCertErrorGeneric(
76 SEC_ERROR_UNTRUSTED_ISSUER,
77 certificateUsageSSLClient
79 await checkCertErrorGeneric(
82 SEC_ERROR_CA_CERT_INVALID,
85 await checkCertErrorGeneric(
88 SEC_ERROR_UNTRUSTED_ISSUER,
89 certificateUsageEmailSigner
91 await checkCertErrorGeneric(
94 SEC_ERROR_UNTRUSTED_ISSUER,
95 certificateUsageEmailRecipient
98 // Trust set to T - trusted CA to issue client certs, where client cert is
100 setCertTrust(cert_to_modify_trust, "T,T,T");
101 await checkCertErrorGeneric(
104 isRootCA ? SEC_ERROR_UNKNOWN_ISSUER : PRErrorCodeSuccess,
105 certificateUsageSSLServer
109 await checkCertErrorGeneric(
112 isRootCA ? SEC_ERROR_UNKNOWN_ISSUER : PRErrorCodeSuccess,
113 certificateUsageSSLClient
116 await checkCertErrorGeneric(
119 SEC_ERROR_CA_CERT_INVALID,
120 certificateUsageSSLCA
123 await checkCertErrorGeneric(
126 isRootCA ? SEC_ERROR_UNKNOWN_ISSUER : PRErrorCodeSuccess,
127 certificateUsageEmailSigner
129 await checkCertErrorGeneric(
132 isRootCA ? SEC_ERROR_UNKNOWN_ISSUER : PRErrorCodeSuccess,
133 certificateUsageEmailRecipient
136 // Now tests on the SSL trust bit
137 setCertTrust(cert_to_modify_trust, "p,C,C");
138 await checkCertErrorGeneric(
141 SEC_ERROR_UNTRUSTED_ISSUER,
142 certificateUsageSSLServer
146 await checkCertErrorGeneric(
150 certificateUsageSSLClient
152 await checkCertErrorGeneric(
155 SEC_ERROR_CA_CERT_INVALID,
156 certificateUsageSSLCA
158 await checkCertErrorGeneric(
162 certificateUsageEmailSigner
164 await checkCertErrorGeneric(
168 certificateUsageEmailRecipient
171 // Inherited trust SSL
172 setCertTrust(cert_to_modify_trust, ",C,C");
173 await checkCertErrorGeneric(
176 isRootCA ? SEC_ERROR_UNKNOWN_ISSUER : PRErrorCodeSuccess,
177 certificateUsageSSLServer
180 await checkCertErrorGeneric(
184 certificateUsageSSLClient
186 await checkCertErrorGeneric(
189 SEC_ERROR_CA_CERT_INVALID,
190 certificateUsageSSLCA
192 await checkCertErrorGeneric(
196 certificateUsageEmailSigner
198 await checkCertErrorGeneric(
202 certificateUsageEmailRecipient
205 // Now tests on the EMAIL trust bit
206 setCertTrust(cert_to_modify_trust, "C,p,C");
207 await checkCertErrorGeneric(
211 certificateUsageSSLServer
213 await checkCertErrorGeneric(
216 SEC_ERROR_UNTRUSTED_ISSUER,
217 certificateUsageSSLClient
219 await checkCertErrorGeneric(
222 SEC_ERROR_CA_CERT_INVALID,
223 certificateUsageSSLCA
225 await checkCertErrorGeneric(
228 SEC_ERROR_UNTRUSTED_ISSUER,
229 certificateUsageEmailSigner
231 await checkCertErrorGeneric(
234 SEC_ERROR_UNTRUSTED_ISSUER,
235 certificateUsageEmailRecipient
238 // inherited EMAIL Trust
239 setCertTrust(cert_to_modify_trust, "C,,C");
240 await checkCertErrorGeneric(
244 certificateUsageSSLServer
246 await checkCertErrorGeneric(
249 isRootCA ? SEC_ERROR_UNKNOWN_ISSUER : PRErrorCodeSuccess,
250 certificateUsageSSLClient
252 await checkCertErrorGeneric(
255 SEC_ERROR_CA_CERT_INVALID,
256 certificateUsageSSLCA
258 await checkCertErrorGeneric(
261 isRootCA ? SEC_ERROR_UNKNOWN_ISSUER : PRErrorCodeSuccess,
262 certificateUsageEmailSigner
264 await checkCertErrorGeneric(
267 isRootCA ? SEC_ERROR_UNKNOWN_ISSUER : PRErrorCodeSuccess,
268 certificateUsageEmailRecipient
272 add_task(async function () {
273 let certList = ["ca", "int", "ee"];
274 let loadedCerts = [];
275 for (let certName of certList) {
276 loadedCerts.push(load_cert(certName, ",,"));
279 let ca_cert = loadedCerts[0];
280 notEqual(ca_cert, null, "CA cert should have successfully loaded");
281 let int_cert = loadedCerts[1];
282 notEqual(int_cert, null, "Intermediate cert should have successfully loaded");
283 let ee_cert = loadedCerts[2];
284 notEqual(ee_cert, null, "EE cert should have successfully loaded");
286 let init_num_trustObj = certdb.countTrustObjects();
287 setup_basic_trusts(ca_cert, int_cert);
288 await test_ca_distrust(ee_cert, ca_cert, true);
290 // testing countTrustObjects(), loaded 2 certs from above code
291 let num_trustObj = certdb.countTrustObjects();
294 init_num_trustObj + 2,
295 "Number of trust objects should be 2"
298 setup_basic_trusts(ca_cert, int_cert);
299 await test_ca_distrust(ee_cert, int_cert, false);
301 // Reset trust to default ("inherit trust")
302 setCertTrust(ca_cert, ",,");
303 setCertTrust(int_cert, ",,");
305 // End-entities can be trust anchors for interoperability with users who
306 // prefer not to build a hierarchy and instead directly trust a particular
307 // server certificate.
308 setCertTrust(ee_cert, "CTu,CTu,CTu");
309 await checkCertErrorGeneric(
313 certificateUsageSSLServer
315 await checkCertErrorGeneric(
319 certificateUsageSSLClient
321 await checkCertErrorGeneric(
325 certificateUsageEmailSigner
327 await checkCertErrorGeneric(
331 certificateUsageEmailRecipient