Skip to content

HDFS-15918. Replace deprecated RAND_pseudo_bytes #2811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DigestMD5Authenticator {

static size_t NextToken(const std::string &payload, size_t off,
std::string *tok);
void GenerateCNonce();
Status GenerateCNonce();
std::string username_;
std::string password_;
std::string nonce_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <openssl/rand.h>
#include <openssl/md5.h>
#include <openssl/err.h>

#include <iomanip>
#include <map>
Expand Down Expand Up @@ -91,12 +92,19 @@ size_t DigestMD5Authenticator::NextToken(const std::string &payload, size_t off,
return off;
}

void DigestMD5Authenticator::GenerateCNonce() {
if (!TEST_mock_cnonce_) {
char buf[8] = {0,};
RAND_pseudo_bytes(reinterpret_cast<unsigned char *>(buf), sizeof(buf));
Status DigestMD5Authenticator::GenerateCNonce() {
if (TEST_mock_cnonce_) {
return Status::OK();
}

char buf[8] = { 0, };
if (RAND_bytes(reinterpret_cast<unsigned char*>(buf), sizeof(buf)) == 1) {
cnonce_ = Base64Encode(std::string(buf, sizeof(buf)));
return Status::OK();
}

const auto* error = ERR_reason_error_string(ERR_get_error());
return Status::Error(error);
}

Status DigestMD5Authenticator::ParseFirstChallenge(const std::string &payload) {
Expand Down Expand Up @@ -155,8 +163,11 @@ Status DigestMD5Authenticator::GenerateFirstResponse(std::string *result) {
return Status::Unimplemented();
}

if (auto status = GenerateCNonce(); !status.ok()) {
return status;
}

std::stringstream ss;
GenerateCNonce();
ss << "charset=utf-8,username=\"" << QuoteString(username_) << "\""
<< ",authzid=\"" << QuoteString(username_) << "\""
<< ",nonce=\"" << QuoteString(nonce_) << "\""
Expand Down