Red Hat Application Migration Toolkit
package iaik.pkcs.pkcs7;
import iaik.asn1.ASN;
import iaik.asn1.ASN1Object;
import iaik.asn1.CON_SPEC;
import iaik.asn1.CodingException;
import iaik.asn1.DerCoder;
import iaik.asn1.DerInputStream;
import iaik.asn1.INTEGER;
import iaik.asn1.ObjectID;
import iaik.asn1.SEQUENCE;
import iaik.asn1.structures.AlgorithmID;
import iaik.asn1.structures.Attribute;
import iaik.pkcs.PKCSException;
import iaik.pkcs.PKCSParsingException;
import iaik.pkcs.pkcs7.ContentInfoStream;
import iaik.pkcs.pkcs7.ContentStream;
import iaik.pkcs.pkcs7.DataStream;
import iaik.pkcs.pkcs7.IssuerAndSerialNumber;
import iaik.pkcs.pkcs7.SignerInfo;
import iaik.pkcs.pkcs7.a;
import iaik.pkcs.pkcs7.d;
import iaik.utils.CryptoUtils;
import iaik.utils.EOFListener;
import iaik.utils.NotifyEOFInputStream;
import iaik.utils.Util;
import iaik.x509.X509CRL;
import iaik.x509.X509Certificate;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.SignatureException;
import java.security.cert.CRLException;
import java.security.cert.CertificateException;
import java.util.Enumeration;
import java.util.Vector;
public class SignedDataStream implements d, EOFListener, ContentStream {
// $FF: synthetic field
static Class b;
// $FF: synthetic field
static Class c;
protected int block_size;
protected int mode;
protected InputStream input_stream;
protected DerInputStream this_object;
protected Vector signer_infos;
protected X509CRL[] crls;
protected X509Certificate[] certificates;
protected ContentInfoStream content_info;
protected ObjectID content_type;
a a;
protected int version;
public static final int EXPLICIT = 2;
public static final int IMPLICIT = 1;
private static final boolean d = false;
static Class a(String var0) {
try {
return Class.forName(var0);
} catch (ClassNotFoundException var2) {
throw new NoClassDefFoundError(var2.getMessage());
}
}
public void writeTo(OutputStream var1, int var2) throws IOException {
try {
DerCoder.encodeTo(this.toASN1Object(var2), var1);
} catch (PKCSException var4) {
throw new IOException(var4.toString());
}
}
public void writeTo(OutputStream var1) throws IOException {
try {
DerCoder.encodeTo(this.toASN1Object(), var1);
} catch (PKCSException var3) {
throw new IOException(var3.toString());
}
}
public void verify(PublicKey var1, int var2) throws SignatureException {
if(var2 >= 0 && var2 < this.signer_infos.size()) {
Attribute[] var3 = ((SignerInfo)this.signer_infos.elementAt(var2)).getAuthenticatedAttributes();
try {
AlgorithmID var4 = ((SignerInfo)this.signer_infos.elementAt(var2)).getDigestAlgorithm();
byte[] var6 = this.getMessageDigest(var4);
byte[] var5;
if(var3 != null) {
var5 = this.getSignedDigest(var2);
if(!CryptoUtils.equalsBlock(var6, var5)) {
throw new SignatureException("Signature verification error: message hash!");
}
MessageDigest var7 = var4.getMessageDigestInstance();
if(this.version == 2) {
var6 = var7.digest(DerCoder.encode(ASN.createSequenceOf(var3)));
} else {
var6 = var7.digest(DerCoder.encode(ASN.createSetOf(var3)));
}
}
var5 = ((SignerInfo)this.signer_infos.elementAt(var2)).getDigest(var1);
if(!CryptoUtils.equalsBlock(var6, var5)) {
throw new SignatureException("Signature verification error: signature value!");
}
} catch (SignatureException var8) {
throw var8;
} catch (Exception var9) {
throw new SignatureException("Error verifying the signature: " + var9.getMessage());
}
} else {
throw new SignatureException("SignerInfo does not exist. Wrong index.");
}
}
public SignerInfo verify(X509Certificate var1) throws SignatureException {
int var2 = this.a(var1);
if(var2 == -1) {
throw new SignatureException("Cannot do verification. No signer for this certificate!");
} else {
this.verify(var1.getPublicKey(), var2);
return (SignerInfo)this.signer_infos.elementAt(var2);
}
}
public X509Certificate verify(int var1) throws SignatureException {
if(var1 >= 0 && var1 < this.signer_infos.size()) {
try {
X509Certificate var2 = this.getCertificate(((SignerInfo)this.signer_infos.elementAt(var1)).getIssuerAndSerialNumber());
if(var2 == null) {
throw new SignatureException("Certificate for verifying the signature not found!");
} else {
this.verify(var2.getPublicKey(), var1);
return var2;
}
} catch (Exception var3) {
throw new SignatureException(var3.getMessage());
}
} else {
throw new SignatureException("SignerInfo does not exist. Wrong index.");
}
}
public String toString(boolean var1) {
StringBuffer var2 = new StringBuffer();
var2.append("Version: " + this.version + "\n");
AlgorithmID[] var3 = this.a.a();
int var4;
if(var3.length > 0) {
var2.append("digestAlgorithms: ");
for(var4 = 0; var4 < var3.length; ++var4) {
var2.append(var3[var4].getName() + ",");
}
var2.setLength(var2.length() - 1);
var2.append("\n");
}
var2.append("ContentInfo: {\n" + this.content_info.toString(var1));
var2.append("\n}\n");
if(this.certificates != null) {
var2.append("certificates: " + this.certificates.length + "\n");
}
if(this.crls != null) {
var2.append("crls: " + this.crls.length + "\n");
}
if(var1) {
var4 = 1;
for(Enumeration var5 = this.signer_infos.elements(); var5.hasMoreElements(); ++var4) {
var2.append("signerInfo " + var4 + ": {\n");
var2.append(((SignerInfo)var5.nextElement()).toString(true) + "}");
}
} else {
var2.append("signerInfos: " + this.signer_infos.size() + "\n");
}
return var2.toString();
}
public String toString() {
return this.toString(false);
}
protected ASN1Object toASN1Object(int var1) throws PKCSException {
if(var1 <= 0) {
var1 = this.block_size;
}
if(this.signer_infos == null) {
throw new PKCSException("No SignerInfo specified!");
} else {
if(this.mode == 1) {
try {
this.input_stream = this.a.a(this.input_stream, true);
} catch (NoSuchAlgorithmException var5) {
throw new PKCSException("No implementation for hash algorithm: " + var5.getMessage());
}
}
if(this.mode == 1 && this.input_stream != null) {
this.content_info = new ContentInfoStream(new DataStream(this.input_stream, var1));
} else {
this.content_info = new ContentInfoStream(ObjectID.pkcs7_data);
}
SEQUENCE var2 = new SEQUENCE(true);
try {
var2.addComponent(new INTEGER(this.version));
var2.addComponent(ASN.createSetOf(this.a.a()));
var2.addComponent(this.content_info.toASN1Object());
ASN1Object var3;
if(this.certificates != null) {
var3 = ASN.createSetOf(this.certificates);
var2.addComponent(new CON_SPEC(0, var3, true));
}
if(this.crls != null) {
var3 = ASN.createSetOf(this.crls);
var2.addComponent(new CON_SPEC(1, var3, true));
}
var2.addComponent(ASN.createSetOf(this.signer_infos));
return var2;
} catch (CodingException var4) {
throw new PKCSException(var4.toString());
}
}
}
public ASN1Object toASN1Object() throws PKCSException {
return this.toASN1Object(-1);
}
public void setSignerInfos(SignerInfo[] var1) throws NoSuchAlgorithmException {
for(int var2 = 0; var2 < var1.length; ++var2) {
this.addSignerInfo(var1[var2]);
}
}
public void setMessageDigest(AlgorithmID var1, byte[] var2) throws NoSuchAlgorithmException {
this.a.b(var1, var2);
}
public void setInputStream(InputStream var1) {
this.input_stream = var1;
}
public void setCertificates(X509Certificate[] var1) {
this.certificates = var1;
}
public void setCRLs(X509CRL[] var1) {
this.crls = var1;
}
public void setBlockSize(int var1) {
this.block_size = var1;
}
public void notifyEOF() throws IOException {
try {
while(this.this_object.nextIsContextSpecific()) {
int var1 = this.this_object.readContextSpecific(17);
DerInputStream var2 = this.this_object.readSet();
switch(var1) {
case 0:
case 2:
Vector var3 = new Vector();
try {
while(var2.nextTag() > -1) {
var3.addElement(new X509Certificate(var2));
}
} catch (CertificateException var7) {
throw new IOException("Cannot parse certificate: " + var7.getMessage());
}
this.certificates = new X509Certificate[var3.size()];
var3.copyInto(this.certificates);
break;
case 1:
case 3:
Vector var4 = new Vector();
try {
while(var2.nextTag() > -1) {
var4.addElement(new X509CRL(var2));
}
} catch (CRLException var6) {
throw new IOException("Cannot parse crl: " + var6.getMessage());
}
this.crls = new X509CRL[var4.size()];
var4.copyInto(this.crls);
}
}
this.signer_infos = Util.getVector(ASN.parseSequenceOf(DerCoder.decode(this.this_object), b != null?b:(b = a("iaik.pkcs.pkcs7.SignerInfo"))));
Object[] var9 = Util.toArray(this.signer_infos);
for(int var10 = 0; var10 < var9.length; ++var10) {
SignerInfo var11 = (SignerInfo)var9[var10];
var11.f = this;
}
this.this_object.readEOC();
} catch (CodingException var8) {
throw new IOException("Error parsing Object! " + var8.getMessage());
}
}
public int getVersion() {
return this.version;
}
public SignerInfo[] getSignerInfos() {
return (SignerInfo[])Util.toArray(this.signer_infos, b != null?b:(b = a("iaik.pkcs.pkcs7.SignerInfo")));
}
private int a(X509Certificate var1) {
Enumeration var2 = this.signer_infos.elements();
for(int var3 = 0; var2.hasMoreElements(); ++var3) {
SignerInfo var4 = (SignerInfo)var2.nextElement();
IssuerAndSerialNumber var5 = var4.getIssuerAndSerialNumber();
if(var5.isIssuerOf(var1)) {
return var3;
}
}
return -1;
}
public byte[] getSignedDigest(int var1) throws PKCSException {
Attribute[] var2 = ((SignerInfo)this.signer_infos.elementAt(var1)).getAuthenticatedAttributes();
if(var2 == null) {
throw new PKCSException("No authenticated attributes included in SignerInfo!");
} else {
for(int var3 = 0; var3 < var2.length; ++var3) {
if(var2[var3].getType().equals(ObjectID.messageDigest)) {
return (byte[])var2[var3].getValue()[0].getValue();
}
}
throw new PKCSException("Message digest not included in authenticated attributes!");
}
}
public int getMode() {
return this.mode;
}
public byte[] getMessageDigest(AlgorithmID var1) throws NoSuchAlgorithmException {
return this.a.c(var1);
}
public InputStream getInputStream() {
return this.input_stream;
}
public AlgorithmID[] getDigestAlgorithms() {
return this.a.a();
}
public ObjectID getContentType() {
return ObjectID.pkcs7_signedData;
}
public X509Certificate[] getCertificates() {
return this.certificates;
}
public X509Certificate getCertificate(IssuerAndSerialNumber var1) throws PKCSException {
if(this.certificates == null) {
throw new PKCSException("Certificate not found!");
} else {
int var2;
for(var2 = 0; var2 < this.certificates.length && !var1.isIssuerOf(this.certificates[var2]); ++var2) {
;
}
if(var2 == this.certificates.length) {
throw new PKCSException("Certificate not found!");
} else {
return this.certificates[var2];
}
}
}
public X509CRL[] getCRLs() {
return this.crls;
}
public int getBlockSize() {
return this.block_size;
}
public void decode(InputStream var1) throws PKCSParsingException, IOException {
if(!(var1 instanceof DerInputStream)) {
var1 = new DerInputStream((InputStream)var1);
}
this.this_object = ((DerInputStream)var1).readSequence();
this.version = this.this_object.readInteger().intValue();
try {
this.a.a((AlgorithmID[])ASN.parseSequenceOf(DerCoder.decode(this.this_object), c != null?c:(c = a("iaik.asn1.structures.AlgorithmID"))));
} catch (CodingException var6) {
throw new IOException("Error parsing digest algorithms!");
}
this.content_info = new ContentInfoStream(this.this_object);
this.content_type = this.content_info.getContentType();
DataStream var2;
if(this.mode != 2 && this.content_info.hasContent()) {
if(this.content_type.equals(ObjectID.pkcs7_data)) {
var2 = (DataStream)this.content_info.getContent();
this.input_stream = var2.getInputStream();
try {
this.input_stream = this.a.a(this.input_stream, true);
} catch (NoSuchAlgorithmException var5) {
throw new IOException("No implementation for hash algorithm: " + var5.getMessage());
}
this.input_stream = new NotifyEOFInputStream(this.input_stream);
((NotifyEOFInputStream)this.input_stream).addEOFListener(this);
} else {
throw new IOException("SignedData only for content type Data at this time!");
}
} else {
if(this.content_info.hasContent()) {
try {
var2 = (DataStream)this.content_info.getContent();
InputStream var3 = var2.getInputStream();
byte[] var4 = new byte[1024];
while(var3.read(var4) > -1) {
;
}
} catch (Exception var7) {
;
}
}
this.mode = 2;
this.notifyEOF();
}
}
public void addSignerInfo(SignerInfo var1) throws NoSuchAlgorithmException {
var1.f = this;
AlgorithmID var2 = var1.getDigestAlgorithm();
if(!this.a.b(var2)) {
this.a.a(var2);
}
if(this.mode == 2) {
this.input_stream = this.a.a(this.input_stream, false);
}
this.signer_infos.addElement(var1);
}
public SignedDataStream(InputStream var1, AlgorithmID[] var2) throws IOException {
this();
this.input_stream = var1;
this.mode = 2;
this.a = new a(this, var2);
try {
this.input_stream = this.a.a(this.input_stream, true);
} catch (NoSuchAlgorithmException var4) {
throw new IOException("No implementation for hash algorithm: " + var4.getMessage());
}
}
public SignedDataStream(InputStream var1, int var2) {
this();
this.content_type = ObjectID.pkcs7_data;
this.input_stream = var1;
this.mode = var2;
}
public SignedDataStream(InputStream var1) throws IOException, PKCSParsingException {
this();
this.decode(var1);
}
public SignedDataStream(ObjectID var1) {
this();
this.mode = 2;
this.content_type = var1;
}
protected SignedDataStream() {
this.version = 1;
this.a = new a(this);
this.signer_infos = new Vector();
this.block_size = -1;
this.mode = 1;
}
}