Skip to content

Commit 8acccd9

Browse files
authored
Add req to OpenSSL CLI tool (#2284)
### Issues: `CryptoAlg-2992` ### Description of changes: Add the req tool. Only some options are supported. We don't support the default config file interface that OpenSSL does, default values are hardcoded where appropriate. This PR also modified the createTempDirPath utility function. This func previously had a race condition causing intermittent CI failures. ### Testing: Unit test for parsing subject function. OpenSSL comparison tests for CSR and Cert generation with cross-checking of attributes. 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.
1 parent c540a7d commit 8acccd9

File tree

8 files changed

+1172
-16
lines changed

8 files changed

+1172
-16
lines changed

crypto/dsa/internal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ struct dsa_st {
4040
CRYPTO_EX_DATA ex_data;
4141
};
4242

43-
#define OPENSSL_DSA_MAX_MODULUS_BITS 10000
44-
4543
// dsa_check_key performs cheap self-checks on |dsa|, and ensures it is within
4644
// DoS bounds. It returns one on success and zero on error.
4745
int dsa_check_key(const DSA *dsa);

crypto/test/test_util.cc

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#include "test_util.h"
1616

1717
#include <ostream>
18+
#include <inttypes.h>
1819

1920
#include <openssl/err.h>
2021

2122
#include "../internal.h"
2223
#include "openssl/pem.h"
24+
#include "openssl/rand.h"
2325

2426

2527
void hexdump(FILE *fp, const char *msg, const void *in, size_t len) {
@@ -166,27 +168,33 @@ size_t createTempFILEpath(char buffer[PATH_MAX]) {
166168
}
167169

168170
size_t createTempDirPath(char buffer[PATH_MAX]) {
169-
char pathname[PATH_MAX];
170-
char tempdir[PATH_MAX];
171-
172-
if (0 == GetTempPathA(PATH_MAX, pathname)) {
171+
char temp_path[PATH_MAX];
172+
union {
173+
uint8_t bytes[8];
174+
uint64_t value;
175+
} random_bytes;
176+
177+
// Get the temporary path
178+
if (0 == GetTempPathA(PATH_MAX, temp_path)) {
173179
return 0;
174180
}
175181

176-
// Generate a unique name using Windows API
177-
if (0 == GetTempFileNameA(pathname, "awslctestdir", 0, tempdir)) {
182+
if (!RAND_bytes(random_bytes.bytes, sizeof(random_bytes.bytes))) {
178183
return 0;
179184
}
180185

181-
// Delete the file that GetTempFileNameA created
182-
DeleteFileA(tempdir);
186+
int written = snprintf(buffer, PATH_MAX, "%s\\awslctest_%" PRIX64, temp_path, random_bytes.value);
183187

184-
if (!CreateDirectoryA(tempdir, NULL)) {
188+
// Check for truncation of dirname
189+
if (written < 0 || written >= PATH_MAX) {
185190
return 0;
186191
}
187192

188-
strncpy(buffer, tempdir, PATH_MAX);
189-
return strnlen(buffer, PATH_MAX);
193+
if (!CreateDirectoryA(buffer, NULL)) {
194+
return 0;
195+
}
196+
197+
return (size_t)written;
190198
}
191199

192200
FILE* createRawTempFILE() {
@@ -196,6 +204,7 @@ FILE* createRawTempFILE() {
196204
}
197205
return fopen(filename, "w+b");
198206
}
207+
199208
#else
200209
#include <cstdlib>
201210
#include <unistd.h>

include/openssl/dsa.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
extern "C" {
7171
#endif
7272

73+
#define OPENSSL_DSA_MAX_MODULUS_BITS 10000
74+
7375

7476
// DSA contains functions for signing and verifying with the Digital Signature
7577
// Algorithm.
@@ -187,8 +189,8 @@ OPENSSL_EXPORT DSA *DSAparams_dup(const DSA *dsa);
187189
// Key generation.
188190

189191
// DSA_generate_key generates a public/private key pair in |dsa|, which must
190-
// already have parameters setup. It returns one on success and zero on
191-
// error.
192+
// already have parameters setup. Only supports generating up to |OPENSSL_DSA_MAX_MODULUS_BITS|
193+
// bit keys. It returns one on success and zero on error.
192194
OPENSSL_EXPORT int DSA_generate_key(DSA *dsa);
193195

194196

tool-openssl/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_executable(
1010
crl.cc
1111
dgst.cc
1212
rehash.cc
13+
req.cc
1314
rsa.cc
1415
s_client.cc
1516
tool.cc
@@ -83,6 +84,8 @@ if(BUILD_TESTING)
8384
dgst_test.cc
8485
rehash.cc
8586
rehash_test.cc
87+
req.cc
88+
req_test.cc
8689
rsa.cc
8790
rsa_test.cc
8891
s_client.cc

tool-openssl/internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ bool CRLTool(const args_list_t &args);
3434
bool dgstTool(const args_list_t &args);
3535
bool md5Tool(const args_list_t &args);
3636
bool RehashTool(const args_list_t &args);
37+
bool reqTool(const args_list_t &args);
3738
bool rsaTool(const args_list_t &args);
3839
bool SClientTool(const args_list_t &args);
3940
bool VerifyTool(const args_list_t &args);
4041
bool VersionTool(const args_list_t &args);
4142
bool X509Tool(const args_list_t &args);
4243

44+
// Req Tool Utilities
45+
bssl::UniquePtr<X509_NAME> parse_subject_name(std::string &subject_string);
46+
4347

4448
// Rehash tool Utils
4549
typedef struct hash_entry_st { // Represents a single certificate/CRL file

0 commit comments

Comments
 (0)