File tree Expand file tree Collapse file tree 3 files changed +41
-11
lines changed Expand file tree Collapse file tree 3 files changed +41
-11
lines changed Original file line number Diff line number Diff line change 10541054 'sources' : [
10551055 'test/cctest/test_crypto_clienthello.cc' ,
10561056 'test/cctest/test_node_crypto.cc' ,
1057+ 'test/cctest/test_node_crypto_env.cc' ,
10571058 'test/cctest/test_quic_cid.cc' ,
10581059 'test/cctest/test_quic_tokens.cc' ,
10591060 ]
Original file line number Diff line number Diff line change @@ -62,18 +62,16 @@ inline X509_STORE* GetOrCreateRootCertStore() {
6262// Takes a string or buffer and loads it into a BIO.
6363// Caller responsible for BIO_free_all-ing the returned object.
6464BIOPointer LoadBIO (Environment* env, Local<Value> v) {
65- HandleScope scope (env->isolate ());
66-
67- if (v->IsString ()) {
68- Utf8Value s (env->isolate (), v);
69- return NodeBIO::NewFixed (*s, s.length ());
70- }
71-
72- if (v->IsArrayBufferView ()) {
73- ArrayBufferViewContents<char > buf (v.As <ArrayBufferView>());
74- return NodeBIO::NewFixed (buf.data (), buf.length ());
65+ if (v->IsString () || v->IsArrayBufferView ()) {
66+ BIOPointer bio (BIO_new (BIO_s_secmem ()));
67+ if (!bio) return nullptr ;
68+ ByteSource bsrc = ByteSource::FromStringOrBuffer (env, v);
69+ if (bsrc.size () > INT_MAX) return nullptr ;
70+ int written = BIO_write (bio.get (), bsrc.data <char >(), bsrc.size ());
71+ if (written < 0 ) return nullptr ;
72+ if (static_cast <size_t >(written) != bsrc.size ()) return nullptr ;
73+ return bio;
7574 }
76-
7775 return nullptr ;
7876}
7977
Original file line number Diff line number Diff line change 1+ #include " crypto/crypto_bio.h"
2+ #include " gtest/gtest.h"
3+ #include " node_options.h"
4+ #include " node_test_fixture.h"
5+ #include " openssl/err.h"
6+
7+ using v8::Local;
8+ using v8::String;
9+
10+ /*
11+ * This test verifies that an object created by LoadBIO supports BIO_tell
12+ * and BIO_seek, otherwise PEM_read_bio_PrivateKey fails on some keys
13+ * (if OpenSSL needs to rewind pointer between pem_read_bio_key()
14+ * and pem_read_bio_key_legacy() inside PEM_read_bio_PrivateKey).
15+ */
16+ class NodeCryptoEnv : public EnvironmentTestFixture {};
17+
18+ TEST_F (NodeCryptoEnv, LoadBIO) {
19+ v8::HandleScope handle_scope (isolate_);
20+ Argv argv;
21+ Env env{handle_scope, argv};
22+ // just put a random string into BIO
23+ Local<String> key = String::NewFromUtf8 (isolate_, " abcdef" ).ToLocalChecked ();
24+ node::crypto::BIOPointer bio (node::crypto::LoadBIO (*env, key));
25+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
26+ BIO_seek (bio.get (), 2 );
27+ ASSERT_EQ (BIO_tell (bio.get ()), 2 );
28+ #endif
29+ ASSERT_EQ (ERR_peek_error (), 0UL ) << " There should not have left "
30+ " any errors on the OpenSSL error stack\n " ;
31+ }
You can’t perform that action at this time.
0 commit comments