1 // -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/.
10 const gCertDB = Cc["@mozilla.org/security/x509certdb;1"].getService(
15 // This certificate has a number of placeholder byte sequences that we can
16 // replace with invalid UTF-8 to ensure that we handle these cases safely.
17 let certificateToAlterFile = do_get_file(
18 "test_cert_utf8/certificateToAlter.pem",
21 let certificateBytesToAlter = atob(
22 pemToBase64(readFile(certificateToAlterFile))
24 testUTF8InField("issuerName", "ISSUER CN", certificateBytesToAlter);
25 testUTF8InField("issuerOrganization", "ISSUER O", certificateBytesToAlter);
27 "issuerOrganizationUnit",
29 certificateBytesToAlter
31 testUTF8InField("issuerCommonName", "ISSUER CN", certificateBytesToAlter);
32 testUTF8InField("organization", "SUBJECT O", certificateBytesToAlter);
33 testUTF8InField("organizationalUnit", "SUBJECT OU", certificateBytesToAlter);
34 testUTF8InField("subjectName", "SUBJECT CN", certificateBytesToAlter);
35 testUTF8InField("displayName", "SUBJECT CN", certificateBytesToAlter);
36 testUTF8InField("commonName", "SUBJECT CN", certificateBytesToAlter);
39 "SUBJECT EMAILADDRESS",
40 certificateBytesToAlter
44 // Every (issuer, serial number) pair must be unique. If NSS ever encounters two
45 // different (in terms of encoding) certificates with the same values for this
46 // pair, it will refuse to import it (even as a temporary certificate). Since
47 // we're creating a number of different certificates, we need to ensure this
48 // pair is always unique. The easiest way to do this is to change the issuer
49 // distinguished name each time. To make sure this doesn't introduce additional
50 // UTF8 issues, always use a printable ASCII value.
51 var gUniqueIssuerCounter = 32;
53 function testUTF8InField(field, replacementPrefix, certificateBytesToAlter) {
54 let toReplace = `${replacementPrefix} REPLACE ME`;
56 for (let i = 0; i < toReplace.length; i++) {
57 replacement += "\xEB";
59 let bytes = certificateBytesToAlter.replace(toReplace, replacement);
60 let uniqueIssuerReplacement =
61 "ALWAYS MAKE ME UNIQU" + String.fromCharCode(gUniqueIssuerCounter);
62 bytes = bytes.replace("ALWAYS MAKE ME UNIQUE", uniqueIssuerReplacement);
66 "should have enough ASCII replacements to make a unique issuer DN"
68 gUniqueIssuerCounter++;
69 let cert = gCertDB.constructX509(stringToArray(bytes));
70 notEqual(cert[field], null, `accessing nsIX509Cert.${field} shouldn't fail`);
72 cert.getEmailAddresses(),
74 "calling nsIX509Cert.getEmailAddresses() shouldn't assert"
77 !cert.containsEmailAddress("test@test.test"),
78 "calling nsIX509Cert.containsEmailAddress() shouldn't assert"