Skip to content

Commit 4c87774

Browse files
committed
[refactor] and do not print-stack-trace in PKCS7 impl -> wrap causes
1 parent 98fbfc6 commit 4c87774

3 files changed

Lines changed: 40 additions & 55 deletions

File tree

src/main/java/org/jruby/ext/openssl/impl/NotVerifiedPKCS7Exception.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ public class NotVerifiedPKCS7Exception extends PKCS7Exception {
3737
public NotVerifiedPKCS7Exception() {
3838
super(-1, -1);
3939
}
40+
41+
public NotVerifiedPKCS7Exception(Exception cause) {
42+
super(-1, -1, cause);
43+
}
4044
}// NotVerifiedPKCS7Exception

src/main/java/org/jruby/ext/openssl/impl/PKCS7.java

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,17 @@
2929

3030
import java.io.IOException;
3131
import java.math.BigInteger;
32+
import java.security.*;
3233
import java.util.ArrayList;
3334
import java.util.Arrays;
3435
import java.util.Calendar;
3536
import java.util.Collection;
3637
import java.util.Iterator;
3738
import java.util.List;
3839
import java.util.TimeZone;
39-
import java.security.MessageDigest;
40-
import java.security.PrivateKey;
41-
import java.security.PublicKey;
42-
import java.security.Signature;
4340
import java.security.cert.X509CRL;
4441

45-
import javax.crypto.Cipher;
46-
import javax.crypto.KeyGenerator;
47-
import javax.crypto.SecretKey;
42+
import javax.crypto.*;
4843
import javax.crypto.spec.IvParameterSpec;
4944
import javax.crypto.spec.RC2ParameterSpec;
5045
import javax.crypto.spec.SecretKeySpec;
@@ -142,7 +137,7 @@ public static PKCS7 fromASN1(ASN1Encodable obj) throws PKCS7Exception {
142137
else {
143138
final int nid = ASN1Registry.oid2nid(contentType);
144139

145-
ASN1Encodable content = size == 1 ? (ASN1Encodable) null : ((ASN1Sequence) obj).getObjectAt(1);
140+
ASN1Encodable content = size == 1 ? null : ((ASN1Sequence) obj).getObjectAt(1);
146141

147142
if (content != null && content instanceof ASN1TaggedObject && ((ASN1TaggedObject) content).getTagNo() == 0) {
148143
content = ((ASN1TaggedObject) content).getObject();
@@ -152,7 +147,7 @@ public static PKCS7 fromASN1(ASN1Encodable obj) throws PKCS7Exception {
152147
}
153148
// somewhere the object does not obey to be PKCS7 object
154149
catch (ClassCastException e) {
155-
throw new IllegalArgumentException("not a PKCS7 Object");
150+
throw new IllegalArgumentException("not a PKCS7 Object", e);
156151
}
157152

158153
return p7;
@@ -232,9 +227,6 @@ public List<X509AuxCertificate> getSigners(Collection<X509AuxCertificate> certs,
232227
for ( final SignerInfoWithPkey info : infos ) {
233228
final IssuerAndSerialNumber ias = info.getIssuerAndSerialNumber();
234229
X509AuxCertificate signer = null;
235-
// System.err.println("looking for: " + ias.getName() + " and " + ias.getCertificateSerialNumber());
236-
// System.err.println(" in: " + certs);
237-
// System.err.println(" in: " + getSign().getCert());
238230
if(certs != null) {
239231
signer = findByIssuerAndSerial(certs, ias.getName(), ias.getCertificateSerialNumber().getValue());
240232
}
@@ -266,7 +258,7 @@ public void signatureVerify(BIO bio, SignerInfoWithPkey si, X509AuxCertificate x
266258

267259
final int md_type = ASN1Registry.oid2nid( si.getDigestAlgorithm().getAlgorithm() );
268260
BIO btmp = bio;
269-
MessageDigest mdc = null;
261+
MessageDigest mdc;
270262

271263
for(;;) {
272264
if(btmp == null || (btmp = bio.findType(BIO.TYPE_MD)) == null) {
@@ -316,12 +308,10 @@ public void signatureVerify(BIO bio, SignerInfoWithPkey si, X509AuxCertificate x
316308
if(!sign.verify(os.getOctets())) {
317309
throw new NotVerifiedPKCS7Exception();
318310
}
319-
} catch(NotVerifiedPKCS7Exception e) {
311+
} catch (NotVerifiedPKCS7Exception e) {
320312
throw e;
321-
} catch(Exception e) {
322-
System.err.println("Other exception");
323-
e.printStackTrace(System.err);
324-
throw new NotVerifiedPKCS7Exception();
313+
} catch (GeneralSecurityException|IOException e) {
314+
throw new NotVerifiedPKCS7Exception(e);
325315
}
326316
}
327317

@@ -502,7 +492,7 @@ public static PKCS7 encrypt(Collection<X509AuxCertificate> certs, byte[] in, Cip
502492
*
503493
*/
504494
public void decrypt(PrivateKey pkey, X509AuxCertificate cert, BIO data, int flags) throws PKCS7Exception {
505-
if(!isEnveloped()) {
495+
if (!isEnveloped()) {
506496
throw new PKCS7Exception(F_PKCS7_DECRYPT, R_WRONG_CONTENT_TYPE);
507497
}
508498
try {
@@ -669,17 +659,16 @@ public BIO bioAddDigest(BIO pbio, AlgorithmIdentifier alg) throws PKCS7Exception
669659
/** c: PKCS7_dataDecode
670660
*
671661
*/
672-
public BIO dataDecode(PrivateKey pkey, BIO inBio, X509AuxCertificate pcert) throws PKCS7Exception {
662+
public BIO dataDecode(final PrivateKey pkey, BIO inBio, final X509AuxCertificate pcert) throws PKCS7Exception {
673663
BIO out = null; BIO btmp; BIO etmp; BIO bio;
674-
byte[] dataBody = null;
664+
byte[] dataBody;
675665
Collection<AlgorithmIdentifier> mdSk = null;
676666
Collection<RecipInfo> rsk = null;
677667
AlgorithmIdentifier encAlg = null;
678668
Cipher evpCipher = null;
679669
RecipInfo ri = null;
680670

681-
int i = getType();
682-
switch(i) {
671+
switch(getType()) {
683672
case ASN1Registry.NID_pkcs7_signed:
684673
dataBody = getSign().getContents().getOctetString().getOctets();
685674
mdSk = getSign().getMdAlgs();
@@ -692,7 +681,6 @@ public BIO dataDecode(PrivateKey pkey, BIO inBio, X509AuxCertificate pcert) thro
692681
try {
693682
evpCipher = EVP.getCipher(encAlg.getAlgorithm());
694683
} catch(Exception e) {
695-
e.printStackTrace(System.err);
696684
throw new PKCS7Exception(F_PKCS7_DATADECODE, R_UNSUPPORTED_CIPHER_TYPE, e);
697685
}
698686
break;
@@ -703,7 +691,6 @@ public BIO dataDecode(PrivateKey pkey, BIO inBio, X509AuxCertificate pcert) thro
703691
try {
704692
evpCipher = EVP.getCipher(encAlg.getAlgorithm());
705693
} catch(Exception e) {
706-
e.printStackTrace(System.err);
707694
throw new PKCS7Exception(F_PKCS7_DATADECODE, R_UNSUPPORTED_CIPHER_TYPE, e);
708695
}
709696
break;
@@ -722,68 +709,63 @@ public BIO dataDecode(PrivateKey pkey, BIO inBio, X509AuxCertificate pcert) thro
722709
} else {
723710
out.push(btmp);
724711
}
725-
} catch(Exception e) {
726-
e.printStackTrace(System.err);
712+
} catch (Exception e) {
727713
throw new PKCS7Exception(F_PKCS7_DATADECODE, R_UNKNOWN_DIGEST_TYPE, e);
728714
}
729715
}
730716
}
731717

732718

733-
if(evpCipher != null) {
719+
if (evpCipher != null) {
734720

735721
/* It was encrypted, we need to decrypt the secret key
736722
* with the private key */
737723

738724
/* Find the recipientInfo which matches the passed certificate
739725
* (if any)
740726
*/
741-
if(pcert != null) {
742-
for(Iterator<RecipInfo> iter = rsk.iterator(); iter.hasNext();) {
727+
if (pcert != null) {
728+
for (Iterator<RecipInfo> iter = rsk.iterator(); iter.hasNext();) {
743729
ri = iter.next();
744-
if(ri.compare(pcert)) {
745-
break;
746-
}
730+
if (ri.compare(pcert)) break;
747731
ri = null;
748732
}
749-
if(null == ri) {
733+
if (null == ri) {
750734
throw new PKCS7Exception(F_PKCS7_DATADECODE, R_NO_RECIPIENT_MATCHES_CERTIFICATE);
751735
}
752736
}
753737

754738
byte[] tmp = null;
755739
/* If we haven't got a certificate try each ri in turn */
756-
if(null == pcert) {
757-
for(Iterator<RecipInfo> iter = rsk.iterator(); iter.hasNext();) {
740+
if (null == pcert) {
741+
Exception cause = null;
742+
for (Iterator<RecipInfo> iter = rsk.iterator(); iter.hasNext();) {
758743
ri = iter.next();
759744
try {
760745
tmp = EVP.decrypt(ri.getEncKey().getOctets(), pkey);
761-
if(tmp != null) {
762-
break;
763-
}
764-
} catch(Exception e) {
765-
tmp = null;
746+
if (tmp != null) break;
747+
} catch (GeneralSecurityException e) {
748+
tmp = null; cause = e;
766749
}
767750
ri = null;
768751
}
769-
if(ri == null) {
770-
throw new PKCS7Exception(F_PKCS7_DATADECODE, R_NO_RECIPIENT_MATCHES_KEY);
752+
if (ri == null) {
753+
throw new PKCS7Exception(F_PKCS7_DATADECODE, R_NO_RECIPIENT_MATCHES_KEY, cause);
771754
}
772755
} else {
773756
try {
774757
Cipher cipher = SecurityHelper.getCipher(CipherSpec.getWrappingAlgorithm(pkey.getAlgorithm()));
775758
cipher.init(Cipher.DECRYPT_MODE, pkey);
776759
tmp = cipher.doFinal(ri.getEncKey().getOctets());
777-
} catch (Exception e) {
778-
e.printStackTrace(System.err);
760+
} catch (GeneralSecurityException e) {
779761
throw new PKCS7Exception(F_PKCS7_DATADECODE, -1, e);
780762
}
781763
}
782764

783765
ASN1Encodable params = encAlg.getParameters();
784766
try {
785767
String algo = org.jruby.ext.openssl.Cipher.Algorithm.getAlgorithmBase(evpCipher);
786-
if(params != null && params instanceof ASN1OctetString) {
768+
if (params instanceof ASN1OctetString) {
787769
if (algo.startsWith("RC2")) {
788770
// J9's IBMJCE needs this exceptional RC2 support.
789771
// Giving IvParameterSpec throws 'Illegal parameter' on IBMJCE.
@@ -798,8 +780,8 @@ public BIO dataDecode(PrivateKey pkey, BIO inBio, X509AuxCertificate pcert) thro
798780
} else {
799781
evpCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(tmp, algo));
800782
}
801-
} catch(Exception e) {
802-
e.printStackTrace(System.err);
783+
}
784+
catch (Exception e) {
803785
throw new PKCS7Exception(F_PKCS7_DATADECODE, -1, e);
804786
}
805787

@@ -901,7 +883,6 @@ public BIO dataInit(BIO bio) throws PKCS7Exception {
901883
}
902884
}
903885
} catch (Exception e) {
904-
e.printStackTrace(System.err);
905886
throw new PKCS7Exception(F_PKCS7_DATAINIT, R_ERROR_SETTING_CIPHER, e);
906887
}
907888

@@ -1278,10 +1259,10 @@ public int getType() {
12781259
*
12791260
*/
12801261
public ASN1OctetString getOctetString() {
1281-
if(isData()) {
1282-
return getData();
1283-
} else if(isOther() && getOther() != null && getOther() instanceof ASN1OctetString) {
1284-
return (ASN1OctetString)getOther();
1262+
if (isData()) return getData();
1263+
Object other;
1264+
if (isOther() && (other = getOther()) instanceof ASN1OctetString) {
1265+
return (ASN1OctetString) other;
12851266
}
12861267
return null;
12871268
}

src/main/java/org/jruby/ext/openssl/impl/PKCS7Exception.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class PKCS7Exception extends Exception {
3939
private String errorData;
4040

4141
public PKCS7Exception(int method, int reason) {
42-
this(method, reason, ""+null);
42+
this(method, reason, (String) null);
4343
}
4444

4545
public PKCS7Exception(int method, int reason, String errorData) {
@@ -53,7 +53,7 @@ public PKCS7Exception(int method, int reason, Throwable cause) {
5353
super("PKCS7[Method: " + method + ", Reason: " + reason + "]", cause);
5454
this.method = method;
5555
this.reason = reason;
56-
this.errorData = cause.getMessage();
56+
this.errorData = cause != null ? cause.getMessage() : null;
5757
}
5858

5959
public int getMethod() {

0 commit comments

Comments
 (0)