OLD | NEW |
---|---|
1 /* ***** BEGIN LICENSE BLOCK ***** | 1 /* ***** BEGIN LICENSE BLOCK ***** |
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
3 * | 3 * |
4 * The contents of this file are subject to the Mozilla Public License Version | 4 * The contents of this file are subject to the Mozilla Public License Version |
5 * 1.1 (the "License"); you may not use this file except in compliance with | 5 * 1.1 (the "License"); you may not use this file except in compliance with |
6 * the License. You may obtain a copy of the License at | 6 * the License. You may obtain a copy of the License at |
7 * http://www.mozilla.org/MPL/ | 7 * http://www.mozilla.org/MPL/ |
8 * | 8 * |
9 * Software distributed under the License is distributed on an "AS IS" basis, | 9 * Software distributed under the License is distributed on an "AS IS" basis, |
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
(...skipping 21 matching lines...) Expand all Loading... | |
32 * decision by deleting the provisions above and replace them with the notice | 32 * decision by deleting the provisions above and replace them with the notice |
33 * and other provisions required by the GPL or the LGPL. If you do not delete | 33 * and other provisions required by the GPL or the LGPL. If you do not delete |
34 * the provisions above, a recipient may use your version of this file under | 34 * the provisions above, a recipient may use your version of this file under |
35 * the terms of any one of the MPL, the GPL or the LGPL. | 35 * the terms of any one of the MPL, the GPL or the LGPL. |
36 * | 36 * |
37 * ***** END LICENSE BLOCK ***** */ | 37 * ***** END LICENSE BLOCK ***** */ |
38 | 38 |
39 #include "net/third_party/mozilla_security_manager/nsNSSCertificateDB.h" | 39 #include "net/third_party/mozilla_security_manager/nsNSSCertificateDB.h" |
40 | 40 |
41 #include <cert.h> | 41 #include <cert.h> |
42 #include <certdb.h> | |
42 #include <pk11pub.h> | 43 #include <pk11pub.h> |
43 #include <secerr.h> | 44 #include <secerr.h> |
44 | 45 |
45 #include "base/logging.h" | 46 #include "base/logging.h" |
46 #include "crypto/nss_util_internal.h" | 47 #include "crypto/nss_util_internal.h" |
47 #include "crypto/scoped_nss_types.h" | 48 #include "crypto/scoped_nss_types.h" |
48 #include "net/base/net_errors.h" | 49 #include "net/base/net_errors.h" |
49 #include "net/base/x509_certificate.h" | 50 #include "net/base/x509_certificate.h" |
50 #include "net/third_party/mozilla_security_manager/nsNSSCertTrust.h" | 51 |
52 #if !defined(CERTDB_TERMINAL_RECORD) | |
53 /* NSS 3.13 renames CERTDB_VALID_PEER to CERTDB_TERMINAL_RECORD | |
54 * and marks CERTDB_VALID_PEER as deprecated. | |
55 * If we're using an older version, rename it ourselves. | |
56 */ | |
57 #define CERTDB_TERMINAL_RECORD CERTDB_VALID_PEER | |
58 #endif | |
51 | 59 |
52 namespace mozilla_security_manager { | 60 namespace mozilla_security_manager { |
53 | 61 |
54 // Based on nsNSSCertificateDB::handleCACertDownload, minus the UI bits. | 62 // Based on nsNSSCertificateDB::handleCACertDownload, minus the UI bits. |
55 bool ImportCACerts(const net::CertificateList& certificates, | 63 bool ImportCACerts(const net::CertificateList& certificates, |
56 net::X509Certificate* root, | 64 net::X509Certificate* root, |
57 net::CertDatabase::TrustBits trustBits, | 65 net::CertDatabase::TrustBits trustBits, |
58 net::CertDatabase::ImportCertFailureList* not_imported) { | 66 net::CertDatabase::ImportCertFailureList* not_imported) { |
67 if (certificates.empty() || !root) | |
68 return false; | |
69 | |
59 crypto::ScopedPK11Slot slot(crypto::GetPublicNSSKeySlot()); | 70 crypto::ScopedPK11Slot slot(crypto::GetPublicNSSKeySlot()); |
60 if (!slot.get()) { | 71 if (!slot.get()) { |
61 LOG(ERROR) << "Couldn't get internal key slot!"; | 72 LOG(ERROR) << "Couldn't get internal key slot!"; |
62 return false; | 73 return false; |
63 } | 74 } |
64 | 75 |
65 // Mozilla had some code here to check if a perm version of the cert exists | 76 // Mozilla had some code here to check if a perm version of the cert exists |
66 // already and use that, but CERT_NewTempCertificate actually does that | 77 // already and use that, but CERT_NewTempCertificate actually does that |
67 // itself, so we skip it here. | 78 // itself, so we skip it here. |
68 | 79 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 cert, net::ERR_IMPORT_CA_CERT_FAILED)); | 162 cert, net::ERR_IMPORT_CA_CERT_FAILED)); |
152 } | 163 } |
153 } | 164 } |
154 | 165 |
155 // Any errors importing individual certs will be in listed in |not_imported|. | 166 // Any errors importing individual certs will be in listed in |not_imported|. |
156 return true; | 167 return true; |
157 } | 168 } |
158 | 169 |
159 // Based on nsNSSCertificateDB::ImportServerCertificate. | 170 // Based on nsNSSCertificateDB::ImportServerCertificate. |
160 bool ImportServerCert(const net::CertificateList& certificates, | 171 bool ImportServerCert(const net::CertificateList& certificates, |
172 net::CertDatabase::TrustBits trustBits, | |
161 net::CertDatabase::ImportCertFailureList* not_imported) { | 173 net::CertDatabase::ImportCertFailureList* not_imported) { |
174 if (certificates.empty()) | |
175 return false; | |
176 | |
162 crypto::ScopedPK11Slot slot(crypto::GetPublicNSSKeySlot()); | 177 crypto::ScopedPK11Slot slot(crypto::GetPublicNSSKeySlot()); |
163 if (!slot.get()) { | 178 if (!slot.get()) { |
164 LOG(ERROR) << "Couldn't get internal key slot!"; | 179 LOG(ERROR) << "Couldn't get internal key slot!"; |
165 return false; | 180 return false; |
166 } | 181 } |
167 | 182 |
168 for (size_t i = 0; i < certificates.size(); ++i) { | 183 for (size_t i = 0; i < certificates.size(); ++i) { |
169 const scoped_refptr<net::X509Certificate>& cert = certificates[i]; | 184 const scoped_refptr<net::X509Certificate>& cert = certificates[i]; |
170 | 185 |
171 // Mozilla uses CERT_ImportCerts, which doesn't take a slot arg. We use | 186 // Mozilla uses CERT_ImportCerts, which doesn't take a slot arg. We use |
172 // PK11_ImportCert instead. | 187 // PK11_ImportCert instead. |
173 SECStatus srv = PK11_ImportCert( | 188 SECStatus srv = PK11_ImportCert( |
174 slot.get(), | 189 slot.get(), |
175 cert->os_cert_handle(), | 190 cert->os_cert_handle(), |
176 CK_INVALID_HANDLE, | 191 CK_INVALID_HANDLE, |
177 cert->GetDefaultNickname(net::SERVER_CERT).c_str(), | 192 cert->GetDefaultNickname(net::SERVER_CERT).c_str(), |
178 PR_FALSE /* includeTrust (unused) */); | 193 PR_FALSE /* includeTrust (unused) */); |
179 if (srv != SECSuccess) { | 194 if (srv != SECSuccess) { |
180 LOG(ERROR) << "PK11_ImportCert failed with error " << PORT_GetError(); | 195 LOG(ERROR) << "PK11_ImportCert failed with error " << PORT_GetError(); |
181 not_imported->push_back(net::CertDatabase::ImportCertFailure( | 196 not_imported->push_back(net::CertDatabase::ImportCertFailure( |
182 cert, net::ERR_IMPORT_SERVER_CERT_FAILED)); | 197 cert, net::ERR_IMPORT_SERVER_CERT_FAILED)); |
183 continue; | 198 continue; |
184 } | 199 } |
185 } | 200 } |
186 | 201 |
187 // Set as valid peer, but without any extra trust. | 202 SetCertTrust(certificates[0].get(), net::SERVER_CERT, trustBits); |
188 SetCertTrust(certificates[0].get(), net::SERVER_CERT, | |
189 net::CertDatabase::UNTRUSTED); | |
190 // TODO(mattm): Report SetCertTrust result? Putting in not_imported | 203 // TODO(mattm): Report SetCertTrust result? Putting in not_imported |
191 // wouldn't quite match up since it was imported... | 204 // wouldn't quite match up since it was imported... |
192 | 205 |
193 // Any errors importing individual certs will be in listed in |not_imported|. | 206 // Any errors importing individual certs will be in listed in |not_imported|. |
194 return true; | 207 return true; |
195 } | 208 } |
196 | 209 |
197 // Based on nsNSSCertificateDB::SetCertTrust. | 210 // Based on nsNSSCertificateDB::SetCertTrust. |
198 bool | 211 bool |
199 SetCertTrust(const net::X509Certificate* cert, | 212 SetCertTrust(const net::X509Certificate* cert, |
200 net::CertType type, | 213 net::CertType type, |
201 net::CertDatabase::TrustBits trustBits) | 214 net::CertDatabase::TrustBits trustBits) |
202 { | 215 { |
216 if (trustBits & net::CertDatabase::EXPLICIT_DISTRUST) { | |
217 DCHECK_EQ(trustBits & (net::CertDatabase::TRUSTED_SSL | | |
218 net::CertDatabase::TRUSTED_EMAIL | | |
219 net::CertDatabase::TRUSTED_OBJ_SIGN), 0U); | |
220 } | |
203 SECStatus srv; | 221 SECStatus srv; |
204 nsNSSCertTrust trust; | |
205 CERTCertificate *nsscert = cert->os_cert_handle(); | 222 CERTCertificate *nsscert = cert->os_cert_handle(); |
206 if (type == net::CA_CERT) { | 223 if (type == net::CA_CERT) { |
207 // always start with untrusted and move up | 224 CERTCertTrust trust = {CERTDB_VALID_CA, CERTDB_VALID_CA, CERTDB_VALID_CA}; |
208 trust.SetValidCA(); | 225 if (trustBits & net::CertDatabase::EXPLICIT_DISTRUST) { |
209 trust.AddCATrust(trustBits & net::CertDatabase::TRUSTED_SSL, | 226 trust.sslFlags |= CERTDB_TERMINAL_RECORD; |
210 trustBits & net::CertDatabase::TRUSTED_EMAIL, | 227 trust.emailFlags |= CERTDB_TERMINAL_RECORD; |
211 trustBits & net::CertDatabase::TRUSTED_OBJ_SIGN); | 228 trust.objectSigningFlags |= CERTDB_TERMINAL_RECORD; |
229 } else { | |
230 if (trustBits & net::CertDatabase::TRUSTED_SSL) | |
231 trust.sslFlags |= CERTDB_TRUSTED_CA | CERTDB_TRUSTED_CLIENT_CA; | |
232 if (trustBits & net::CertDatabase::TRUSTED_EMAIL) | |
233 trust.emailFlags |= CERTDB_TRUSTED_CA | CERTDB_TRUSTED_CLIENT_CA; | |
234 if (trustBits & net::CertDatabase::TRUSTED_OBJ_SIGN) | |
235 trust.objectSigningFlags |= CERTDB_TRUSTED_CA | | |
236 CERTDB_TRUSTED_CLIENT_CA; | |
wtc
2012/05/16 23:37:12
Nit: add curly braces around the body of this if s
| |
237 } | |
212 srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), | 238 srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), |
213 nsscert, | 239 nsscert, |
214 trust.GetTrust()); | 240 &trust); |
215 } else if (type == net::SERVER_CERT) { | 241 } else if (type == net::SERVER_CERT) { |
216 // always start with untrusted and move up | 242 CERTCertTrust trust = {0}; |
217 trust.SetValidPeer(); | 243 if (trustBits & net::CertDatabase::EXPLICIT_DISTRUST) { |
218 trust.AddPeerTrust(trustBits & net::CertDatabase::TRUSTED_SSL, 0, 0); | 244 trust.sslFlags |= CERTDB_TERMINAL_RECORD; |
245 trust.emailFlags |= CERTDB_TERMINAL_RECORD; | |
246 trust.objectSigningFlags |= CERTDB_TERMINAL_RECORD; | |
wtc
2012/05/16 23:37:12
It seems that we should not change trust.emailFlag
mattm
2012/05/18 03:40:54
Done.
| |
247 } else if (trustBits & net::CertDatabase::TRUSTED_SSL) { | |
248 trust.sslFlags |= CERTDB_TRUSTED | CERTDB_TERMINAL_RECORD; | |
249 } | |
250 | |
219 srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), | 251 srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), |
220 nsscert, | 252 nsscert, |
221 trust.GetTrust()); | 253 &trust); |
222 } else { | 254 } else { |
223 // ignore user and email/unknown certs | 255 // ignore user and email/unknown certs |
224 return true; | 256 return true; |
225 } | 257 } |
226 if (srv != SECSuccess) | 258 if (srv != SECSuccess) |
227 LOG(ERROR) << "SetCertTrust failed with error " << PORT_GetError(); | 259 LOG(ERROR) << "SetCertTrust failed with error " << PORT_GetError(); |
228 return srv == SECSuccess; | 260 return srv == SECSuccess; |
229 } | 261 } |
230 | 262 |
231 } // namespace mozilla_security_manager | 263 } // namespace mozilla_security_manager |
OLD | NEW |