1 47 package com.lowagie.text.pdf; 48 49 import java.io.ByteArrayOutputStream ; 50 import java.security.PrivateKey ; 51 import java.security.cert.CRL ; 52 import java.security.cert.Certificate ; 53 54 import com.lowagie.text.ExceptionConverter; 55 56 59 public abstract class PdfSigGenericPKCS extends PdfSignature { 60 63 protected String hashAlgorithm; 64 67 protected String provider = null; 68 71 protected PdfPKCS7 pkcs; 72 75 protected String name; 76 77 private byte externalDigest[]; 78 private byte externalRSAdata[]; 79 private String digestEncryptionAlgorithm; 80 81 86 public PdfSigGenericPKCS(PdfName filter, PdfName subFilter) { 87 super(filter, subFilter); 88 } 89 90 96 public void setSignInfo(PrivateKey privKey, Certificate [] certChain, CRL [] crlList) { 97 try { 98 pkcs = new PdfPKCS7(privKey, certChain, crlList, hashAlgorithm, provider, PdfName.ADBE_PKCS7_SHA1.equals(get(PdfName.SUBFILTER))); 99 pkcs.setExternalDigest(externalDigest, externalRSAdata, digestEncryptionAlgorithm); 100 if (PdfName.ADBE_X509_RSA_SHA1.equals(get(PdfName.SUBFILTER))) { 101 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 102 for (int k = 0; k < certChain.length; ++k) { 103 bout.write(certChain[k].getEncoded()); 104 } 105 bout.close(); 106 setCert(bout.toByteArray()); 107 setContents(pkcs.getEncodedPKCS1()); 108 } 109 else 110 setContents(pkcs.getEncodedPKCS7()); 111 name = PdfPKCS7.getSubjectFields(pkcs.getSigningCertificate()).getField("CN"); 112 if (name != null) 113 put(PdfName.NAME, new PdfString(name, PdfObject.TEXT_UNICODE)); 114 pkcs = new PdfPKCS7(privKey, certChain, crlList, hashAlgorithm, provider, PdfName.ADBE_PKCS7_SHA1.equals(get(PdfName.SUBFILTER))); 115 pkcs.setExternalDigest(externalDigest, externalRSAdata, digestEncryptionAlgorithm); 116 } 117 catch (Exception e) { 118 throw new ExceptionConverter(e); 119 } 120 } 121 122 130 public void setExternalDigest(byte digest[], byte RSAdata[], String digestEncryptionAlgorithm) { 131 externalDigest = digest; 132 externalRSAdata = RSAdata; 133 this.digestEncryptionAlgorithm = digestEncryptionAlgorithm; 134 } 135 136 140 public String getName() { 141 return name; 142 } 143 144 148 public PdfPKCS7 getSigner() { 149 return pkcs; 150 } 151 152 157 public byte[] getSignerContents() { 158 if (PdfName.ADBE_X509_RSA_SHA1.equals(get(PdfName.SUBFILTER))) 159 return pkcs.getEncodedPKCS1(); 160 else 161 return pkcs.getEncodedPKCS7(); 162 } 163 164 167 public static class VeriSign extends PdfSigGenericPKCS { 168 171 public VeriSign() { 172 super(PdfName.VERISIGN_PPKVS, PdfName.ADBE_PKCS7_DETACHED); 173 hashAlgorithm = "MD5"; 174 put(PdfName.R, new PdfNumber(65537)); 175 } 176 177 181 public VeriSign(String provider) { 182 this(); 183 this.provider = provider; 184 } 185 } 186 187 190 public static class PPKLite extends PdfSigGenericPKCS { 191 194 public PPKLite() { 195 super(PdfName.ADOBE_PPKLITE, PdfName.ADBE_X509_RSA_SHA1); 196 hashAlgorithm = "SHA1"; 197 put(PdfName.R, new PdfNumber(65541)); 198 } 199 200 204 public PPKLite(String provider) { 205 this(); 206 this.provider = provider; 207 } 208 } 209 210 213 public static class PPKMS extends PdfSigGenericPKCS { 214 217 public PPKMS() { 218 super(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_SHA1); 219 hashAlgorithm = "SHA1"; 220 } 221 222 226 public PPKMS(String provider) { 227 this(); 228 this.provider = provider; 229 } 230 } 231 } 232 | Popular Tags |