diff --git a/ext/openssl/ossl_x509cert.c b/ext/openssl/ossl_x509cert.c
index 40542c4..ddc0b7d 100644
--- a/ext/openssl/ossl_x509cert.c
+++ b/ext/openssl/ossl_x509cert.c
@@ -114,23 +114,50 @@ ossl_x509_alloc(VALUE klass)
 static VALUE
 ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
 {
+    static ID format_kw[3];
     BIO *in;
     X509 *x509, *x = DATA_PTR(self);
-    VALUE arg;
+    VALUE arg, opt, format;
+    int pem_only = 0, der_only = 0;
 
-    if (rb_scan_args(argc, argv, "01", &arg) == 0) {
+    if (rb_scan_args(argc, argv, "01:", &arg, &opt) == 0) {
 	/* create just empty X509Cert */
 	return self;
     }
+
+    if (!NIL_P(opt)) {
+        if (!format_kw[0]) {
+            CONST_ID(format_kw[0], "format");
+            CONST_ID(format_kw[1], "pem");
+            CONST_ID(format_kw[2], "der");
+        }
+	rb_get_kwargs(opt, format_kw, 0, 1, &format);
+        if (format != Qundef) {
+            if (rb_sym2id(format) == format_kw[1]) {
+                pem_only = 1;
+            }
+            else if (rb_sym2id(format) == format_kw[2]) {
+                der_only = 1;
+            }
+            else {
+                rb_raise(rb_eArgError, "invalid :format keyword value");
+            }
+        }
+    }
+
     arg = ossl_to_der_if_possible(arg);
     in = ossl_obj2bio(&arg);
-    x509 = PEM_read_bio_X509(in, &x, NULL, NULL);
-    DATA_PTR(self) = x;
-    if (!x509) {
-	OSSL_BIO_reset(in);
+    if (der_only) {
 	x509 = d2i_X509_bio(in, &x);
-	DATA_PTR(self) = x;
     }
+    else {
+        x509 = PEM_read_bio_X509(in, &x, NULL, NULL);
+        if (!x509 && !pem_only) {
+            OSSL_BIO_reset(in);
+            x509 = d2i_X509_bio(in, &x);
+        }
+    }
+    DATA_PTR(self) = x;
     BIO_free(in);
     if (!x509) ossl_raise(eX509CertError, NULL);
 
diff --git a/test/fixtures/chain/x509_with_embedded_x509 b/test/fixtures/chain/x509_with_embedded_x509
new file mode 100644
index 0000000..b942e1e
--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YDVR0gBAowCDAGBgRVHSAAMA0GCSqGSIb3DQEBBQUAA4GBAGJzh6GI3y8UgYFpzwdrzvXmCSwfHwcL0ZIXVvdhvB86LwmKOS7hwe5zN3sv7dwIKHVoEnoSiGDWPaTKqu4gAyO2KqYf2hANSNvhXWfy5OctsoUJPlPH21oCvO4l7/uQPF6YLRJn+174y/+dXy+SYevxM0OhZQwAlKa0fzKHjwCf
\ No newline at end of file
diff --git a/test/test_x509cert.rb b/test/test_x509cert.rb
index 40a5b0a..c69ffc7 100644
--- a/test/test_x509cert.rb
+++ b/test/test_x509cert.rb
@@ -189,6 +189,12 @@ class OpenSSL::TestX509Certificate < OpenSSL::TestCase
     assert_equal false, cert3 == cert4
   end
 
+  def test_load_certificate_with_format
+    raw = File.binread('test/fixtures/chain/x509_with_embedded_x509')
+    cert = OpenSSL::X509::Certificate.new(raw, :format=>:der)
+    assert_equal(raw, cert.to_der)
+  end
+
   private
 
   def certificate_error_returns_false
