Project

General

Profile

Bug #9504 ยป nested-asn1-9504.patch

jeremyevans0 (Jeremy Evans), 08/11/2019 05:38 PM

View differences:

ext/openssl/ossl_x509cert.c
114 114
static VALUE
115 115
ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
116 116
{
117
    static ID format_kw[3];
117 118
    BIO *in;
118 119
    X509 *x509, *x = DATA_PTR(self);
119
    VALUE arg;
120
    VALUE arg, opt, format;
121
    int pem_only = 0, der_only = 0;
120 122

  
121
    if (rb_scan_args(argc, argv, "01", &arg) == 0) {
123
    if (rb_scan_args(argc, argv, "01:", &arg, &opt) == 0) {
122 124
	/* create just empty X509Cert */
123 125
	return self;
124 126
    }
127

  
128
    if (!NIL_P(opt)) {
129
        if (!format_kw[0]) {
130
            CONST_ID(format_kw[0], "format");
131
            CONST_ID(format_kw[1], "pem");
132
            CONST_ID(format_kw[2], "der");
133
        }
134
	rb_get_kwargs(opt, format_kw, 0, 1, &format);
135
        if (format != Qundef) {
136
            if (rb_sym2id(format) == format_kw[1]) {
137
                pem_only = 1;
138
            }
139
            else if (rb_sym2id(format) == format_kw[2]) {
140
                der_only = 1;
141
            }
142
            else {
143
                rb_raise(rb_eArgError, "invalid :format keyword value");
144
            }
145
        }
146
    }
147

  
125 148
    arg = ossl_to_der_if_possible(arg);
126 149
    in = ossl_obj2bio(&arg);
127
    x509 = PEM_read_bio_X509(in, &x, NULL, NULL);
128
    DATA_PTR(self) = x;
129
    if (!x509) {
130
	OSSL_BIO_reset(in);
150
    if (der_only) {
131 151
	x509 = d2i_X509_bio(in, &x);
132
	DATA_PTR(self) = x;
133 152
    }
153
    else {
154
        x509 = PEM_read_bio_X509(in, &x, NULL, NULL);
155
        if (!x509 && !pem_only) {
156
            OSSL_BIO_reset(in);
157
            x509 = d2i_X509_bio(in, &x);
158
        }
159
    }
160
    DATA_PTR(self) = x;
134 161
    BIO_free(in);
135 162
    if (!x509) ossl_raise(eX509CertError, NULL);
136 163

  
test/fixtures/chain/x509_with_embedded_x509
1
M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6GI3y8UgYFpzwdrzvXmCSwfHwcL0ZIXVvdhvB86LwmKOS7hwe5zN3sv7dwIKHVoEnoSiGDWPaTKqu4gAyO2KqYf2hANSNvhXWfy5OctsoUJPlPH21oCvO4l7/uQPF6YLRJn+174y/+dXy+SYevxM0OhZQwAlKa0fzKHjwCf
test/test_x509cert.rb
189 189
    assert_equal false, cert3 == cert4
190 190
  end
191 191

  
192
  def test_load_certificate_with_format
193
    raw = File.binread('test/fixtures/chain/x509_with_embedded_x509')
194
    cert = OpenSSL::X509::Certificate.new(raw, :format=>:der)
195
    assert_equal(raw, cert.to_der)
196
  end
197

  
192 198
  private
193 199

  
194 200
  def certificate_error_returns_false