@@ -133,7 +133,7 @@ private X509Cert(Ruby runtime) {
133
133
134
134
private transient PKey public_key ; // lazy initialized
135
135
136
- private final List <X509Extension > extensions = new ArrayList <X509Extension >( );
136
+ private final List <X509Extension > extensions = new ArrayList <>( 4 );
137
137
138
138
private boolean changed = true ;
139
139
@@ -147,11 +147,16 @@ final X509AuxCertificate getAuxCert() {
147
147
148
148
public static IRubyObject wrap (Ruby runtime , Certificate cert )
149
149
throws CertificateEncodingException {
150
- return wrap (runtime .getCurrentContext (), cert . getEncoded () );
150
+ return wrap (runtime .getCurrentContext (), cert );
151
151
}
152
152
153
153
static X509Cert wrap (ThreadContext context , Certificate cert )
154
154
throws CertificateEncodingException {
155
+ if (cert instanceof X509Certificate ) {
156
+ final X509Cert c = new X509Cert (context .runtime );
157
+ c .initialize (context , (X509Certificate ) cert );
158
+ return c ;
159
+ }
155
160
return wrap (context , cert .getEncoded ());
156
161
}
157
162
@@ -161,23 +166,14 @@ public static IRubyObject wrap(Ruby runtime, javax.security.cert.Certificate cer
161
166
return wrap (runtime .getCurrentContext (), cert .getEncoded ());
162
167
}
163
168
164
- static X509Cert wrap (ThreadContext context , javax .security .cert .Certificate cert )
165
- throws javax .security .cert .CertificateEncodingException {
166
- return wrap (context , cert .getEncoded ());
167
- }
168
-
169
169
static X509Cert wrap (final ThreadContext context , final byte [] encoded ) {
170
- //final Ruby runtime = context.runtime;
171
- //final RubyString enc = StringHelper.newString(runtime, encoded);
172
- //return _Certificate(runtime).callMethod(context, "new", enc);
173
170
final X509Cert cert = new X509Cert (context .runtime );
174
171
cert .initialize (context , encoded );
175
172
return cert ;
176
173
}
177
174
178
175
@ JRubyMethod (name ="initialize" , optional = 1 , visibility = Visibility .PRIVATE )
179
- public IRubyObject initialize (final ThreadContext context ,
180
- final IRubyObject [] args , final Block unusedBlock ) {
176
+ public IRubyObject initialize (final ThreadContext context , final IRubyObject [] args , final Block unusedBlock ) {
181
177
182
178
if ( args .length == 0 ) {
183
179
this .subject = X509Name .newName (context .runtime );
@@ -197,22 +193,28 @@ private void initialize(final ThreadContext context, final byte[] encoded) {
197
193
}
198
194
199
195
private void initialize (final ThreadContext context , final byte [] encoded , final int offset , final int length ) {
200
- final Ruby runtime = context .runtime ;
201
-
202
196
byte [] bytes = StringHelper .readX509PEM (encoded , offset , length );
203
-
197
+ final X509Certificate cert ;
204
198
try {
205
199
final ByteArrayInputStream bis = new ByteArrayInputStream (bytes );
206
200
cert = (X509Certificate ) SecurityHelper .getCertificateFactory ("X.509" ).generateCertificate (bis );
207
201
}
208
202
catch (CertificateException e ) {
209
- throw newCertificateError (runtime , e );
203
+ throw newCertificateError (context . runtime , e );
210
204
}
211
205
206
+ initialize (context , cert );
207
+ }
208
+
209
+ private void initialize (final ThreadContext context , final X509Certificate cert ) {
210
+ final Ruby runtime = context .runtime ;
211
+
212
212
if ( cert == null ) {
213
213
throw newCertificateError (runtime , (String ) null );
214
214
}
215
215
216
+ this .cert = cert ;
217
+
216
218
set_serial ( RubyNumeric .str2inum (runtime , runtime .newString (cert .getSerialNumber ().toString ()), 10 ) );
217
219
set_not_before ( context , RubyTime .newTime ( runtime , cert .getNotBefore ().getTime () ) );
218
220
set_not_after ( context , RubyTime .newTime ( runtime , cert .getNotAfter ().getTime () ) );
0 commit comments