Skip to content

Commit 4166128

Browse files
danbevMylesBorins
authored andcommitted
src: split CryptoPemCallback into two functions
Currently the function CryptoPemCallback is used for two things: 1. As a passphrase callback. 2. To avoid the default OpenSSL passphrase routine. The default OpenSSL passphase routine would apply if both the callback and the passphrase are null pointers and the typical behaviour is to prompt for the passphase which is not appropriate in node. This commit suggests that the PasswordCallback function only handle passphrases, and that an additional function named NoPasswordCallback used for the second case to avoid OpenSSL's passphase routine. PR-URL: #12827 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 2aa6828 commit 4166128

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/node_crypto.cc

+22-11
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,6 @@ static void crypto_lock_cb(int mode, int n, const char* file, int line) {
204204
}
205205

206206

207-
// This callback is used by OpenSSL when it needs to query for the passphrase
208-
// which may be used for encrypted PEM structures.
209207
static int PasswordCallback(char *buf, int size, int rwflag, void *u) {
210208
if (u) {
211209
size_t buflen = static_cast<size_t>(size);
@@ -219,6 +217,16 @@ static int PasswordCallback(char *buf, int size, int rwflag, void *u) {
219217
}
220218

221219

220+
// This callback is used to avoid the default passphrase callback in OpenSSL
221+
// which will typically prompt for the passphrase. The prompting is designed
222+
// for the OpenSSL CLI, but works poorly for Node.js because it involves
223+
// synchronous interaction with the controlling terminal, something we never
224+
// want, and use this function to avoid it.
225+
static int NoPasswordCallback(char *buf, int size, int rwflag, void *u) {
226+
return 0;
227+
}
228+
229+
222230
void ThrowCryptoError(Environment* env,
223231
unsigned long err, // NOLINT(runtime/int)
224232
const char* default_message = nullptr) {
@@ -588,7 +596,7 @@ int SSL_CTX_use_certificate_chain(SSL_CTX* ctx,
588596
// that we are interested in
589597
ERR_clear_error();
590598

591-
x = PEM_read_bio_X509_AUX(in, nullptr, PasswordCallback, nullptr);
599+
x = PEM_read_bio_X509_AUX(in, nullptr, NoPasswordCallback, nullptr);
592600

593601
if (x == nullptr) {
594602
SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
@@ -606,7 +614,10 @@ int SSL_CTX_use_certificate_chain(SSL_CTX* ctx,
606614
goto done;
607615
}
608616

609-
while ((extra = PEM_read_bio_X509(in, nullptr, PasswordCallback, nullptr))) {
617+
while ((extra = PEM_read_bio_X509(in,
618+
nullptr,
619+
NoPasswordCallback,
620+
nullptr))) {
610621
if (sk_X509_push(extra_certs, extra))
611622
continue;
612623

@@ -702,7 +713,7 @@ static X509_STORE* NewRootCertStore() {
702713
if (root_certs_vector.empty()) {
703714
for (size_t i = 0; i < arraysize(root_certs); i++) {
704715
BIO* bp = NodeBIO::NewFixed(root_certs[i], strlen(root_certs[i]));
705-
X509 *x509 = PEM_read_bio_X509(bp, nullptr, PasswordCallback, nullptr);
716+
X509 *x509 = PEM_read_bio_X509(bp, nullptr, NoPasswordCallback, nullptr);
706717
BIO_free(bp);
707718

708719
// Parse errors from the built-in roots are fatal.
@@ -745,7 +756,7 @@ void SecureContext::AddCACert(const FunctionCallbackInfo<Value>& args) {
745756

746757
X509_STORE* cert_store = SSL_CTX_get_cert_store(sc->ctx_);
747758
while (X509* x509 =
748-
PEM_read_bio_X509(bio, nullptr, PasswordCallback, nullptr)) {
759+
PEM_read_bio_X509(bio, nullptr, NoPasswordCallback, nullptr)) {
749760
if (cert_store == root_cert_store) {
750761
cert_store = NewRootCertStore();
751762
SSL_CTX_set_cert_store(sc->ctx_, cert_store);
@@ -777,7 +788,7 @@ void SecureContext::AddCRL(const FunctionCallbackInfo<Value>& args) {
777788
return;
778789

779790
X509_CRL* crl =
780-
PEM_read_bio_X509_CRL(bio, nullptr, PasswordCallback, nullptr);
791+
PEM_read_bio_X509_CRL(bio, nullptr, NoPasswordCallback, nullptr);
781792

782793
if (crl == nullptr) {
783794
BIO_free_all(bio);
@@ -816,7 +827,7 @@ static unsigned long AddCertsFromFile( // NOLINT(runtime/int)
816827
}
817828

818829
while (X509* x509 =
819-
PEM_read_bio_X509(bio, nullptr, PasswordCallback, nullptr)) {
830+
PEM_read_bio_X509(bio, nullptr, NoPasswordCallback, nullptr)) {
820831
X509_STORE_add_cert(store, x509);
821832
X509_free(x509);
822833
}
@@ -4295,7 +4306,7 @@ SignBase::Error Verify::VerifyFinal(const char* key_pem,
42954306
// Split this out into a separate function once we have more than one
42964307
// consumer of public keys.
42974308
if (strncmp(key_pem, PUBLIC_KEY_PFX, PUBLIC_KEY_PFX_LEN) == 0) {
4298-
pkey = PEM_read_bio_PUBKEY(bp, nullptr, PasswordCallback, nullptr);
4309+
pkey = PEM_read_bio_PUBKEY(bp, nullptr, NoPasswordCallback, nullptr);
42994310
if (pkey == nullptr)
43004311
goto exit;
43014312
} else if (strncmp(key_pem, PUBRSA_KEY_PFX, PUBRSA_KEY_PFX_LEN) == 0) {
@@ -4311,7 +4322,7 @@ SignBase::Error Verify::VerifyFinal(const char* key_pem,
43114322
goto exit;
43124323
} else {
43134324
// X.509 fallback
4314-
x509 = PEM_read_bio_X509(bp, nullptr, PasswordCallback, nullptr);
4325+
x509 = PEM_read_bio_X509(bp, nullptr, NoPasswordCallback, nullptr);
43154326
if (x509 == nullptr)
43164327
goto exit;
43174328

@@ -4429,7 +4440,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
44294440
goto exit;
44304441
} else if (operation == kPublic &&
44314442
strncmp(key_pem, CERTIFICATE_PFX, CERTIFICATE_PFX_LEN) == 0) {
4432-
x509 = PEM_read_bio_X509(bp, nullptr, PasswordCallback, nullptr);
4443+
x509 = PEM_read_bio_X509(bp, nullptr, NoPasswordCallback, nullptr);
44334444
if (x509 == nullptr)
44344445
goto exit;
44354446

0 commit comments

Comments
 (0)