| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/keygen_handler.h" | 5 #include "net/base/keygen_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 11 #include "base/location.h" | 12 #include "base/location.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/threading/worker_pool.h" | 15 #include "base/threading/worker_pool.h" |
| 14 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
| 15 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
| 16 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 17 #include "crypto/nss_util.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 #if defined(USE_NSS) | 21 #if defined(USE_NSS) |
| 21 #include <private/pprthred.h> // PR_DetachThread | 22 #include <private/pprthred.h> // PR_DetachThread |
| 23 |
| 24 #include "crypto/nss_util.h" |
| 25 #include "crypto/nss_util_internal.h" |
| 22 #endif | 26 #endif |
| 23 | 27 |
| 24 namespace net { | 28 namespace net { |
| 25 | 29 |
| 26 namespace { | 30 namespace { |
| 27 | 31 |
| 28 class KeygenHandlerTest : public ::testing::Test { | 32 class KeygenHandlerTest : public ::testing::Test { |
| 29 public: | 33 public: |
| 30 KeygenHandlerTest() {} | 34 KeygenHandlerTest() {} |
| 31 virtual ~KeygenHandlerTest() {} | 35 virtual ~KeygenHandlerTest() {} |
| 32 | 36 |
| 33 private: | 37 protected: |
| 34 #if defined(OS_CHROMEOS) && defined(USE_NSS) | 38 void SetUpKeyHandler(KeygenHandler* handler) { |
| 39 #if defined(USE_NSS) |
| 40 crypto::ScopedPK11Slot slot(crypto::GetPersistentNSSKeySlot()); |
| 41 handler->set_key_slot( |
| 42 crypto::ScopedPK11Slot(PK11_ReferenceSlot(slot.get()))); |
| 43 #endif |
| 44 } |
| 45 |
| 46 #if defined(USE_NSS) |
| 35 crypto::ScopedTestNSSDB test_nss_db_; | 47 crypto::ScopedTestNSSDB test_nss_db_; |
| 36 #endif | 48 #endif |
| 37 }; | 49 }; |
| 38 | 50 |
| 39 // Assert that |result| is a valid output for KeygenHandler given challenge | 51 // Assert that |result| is a valid output for KeygenHandler given challenge |
| 40 // string of |challenge|. | 52 // string of |challenge|. |
| 41 void AssertValidSignedPublicKeyAndChallenge(const std::string& result, | 53 void AssertValidSignedPublicKeyAndChallenge(const std::string& result, |
| 42 const std::string& challenge) { | 54 const std::string& challenge) { |
| 43 ASSERT_GT(result.length(), 0U); | 55 ASSERT_GT(result.length(), 0U); |
| 44 | 56 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 67 // Signature Algorithm: md5WithRSAEncryption | 79 // Signature Algorithm: md5WithRSAEncryption |
| 68 // 92:f3:cc:ff:0b:d3:d0:4a:3a:4c:ba:ff:d6:38:7f:a5:4b:b5: ..... | 80 // 92:f3:cc:ff:0b:d3:d0:4a:3a:4c:ba:ff:d6:38:7f:a5:4b:b5: ..... |
| 69 // Signature OK | 81 // Signature OK |
| 70 // | 82 // |
| 71 // The value of |spkac| can be ASN.1-parsed with: | 83 // The value of |spkac| can be ASN.1-parsed with: |
| 72 // openssl asn1parse -inform DER | 84 // openssl asn1parse -inform DER |
| 73 } | 85 } |
| 74 | 86 |
| 75 TEST_F(KeygenHandlerTest, SmokeTest) { | 87 TEST_F(KeygenHandlerTest, SmokeTest) { |
| 76 KeygenHandler handler(768, "some challenge", GURL("http://www.example.com")); | 88 KeygenHandler handler(768, "some challenge", GURL("http://www.example.com")); |
| 89 SetUpKeyHandler(&handler); |
| 77 handler.set_stores_key(false); // Don't leave the key-pair behind | 90 handler.set_stores_key(false); // Don't leave the key-pair behind |
| 78 std::string result = handler.GenKeyAndSignChallenge(); | 91 std::string result = handler.GenKeyAndSignChallenge(); |
| 79 VLOG(1) << "KeygenHandler produced: " << result; | 92 VLOG(1) << "KeygenHandler produced: " << result; |
| 80 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); | 93 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); |
| 81 } | 94 } |
| 82 | 95 |
| 83 void ConcurrencyTestCallback(base::WaitableEvent* event, | 96 void ConcurrencyTestCallback(base::WaitableEvent* event, |
| 84 const std::string& challenge, | 97 scoped_ptr<KeygenHandler> handler, |
| 85 std::string* result) { | 98 std::string* result) { |
| 86 // We allow Singleton use on the worker thread here since we use a | 99 // We allow Singleton use on the worker thread here since we use a |
| 87 // WaitableEvent to synchronize, so it's safe. | 100 // WaitableEvent to synchronize, so it's safe. |
| 88 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; | 101 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; |
| 89 KeygenHandler handler(768, challenge, GURL("http://www.example.com")); | 102 handler->set_stores_key(false); // Don't leave the key-pair behind. |
| 90 handler.set_stores_key(false); // Don't leave the key-pair behind. | 103 *result = handler->GenKeyAndSignChallenge(); |
| 91 *result = handler.GenKeyAndSignChallenge(); | |
| 92 event->Signal(); | 104 event->Signal(); |
| 93 #if defined(USE_NSS) | 105 #if defined(USE_NSS) |
| 94 // Detach the thread from NSPR. | 106 // Detach the thread from NSPR. |
| 95 // Calling NSS functions attaches the thread to NSPR, which stores | 107 // Calling NSS functions attaches the thread to NSPR, which stores |
| 96 // the NSPR thread ID in thread-specific data. | 108 // the NSPR thread ID in thread-specific data. |
| 97 // The threads in our thread pool terminate after we have called | 109 // The threads in our thread pool terminate after we have called |
| 98 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 110 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
| 99 // segfaults on shutdown when the threads' thread-specific data | 111 // segfaults on shutdown when the threads' thread-specific data |
| 100 // destructors run. | 112 // destructors run. |
| 101 PR_DetachThread(); | 113 PR_DetachThread(); |
| 102 #endif | 114 #endif |
| 103 } | 115 } |
| 104 | 116 |
| 105 // We asynchronously generate the keys so as not to hang up the IO thread. This | 117 // We asynchronously generate the keys so as not to hang up the IO thread. This |
| 106 // test tries to catch concurrency problems in the keygen implementation. | 118 // test tries to catch concurrency problems in the keygen implementation. |
| 107 TEST_F(KeygenHandlerTest, ConcurrencyTest) { | 119 TEST_F(KeygenHandlerTest, ConcurrencyTest) { |
| 108 const int NUM_HANDLERS = 5; | 120 const int NUM_HANDLERS = 5; |
| 109 base::WaitableEvent* events[NUM_HANDLERS] = { NULL }; | 121 base::WaitableEvent* events[NUM_HANDLERS] = {NULL}; |
| 110 std::string results[NUM_HANDLERS]; | 122 std::string results[NUM_HANDLERS]; |
| 111 for (int i = 0; i < NUM_HANDLERS; i++) { | 123 for (int i = 0; i < NUM_HANDLERS; i++) { |
| 124 scoped_ptr<KeygenHandler> handler(new KeygenHandler( |
| 125 768, "some challenge", GURL("http://www.example.com"))); |
| 126 SetUpKeyHandler(handler.get()); |
| 112 events[i] = new base::WaitableEvent(false, false); | 127 events[i] = new base::WaitableEvent(false, false); |
| 113 base::WorkerPool::PostTask( | 128 base::WorkerPool::PostTask(FROM_HERE, |
| 114 FROM_HERE, | 129 base::Bind(ConcurrencyTestCallback, |
| 115 base::Bind(ConcurrencyTestCallback, events[i], "some challenge", | 130 events[i], |
| 116 &results[i]), | 131 base::Passed(&handler), |
| 117 true); | 132 &results[i]), |
| 133 true); |
| 118 } | 134 } |
| 119 | 135 |
| 120 for (int i = 0; i < NUM_HANDLERS; i++) { | 136 for (int i = 0; i < NUM_HANDLERS; i++) { |
| 121 // Make sure the job completed | 137 // Make sure the job completed |
| 122 events[i]->Wait(); | 138 events[i]->Wait(); |
| 123 delete events[i]; | 139 delete events[i]; |
| 124 events[i] = NULL; | 140 events[i] = NULL; |
| 125 | 141 |
| 126 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; | 142 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; |
| 127 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); | 143 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); |
| 128 } | 144 } |
| 129 } | 145 } |
| 130 | 146 |
| 131 } // namespace | 147 } // namespace |
| 132 | 148 |
| 133 } // namespace net | 149 } // namespace net |
| OLD | NEW |