| 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/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/threading/worker_pool.h" | 13 #include "base/threading/worker_pool.h" |
| 14 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
| 15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "crypto/nss_util.h" | 17 #include "crypto/nss_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 #if defined(USE_NSS) | 20 #if defined(USE_NSS) |
| 21 #include <private/pprthred.h> // PR_DetachThread | 21 #include <private/pprthred.h> // PR_DetachThread |
| 22 |
| 23 #include "crypto/nss_util.h" |
| 24 #include "crypto/nss_util_internal.h" |
| 22 #endif | 25 #endif |
| 23 | 26 |
| 24 namespace net { | 27 namespace net { |
| 25 | 28 |
| 26 namespace { | 29 namespace { |
| 27 | 30 |
| 28 class KeygenHandlerTest : public ::testing::Test { | 31 class KeygenHandlerTest : public ::testing::Test { |
| 29 public: | 32 public: |
| 30 KeygenHandlerTest() {} | 33 KeygenHandlerTest() {} |
| 31 virtual ~KeygenHandlerTest() {} | 34 virtual ~KeygenHandlerTest() {} |
| 32 | 35 |
| 33 virtual void SetUp() { | 36 virtual void SetUp() { |
| 37 #if defined(USE_NSS) |
| 38 crypto::EnsureNSSInit(); |
| 39 #endif |
| 34 #if defined(OS_CHROMEOS) | 40 #if defined(OS_CHROMEOS) |
| 35 crypto::OpenPersistentNSSDB(); | 41 crypto::OpenPersistentNSSDB(); |
| 36 #endif | 42 #endif |
| 37 } | 43 } |
| 38 }; | 44 }; |
| 39 | 45 |
| 40 // Assert that |result| is a valid output for KeygenHandler given challenge | 46 // Assert that |result| is a valid output for KeygenHandler given challenge |
| 41 // string of |challenge|. | 47 // string of |challenge|. |
| 42 void AssertValidSignedPublicKeyAndChallenge(const std::string& result, | 48 void AssertValidSignedPublicKeyAndChallenge(const std::string& result, |
| 43 const std::string& challenge) { | 49 const std::string& challenge) { |
| 44 ASSERT_GT(result.length(), 0U); | 50 ASSERT_GT(result.length(), 0U); |
| 45 | 51 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 68 // Signature Algorithm: md5WithRSAEncryption | 74 // Signature Algorithm: md5WithRSAEncryption |
| 69 // 92:f3:cc:ff:0b:d3:d0:4a:3a:4c:ba:ff:d6:38:7f:a5:4b:b5: ..... | 75 // 92:f3:cc:ff:0b:d3:d0:4a:3a:4c:ba:ff:d6:38:7f:a5:4b:b5: ..... |
| 70 // Signature OK | 76 // Signature OK |
| 71 // | 77 // |
| 72 // The value of |spkac| can be ASN.1-parsed with: | 78 // The value of |spkac| can be ASN.1-parsed with: |
| 73 // openssl asn1parse -inform DER | 79 // openssl asn1parse -inform DER |
| 74 } | 80 } |
| 75 | 81 |
| 76 TEST_F(KeygenHandlerTest, SmokeTest) { | 82 TEST_F(KeygenHandlerTest, SmokeTest) { |
| 77 KeygenHandler handler(768, "some challenge", GURL("http://www.example.com")); | 83 KeygenHandler handler(768, "some challenge", GURL("http://www.example.com")); |
| 84 #if defined(USE_NSS) |
| 85 handler.set_key_slot(crypto::ScopedPK11Slot(crypto::GetPrivateNSSKeySlot())); |
| 86 #endif |
| 78 handler.set_stores_key(false); // Don't leave the key-pair behind | 87 handler.set_stores_key(false); // Don't leave the key-pair behind |
| 79 std::string result = handler.GenKeyAndSignChallenge(); | 88 std::string result = handler.GenKeyAndSignChallenge(); |
| 80 VLOG(1) << "KeygenHandler produced: " << result; | 89 VLOG(1) << "KeygenHandler produced: " << result; |
| 81 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); | 90 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); |
| 82 } | 91 } |
| 83 | 92 |
| 84 void ConcurrencyTestCallback(base::WaitableEvent* event, | 93 void ConcurrencyTestCallback(base::WaitableEvent* event, |
| 85 const std::string& challenge, | 94 const std::string& challenge, |
| 86 std::string* result) { | 95 std::string* result) { |
| 87 // We allow Singleton use on the worker thread here since we use a | 96 // We allow Singleton use on the worker thread here since we use a |
| 88 // WaitableEvent to synchronize, so it's safe. | 97 // WaitableEvent to synchronize, so it's safe. |
| 89 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; | 98 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; |
| 90 KeygenHandler handler(768, challenge, GURL("http://www.example.com")); | 99 KeygenHandler handler(768, challenge, GURL("http://www.example.com")); |
| 100 #if defined(USE_NSS) |
| 101 handler.set_key_slot(crypto::ScopedPK11Slot(crypto::GetPrivateNSSKeySlot())); |
| 102 #endif |
| 91 handler.set_stores_key(false); // Don't leave the key-pair behind. | 103 handler.set_stores_key(false); // Don't leave the key-pair behind. |
| 92 *result = handler.GenKeyAndSignChallenge(); | 104 *result = handler.GenKeyAndSignChallenge(); |
| 93 event->Signal(); | 105 event->Signal(); |
| 94 #if defined(USE_NSS) | 106 #if defined(USE_NSS) |
| 95 // Detach the thread from NSPR. | 107 // Detach the thread from NSPR. |
| 96 // Calling NSS functions attaches the thread to NSPR, which stores | 108 // Calling NSS functions attaches the thread to NSPR, which stores |
| 97 // the NSPR thread ID in thread-specific data. | 109 // the NSPR thread ID in thread-specific data. |
| 98 // The threads in our thread pool terminate after we have called | 110 // The threads in our thread pool terminate after we have called |
| 99 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 111 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
| 100 // segfaults on shutdown when the threads' thread-specific data | 112 // segfaults on shutdown when the threads' thread-specific data |
| (...skipping 24 matching lines...) Expand all Loading... |
| 125 events[i] = NULL; | 137 events[i] = NULL; |
| 126 | 138 |
| 127 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; | 139 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; |
| 128 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); | 140 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); |
| 129 } | 141 } |
| 130 } | 142 } |
| 131 | 143 |
| 132 } // namespace | 144 } // namespace |
| 133 | 145 |
| 134 } // namespace net | 146 } // namespace net |
| OLD | NEW |