Skip to content

Commit 2a84d5b

Browse files
Tool util functions in tool_util.cc (#2778)
### Description of changes: Begin some refactoring. Move tool utility functions into tool_util.cc. * Set umask when creating private keys. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license. --------- Co-authored-by: Will Childs-Klein <[email protected]>
1 parent 87208e9 commit 2a84d5b

File tree

8 files changed

+41
-20
lines changed

8 files changed

+41
-20
lines changed

tool-openssl/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ add_executable(
66
../tool/fd.cc
77
../tool/client.cc
88
../tool/transport_common.cc
9-
9+
1010
crl.cc
1111
dgst.cc
1212
ecparam.cc
@@ -22,6 +22,7 @@ add_executable(
2222
rsa.cc
2323
s_client.cc
2424
tool.cc
25+
tool_util.cc
2526
verify.cc
2627
version.cc
2728
x509.cc
@@ -87,17 +88,17 @@ if(BUILD_TESTING)
8788
../tool/client.cc
8889
../tool/transport_common.cc
8990

90-
test_util.cc
9191
crl.cc
9292
crl_test.cc
9393
dgst.cc
9494
dgst_test.cc
95-
ecparam.cc
96-
ecparam_test.cc
9795
ec.cc
9896
ec_test.cc
97+
ecparam.cc
98+
ecparam_test.cc
9999
genrsa.cc
100100
genrsa_test.cc
101+
ordered_args.cc
101102
pass_util.cc
102103
pass_util_test.cc
103104
pkcs8.cc
@@ -113,7 +114,8 @@ if(BUILD_TESTING)
113114
rsa.cc
114115
rsa_test.cc
115116
s_client.cc
116-
ordered_args.cc
117+
test_util.cc
118+
tool_util.cc
117119
verify.cc
118120
verify_test.cc
119121
x509.cc

tool-openssl/ecparam.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,17 @@ bool ecparamTool(const args_list_t &args) {
144144
fprintf(stderr, "Failed to create EC key for curve\n");
145145
goto err;
146146
}
147-
147+
148148
if (!EC_KEY_generate_key(eckey.get())) {
149149
fprintf(stderr, "Failed to generate EC key\n");
150150
goto err;
151151
}
152-
152+
153153
// Set point conversion form on the key
154154
EC_KEY_set_conv_form(eckey.get(), point_form);
155-
155+
156156
if (!noout) {
157+
SetUmaskForPrivateKey();
157158
if (output_format == FORMAT_PEM) {
158159
if (!PEM_write_bio_ECPrivateKey(out_bio.get(), eckey.get(), nullptr, nullptr, 0, nullptr, nullptr)) {
159160
fprintf(stderr, "Failed to write private key in PEM format\n");

tool-openssl/genrsa.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static bssl::UniquePtr<BIO> CreateOutputBIO(const std::string &out_path) {
9696
return nullptr;
9797
}
9898
} else {
99+
SetUmaskForPrivateKey();
99100
bio.reset(BIO_new_file(out_path.c_str(), "wb"));
100101
if (!bio) {
101102
fprintf(stderr, "Error: Could not open output file '%s'\n",
@@ -203,7 +204,7 @@ bool genrsaTool(const args_list_t &args) {
203204
// Write the key with optional encryption
204205
password = (!passout_arg->empty()) ? passout_arg->c_str() : NULL;
205206
password_len = (!passout_arg->empty()) ? static_cast<int>(passout_arg->length()) : 0;
206-
207+
207208
if (!PEM_write_bio_PrivateKey(bio.get(), pkey.get(), cipher,
208209
(unsigned char*)password, password_len,
209210
NULL, NULL)) {

tool-openssl/internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include <openssl/digest.h>
88
#include <algorithm>
9-
#include <memory>
109
#include <string>
1110
#include <utility>
1211
#include <vector>
@@ -196,4 +195,6 @@ bool GetExclusiveBoolArgument(std::string *out_arg, const argument_t *templates,
196195
const ordered_args_map_t &args);
197196
} // namespace ordered_args
198197

198+
void SetUmaskForPrivateKey();
199+
199200
#endif // TOOL_OPENSSL_INTERNAL_H

tool-openssl/req.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ bool reqTool(const args_list_t &args) {
511511
}
512512

513513
bssl::UniquePtr<BIO> out_bio;
514+
SetUmaskForPrivateKey();
514515
if (!keyout.empty()) {
515516
fprintf(stderr, "Writing private key to %s\n", keyout.c_str());
516517
out_bio.reset(BIO_new_file(keyout.c_str(), "w"));

tool-openssl/tool.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#endif
1515

1616
#include "./internal.h"
17-
1817

1918
static const std::array<Tool, 16> kTools = {{
2019
{"crl", CRLTool},

tool-openssl/tool_util.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC
3+
4+
#include "internal.h"
5+
#include <string>
6+
#if !defined(OPENSSL_WINDOWS)
7+
#include <sys/stat.h>
8+
#endif
9+
10+
static bool isCharUpperCaseEqual(char a, char b) {
11+
return ::toupper(a) == ::toupper(b);
12+
}
13+
14+
bool isStringUpperCaseEqual(const std::string &a, const std::string &b) {
15+
return a.size() == b.size() &&
16+
std::equal(a.begin(), a.end(), b.begin(), isCharUpperCaseEqual);
17+
}
18+
19+
void SetUmaskForPrivateKey() {
20+
// On Windows, the default behavior for new files is to inherit ACLs from the parent directory,
21+
// and there's no process-wide "mask" to subtract permissions like in POSIX systems.
22+
#if !defined(OPENSSL_WINDOWS)
23+
umask(0077);
24+
#endif
25+
}

tool-openssl/x509.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,6 @@ static bool WriteSignedCertificate(X509 *x509, bssl::UniquePtr<BIO> &output_bio,
9191
return true;
9292
}
9393

94-
static bool isCharUpperCaseEqual(char a, char b) {
95-
return ::toupper(a) == ::toupper(b);
96-
}
97-
98-
bool isStringUpperCaseEqual(const std::string &a, const std::string &b) {
99-
return a.size() == b.size() &&
100-
std::equal(a.begin(), a.end(), b.begin(), isCharUpperCaseEqual);
101-
}
102-
10394
static int AdaptKeyIDExtension(X509 *cert, X509V3_CTX *ext_ctx,
10495
const char *name, const char *value,
10596
int add_if_missing) {

0 commit comments

Comments
 (0)