@@ -108,34 +108,76 @@ Handle<Value> SecureContext::Init(const Arguments& args) {
108108}
109109
110110
111- Handle<Value> SecureContext::SetKey (const Arguments& args) {
111+ // Takes a string or buffer and loads it into a BIO.
112+ // Caller responsible for BIO_free-ing the returned object.
113+ static BIO * LoadBIO (Handle<Value> v) {
114+ BIO *bio = BIO_new (BIO_s_mem ());
115+ if (!bio) return NULL ;
116+
112117 HandleScope scope;
113118
114- SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args. Holder ()) ;
119+ int r ;
115120
116- if (args.Length () != 1 || !args[0 ]->IsString ()) {
117- return ThrowException (Exception::TypeError (String::New (" Bad parameter" )));
121+ if (v->IsString ()) {
122+ String::Utf8Value s (v->ToString ());
123+ r = BIO_write (bio, *s, s.length ());
124+ } else if (Buffer::HasInstance (v)) {
125+ Local<Object> buffer_obj = v->ToObject ();
126+ char *buffer_data = Buffer::Data (buffer_obj);
127+ size_t buffer_length = Buffer::Length (buffer_obj);
128+ r = BIO_write (bio, buffer_data, buffer_length);
129+ }
130+
131+ if (r <= 0 ) {
132+ BIO_free (bio);
133+ return NULL ;
118134 }
119135
120- String::Utf8Value key_pem (args[0 ]->ToString ());
136+ return bio;
137+ }
138+
121139
122- BIO *bp = BIO_new (BIO_s_mem ());
140+ // Takes a string or buffer and loads it into an X509
141+ // Caller responsible for X509_free-ing the returned object.
142+ static X509 * LoadX509 (Handle<Value> v) {
143+ HandleScope scope; // necessary?
123144
124- if (!BIO_write (bp, *key_pem, key_pem.length ())) {
125- BIO_free (bp);
126- return False ();
145+ BIO *bio = LoadBIO (v);
146+ if (!bio) return NULL ;
147+
148+ X509 * x509 = PEM_read_bio_X509 (bio, NULL , NULL , NULL );
149+ if (!x509) {
150+ BIO_free (bio);
151+ return NULL ;
127152 }
128153
129- EVP_PKEY * pkey = PEM_read_bio_PrivateKey (bp, NULL , NULL , NULL );
154+ BIO_free (bio);
155+ return x509;
156+ }
157+
130158
131- if (pkey == NULL ) {
132- BIO_free (bp);
159+ Handle<Value> SecureContext::SetKey (const Arguments& args) {
160+ HandleScope scope;
161+
162+ SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder ());
163+
164+ if (args.Length () != 1 ) {
165+ return ThrowException (Exception::TypeError (String::New (" Bad parameter" )));
166+ }
167+
168+ BIO *bio = LoadBIO (args[0 ]);
169+ if (!bio) return False ();
170+
171+ EVP_PKEY * key = PEM_read_bio_PrivateKey (bio, NULL , NULL , NULL );
172+
173+ if (!key) {
174+ BIO_free (bio);
133175 return False ();
134176 }
135177
136- SSL_CTX_use_PrivateKey (sc->ctx_ , pkey );
137- BIO_free (bp );
138- // XXX Free pkey?
178+ SSL_CTX_use_PrivateKey (sc->ctx_ , key );
179+ BIO_free (bio );
180+ // XXX Free key?
139181
140182 return True ();
141183}
@@ -146,30 +188,15 @@ Handle<Value> SecureContext::SetCert(const Arguments& args) {
146188
147189 SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder ());
148190
149- if (args.Length () != 1 ||
150- !args[0 ]->IsString ()) {
191+ if (args.Length () != 1 ) {
151192 return ThrowException (Exception::TypeError (
152193 String::New (" Bad parameter" )));
153194 }
154- String::Utf8Value cert_pem (args[0 ]->ToString ());
155-
156- BIO *bp = BIO_new (BIO_s_mem ());
157-
158- if (!BIO_write (bp, *cert_pem, cert_pem.length ())) {
159- BIO_free (bp);
160- return False ();
161- }
162195
163- X509 * x509 = PEM_read_bio_X509 (bp, NULL , NULL , NULL );
164-
165- if (x509 == NULL ) {
166- BIO_free (bp);
167- return False ();
168- }
196+ X509 * x509 = LoadX509 (args[0 ]);
197+ if (!x509) return False ();
169198
170199 SSL_CTX_use_certificate (sc->ctx_ , x509);
171-
172- BIO_free (bp);
173200 X509_free (x509);
174201
175202 return True ();
@@ -181,28 +208,15 @@ Handle<Value> SecureContext::AddCACert(const Arguments& args) {
181208
182209 SecureContext *sc = ObjectWrap::Unwrap<SecureContext>(args.Holder ());
183210
184- if (args.Length () != 1 || !args[ 0 ]-> IsString () ) {
211+ if (args.Length () != 1 ) {
185212 return ThrowException (Exception::TypeError (String::New (" Bad parameter" )));
186213 }
187- String::Utf8Value cert_pem (args[0 ]->ToString ());
188-
189- BIO *bp = BIO_new (BIO_s_mem ());
190-
191- if (!BIO_write (bp, *cert_pem, cert_pem.length ())) {
192- BIO_free (bp);
193- return False ();
194- }
195-
196- X509 *x509 = PEM_read_bio_X509 (bp, NULL , NULL , NULL );
197214
198- if (x509 == NULL ) {
199- BIO_free (bp);
200- return False ();
201- }
215+ X509 * x509 = LoadX509 (args[0 ]);
216+ if (!x509) return False ();
202217
203218 X509_STORE_add_cert (sc->ca_store_ , x509);
204219
205- BIO_free (bp);
206220 X509_free (x509);
207221
208222 return True ();
0 commit comments