summaryrefslogtreecommitdiffstats
path: root/net/base
diff options
context:
space:
mode:
authorwtc@chromium.org <wtc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-23 16:58:37 +0000
committerwtc@chromium.org <wtc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-23 16:58:37 +0000
commita755e1076f116a71e2888eeea635082baabc69ce (patch)
tree2dde81e236a55d88d7768495e78194df72a59550 /net/base
parenta5684464f46687c95ff8e62b48de4d09b914873d (diff)
downloadchromium_src-a755e1076f116a71e2888eeea635082baabc69ce.zip
chromium_src-a755e1076f116a71e2888eeea635082baabc69ce.tar.gz
chromium_src-a755e1076f116a71e2888eeea635082baabc69ce.tar.bz2
Adds support for the <keygen> tag for client certificate enrollment
under Linux. Currently, no notifications are given to the user that the certificate was successfully enrolled. Patch by Gaurav Shah <gauravsh@chromium.org> of Google. Original review URL: http://codereview.chromium.org/261035 BUG=148 TEST=Can test on the following sites: http://foaf.me/simple_KEYGEN_CreateClientCertificate.php http://www.myopenid.com Review URL: http://codereview.chromium.org/271112 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29900 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r--net/base/cert_database.h34
-rw-r--r--net/base/cert_database_mac.cc24
-rw-r--r--net/base/cert_database_nss.cc102
-rw-r--r--net/base/cert_database_win.cc24
-rw-r--r--net/base/keygen_handler.h27
-rw-r--r--net/base/keygen_handler_mac.cc23
-rw-r--r--net/base/keygen_handler_nss.cc255
-rw-r--r--net/base/keygen_handler_win.cc23
-rw-r--r--net/base/mime_util.cc1
9 files changed, 513 insertions, 0 deletions
diff --git a/net/base/cert_database.h b/net/base/cert_database.h
new file mode 100644
index 0000000..e78c22e
--- /dev/null
+++ b/net/base/cert_database.h
@@ -0,0 +1,34 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_BASE_CERT_DATABASE_H_
+#define NET_BASE_CERT_DATABASE_H_
+
+#include "net/base/x509_certificate.h"
+
+namespace net {
+
+// This class provides functions to manipulate the local
+// certificate store.
+
+// TODO(gauravsh): This class could be augmented with methods
+// for all operations that manipulate the underlying system
+// certificate store.
+
+class CertDatabase {
+ public:
+ CertDatabase();
+
+ // Extract and Store User (Client) Certificate from a data blob.
+ // Return true if successful.
+ bool AddUserCert(const char* data, int len);
+
+ private:
+ void Init();
+ DISALLOW_COPY_AND_ASSIGN(CertDatabase);
+};
+
+} // namespace net
+
+#endif // NET_BASE_CERT_DATABASE_H_
diff --git a/net/base/cert_database_mac.cc b/net/base/cert_database_mac.cc
new file mode 100644
index 0000000..5caf9db
--- /dev/null
+++ b/net/base/cert_database_mac.cc
@@ -0,0 +1,24 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/cert_database.h"
+
+#include "base/logging.h"
+
+namespace net {
+
+CertDatabase::CertDatabase() {
+ NOTIMPLEMENTED();
+}
+
+bool CertDatabase::AddUserCert(const char* data, int len) {
+ NOTIMPLEMENTED();
+ return false;
+}
+
+void CertDatabase::Init() {
+ NOTIMPLEMENTED();
+}
+
+} // namespace net
diff --git a/net/base/cert_database_nss.cc b/net/base/cert_database_nss.cc
new file mode 100644
index 0000000..5f43fef
--- /dev/null
+++ b/net/base/cert_database_nss.cc
@@ -0,0 +1,102 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/cert_database.h"
+
+// Work around https://bugzilla.mozilla.org/show_bug.cgi?id=455424
+// until NSS 3.12.2 comes out and we update to it.
+#define Lock FOO_NSS_Lock
+#include <pk11pub.h>
+#include <secmod.h>
+#include <ssl.h>
+#include <nssb64.h> // NSSBase64_EncodeItem()
+#include <secder.h> // DER_Encode()
+#include <cryptohi.h> // SEC_DerSignData()
+#include <keyhi.h> // SECKEY_CreateSubjectPublicKeyInfo()
+#undef Lock
+
+#include "base/logging.h"
+#include "base/scoped_ptr.h"
+#include "base/nss_init.h"
+
+namespace net {
+
+CertDatabase::CertDatabase() {
+ Init();
+}
+
+bool CertDatabase::AddUserCert(const char* data, int len) {
+ CERTCertificate* cert = NULL;
+ PK11SlotInfo* slot = NULL;
+ std::string nickname;
+ bool is_success = true;
+
+ // Make a copy of "data" since CERT_DecodeCertPackage
+ // might modify it.
+ char* data_copy = new char[len];
+ memcpy(data_copy, data, len);
+
+ // Parse into a certificate structure.
+ cert = CERT_DecodeCertFromPackage(data_copy, len);
+ delete [] data_copy;
+ if (!cert) {
+ LOG(ERROR) << "Couldn't create a temporary certificate";
+ return false;
+ }
+
+ // Check if the private key corresponding to the certificate exist
+ // We shouldn't accept any random client certificate sent by a CA.
+
+ // Note: The NSS source documentation wrongly suggests that this
+ // also imports the certificate if the private key exists. This
+ // doesn't seem to be the case.
+
+ slot = PK11_KeyForCertExists(cert, NULL, NULL);
+ if (!slot) {
+ LOG(ERROR) << "No corresponding private key in store";
+ CERT_DestroyCertificate(cert);
+ return false;
+ }
+ PK11_FreeSlot(slot);
+ slot = NULL;
+
+ // TODO(gauravsh): We also need to make sure another certificate
+ // doesn't already exist for the same private key.
+
+ // Create a nickname for this certificate.
+ // We use the scheme used by Firefox:
+ // --> <subject's common name>'s <issuer's common name> ID.
+ //
+
+ std::string username, ca_name;
+ char* temp_username = CERT_GetCommonName(&cert->subject);
+ char* temp_ca_name = CERT_GetCommonName(&cert->issuer);
+ if (temp_username) {
+ username = temp_username;
+ PORT_Free(temp_username);
+ }
+ if (temp_ca_name) {
+ ca_name = temp_ca_name;
+ PORT_Free(temp_ca_name);
+ }
+ nickname = username + "'s " + ca_name + " ID";
+
+ slot = PK11_ImportCertForKey(cert,
+ const_cast<char*>(nickname.c_str()),
+ NULL);
+ if (slot) {
+ PK11_FreeSlot(slot);
+ } else {
+ LOG(ERROR) << "Couldn't import user certificate.";
+ is_success = false;
+ }
+ CERT_DestroyCertificate(cert);
+ return is_success;
+}
+
+void CertDatabase::Init() {
+ base::EnsureNSSInit();
+}
+
+} // namespace net
diff --git a/net/base/cert_database_win.cc b/net/base/cert_database_win.cc
new file mode 100644
index 0000000..5caf9db
--- /dev/null
+++ b/net/base/cert_database_win.cc
@@ -0,0 +1,24 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/cert_database.h"
+
+#include "base/logging.h"
+
+namespace net {
+
+CertDatabase::CertDatabase() {
+ NOTIMPLEMENTED();
+}
+
+bool CertDatabase::AddUserCert(const char* data, int len) {
+ NOTIMPLEMENTED();
+ return false;
+}
+
+void CertDatabase::Init() {
+ NOTIMPLEMENTED();
+}
+
+} // namespace net
diff --git a/net/base/keygen_handler.h b/net/base/keygen_handler.h
new file mode 100644
index 0000000..346b577
--- /dev/null
+++ b/net/base/keygen_handler.h
@@ -0,0 +1,27 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_BASE_KEYGEN_HANDLER_H_
+#define NET_BASE_KEYGEN_HANDLER_H_
+
+#include <string>
+
+namespace net {
+
+// This class handles keypair generation for generating client
+// certificates via the Netscape <keygen> tag.
+
+class KeygenHandler {
+ public:
+ KeygenHandler(int key_size_index, const std::string& challenge);
+ std::string GenKeyAndSignChallenge();
+
+ private:
+ int key_size_index_;
+ std::string challenge_;
+};
+
+} // namespace net
+
+#endif // NET_BASE_KEYGEN_HANDLER_H_
diff --git a/net/base/keygen_handler_mac.cc b/net/base/keygen_handler_mac.cc
new file mode 100644
index 0000000..f6b4551
--- /dev/null
+++ b/net/base/keygen_handler_mac.cc
@@ -0,0 +1,23 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/keygen_handler.h"
+
+#include "base/logging.h"
+
+namespace net {
+
+KeygenHandler::KeygenHandler(int key_size_index,
+ const std::string& challenge)
+ : key_size_index_(key_size_index),
+ challenge_(challenge) {
+ NOTIMPLEMENTED();
+}
+
+std::string KeygenHandler::GenKeyAndSignChallenge() {
+ NOTIMPLEMENTED();
+ return std::string();
+}
+
+} // namespace net
diff --git a/net/base/keygen_handler_nss.cc b/net/base/keygen_handler_nss.cc
new file mode 100644
index 0000000..9707dfc
--- /dev/null
+++ b/net/base/keygen_handler_nss.cc
@@ -0,0 +1,255 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/keygen_handler.h"
+
+// Work around https://bugzilla.mozilla.org/show_bug.cgi?id=455424
+// until NSS 3.12.2 comes out and we update to it.
+#define Lock FOO_NSS_Lock
+#include <pk11pub.h>
+#include <secmod.h>
+#include <ssl.h>
+#include <nssb64.h> // NSSBase64_EncodeItem()
+#include <secder.h> // DER_Encode()
+#include <cryptohi.h> // SEC_DerSignData()
+#include <keyhi.h> // SECKEY_CreateSubjectPublicKeyInfo()
+#undef Lock
+
+#include "base/nss_init.h"
+#include "base/logging.h"
+
+namespace net {
+
+const int64 DEFAULT_RSA_PUBLIC_EXPONENT = 0x10001;
+
+// Template for creating the signed public key structure to be sent to the CA.
+DERTemplate SECAlgorithmIDTemplate[] = {
+ { DER_SEQUENCE,
+ 0, NULL, sizeof(SECAlgorithmID) },
+ { DER_OBJECT_ID,
+ offsetof(SECAlgorithmID, algorithm), },
+ { DER_OPTIONAL | DER_ANY,
+ offsetof(SECAlgorithmID, parameters), },
+ { 0, }
+};
+
+DERTemplate CERTSubjectPublicKeyInfoTemplate[] = {
+ { DER_SEQUENCE,
+ 0, NULL, sizeof(CERTSubjectPublicKeyInfo) },
+ { DER_INLINE,
+ offsetof(CERTSubjectPublicKeyInfo, algorithm),
+ SECAlgorithmIDTemplate, },
+ { DER_BIT_STRING,
+ offsetof(CERTSubjectPublicKeyInfo, subjectPublicKey), },
+ { 0, }
+};
+
+DERTemplate CERTPublicKeyAndChallengeTemplate[] = {
+ { DER_SEQUENCE,
+ 0, NULL, sizeof(CERTPublicKeyAndChallenge) },
+ { DER_ANY,
+ offsetof(CERTPublicKeyAndChallenge, spki), },
+ { DER_IA5_STRING,
+ offsetof(CERTPublicKeyAndChallenge, challenge), },
+ { 0, }
+};
+
+// This maps displayed strings indicating level of keysecurity in the <keygen>
+// menu to the key size in bits.
+// TODO(gauravsh): Should this mapping be moved else where?
+int RSAkeySizeMap[] = {2048, 1024};
+
+KeygenHandler::KeygenHandler(int key_size_index,
+ const std::string& challenge)
+ : key_size_index_(key_size_index),
+ challenge_(challenge) {
+}
+
+// This function is largely copied from the Firefox's
+// <keygen> implementation in security/manager/ssl/src/nsKeygenHandler.cpp
+// FIXME(gauravsh): Do we need a copy of the Mozilla license here?
+
+std::string KeygenHandler::GenKeyAndSignChallenge() {
+ // Key pair generation mechanism - only RSA is supported at present.
+ PRUint32 keyGenMechanism = CKM_RSA_PKCS_KEY_PAIR_GEN; // from nss/pkcs11t.h
+ char *keystring = NULL; // Temporary store for result/
+
+ // Temporary structures used for generating the result
+ // in the right format.
+ PK11SlotInfo *slot = NULL;
+ PK11RSAGenParams rsaKeyGenParams; // Keygen parameters.
+ SECOidTag algTag; // used by SEC_DerSignData().
+ SECKEYPrivateKey *privateKey = NULL;
+ SECKEYPublicKey *publicKey = NULL;
+ CERTSubjectPublicKeyInfo *spkInfo = NULL;
+ PRArenaPool *arena = NULL;
+ SECStatus sec_rv =SECFailure;
+ SECItem spkiItem;
+ SECItem pkacItem;
+ SECItem signedItem;
+ CERTPublicKeyAndChallenge pkac;
+ void *keyGenParams;
+ pkac.challenge.data = NULL;
+ bool isSuccess = true; // Set to false as soon as a step fails.
+
+ std::string result_blob; // the result.
+
+ // Ensure NSS is initialized.
+ base::EnsureNSSInit();
+
+ slot = PK11_GetInternalKeySlot();
+ if (!slot) {
+ LOG(ERROR) << "Couldn't get Internal key slot!";
+ isSuccess = false;
+ goto failure;
+ }
+
+ switch (keyGenMechanism) {
+ case CKM_RSA_PKCS_KEY_PAIR_GEN:
+ rsaKeyGenParams.keySizeInBits = RSAkeySizeMap[key_size_index_];
+ rsaKeyGenParams.pe = DEFAULT_RSA_PUBLIC_EXPONENT;
+ keyGenParams = &rsaKeyGenParams;
+
+ algTag = SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION; // from <nss/secoidt.h>.
+ break;
+ default:
+ // TODO(gauravsh): If we ever support other mechanisms,
+ // this can be changed.
+ LOG(ERROR) << "Only RSA keygen mechanism is supported";
+ isSuccess = false;
+ goto failure;
+ break;
+ }
+
+ // Need to make sure that the token was initialized.
+ // Assume a null password.
+ sec_rv = PK11_Authenticate(slot, PR_TRUE, NULL);
+ if (SECSuccess != sec_rv) {
+ LOG(ERROR) << "Couldn't initialze PK11 token!";
+ isSuccess = false;
+ goto failure;
+ }
+
+ LOG(INFO) << "Creating key pair...";
+ privateKey = PK11_GenerateKeyPair(slot,
+ keyGenMechanism,
+ keyGenParams,
+ &publicKey,
+ PR_TRUE, // isPermanent?
+ PR_TRUE, // isSensitive?
+ NULL);
+ LOG(INFO) << "done.";
+
+ if (!privateKey) {
+ LOG(INFO) << "Generation of Keypair failed!";
+ isSuccess = false;
+ goto failure;
+ }
+
+ // The CA expects the signed public key in a specific format
+ // Let's create that now.
+
+ // Create a subject public key info from the public key.
+ spkInfo = SECKEY_CreateSubjectPublicKeyInfo(publicKey);
+ if (!spkInfo) {
+ LOG(ERROR) << "Couldn't create SubjectPublicKeyInfo from public key";
+ isSuccess = false;
+ goto failure;
+ }
+
+ // Temporary work store used by NSS.
+ arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
+ if (!arena) {
+ LOG(ERROR) << "PORT_NewArena: Couldn't allocate memory";
+ isSuccess = false;
+ goto failure;
+ }
+
+ // DER encode the whole subjectPublicKeyInfo.
+ sec_rv = DER_Encode(arena, &spkiItem, CERTSubjectPublicKeyInfoTemplate,
+ spkInfo);
+ if (SECSuccess != sec_rv) {
+ LOG(ERROR) << "Couldn't DER Encode subjectPublicKeyInfo";
+ isSuccess = false;
+ goto failure;
+ }
+
+ // Set up the PublicKeyAndChallenge data structure, then DER encode it.
+ pkac.spki = spkiItem;
+ pkac.challenge.len = challenge_.length();
+ pkac.challenge.data = (unsigned char *)strdup(challenge_.c_str());
+ if (!pkac.challenge.data) {
+ LOG(ERROR) << "Out of memory while making a copy of challenge data";
+ isSuccess = false;
+ goto failure;
+ }
+ sec_rv = DER_Encode(arena, &pkacItem, CERTPublicKeyAndChallengeTemplate,
+ &pkac);
+ if (SECSuccess != sec_rv) {
+ LOG(ERROR) << "Couldn't DER Encode PublicKeyAndChallenge";
+ isSuccess = false;
+ goto failure;
+ }
+
+ // Sign the DER encoded PublicKeyAndChallenge.
+ sec_rv = SEC_DerSignData(arena, &signedItem, pkacItem.data, pkacItem.len,
+ privateKey, algTag);
+ if (SECSuccess != sec_rv) {
+ LOG(ERROR) << "Couldn't sign the DER encoded PublicKeyandChallenge";
+ isSuccess = false;
+ goto failure;
+ }
+
+ // Convert the signed public key and challenge into base64/ascii.
+ keystring = NSSBase64_EncodeItem(arena,
+ NULL, // NSS will allocate a buffer for us.
+ 0,
+ &signedItem);
+ if (!keystring) {
+ LOG(ERROR) << "Couldn't convert signed public key into base64";
+ isSuccess = false;
+ goto failure;
+ }
+
+ result_blob = keystring;
+
+ failure:
+ if (!isSuccess) {
+ LOG(ERROR) << "SSL Keygen failed!";
+ } else {
+ LOG(INFO) << "SSl Keygen succeeded!";
+ }
+
+ // Do cleanups
+ if (privateKey) {
+ // TODO(gauravsh): We still need to maintain the private key because it's
+ // used for certificate enrollment checks.
+
+ // PK11_DestroyTokenObject(privateKey->pkcs11Slot,privateKey->pkcs11ID);
+ // SECKEY_DestroyPrivateKey(privateKey);
+ }
+
+ if (publicKey) {
+ PK11_DestroyTokenObject(publicKey->pkcs11Slot, publicKey->pkcs11ID);
+ }
+ if (spkInfo) {
+ SECKEY_DestroySubjectPublicKeyInfo(spkInfo);
+ }
+ if (publicKey) {
+ SECKEY_DestroyPublicKey(publicKey);
+ }
+ if (arena) {
+ PORT_FreeArena(arena, PR_TRUE);
+ }
+ if (slot != NULL) {
+ PK11_FreeSlot(slot);
+ }
+ if (pkac.challenge.data) {
+ free(pkac.challenge.data);
+ }
+
+ return (isSuccess ? result_blob : std::string());
+}
+
+} // namespace net
diff --git a/net/base/keygen_handler_win.cc b/net/base/keygen_handler_win.cc
new file mode 100644
index 0000000..f6b4551
--- /dev/null
+++ b/net/base/keygen_handler_win.cc
@@ -0,0 +1,23 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/keygen_handler.h"
+
+#include "base/logging.h"
+
+namespace net {
+
+KeygenHandler::KeygenHandler(int key_size_index,
+ const std::string& challenge)
+ : key_size_index_(key_size_index),
+ challenge_(challenge) {
+ NOTIMPLEMENTED();
+}
+
+std::string KeygenHandler::GenKeyAndSignChallenge() {
+ NOTIMPLEMENTED();
+ return std::string();
+}
+
+} // namespace net
diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc
index 0fbb8c3..83e41a9 100644
--- a/net/base/mime_util.cc
+++ b/net/base/mime_util.cc
@@ -233,6 +233,7 @@ static const char* const supported_non_image_types[] = {
"application/xhtml+xml",
"application/rss+xml",
"application/atom+xml",
+ "application/x-x509-user-cert",
"multipart/x-mixed-replace"
};