summaryrefslogtreecommitdiffstats
path: root/security/manager/ssl/tests/unit/test_cert_version.js
blob: 063bcb1ccaeff9eb76ba31642ccdb32e58043d5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// Tests the interaction between the basic constraints extension and the
// certificate version field. In general, the testcases consist of verifying
// certificate chains of the form:
//
// end-entity (issued by) intermediate (issued by) trusted X509v3 root
//
// where the intermediate is one of X509 v1, v2, v3, or v4, and either does or
// does not have the basic constraints extension. If it has the extension, it
// either does or does not specify that it is a CA.
//
// To test cases where the trust anchor has a different version and/or does or
// does not have the basic constraint extension, there are testcases where the
// intermediate is trusted as an anchor and the verification is repeated.
// (Loading a certificate with trust "CTu,," means that it is a trust anchor
// for SSL. Loading a certificate with trust ",," means that it inherits its
// trust.)
//
// There are also testcases for end-entities issued by a trusted X509v3 root
// where the end-entities similarly cover the range of versions and basic
// constraint extensions.
//
// Finally, there are testcases for self-signed certificates that, again, cover
// the range of versions and basic constraint extensions.

"use strict";

do_get_profile(); // must be called before getting nsIX509CertDB
const certdb = Cc["@mozilla.org/security/x509certdb;1"]
                 .getService(Ci.nsIX509CertDB);

function certFromFile(certName) {
  return constructCertFromFile("test_cert_version/" + certName + ".pem");
}

function loadCertWithTrust(certName, trustString) {
  addCertFromFile(certdb, "test_cert_version/" + certName + ".pem", trustString);
}

function checkEndEntity(cert, expectedResult) {
  checkCertErrorGeneric(certdb, cert, expectedResult, certificateUsageSSLServer);
}

function checkIntermediate(cert, expectedResult) {
  checkCertErrorGeneric(certdb, cert, expectedResult, certificateUsageSSLCA);
}

// Test that the code that decodes certificates to display them in the
// certificate manager correctly handles the version field.
function checkCertVersion(cert, expectedVersionString) {
  let asn1 = cert.ASN1Structure.QueryInterface(Ci.nsIASN1Sequence);
  let tbsCertificate = asn1.ASN1Objects.queryElementAt(0, Ci.nsIASN1Sequence);
  let version = tbsCertificate.ASN1Objects.queryElementAt(0, Ci.nsIASN1Object);
  equal(version.displayValue, expectedVersionString,
        "Actual and expected version strings should match");
}

function run_test() {
  loadCertWithTrust("ca", "CTu,,");

  // Section for CAs lacking the basicConstraints extension entirely:
  loadCertWithTrust("int-v1-noBC_ca", ",,");
  checkIntermediate(certFromFile("int-v1-noBC_ca"), MOZILLA_PKIX_ERROR_V1_CERT_USED_AS_CA);
  checkEndEntity(certFromFile("ee_int-v1-noBC"), MOZILLA_PKIX_ERROR_V1_CERT_USED_AS_CA);
  // A v1 certificate with no basicConstraints extension may issue certificates
  // if it is a trust anchor.
  loadCertWithTrust("int-v1-noBC_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v1-noBC_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee_int-v1-noBC"), PRErrorCodeSuccess);

  loadCertWithTrust("int-v2-noBC_ca", ",,");
  checkIntermediate(certFromFile("int-v2-noBC_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v2-noBC"), SEC_ERROR_CA_CERT_INVALID);
  loadCertWithTrust("int-v2-noBC_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v2-noBC_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v2-noBC"), SEC_ERROR_CA_CERT_INVALID);

  loadCertWithTrust("int-v3-noBC_ca", ",,");
  checkIntermediate(certFromFile("int-v3-noBC_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v3-noBC"), SEC_ERROR_CA_CERT_INVALID);
  loadCertWithTrust("int-v3-noBC_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v3-noBC_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v3-noBC"), SEC_ERROR_CA_CERT_INVALID);

  loadCertWithTrust("int-v4-noBC_ca", ",,");
  checkIntermediate(certFromFile("int-v4-noBC_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v4-noBC"), SEC_ERROR_CA_CERT_INVALID);
  loadCertWithTrust("int-v4-noBC_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v4-noBC_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v4-noBC"), SEC_ERROR_CA_CERT_INVALID);

  // Section for CAs with basicConstraints not specifying cA:
  loadCertWithTrust("int-v1-BC-not-cA_ca", ",,");
  checkIntermediate(certFromFile("int-v1-BC-not-cA_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v1-BC-not-cA"), SEC_ERROR_CA_CERT_INVALID);
  loadCertWithTrust("int-v1-BC-not-cA_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v1-BC-not-cA_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v1-BC-not-cA"), SEC_ERROR_CA_CERT_INVALID);

  loadCertWithTrust("int-v2-BC-not-cA_ca", ",,");
  checkIntermediate(certFromFile("int-v2-BC-not-cA_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v2-BC-not-cA"), SEC_ERROR_CA_CERT_INVALID);
  loadCertWithTrust("int-v2-BC-not-cA_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v2-BC-not-cA_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v2-BC-not-cA"), SEC_ERROR_CA_CERT_INVALID);

  loadCertWithTrust("int-v3-BC-not-cA_ca", ",,");
  checkIntermediate(certFromFile("int-v3-BC-not-cA_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v3-BC-not-cA"), SEC_ERROR_CA_CERT_INVALID);
  loadCertWithTrust("int-v3-BC-not-cA_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v3-BC-not-cA_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v3-BC-not-cA"), SEC_ERROR_CA_CERT_INVALID);

  loadCertWithTrust("int-v4-BC-not-cA_ca", ",,");
  checkIntermediate(certFromFile("int-v4-BC-not-cA_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v4-BC-not-cA"), SEC_ERROR_CA_CERT_INVALID);
  loadCertWithTrust("int-v4-BC-not-cA_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v4-BC-not-cA_ca"), SEC_ERROR_CA_CERT_INVALID);
  checkEndEntity(certFromFile("ee_int-v4-BC-not-cA"), SEC_ERROR_CA_CERT_INVALID);

  // Section for CAs with basicConstraints specifying cA:
  loadCertWithTrust("int-v1-BC-cA_ca", ",,");
  checkIntermediate(certFromFile("int-v1-BC-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee_int-v1-BC-cA"), PRErrorCodeSuccess);
  loadCertWithTrust("int-v1-BC-cA_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v1-BC-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee_int-v1-BC-cA"), PRErrorCodeSuccess);

  loadCertWithTrust("int-v2-BC-cA_ca", ",,");
  checkIntermediate(certFromFile("int-v2-BC-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee_int-v2-BC-cA"), PRErrorCodeSuccess);
  loadCertWithTrust("int-v2-BC-cA_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v2-BC-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee_int-v2-BC-cA"), PRErrorCodeSuccess);

  loadCertWithTrust("int-v3-BC-cA_ca", ",,");
  checkIntermediate(certFromFile("int-v3-BC-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee_int-v3-BC-cA"), PRErrorCodeSuccess);
  loadCertWithTrust("int-v3-BC-cA_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v3-BC-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee_int-v3-BC-cA"), PRErrorCodeSuccess);

  loadCertWithTrust("int-v4-BC-cA_ca", ",,");
  checkIntermediate(certFromFile("int-v4-BC-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee_int-v4-BC-cA"), PRErrorCodeSuccess);
  loadCertWithTrust("int-v4-BC-cA_ca", "CTu,,");
  checkIntermediate(certFromFile("int-v4-BC-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee_int-v4-BC-cA"), PRErrorCodeSuccess);

  // Section for end-entity certificates with various basicConstraints:
  checkEndEntity(certFromFile("ee-v1-noBC_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee-v2-noBC_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee-v3-noBC_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee-v4-noBC_ca"), PRErrorCodeSuccess);

  checkEndEntity(certFromFile("ee-v1-BC-not-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee-v2-BC-not-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee-v3-BC-not-cA_ca"), PRErrorCodeSuccess);
  checkEndEntity(certFromFile("ee-v4-BC-not-cA_ca"), PRErrorCodeSuccess);

  checkEndEntity(certFromFile("ee-v1-BC-cA_ca"), MOZILLA_PKIX_ERROR_CA_CERT_USED_AS_END_ENTITY);
  checkEndEntity(certFromFile("ee-v2-BC-cA_ca"), MOZILLA_PKIX_ERROR_CA_CERT_USED_AS_END_ENTITY);
  checkEndEntity(certFromFile("ee-v3-BC-cA_ca"), MOZILLA_PKIX_ERROR_CA_CERT_USED_AS_END_ENTITY);
  checkEndEntity(certFromFile("ee-v4-BC-cA_ca"), MOZILLA_PKIX_ERROR_CA_CERT_USED_AS_END_ENTITY);

  // Section for self-signed certificates:
  checkEndEntity(certFromFile("ss-v1-noBC"), SEC_ERROR_UNKNOWN_ISSUER);
  checkEndEntity(certFromFile("ss-v2-noBC"), SEC_ERROR_UNKNOWN_ISSUER);
  checkEndEntity(certFromFile("ss-v3-noBC"), SEC_ERROR_UNKNOWN_ISSUER);
  checkEndEntity(certFromFile("ss-v4-noBC"), SEC_ERROR_UNKNOWN_ISSUER);

  checkEndEntity(certFromFile("ss-v1-BC-not-cA"), SEC_ERROR_UNKNOWN_ISSUER);
  checkEndEntity(certFromFile("ss-v2-BC-not-cA"), SEC_ERROR_UNKNOWN_ISSUER);
  checkEndEntity(certFromFile("ss-v3-BC-not-cA"), SEC_ERROR_UNKNOWN_ISSUER);
  checkEndEntity(certFromFile("ss-v4-BC-not-cA"), SEC_ERROR_UNKNOWN_ISSUER);

  checkEndEntity(certFromFile("ss-v1-BC-cA"), SEC_ERROR_UNKNOWN_ISSUER);
  checkEndEntity(certFromFile("ss-v2-BC-cA"), SEC_ERROR_UNKNOWN_ISSUER);
  checkEndEntity(certFromFile("ss-v3-BC-cA"), SEC_ERROR_UNKNOWN_ISSUER);
  checkEndEntity(certFromFile("ss-v4-BC-cA"), SEC_ERROR_UNKNOWN_ISSUER);

  checkCertVersion(certFromFile("ss-v1-noBC"), "Version 1");
  checkCertVersion(certFromFile("int-v2-BC-cA_ca"), "Version 2");
  checkCertVersion(certFromFile("ee-v3-BC-not-cA_ca"), "Version 3");
  checkCertVersion(certFromFile("int-v4-BC-not-cA_ca"), "Version 4");
}