final int contentEstimated = (int) (Constants.DEFVAL_SIG_SIZE + 2L * crlInfo.getByteCount());
HashMap exc = new HashMap();
exc.put(PdfName.CONTENTS, new Integer(contentEstimated * 2 + 2));
sap.preClose(exc);
PdfPKCS7 sgn = new PdfPKCS7(key, chain, crlInfo.getCrls(), hashAlgorithm.getAlgorithmName(), null, false);
InputStream data = sap.getRangeStream();
final MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm.getAlgorithmName());
byte buf[] = new byte[8192];
int n;
while ((n = data.read(buf)) > 0) {
messageDigest.update(buf, 0, n);
}
byte hash[] = messageDigest.digest();
Calendar cal = Calendar.getInstance();
byte[] ocsp = null;
if (options.isOcspEnabledX() && chain.length >= 2) {
options.log("console.getOCSPURL");
String url = PdfPKCS7.getOCSPURL((X509Certificate) chain[0]);
if (url != null && url.length() > 0) {
options.log("console.readingOCSP");
final OcspClientBouncyCastle ocspClient = new OcspClientBouncyCastle((X509Certificate) chain[0],
(X509Certificate) chain[1], url);
ocspClient.setProxy(tmpProxy);
ocsp = ocspClient.getEncoded();
}
}
byte sh[] = sgn.getAuthenticatedAttributeBytes(hash, cal, ocsp);
sgn.update(sh, 0, sh.length);
TSAClientBouncyCastle tsc = null;
if (options.isTimestampX() && !StringUtils.isEmpty(options.getTsaUrl())) {
options.log("console.creatingTsaClient");
tsc = new TSAClientBouncyCastle(options.getTsaUrl(), StringUtils.emptyNull(options.getTsaUser()),
StringUtils.emptyNull(options.getTsaPasswd()));
tsc.setProxy(tmpProxy);
final String policyOid = options.getTsaPolicy();
if (StringUtils.hasLength(policyOid)) {
options.log("console.settingTsaPolicy", policyOid);
tsc.setPolicy(policyOid);
}
}
byte[] encodedSig = sgn.getEncodedPKCS7(hash, cal, tsc, ocsp);
if (contentEstimated + 2 < encodedSig.length) {
System.err.println("SigSize - contentEstimated=" + contentEstimated + ", sigLen=" + encodedSig.length);
throw new Exception("Not enough space");
}