Skip to content

Commit a4cc46d

Browse files
committed
always use buffered-reader esp. when reading in loop (closing #67)
fixes incompatibility introduced at e25518f
1 parent fca2e71 commit a4cc46d

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

src/main/java/org/jruby/ext/openssl/x509store/Lookup.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,30 +184,35 @@ public int loadCertificateFile(final String file, final int type) throws IOExcep
184184

185185
final Object[] cached = certCache.get(file);
186186

187-
Reader reader = null;
187+
BufferedReader reader = null;
188188
try {
189189
X509AuxCertificate auxCert;
190190
if ( type == X509_FILETYPE_PEM ) {
191191
int count = 0;
192192
if ( cached != null ) {
193-
for ( int c = 0; c < cached.length; c++ ) {
193+
boolean storeError = false; for ( int c = 0; c < cached.length; c++ ) {
194194
auxCert = buildAuxFromCached((X509Certificate) cached[c]);
195-
final int i = store.addCertificate(auxCert);
196-
if ( i != 0 ) count++;
195+
196+
if ( ! storeError ) {
197+
if ( store.addCertificate(auxCert) != 0 ) count++;
198+
else { storeError = true; count = 0; } // return 0
199+
}
197200
}
198201
}
199202
else {
200-
reader = new InputStreamReader(wrapJRubyNormalizedInputStream(file));
203+
reader = new BufferedReader(new InputStreamReader(wrapJRubyNormalizedInputStream(file)));
201204
final ArrayList<Object> cacheEntry = new ArrayList<Object>(8);
202205

203-
for (;;) {
206+
boolean storeError = false; for (;;) {
204207
auxCert = PEMInputOutput.readX509Aux(reader, null);
205208
if ( auxCert == null ) break;
206209

207210
cacheEntry.add( auxCert.cloneForCache() ); // make sure we cache aux
208211

209-
final int i = store.addCertificate(auxCert);
210-
if ( i != 0 ) count++;
212+
if ( ! storeError ) {
213+
if ( store.addCertificate(auxCert) != 0 ) count++;
214+
else { storeError = true; count = 0; } // return 0
215+
}
211216
}
212217

213218
certCache.put(file, cacheEntry.toArray( new Object[ cacheEntry.size() ] ));
@@ -228,7 +233,6 @@ else if ( type == X509_FILETYPE_ASN1 ) {
228233
auxCert = new X509AuxCertificate(cert);
229234
certCache.put(file, new Object[] { auxCert.cloneForCache() });
230235
}
231-
232236
//if ( auxCert == null ) {
233237
// X509Error.addError(13); return 0;
234238
//}
@@ -258,7 +262,7 @@ private static X509AuxCertificate buildAuxFromCached(final X509Certificate cache
258262
public int loadCRLFile(final String file, final int type) throws Exception {
259263
if ( file == null ) return 1;
260264

261-
Reader reader = null;
265+
BufferedReader reader = null;
262266
try {
263267
InputStream in = wrapJRubyNormalizedInputStream(file);
264268
CRL crl;
@@ -267,8 +271,8 @@ public int loadCRLFile(final String file, final int type) throws Exception {
267271
int count = 0; for (;;) {
268272
crl = PEMInputOutput.readX509CRL(reader, null);
269273
if ( crl == null ) break;
270-
final int i = store.addCRL(crl);
271-
if ( i == 0 ) return 0; count++;
274+
if ( store.addCRL(crl) == 0 ) return 0;
275+
count++;
272276
}
273277
return count;
274278
}
@@ -292,8 +296,6 @@ else if ( type == X509_FILETYPE_ASN1 ) {
292296
}
293297
}
294298

295-
296-
297299
/**
298300
* c: X509_LOOKUP_load_cert_crl_file
299301
*/

src/main/java/org/jruby/ext/openssl/x509store/PEMInputOutput.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,18 @@ private static BufferedWriter makeBuffered(Writer out) {
189189
}
190190

191191
/**
192-
* c: PEM_X509_INFO_read_bio
192+
* @deprecated Prefer passing in a buffered-reader esp. in loops as the
193+
* method might return a X.509 object before reading the full PEM file !
193194
*/
194195
public static Object readPEM(final Reader in, final char[] passwd) throws IOException {
195-
final BufferedReader reader = makeBuffered(in); String line;
196+
return readPEM(makeBuffered(in), passwd);
197+
}
198+
199+
/**
200+
* c: PEM_X509_INFO_read_bio
201+
*/
202+
public static Object readPEM(final BufferedReader reader, final char[] passwd) throws IOException {
203+
String line;
196204
while ( ( line = reader.readLine() ) != null ) {
197205
if ( line.indexOf(BEG_STRING_PUBLIC) != -1 ) {
198206
try {
@@ -573,9 +581,17 @@ public static CMSSignedData readPKCS7(Reader in, char[] f) throws IOException {
573581
return null;
574582
}
575583

576-
public static X509AuxCertificate readX509Certificate(final Reader in, final char[] passwd)
584+
/**
585+
* @deprecated Prefer passing in a buffered-reader esp. in loops as the
586+
* method might return a X.509 object before reading the full PEM file !
587+
*/
588+
public static X509AuxCertificate readX509Certificate(final Reader in, final char[] passwd) throws IOException {
589+
return readX509Certificate(makeBuffered(in), passwd);
590+
}
591+
592+
public static X509AuxCertificate readX509Certificate(final BufferedReader reader, final char[] passwd)
577593
throws IOException {
578-
final BufferedReader reader = makeBuffered(in); String line;
594+
String line;
579595
while ( ( line = reader.readLine() ) != null ) {
580596
if ( line.indexOf(BEG_STRING_X509_OLD) != -1 ) {
581597
try {
@@ -605,9 +621,17 @@ else if ( line.indexOf(BEG_STRING_X509_TRUSTED) != -1 ) {
605621
return null;
606622
}
607623

608-
public static X509AuxCertificate readX509Aux(final Reader in, final char[] passwd)
624+
/**
625+
* @deprecated Prefer passing in a buffered-reader esp. in loops as the
626+
* method might return a X.509 object before reading the full PEM file !
627+
*/
628+
public static X509AuxCertificate readX509Aux(final Reader in, final char[] passwd) throws IOException {
629+
return readX509Aux(makeBuffered(in), passwd);
630+
}
631+
632+
public static X509AuxCertificate readX509Aux(final BufferedReader reader, final char[] passwd)
609633
throws IOException {
610-
final BufferedReader reader = makeBuffered(in); String line;
634+
String line;
611635
while ( ( line = reader.readLine() ) != null ) {
612636
if ( line.indexOf(BEG_STRING_X509_OLD) != -1 ) {
613637
try {
@@ -637,8 +661,16 @@ else if ( line.indexOf(BEG_STRING_X509_TRUSTED) != -1 ) {
637661
return null;
638662
}
639663

640-
public static X509CRL readX509CRL(final Reader in, final char[] passwd) throws IOException {
641-
final BufferedReader reader = makeBuffered(in); String line;
664+
/**
665+
* @deprecated Prefer passing in a buffered-reader esp. in loops as the
666+
* method might return a X.509 object before reading the full PEM file !
667+
*/
668+
public static X509CRL readX509CRL(final Reader reader, final char[] passwd) throws IOException {
669+
return readX509CRL(makeBuffered(reader), passwd);
670+
}
671+
672+
public static X509CRL readX509CRL(final BufferedReader reader, final char[] passwd) throws IOException {
673+
String line;
642674
while ( ( line = reader.readLine() ) != null ) {
643675
if ( line.indexOf(BEG_STRING_X509_CRL) != -1 ) {
644676
try {

0 commit comments

Comments
 (0)