Skip to content

Commit 012cf44

Browse files
Adam Langleysamuel40791765
authored andcommitted
Use a helper function to implement get_all_foo_names functions.
Saves some duplicated logic. Change-Id: I202fa92a88101f9ad735648bc414ab05752641da Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59685 Commit-Queue: David Benjamin <[email protected]> Reviewed-by: David Benjamin <[email protected]> Reviewed-by: Adam Langley <[email protected]> (cherry picked from commit c215ce7e8230786e0d4ec463d95a9e44af513e6a)
1 parent 9d263ef commit 012cf44

File tree

6 files changed

+58
-59
lines changed

6 files changed

+58
-59
lines changed

include/openssl/span.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ auto MakeConstSpan(const C &c) -> decltype(MakeConstSpan(c.data(), c.size())) {
213213
return MakeConstSpan(c.data(), c.size());
214214
}
215215

216+
template <typename T, size_t size>
217+
Span<const T> MakeConstSpan(T (&array)[size]) {
218+
return array;
219+
}
220+
216221
BSSL_NAMESPACE_END
217222

218223
} // extern C++

ssl/internal.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,28 @@ class GrowableArray {
459459
// CBBFinishArray behaves like |CBB_finish| but stores the result in an Array.
460460
OPENSSL_EXPORT bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out);
461461

462+
// GetAllNames helps to implement |*_get_all_*_names| style functions. It
463+
// writes at most |max_out| string pointers to |out| and returns the number that
464+
// it would have liked to have written. The strings written consist of
465+
// |fixed_names_len| strings from |fixed_names| followed by |objects_len|
466+
// strings taken by projecting |objects| through |name|.
467+
template <typename T, typename Name>
468+
inline size_t GetAllNames(const char **out, size_t max_out,
469+
Span<const char *const> fixed_names, Name(T::*name),
470+
Span<const T> objects) {
471+
auto span = bssl::MakeSpan(out, max_out);
472+
for (size_t i = 0; !span.empty() && i < fixed_names.size(); i++) {
473+
span[0] = fixed_names[i];
474+
span = span.subspan(1);
475+
}
476+
span = span.subspan(0, objects.size());
477+
for (size_t i = 0; i < span.size(); i++) {
478+
span[i] = objects[i].*name;
479+
}
480+
return fixed_names.size() + objects.size();
481+
}
482+
483+
462484
// Protocol versions.
463485
//
464486
// Due to DTLS's historical wire version differences, we maintain two notions of

ssl/ssl_cipher.cc

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,13 +1567,15 @@ uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
15671567
return TLS1_2_VERSION;
15681568
}
15691569

1570+
static const char* kUnknownCipher = "(NONE)";
1571+
15701572
// return the actual cipher being used
15711573
const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
15721574
if (cipher != NULL) {
15731575
return cipher->name;
15741576
}
15751577

1576-
return "(NONE)";
1578+
return kUnknownCipher;
15771579
}
15781580

15791581
const char *SSL_CIPHER_standard_name(const SSL_CIPHER *cipher) {
@@ -1805,23 +1807,11 @@ int SSL_COMP_get_id(const SSL_COMP *comp) { return comp->id; }
18051807
void SSL_COMP_free_compression_methods(void) {}
18061808

18071809
size_t SSL_get_all_cipher_names(const char **out, size_t max_out) {
1808-
auto span = MakeSpan(out, max_out);
1809-
if (!span.empty()) {
1810-
// |SSL_CIPHER_get_name| returns "(NONE)" for null.
1811-
span[0] = "(NONE)";
1812-
span = span.subspan(1);
1813-
}
1814-
span = span.subspan(0, OPENSSL_ARRAY_SIZE(kCiphers));
1815-
for (size_t i = 0; i < span.size(); i++) {
1816-
span[i] = kCiphers[i].name;
1817-
}
1818-
return 1 + OPENSSL_ARRAY_SIZE(kCiphers);
1810+
return GetAllNames(out, max_out, MakeConstSpan(&kUnknownCipher, 1),
1811+
&SSL_CIPHER::name, MakeConstSpan(kCiphers));
18191812
}
18201813

18211814
size_t SSL_get_all_standard_cipher_names(const char **out, size_t max_out) {
1822-
auto span = MakeSpan(out, max_out).subspan(0, OPENSSL_ARRAY_SIZE(kCiphers));
1823-
for (size_t i = 0; i < span.size(); i++) {
1824-
span[i] = kCiphers[i].standard_name;
1825-
}
1826-
return OPENSSL_ARRAY_SIZE(kCiphers);
1815+
return GetAllNames(out, max_out, Span<const char *>(),
1816+
&SSL_CIPHER::standard_name, MakeConstSpan(kCiphers));
18271817
}

ssl/ssl_key_share.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,6 @@ const char* SSL_get_curve_name(uint16_t group_id) {
254254
}
255255

256256
size_t SSL_get_all_curve_names(const char **out, size_t max_out) {
257-
auto span =
258-
MakeSpan(out, max_out).subspan(0, OPENSSL_ARRAY_SIZE(kNamedGroups));
259-
for (size_t i = 0; i < span.size(); i++) {
260-
span[i] = kNamedGroups[i].name;
261-
}
262-
return OPENSSL_ARRAY_SIZE(kNamedGroups);
257+
return GetAllNames(out, max_out, Span<const char *>(), &NamedGroup::name,
258+
MakeConstSpan(kNamedGroups));
263259
}

ssl/ssl_privkey.cc

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,14 @@ void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
512512

513513
static constexpr size_t kMaxSignatureAlgorithmNameLen = 23;
514514

515-
// This was "constexpr" rather than "const", but that triggered a bug in MSVC
516-
// where it didn't pad the strings to the correct length.
517-
static const struct {
515+
struct SignatureAlgorithmName {
518516
uint16_t signature_algorithm;
519517
const char name[kMaxSignatureAlgorithmNameLen];
520-
} kSignatureAlgorithmNames[] = {
518+
};
519+
520+
// This was "constexpr" rather than "const", but that triggered a bug in MSVC
521+
// where it didn't pad the strings to the correct length.
522+
static const SignatureAlgorithmName kSignatureAlgorithmNames[] = {
521523
{SSL_SIGN_RSA_PKCS1_MD5_SHA1, "rsa_pkcs1_md5_sha1"},
522524
{SSL_SIGN_RSA_PKCS1_SHA1, "rsa_pkcs1_sha1"},
523525
{SSL_SIGN_RSA_PKCS1_SHA256, "rsa_pkcs1_sha256"},
@@ -543,6 +545,8 @@ const char *SSL_get_signature_algorithm_name(uint16_t sigalg,
543545
return "ecdsa_sha384";
544546
case SSL_SIGN_ECDSA_SECP521R1_SHA512:
545547
return "ecdsa_sha512";
548+
// If adding more here, also update
549+
// |SSL_get_all_signature_algorithm_names|.
546550
}
547551
}
548552

@@ -556,24 +560,11 @@ const char *SSL_get_signature_algorithm_name(uint16_t sigalg,
556560
}
557561

558562
size_t SSL_get_all_signature_algorithm_names(const char **out, size_t max_out) {
559-
auto span = MakeSpan(out, max_out);
560-
if (!span.empty()) {
561-
span[0] = "ecdsa_sha256";
562-
span = span.subspan(1);
563-
}
564-
if (!span.empty()) {
565-
span[0] = "ecdsa_sha384";
566-
span = span.subspan(1);
567-
}
568-
if (!span.empty()) {
569-
span[0] = "ecdsa_sha512";
570-
span = span.subspan(1);
571-
}
572-
span = span.subspan(0, OPENSSL_ARRAY_SIZE(kSignatureAlgorithmNames));
573-
for (size_t i = 0; i < span.size(); i++) {
574-
span[i] = kSignatureAlgorithmNames[i].name;
575-
}
576-
return 3 + OPENSSL_ARRAY_SIZE(kSignatureAlgorithmNames);
563+
const char *kPredefinedNames[] = {"ecdsa_sha256", "ecdsa_sha384",
564+
"ecdsa_sha512"};
565+
return GetAllNames(out, max_out, MakeConstSpan(kPredefinedNames),
566+
&SignatureAlgorithmName::name,
567+
MakeConstSpan(kSignatureAlgorithmNames));
577568
}
578569

579570
int SSL_get_signature_algorithm_key_type(uint16_t sigalg) {

ssl/ssl_versions.cc

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method,
8585
// The following functions map between API versions and wire versions. The
8686
// public API works on wire versions.
8787

88-
static const struct {
88+
static const char* kUnknownVersion = "unknown";
89+
90+
struct VersionInfo {
8991
uint16_t version;
9092
const char *name;
91-
} kVersionNames[] = {
93+
};
94+
95+
static const VersionInfo kVersionNames[] = {
9296
{TLS1_3_VERSION, "TLSv1.3"},
9397
{TLS1_2_VERSION, "TLSv1.2"},
9498
{TLS1_1_VERSION, "TLSv1.1"},
@@ -103,7 +107,7 @@ static const char *ssl_version_to_string(uint16_t version) {
103107
return v.name;
104108
}
105109
}
106-
return "unknown";
110+
return kUnknownVersion;
107111
}
108112

109113
static uint16_t wire_version_to_api(uint16_t version) {
@@ -383,17 +387,8 @@ const char *SSL_get_version(const SSL *ssl) {
383387
}
384388

385389
size_t SSL_get_all_version_names(const char **out, size_t max_out) {
386-
auto span = MakeSpan(out, max_out);
387-
if (!span.empty()) {
388-
// |ssl_version_to_string| returns "unknown" for unknown versions.
389-
span[0] = "unknown";
390-
span = span.subspan(1);
391-
}
392-
span = span.subspan(0, OPENSSL_ARRAY_SIZE(kVersionNames));
393-
for (size_t i = 0; i < span.size(); i++) {
394-
span[i] = kVersionNames[i].name;
395-
}
396-
return 1 + OPENSSL_ARRAY_SIZE(kVersionNames);
390+
return GetAllNames(out, max_out, MakeConstSpan(&kUnknownVersion, 1),
391+
&VersionInfo::name, MakeConstSpan(kVersionNames));
397392
}
398393

399394
const char *SSL_SESSION_get_version(const SSL_SESSION *session) {

0 commit comments

Comments
 (0)