| 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) { |
| 59 crypto::ScopedPK11Slot slot(crypto::GetPublicNSSKeySlot()); | 67 crypto::ScopedPK11Slot slot(crypto::GetPublicNSSKeySlot()); |
| 60 if (!slot.get()) { | 68 if (!slot.get()) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 cert, net::ERR_IMPORT_CA_CERT_FAILED)); | 159 cert, net::ERR_IMPORT_CA_CERT_FAILED)); |
| 152 } | 160 } |
| 153 } | 161 } |
| 154 | 162 |
| 155 // Any errors importing individual certs will be in listed in |not_imported|. | 163 // Any errors importing individual certs will be in listed in |not_imported|. |
| 156 return true; | 164 return true; |
| 157 } | 165 } |
| 158 | 166 |
| 159 // Based on nsNSSCertificateDB::ImportServerCertificate. | 167 // Based on nsNSSCertificateDB::ImportServerCertificate. |
| 160 bool ImportServerCert(const net::CertificateList& certificates, | 168 bool ImportServerCert(const net::CertificateList& certificates, |
| 169 net::CertDatabase::TrustBits trustBits, |
| 161 net::CertDatabase::ImportCertFailureList* not_imported) { | 170 net::CertDatabase::ImportCertFailureList* not_imported) { |
| 162 crypto::ScopedPK11Slot slot(crypto::GetPublicNSSKeySlot()); | 171 crypto::ScopedPK11Slot slot(crypto::GetPublicNSSKeySlot()); |
| 163 if (!slot.get()) { | 172 if (!slot.get()) { |
| 164 LOG(ERROR) << "Couldn't get internal key slot!"; | 173 LOG(ERROR) << "Couldn't get internal key slot!"; |
| 165 return false; | 174 return false; |
| 166 } | 175 } |
| 167 | 176 |
| 168 for (size_t i = 0; i < certificates.size(); ++i) { | 177 for (size_t i = 0; i < certificates.size(); ++i) { |
| 169 const scoped_refptr<net::X509Certificate>& cert = certificates[i]; | 178 const scoped_refptr<net::X509Certificate>& cert = certificates[i]; |
| 170 | 179 |
| 171 // Mozilla uses CERT_ImportCerts, which doesn't take a slot arg. We use | 180 // Mozilla uses CERT_ImportCerts, which doesn't take a slot arg. We use |
| 172 // PK11_ImportCert instead. | 181 // PK11_ImportCert instead. |
| 173 SECStatus srv = PK11_ImportCert( | 182 SECStatus srv = PK11_ImportCert( |
| 174 slot.get(), | 183 slot.get(), |
| 175 cert->os_cert_handle(), | 184 cert->os_cert_handle(), |
| 176 CK_INVALID_HANDLE, | 185 CK_INVALID_HANDLE, |
| 177 cert->GetDefaultNickname(net::SERVER_CERT).c_str(), | 186 cert->GetDefaultNickname(net::SERVER_CERT).c_str(), |
| 178 PR_FALSE /* includeTrust (unused) */); | 187 PR_FALSE /* includeTrust (unused) */); |
| 179 if (srv != SECSuccess) { | 188 if (srv != SECSuccess) { |
| 180 LOG(ERROR) << "PK11_ImportCert failed with error " << PORT_GetError(); | 189 LOG(ERROR) << "PK11_ImportCert failed with error " << PORT_GetError(); |
| 181 not_imported->push_back(net::CertDatabase::ImportCertFailure( | 190 not_imported->push_back(net::CertDatabase::ImportCertFailure( |
| 182 cert, net::ERR_IMPORT_SERVER_CERT_FAILED)); | 191 cert, net::ERR_IMPORT_SERVER_CERT_FAILED)); |
| 183 continue; | 192 continue; |
| 184 } | 193 } |
| 185 } | 194 } |
| 186 | 195 |
| 187 // Set as valid peer, but without any extra trust. | 196 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 | 197 // TODO(mattm): Report SetCertTrust result? Putting in not_imported |
| 191 // wouldn't quite match up since it was imported... | 198 // wouldn't quite match up since it was imported... |
| 192 | 199 |
| 193 // Any errors importing individual certs will be in listed in |not_imported|. | 200 // Any errors importing individual certs will be in listed in |not_imported|. |
| 194 return true; | 201 return true; |
| 195 } | 202 } |
| 196 | 203 |
| 197 // Based on nsNSSCertificateDB::SetCertTrust. | 204 // Based on nsNSSCertificateDB::SetCertTrust. |
| 198 bool | 205 bool |
| 199 SetCertTrust(const net::X509Certificate* cert, | 206 SetCertTrust(const net::X509Certificate* cert, |
| 200 net::CertType type, | 207 net::CertType type, |
| 201 net::CertDatabase::TrustBits trustBits) | 208 net::CertDatabase::TrustBits trustBits) |
| 202 { | 209 { |
| 210 if (trustBits & net::CertDatabase::EXPLICIT_DISTRUST) { |
| 211 DCHECK_EQ(trustBits & (net::CertDatabase::TRUSTED_SSL | |
| 212 net::CertDatabase::TRUSTED_EMAIL | |
| 213 net::CertDatabase::TRUSTED_OBJ_SIGN), 0U); |
| 214 } |
| 203 SECStatus srv; | 215 SECStatus srv; |
| 204 nsNSSCertTrust trust; | |
| 205 CERTCertificate *nsscert = cert->os_cert_handle(); | 216 CERTCertificate *nsscert = cert->os_cert_handle(); |
| 206 if (type == net::CA_CERT) { | 217 if (type == net::CA_CERT) { |
| 207 // always start with untrusted and move up | 218 CERTCertTrust trust = {CERTDB_VALID_CA, CERTDB_VALID_CA, CERTDB_VALID_CA}; |
| 208 trust.SetValidCA(); | 219 if (trustBits & net::CertDatabase::EXPLICIT_DISTRUST) { |
| 209 trust.AddCATrust(trustBits & net::CertDatabase::TRUSTED_SSL, | 220 trust.sslFlags |= CERTDB_TERMINAL_RECORD; |
| 210 trustBits & net::CertDatabase::TRUSTED_EMAIL, | 221 trust.emailFlags |= CERTDB_TERMINAL_RECORD; |
| 211 trustBits & net::CertDatabase::TRUSTED_OBJ_SIGN); | 222 trust.objectSigningFlags |= CERTDB_TERMINAL_RECORD; |
| 223 } else { |
| 224 if (trustBits & net::CertDatabase::TRUSTED_SSL) |
| 225 trust.sslFlags |= CERTDB_TRUSTED_CA | CERTDB_TRUSTED_CLIENT_CA; |
| 226 if (trustBits & net::CertDatabase::TRUSTED_EMAIL) |
| 227 trust.emailFlags |= CERTDB_TRUSTED_CA | CERTDB_TRUSTED_CLIENT_CA; |
| 228 if (trustBits & net::CertDatabase::TRUSTED_OBJ_SIGN) |
| 229 trust.objectSigningFlags |= CERTDB_TRUSTED_CA | |
| 230 CERTDB_TRUSTED_CLIENT_CA; |
| 231 } |
| 212 srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), | 232 srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), |
| 213 nsscert, | 233 nsscert, |
| 214 trust.GetTrust()); | 234 &trust); |
| 215 } else if (type == net::SERVER_CERT) { | 235 } else if (type == net::SERVER_CERT) { |
| 216 // always start with untrusted and move up | 236 CERTCertTrust trust = {0}; |
| 217 trust.SetValidPeer(); | 237 if (trustBits & net::CertDatabase::EXPLICIT_DISTRUST) { |
| 218 trust.AddPeerTrust(trustBits & net::CertDatabase::TRUSTED_SSL, 0, 0); | 238 trust.sslFlags |= CERTDB_TERMINAL_RECORD; |
| 239 trust.emailFlags |= CERTDB_TERMINAL_RECORD; |
| 240 trust.objectSigningFlags |= CERTDB_TERMINAL_RECORD; |
| 241 } else if (trustBits & net::CertDatabase::TRUSTED_SSL) { |
| 242 trust.sslFlags |= CERTDB_TRUSTED | CERTDB_TERMINAL_RECORD; |
| 243 } |
| 244 |
| 219 srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), | 245 srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), |
| 220 nsscert, | 246 nsscert, |
| 221 trust.GetTrust()); | 247 &trust); |
| 222 } else { | 248 } else { |
| 223 // ignore user and email/unknown certs | 249 // ignore user and email/unknown certs |
| 224 return true; | 250 return true; |
| 225 } | 251 } |
| 226 if (srv != SECSuccess) | 252 if (srv != SECSuccess) |
| 227 LOG(ERROR) << "SetCertTrust failed with error " << PORT_GetError(); | 253 LOG(ERROR) << "SetCertTrust failed with error " << PORT_GetError(); |
| 228 return srv == SECSuccess; | 254 return srv == SECSuccess; |
| 229 } | 255 } |
| 230 | 256 |
| 231 } // namespace mozilla_security_manager | 257 } // namespace mozilla_security_manager |
| OLD | NEW |