diff options
author | rsleevi@chromium.org <rsleevi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-20 00:17:20 +0000 |
---|---|---|
committer | rsleevi@chromium.org <rsleevi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-20 00:17:20 +0000 |
commit | dde7cf8802c6951d39f8bcdc77296b0654e87a23 (patch) | |
tree | aef652180c48a4e80d38cb9e31a604e2b317ef6b /net/base | |
parent | 7cb128c307edf2b1842980e0b6d0d17b3927091a (diff) | |
download | chromium_src-dde7cf8802c6951d39f8bcdc77296b0654e87a23.zip chromium_src-dde7cf8802c6951d39f8bcdc77296b0654e87a23.tar.gz chromium_src-dde7cf8802c6951d39f8bcdc77296b0654e87a23.tar.bz2 |
Add basic support for parsing SSL/TLS cipher suite strings.
BUG=58831
TEST=net_unittests --gtest_filter=CipherSuiteNamesTest.Parse*
Review URL: http://codereview.chromium.org/7396014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@93134 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/ssl_cipher_suite_names.cc | 16 | ||||
-rw-r--r-- | net/base/ssl_cipher_suite_names.h | 17 | ||||
-rw-r--r-- | net/base/ssl_cipher_suite_names_unittest.cc | 26 |
3 files changed, 57 insertions, 2 deletions
diff --git a/net/base/ssl_cipher_suite_names.cc b/net/base/ssl_cipher_suite_names.cc index eb6fe46..b91bd0c 100644 --- a/net/base/ssl_cipher_suite_names.cc +++ b/net/base/ssl_cipher_suite_names.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -7,6 +7,8 @@ #include <stdlib.h> #include "base/logging.h" +#include "base/string_number_conversions.h" +#include "base/string_util.h" #include "net/base/ssl_connection_status_flags.h" // Rather than storing the names of all the ciphersuites we eliminate the @@ -372,4 +374,16 @@ void SSLVersionToString(const char** name, int ssl_version) { } } +bool ParseSSLCipherString(const std::string& cipher_string, + uint16* cipher_suite) { + int value = 0; + if (cipher_string.size() == 6 && + StartsWithASCII(cipher_string, "0x", false /* case insensitive */) && + base::HexStringToInt(cipher_string, &value)) { + *cipher_suite = static_cast<uint16>(value); + return true; + } + return false; +} + } // namespace net diff --git a/net/base/ssl_cipher_suite_names.h b/net/base/ssl_cipher_suite_names.h index deec529..f97b17f 100644 --- a/net/base/ssl_cipher_suite_names.h +++ b/net/base/ssl_cipher_suite_names.h @@ -6,6 +6,8 @@ #define NET_BASE_SSL_CIPHER_SUITE_NAMES_H_ #pragma once +#include <string> + #include "base/basictypes.h" #include "net/base/net_api.h" @@ -34,6 +36,21 @@ NET_API void SSLCompressionToString(const char** name, // If the version is unknown, |name| is set to "???". NET_API void SSLVersionToString(const char** name, int ssl_version); +// Parses a string literal that represents a SSL/TLS cipher suite. +// +// Supported literal forms: +// 0xAABB, where AA is cipher_suite[0] and BB is cipher_suite[1], as +// defined in RFC 2246, Section 7.4.1.2. Unrecognized but parsable cipher +// suites in this form will not return an error. +// +// Returns true if the cipher suite was successfully parsed, storing the +// result in |cipher_suite|. +// +// TODO(rsleevi): Support the full strings defined in the IANA TLS parameters +// list. +NET_API bool ParseSSLCipherString(const std::string& cipher_string, + uint16* cipher_suite); + } // namespace net #endif // NET_BASE_SSL_CIPHER_SUITE_NAMES_H_ diff --git a/net/base/ssl_cipher_suite_names_unittest.cc b/net/base/ssl_cipher_suite_names_unittest.cc index 3a9c2ee..03ceef6 100644 --- a/net/base/ssl_cipher_suite_names_unittest.cc +++ b/net/base/ssl_cipher_suite_names_unittest.cc @@ -1,8 +1,10 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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/ssl_cipher_suite_names.h" + +#include "base/basictypes.h" #include "testing/gtest/include/gtest/gtest.h" namespace net { @@ -22,6 +24,28 @@ TEST(CipherSuiteNamesTest, Basic) { EXPECT_STREQ(mac, "???"); } +TEST(CipherSuiteNamesTest, ParseSSLCipherString) { + uint16 cipher_suite = 0; + EXPECT_TRUE(ParseSSLCipherString("0x0004", &cipher_suite)); + EXPECT_EQ(0x00004u, cipher_suite); + + EXPECT_TRUE(ParseSSLCipherString("0xBEEF", &cipher_suite)); + EXPECT_EQ(0xBEEFu, cipher_suite); +} + +TEST(CipherSuiteNamesTest, ParseSSLCipherStringFails) { + const char* const cipher_strings[] = { + "0004", + "0x004", + "0xBEEFY", + }; + + for (size_t i = 0; i < arraysize(cipher_strings); ++i) { + uint16 cipher_suite = 0; + EXPECT_FALSE(ParseSSLCipherString(cipher_strings[i], &cipher_suite)); + } +} + } // anonymous namespace } // namespace net |