Skip to content

Commit

Permalink
[refactor] PKCS7 backing SignedInfoWithPKey cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Apr 8, 2024
1 parent e0c6f9b commit 461ed98
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
18 changes: 10 additions & 8 deletions src/main/java/org/jruby/ext/openssl/impl/Signed.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,19 +354,21 @@ private static X509AuxCertificate certificateFromASN1(ASN1Encodable current) thr
}

private static Set<AlgorithmIdentifier> algorithmIdentifiersFromASN1Set(ASN1Encodable content) {
ASN1Set set = (ASN1Set)content;
Set<AlgorithmIdentifier> result = new HashSet<AlgorithmIdentifier>();
for(Enumeration<?> e = set.getObjects(); e.hasMoreElements();) {
result.add(AlgorithmIdentifier.getInstance(e.nextElement()));
ASN1Set set = (ASN1Set) content;
final int len = set.size();
Set<AlgorithmIdentifier> result = new HashSet<>(len);
for (int i = 0; i < len; i++) {
result.add(AlgorithmIdentifier.getInstance(set.getObjectAt(i)));
}
return result;
}

private static Collection<SignerInfoWithPkey> signerInfosFromASN1Set(ASN1Encodable content) {
ASN1Set set = (ASN1Set)content;
Collection<SignerInfoWithPkey> result = new ArrayList<SignerInfoWithPkey>();
for(Enumeration<?> e = set.getObjects(); e.hasMoreElements();) {
result.add(SignerInfoWithPkey.getInstance(e.nextElement()));
ASN1Set set = (ASN1Set) content;
final int len = set.size();
Collection<SignerInfoWithPkey> result = new ArrayList<>();
for (int i = 0; i < len; i++) {
result.add(SignerInfoWithPkey.getInstance(set.getObjectAt(i)));
}
return result;
}
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/org/jruby/ext/openssl/impl/SignerInfoWithPkey.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ public class SignerInfoWithPkey implements ASN1Encodable {
private ASN1OctetString encryptedDigest;
private ASN1Set unauthenticatedAttributes;

public static SignerInfoWithPkey getInstance(Object o) {
if(o instanceof SignerInfo) {
return (SignerInfoWithPkey)o;
} else if (o instanceof ASN1Sequence) {
return new SignerInfoWithPkey((ASN1Sequence)o);
public static SignerInfoWithPkey getInstance(ASN1Encodable o) {
if (o instanceof SignerInfo) {
final SignerInfo info = (SignerInfo) o;
return new SignerInfoWithPkey(info.getVersion(), info.getIssuerAndSerialNumber(), info.getDigestAlgorithm(),
info.getAuthenticatedAttributes(), info.getDigestEncryptionAlgorithm(),
info.getEncryptedDigest(), info.getUnauthenticatedAttributes());
}
if (o instanceof ASN1Sequence) {
return new SignerInfoWithPkey((ASN1Sequence) o);
}

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

public SignerInfoWithPkey(ASN1Integer version,
IssuerAndSerialNumber issuerAndSerialNumber,
AlgorithmIdentifier digAlgorithm,
ASN1Set authenticatedAttributes,
AlgorithmIdentifier digEncryptionAlgorithm,
ASN1OctetString encryptedDigest,
ASN1Set unauthenticatedAttributes) {
public SignerInfoWithPkey(ASN1Integer version,
IssuerAndSerialNumber issuerAndSerialNumber,
AlgorithmIdentifier digAlgorithm,
ASN1Set authenticatedAttributes,
AlgorithmIdentifier digEncryptionAlgorithm,
ASN1OctetString encryptedDigest,
ASN1Set unauthenticatedAttributes) {
this.version = version;
this.issuerAndSerialNumber = issuerAndSerialNumber;
this.digAlgorithm = digAlgorithm;
Expand All @@ -113,16 +117,16 @@ public SignerInfoWithPkey(ASN1Integer version,
this.unauthenticatedAttributes = unauthenticatedAttributes;
}

public SignerInfoWithPkey(ASN1Sequence seq) {
Enumeration e = seq.getObjects();
SignerInfoWithPkey(ASN1Sequence seq) {
Enumeration e = seq.getObjects();

version = (ASN1Integer)e.nextElement();
issuerAndSerialNumber = IssuerAndSerialNumber.getInstance(e.nextElement());
digAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement());

Object obj = e.nextElement();

if(obj instanceof ASN1TaggedObject) {
if (obj instanceof ASN1TaggedObject) {
authenticatedAttributes = ASN1Set.getInstance((ASN1TaggedObject)obj, false);

digEncryptionAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement());
Expand Down

0 comments on commit 461ed98

Please sign in to comment.