Skip to content

Commit 461ed98

Browse files
committed
[refactor] PKCS7 backing SignedInfoWithPKey cleanup
1 parent e0c6f9b commit 461ed98

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,21 @@ private static X509AuxCertificate certificateFromASN1(ASN1Encodable current) thr
354354
}
355355

356356
private static Set<AlgorithmIdentifier> algorithmIdentifiersFromASN1Set(ASN1Encodable content) {
357-
ASN1Set set = (ASN1Set)content;
358-
Set<AlgorithmIdentifier> result = new HashSet<AlgorithmIdentifier>();
359-
for(Enumeration<?> e = set.getObjects(); e.hasMoreElements();) {
360-
result.add(AlgorithmIdentifier.getInstance(e.nextElement()));
357+
ASN1Set set = (ASN1Set) content;
358+
final int len = set.size();
359+
Set<AlgorithmIdentifier> result = new HashSet<>(len);
360+
for (int i = 0; i < len; i++) {
361+
result.add(AlgorithmIdentifier.getInstance(set.getObjectAt(i)));
361362
}
362363
return result;
363364
}
364365

365366
private static Collection<SignerInfoWithPkey> signerInfosFromASN1Set(ASN1Encodable content) {
366-
ASN1Set set = (ASN1Set)content;
367-
Collection<SignerInfoWithPkey> result = new ArrayList<SignerInfoWithPkey>();
368-
for(Enumeration<?> e = set.getObjects(); e.hasMoreElements();) {
369-
result.add(SignerInfoWithPkey.getInstance(e.nextElement()));
367+
ASN1Set set = (ASN1Set) content;
368+
final int len = set.size();
369+
Collection<SignerInfoWithPkey> result = new ArrayList<>();
370+
for (int i = 0; i < len; i++) {
371+
result.add(SignerInfoWithPkey.getInstance(set.getObjectAt(i)));
370372
}
371373
return result;
372374
}

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,15 @@ public class SignerInfoWithPkey implements ASN1Encodable {
7272
private ASN1OctetString encryptedDigest;
7373
private ASN1Set unauthenticatedAttributes;
7474

75-
public static SignerInfoWithPkey getInstance(Object o) {
76-
if(o instanceof SignerInfo) {
77-
return (SignerInfoWithPkey)o;
78-
} else if (o instanceof ASN1Sequence) {
79-
return new SignerInfoWithPkey((ASN1Sequence)o);
75+
public static SignerInfoWithPkey getInstance(ASN1Encodable o) {
76+
if (o instanceof SignerInfo) {
77+
final SignerInfo info = (SignerInfo) o;
78+
return new SignerInfoWithPkey(info.getVersion(), info.getIssuerAndSerialNumber(), info.getDigestAlgorithm(),
79+
info.getAuthenticatedAttributes(), info.getDigestEncryptionAlgorithm(),
80+
info.getEncryptedDigest(), info.getUnauthenticatedAttributes());
81+
}
82+
if (o instanceof ASN1Sequence) {
83+
return new SignerInfoWithPkey((ASN1Sequence) o);
8084
}
8185

8286
throw new IllegalArgumentException("unknown object in factory: " + o.getClass().getName());
@@ -97,13 +101,13 @@ public SignerInfoWithPkey dup() {
97101
SignerInfoWithPkey() {
98102
}
99103

100-
public SignerInfoWithPkey(ASN1Integer version,
101-
IssuerAndSerialNumber issuerAndSerialNumber,
102-
AlgorithmIdentifier digAlgorithm,
103-
ASN1Set authenticatedAttributes,
104-
AlgorithmIdentifier digEncryptionAlgorithm,
105-
ASN1OctetString encryptedDigest,
106-
ASN1Set unauthenticatedAttributes) {
104+
public SignerInfoWithPkey(ASN1Integer version,
105+
IssuerAndSerialNumber issuerAndSerialNumber,
106+
AlgorithmIdentifier digAlgorithm,
107+
ASN1Set authenticatedAttributes,
108+
AlgorithmIdentifier digEncryptionAlgorithm,
109+
ASN1OctetString encryptedDigest,
110+
ASN1Set unauthenticatedAttributes) {
107111
this.version = version;
108112
this.issuerAndSerialNumber = issuerAndSerialNumber;
109113
this.digAlgorithm = digAlgorithm;
@@ -113,16 +117,16 @@ public SignerInfoWithPkey(ASN1Integer version,
113117
this.unauthenticatedAttributes = unauthenticatedAttributes;
114118
}
115119

116-
public SignerInfoWithPkey(ASN1Sequence seq) {
117-
Enumeration e = seq.getObjects();
120+
SignerInfoWithPkey(ASN1Sequence seq) {
121+
Enumeration e = seq.getObjects();
118122

119123
version = (ASN1Integer)e.nextElement();
120124
issuerAndSerialNumber = IssuerAndSerialNumber.getInstance(e.nextElement());
121125
digAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement());
122126

123127
Object obj = e.nextElement();
124128

125-
if(obj instanceof ASN1TaggedObject) {
129+
if (obj instanceof ASN1TaggedObject) {
126130
authenticatedAttributes = ASN1Set.getInstance((ASN1TaggedObject)obj, false);
127131

128132
digEncryptionAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement());

0 commit comments

Comments
 (0)