Ejemplo n.º 1
0
 protected internal override void CheckPdfString(PdfString @string)
 {
     if (@string.GetValueBytes().Length > GetMaxStringLength())
     {
         throw new PdfAConformanceException(PdfAConformanceException.PdfStringIsTooLong);
     }
 }
Ejemplo n.º 2
0
 protected internal override void CheckPdfString(PdfString @string)
 {
     if (@string.GetValueBytes().Length > GetMaxStringLength())
     {
         throw new PdfAConformanceException(PdfAConformanceException.PDF_STRING_IS_TOO_LONG);
     }
 }
Ejemplo n.º 3
0
        public override String Decode(PdfString content)
        {
            byte[]        contentBytes = content.GetValueBytes();
            StringBuilder builder      = new StringBuilder(contentBytes.Length);

            foreach (byte b in contentBytes)
            {
                int uni = fontEncoding.GetUnicode(b & 0xff);
                if (uni > -1)
                {
                    builder.Append((char)(int)uni);
                }
                else
                {
                    if (fontEncoding.GetBaseEncoding() == null)
                    {
                        Glyph glyph = fontProgram.GetGlyphByCode(b & 0xff);
                        if (glyph != null && glyph.GetChars() != null)
                        {
                            builder.Append(glyph.GetChars());
                        }
                    }
                }
            }
            return(builder.ToString());
        }
Ejemplo n.º 4
0
        internal static byte[] FetchEnvelopedData(ICipherParameters certificateKey, X509Certificate certificate, PdfArray recipients)
        {
            bool foundRecipient = false;

            byte[] envelopedData = null;
            for (int i = 0; i < recipients.Size(); i++)
            {
                try {
                    PdfString        recipient = recipients.GetAsString(i);
                    CmsEnvelopedData data      = new CmsEnvelopedData(recipient.GetValueBytes());

                    foreach (RecipientInformation recipientInfo in data.GetRecipientInfos().GetRecipients())
                    {
                        if (recipientInfo.RecipientID.Match(certificate) && !foundRecipient)
                        {
                            envelopedData  = recipientInfo.GetContent(certificateKey);
                            foundRecipient = true;
                        }
                    }
                } catch (Exception f) {
                    throw new PdfException(PdfException.PdfDecryption, f);
                }
            }
            if (!foundRecipient || envelopedData == null)
            {
                throw new PdfException(PdfException.BadCertificateAndKey);
            }
            return(envelopedData);
        }
Ejemplo n.º 5
0
        public override float GetContentWidth(PdfString content)
        {
            // TODO refactor using decodeIntoGlyphLine?
            float width = 0;

            byte[] contentBytes = content.GetValueBytes();
            foreach (byte b in contentBytes)
            {
                Glyph glyph = null;
                int   uni   = fontEncoding.GetUnicode(b & 0xff);
                if (uni > -1)
                {
                    glyph = GetGlyph(uni);
                }
                else
                {
                    if (fontEncoding.GetBaseEncoding() == null)
                    {
                        glyph = fontProgram.GetGlyphByCode(b & 0xff);
                    }
                }
                width += glyph != null?glyph.GetWidth() : 0;
            }
            return(width);
        }
Ejemplo n.º 6
0
        /// <summary><inheritDoc/></summary>
        public override GlyphLine DecodeIntoGlyphLine(PdfString content)
        {
            byte[]        contentBytes = content.GetValueBytes();
            IList <Glyph> glyphs       = new List <Glyph>(contentBytes.Length);

            foreach (byte b in contentBytes)
            {
                int   code  = b & 0xff;
                int   uni   = fontEncoding.GetUnicode(code);
                Glyph glyph = null;
                if (uni > -1)
                {
                    glyph = GetGlyph(uni);
                }
                else
                {
                    if (fontEncoding.GetBaseEncoding() == null)
                    {
                        glyph = fontProgram.GetGlyphByCode(code);
                    }
                }
                if (glyph != null)
                {
                    glyphs.Add(glyph);
                }
            }
            return(new GlyphLine(glyphs));
        }
Ejemplo n.º 7
0
        public override float GetContentWidth(PdfString content)
        {
            float width = 0;

            byte[] contentBytes = content.GetValueBytes();
            foreach (byte b in contentBytes)
            {
                Glyph glyph = fontProgram.GetGlyphByCode(b & 0xff);
                width += glyph != null?glyph.GetWidth() : 0;
            }
            return(width);
        }
Ejemplo n.º 8
0
        internal static void ValidateTemplateForSignedDeferredResult(String output, String sigFieldName, PdfName filter
                                                                     , PdfName subFilter, int estimatedSize)
        {
            PdfDocument outDocument   = new PdfDocument(new PdfReader(output));
            PdfObject   outSigDictObj = PdfAcroForm.GetAcroForm(outDocument, false).GetField(sigFieldName).GetValue();

            NUnit.Framework.Assert.IsTrue(outSigDictObj.IsDictionary());
            PdfDictionary outSigDict = (PdfDictionary)outSigDictObj;
            PdfArray      byteRange  = outSigDict.GetAsArray(PdfName.ByteRange);

            NUnit.Framework.Assert.IsNotNull(byteRange);
            NUnit.Framework.Assert.IsTrue(byteRange.Size() == 4);
            NUnit.Framework.Assert.AreEqual(filter, outSigDict.GetAsName(PdfName.Filter));
            NUnit.Framework.Assert.AreEqual(subFilter, outSigDict.GetAsName(PdfName.SubFilter));
            PdfString outSigContents = outSigDict.GetAsString(PdfName.Contents);

            NUnit.Framework.Assert.IsTrue(outSigContents.IsHexWriting());
            NUnit.Framework.Assert.AreEqual(new byte[estimatedSize], outSigContents.GetValueBytes());
        }
Ejemplo n.º 9
0
        /// <summary><inheritDoc/></summary>
        public override GlyphLine DecodeIntoGlyphLine(PdfString content)
        {
            byte[]        contentBytes = content.GetValueBytes();
            IList <Glyph> glyphs       = new List <Glyph>(contentBytes.Length);

            foreach (byte b in contentBytes)
            {
                int   code  = b & 0xff;
                Glyph glyph = null;
                if (toUnicode != null && toUnicode.Lookup(code) != null && (glyph = fontProgram.GetGlyphByCode(code)) != null
                    )
                {
                    if (!JavaUtil.ArraysEquals(toUnicode.Lookup(code), glyph.GetChars()))
                    {
                        // Copy the glyph because the original one may be reused (e.g. standard Helvetica font program)
                        glyph = new Glyph(glyph);
                        glyph.SetChars(toUnicode.Lookup(code));
                    }
                }
                else
                {
                    int uni = fontEncoding.GetUnicode(code);
                    if (uni > -1)
                    {
                        glyph = GetGlyph(uni);
                    }
                    else
                    {
                        if (fontEncoding.GetBaseEncoding() == null)
                        {
                            glyph = fontProgram.GetGlyphByCode(code);
                        }
                    }
                }
                if (glyph != null)
                {
                    glyphs.Add(glyph);
                }
            }
            return(new GlyphLine(glyphs));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Prepares an
        /// <see cref="PdfPKCS7"/>
        /// instance for the given signature.
        /// This method handles signature parsing and might throw an exception if
        /// signature is malformed.
        /// <p>
        /// The returned
        /// <see cref="PdfPKCS7"/>
        /// can be used to fetch additional info about the signature
        /// and also to perform integrity check of data signed by the given signature field.
        /// </p>
        /// Prepared
        /// <see cref="PdfPKCS7"/>
        /// instance calculates digest based on signature's /ByteRange entry.
        /// In order to check that /ByteRange is properly defined and given signature indeed covers the current PDF document
        /// revision please use
        /// <see cref="SignatureCoversWholeDocument(System.String)"/>
        /// method.
        /// </summary>
        /// <param name="signatureFieldName">the signature field name</param>
        /// <param name="securityProvider">the security provider or null for the default provider</param>
        /// <returns>
        /// a
        /// <see cref="PdfPKCS7"/>
        /// instance which can be used to fetch additional info about the signature
        /// and also to perform integrity check of data signed by the given signature field.
        /// </returns>
        public virtual PdfPKCS7 ReadSignatureData(String signatureFieldName)
        {
            PdfSignature signature = GetSignature(signatureFieldName);

            if (signature == null)
            {
                return(null);
            }
            try {
                PdfName   sub      = signature.GetSubFilter();
                PdfString contents = signature.GetContents();
                PdfPKCS7  pk       = null;
                if (sub.Equals(PdfName.Adbe_x509_rsa_sha1))
                {
                    PdfString cert = signature.GetPdfObject().GetAsString(PdfName.Cert);
                    if (cert == null)
                    {
                        cert = signature.GetPdfObject().GetAsArray(PdfName.Cert).GetAsString(0);
                    }
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), cert.GetValueBytes());
                }
                else
                {
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), sub);
                }
                UpdateByteRange(pk, signature);
                PdfString date = signature.GetDate();
                if (date != null)
                {
                    pk.SetSignDate(PdfDate.Decode(date.ToString()));
                }
                String signName = signature.GetName();
                pk.SetSignName(signName);
                String reason = signature.GetReason();
                if (reason != null)
                {
                    pk.SetReason(reason);
                }
                String location = signature.GetLocation();
                if (location != null)
                {
                    pk.SetLocation(location);
                }
                return(pk);
            }
            catch (Exception e) {
                throw new PdfException(e);
            }
        }
Ejemplo n.º 11
0
        /// <summary>Verifies a signature.</summary>
        /// <remarks>
        /// Verifies a signature. Further verification can be done on the returned
        /// <see cref="PdfPKCS7"/>
        /// object.
        /// </remarks>
        /// <param name="name">the signature field name</param>
        /// <param name="provider">the provider or null for the default provider</param>
        /// <returns>PdfPKCS7 object to continue the verification</returns>
        public virtual PdfPKCS7 VerifySignature(String name)
        {
            PdfDictionary v = GetSignatureDictionary(name);

            if (v == null)
            {
                return(null);
            }
            try {
                PdfName   sub      = v.GetAsName(PdfName.SubFilter);
                PdfString contents = v.GetAsString(PdfName.Contents);
                PdfPKCS7  pk       = null;
                if (sub.Equals(PdfName.Adbe_x509_rsa_sha1))
                {
                    PdfString cert = v.GetAsString(PdfName.Cert);
                    if (cert == null)
                    {
                        cert = v.GetAsArray(PdfName.Cert).GetAsString(0);
                    }
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), cert.GetValueBytes());
                }
                else
                {
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), sub);
                }
                UpdateByteRange(pk, v);
                PdfString str = v.GetAsString(PdfName.M);
                if (str != null)
                {
                    pk.SetSignDate(PdfDate.Decode(str.ToString()));
                }
                PdfObject obj = v.Get(PdfName.Name);
                if (obj != null)
                {
                    if (obj.IsString())
                    {
                        pk.SetSignName(((PdfString)obj).ToUnicodeString());
                    }
                    else
                    {
                        if (obj.IsName())
                        {
                            pk.SetSignName(((PdfName)obj).GetValue());
                        }
                    }
                }
                str = v.GetAsString(PdfName.Reason);
                if (str != null)
                {
                    pk.SetReason(str.ToUnicodeString());
                }
                str = v.GetAsString(PdfName.Location);
                if (str != null)
                {
                    pk.SetLocation(str.ToUnicodeString());
                }
                return(pk);
            }
            catch (Exception e) {
                throw new PdfException(e);
            }
        }