Search in sources :

Example 16 with ExceptionConverter

use of pdftk.com.lowagie.text.ExceptionConverter in project staplr by pridiltal.

the class PdfPKCS7 method loadCacertsKeyStore.

/**
 * Loads the default root certificates at <java.home>/lib/security/cacerts.
 * @param provider the provider or <code>null</code> for the default provider
 * @return a <CODE>KeyStore</CODE>
 */
public static KeyStore loadCacertsKeyStore(String provider) {
    File file = new File(System.getProperty("java.home"), "lib");
    file = new File(file, "security");
    file = new File(file, "cacerts");
    FileInputStream fin = null;
    try {
        fin = new FileInputStream(file);
        KeyStore k;
        if (provider == null)
            k = KeyStore.getInstance("JKS");
        else
            k = KeyStore.getInstance("JKS", provider);
        k.load(fin, null);
        return k;
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    } finally {
        try {
            fin.close();
        } catch (Exception ex) {
        }
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) File(java.io.File) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CRLException(java.security.cert.CRLException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 17 with ExceptionConverter

use of pdftk.com.lowagie.text.ExceptionConverter in project staplr by pridiltal.

the class PdfPKCS7 method getIssuer.

/**
 * Get the "issuer" from the TBSCertificate bytes that are passed in
 * @param enc a TBSCertificate in a byte array
 * @return a ASN1Primitive
 */
private static ASN1Primitive getIssuer(byte[] enc) {
    try {
        ASN1InputStream in = new ASN1InputStream(new ByteArrayInputStream(enc));
        ASN1Sequence seq = (ASN1Sequence) in.readObject();
        return (ASN1Primitive) seq.getObjectAt(seq.getObjectAt(0) instanceof DERTaggedObject ? 3 : 2);
    } catch (IOException e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ByteArrayInputStream(java.io.ByteArrayInputStream) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) IOException(java.io.IOException) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Example 18 with ExceptionConverter

use of pdftk.com.lowagie.text.ExceptionConverter in project staplr by pridiltal.

the class PdfPKCS7 method getEncodedPKCS1.

/**
 * Gets the bytes for the PKCS#1 object.
 * @return a byte array
 */
public byte[] getEncodedPKCS1() {
    try {
        if (externalDigest != null)
            digest = externalDigest;
        else
            digest = sig.sign();
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        ASN1OutputStream dout = new ASN1OutputStream(bOut);
        dout.writeObject(new DEROctetString(digest));
        dout.close();
        return bOut.toByteArray();
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ASN1OutputStream(org.bouncycastle.asn1.ASN1OutputStream) DEROctetString(org.bouncycastle.asn1.DEROctetString) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CRLException(java.security.cert.CRLException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 19 with ExceptionConverter

use of pdftk.com.lowagie.text.ExceptionConverter in project staplr by pridiltal.

the class PdfPKCS7 method getSubject.

/**
 * Get the "subject" from the TBSCertificate bytes that are passed in
 * @param enc A TBSCertificate in a byte array
 * @return a ASN1Primitive
 */
private static ASN1Primitive getSubject(byte[] enc) {
    try {
        ASN1InputStream in = new ASN1InputStream(new ByteArrayInputStream(enc));
        ASN1Sequence seq = (ASN1Sequence) in.readObject();
        return (ASN1Primitive) seq.getObjectAt(seq.getObjectAt(0) instanceof DERTaggedObject ? 5 : 4);
    } catch (IOException e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ByteArrayInputStream(java.io.ByteArrayInputStream) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) IOException(java.io.IOException) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Example 20 with ExceptionConverter

use of pdftk.com.lowagie.text.ExceptionConverter in project staplr by pridiltal.

the class PdfStamperImp method alterContents.

void alterContents() throws IOException {
    for (Iterator i = pagesToContent.values().iterator(); i.hasNext(); ) {
        PageStamp ps = (PageStamp) i.next();
        PdfDictionary pageN = ps.pageN;
        markUsed(pageN);
        PdfArray ar = null;
        PdfObject content = PdfReader.getPdfObject(pageN.get(PdfName.CONTENTS), pageN);
        if (content == null) {
            ar = new PdfArray();
            pageN.put(PdfName.CONTENTS, ar);
        } else if (content.isArray()) {
            ar = (PdfArray) content;
            markUsed(ar);
        } else if (content.isStream()) {
            ar = new PdfArray();
            ar.add(pageN.get(PdfName.CONTENTS));
            pageN.put(PdfName.CONTENTS, ar);
        } else {
            ar = new PdfArray();
            pageN.put(PdfName.CONTENTS, ar);
        }
        ByteBuffer out = new ByteBuffer();
        if (ps.under != null) {
            out.append(PdfContents.SAVESTATE);
            applyRotation(pageN, out);
            out.append(ps.under.getInternalBuffer());
            out.append(PdfContents.RESTORESTATE);
        }
        if (ps.over != null)
            out.append(PdfContents.SAVESTATE);
        PdfStream stream = new PdfStream(out.toByteArray());
        try {
            stream.flateCompress();
        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }
        ar.addFirst(addToBody(stream).getIndirectReference());
        out.reset();
        if (ps.over != null) {
            out.append(' ');
            out.append(PdfContents.RESTORESTATE);
            out.append(PdfContents.SAVESTATE);
            applyRotation(pageN, out);
            out.append(ps.over.getInternalBuffer());
            out.append(PdfContents.RESTORESTATE);
            stream = new PdfStream(out.toByteArray());
            try {
                stream.flateCompress();
            } catch (Exception e) {
                throw new ExceptionConverter(e);
            }
            ar.add(addToBody(stream).getIndirectReference());
        }
        alterResources(ps);
    }
}
Also used : ExceptionConverter(pdftk.com.lowagie.text.ExceptionConverter) Iterator(java.util.Iterator) IOException(java.io.IOException) DocumentException(pdftk.com.lowagie.text.DocumentException)

Aggregations

ExceptionConverter (pdftk.com.lowagie.text.ExceptionConverter)46 IOException (java.io.IOException)34 DocumentException (pdftk.com.lowagie.text.DocumentException)17 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 SignatureException (java.security.SignatureException)5 InvalidKeyException (java.security.InvalidKeyException)4 NoSuchProviderException (java.security.NoSuchProviderException)4 CRLException (java.security.cert.CRLException)4 CertificateException (java.security.cert.CertificateException)4 ArrayList (java.util.ArrayList)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)3 ASN1OutputStream (org.bouncycastle.asn1.ASN1OutputStream)3 DEROctetString (org.bouncycastle.asn1.DEROctetString)3 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)3 Color (java.awt.Color)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2