示例#1
0
 public static bool ValidatePdfVersionForDictEntry(PdfDocument document, PdfVersion expectedVersion, PdfName
                                                   entryKey, PdfName dictType)
 {
     if (document != null && document.GetPdfVersion().CompareTo(expectedVersion) < 0)
     {
         logger.Warn(String.Format(iText.IO.LogMessageConstant.VERSION_INCOMPATIBILITY_FOR_DICTIONARY_ENTRY, entryKey
                                   , dictType, expectedVersion, document.GetPdfVersion()));
         return(true);
     }
     else
     {
         return(false);
     }
 }
        /// <summary>Creates the encryption.</summary>
        /// <param name="userPassword">
        /// the user password. Can be null or of zero length, which is equal to
        /// omitting the user password
        /// </param>
        /// <param name="ownerPassword">
        /// the owner password. If it's null or empty, iText will generate
        /// a random string to be used as the owner password
        /// </param>
        /// <param name="permissions">
        /// the user permissions
        /// The open permissions for the document can be
        /// <see cref="EncryptionConstants.ALLOW_PRINTING"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_MODIFY_CONTENTS"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_COPY"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_MODIFY_ANNOTATIONS"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_FILL_IN"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_SCREENREADERS"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_ASSEMBLY"/>
        /// and
        /// <see cref="EncryptionConstants.ALLOW_DEGRADED_PRINTING"/>.
        /// The permissions can be combined by ORing them
        /// </param>
        /// <param name="encryptionType">
        /// the type of encryption. It can be one of
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_40"/>
        /// ,
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_128"/>
        /// ,
        /// <see cref="EncryptionConstants.ENCRYPTION_AES_128"/>
        /// or
        /// <see cref="EncryptionConstants.ENCRYPTION_AES_256"/>.
        /// Optionally
        /// <see cref="EncryptionConstants.DO_NOT_ENCRYPT_METADATA"/>
        /// can be
        /// ORed to output the metadata in cleartext.
        /// <see cref="EncryptionConstants.EMBEDDED_FILES_ONLY"/>
        /// can be ORed as well.
        /// Please be aware that the passed encryption types may override permissions:
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_40"/>
        /// implicitly sets
        /// <see cref="EncryptionConstants.DO_NOT_ENCRYPT_METADATA"/>
        /// and
        /// <see cref="EncryptionConstants.EMBEDDED_FILES_ONLY"/>
        /// as false;
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_128"/>
        /// implicitly sets
        /// <see cref="EncryptionConstants.EMBEDDED_FILES_ONLY"/>
        /// as false;
        /// </param>
        /// <param name="documentId">document id which will be used for encryption</param>
        /// <param name="version">
        /// the
        /// <see cref="PdfVersion"/>
        /// of the target document for encryption
        /// </param>
        public PdfEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType, byte[]
                             documentId, PdfVersion version)
            : base(new PdfDictionary())
        {
            this.documentId = documentId;
            if (version != null && version.CompareTo(PdfVersion.PDF_2_0) >= 0)
            {
                permissions = FixAccessibilityPermissionPdf20(permissions);
            }
            int revision = SetCryptoMode(encryptionType);

            switch (revision)
            {
            case STANDARD_ENCRYPTION_40: {
                StandardHandlerUsingStandard40 handlerStd40 = new StandardHandlerUsingStandard40(this.GetPdfObject(), userPassword
                                                                                                 , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, documentId);
                this.permissions = handlerStd40.GetPermissions();
                securityHandler  = handlerStd40;
                break;
            }

            case STANDARD_ENCRYPTION_128: {
                StandardHandlerUsingStandard128 handlerStd128 = new StandardHandlerUsingStandard128(this.GetPdfObject(), userPassword
                                                                                                    , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, documentId);
                this.permissions = handlerStd128.GetPermissions();
                securityHandler  = handlerStd128;
                break;
            }

            case AES_128: {
                StandardHandlerUsingAes128 handlerAes128 = new StandardHandlerUsingAes128(this.GetPdfObject(), userPassword
                                                                                          , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, documentId);
                this.permissions = handlerAes128.GetPermissions();
                securityHandler  = handlerAes128;
                break;
            }

            case AES_256: {
                StandardHandlerUsingAes256 handlerAes256 = new StandardHandlerUsingAes256(this.GetPdfObject(), userPassword
                                                                                          , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, version);
                this.permissions = handlerAes256.GetPermissions();
                securityHandler  = handlerAes256;
                break;
            }
            }
        }
示例#3
0
        protected internal virtual void InitCryptoIfSpecified(PdfVersion version)
        {
            EncryptionProperties encryptProps = properties.encryptionProperties;

            if (properties.IsStandardEncryptionUsed())
            {
                crypto = new PdfEncryption(encryptProps.userPassword, encryptProps.ownerPassword, encryptProps.standardEncryptPermissions
                                           , encryptProps.encryptionAlgorithm, PdfEncryption.GenerateNewDocumentId(), version);
            }
            else
            {
                if (properties.IsPublicKeyEncryptionUsed())
                {
                    crypto = new PdfEncryption(encryptProps.publicCertificates, encryptProps.publicKeyEncryptPermissions, encryptProps
                                               .encryptionAlgorithm, version);
                }
            }
        }
示例#4
0
        protected internal virtual void InitCryptoIfSpecified(PdfVersion version)
        {
            EncryptionProperties encryptProps = properties.encryptionProperties;

            if (properties.IsStandardEncryptionUsed())
            {
                crypto = new PdfEncryption(encryptProps.userPassword, encryptProps.ownerPassword, encryptProps.standardEncryptPermissions
                                           , encryptProps.encryptionAlgorithm, ByteUtils.GetIsoBytes(this.document.GetOriginalDocumentId().GetValue
                                                                                                         ()), version);
            }
            else
            {
                if (properties.IsPublicKeyEncryptionUsed())
                {
                    crypto = new PdfEncryption(encryptProps.publicCertificates, encryptProps.publicKeyEncryptPermissions, encryptProps
                                               .encryptionAlgorithm, version);
                }
            }
        }
示例#5
0
 public static bool ValidatePdfVersionForDeprecatedFeatureLogError(PdfDocument document, PdfVersion expectedVersion
                                                                   , String deprecatedFeatureLogMessage)
 {
     if (document.GetPdfVersion().CompareTo(expectedVersion) >= 0)
     {
         logger.Error(deprecatedFeatureLogMessage);
         return(true);
     }
     else
     {
         return(false);
     }
 }
示例#6
0
 /// <summary>Defines pdf version for the created document.</summary>
 /// <remarks>Defines pdf version for the created document. Default value is PDF_1_7.</remarks>
 /// <param name="version">version for the document.</param>
 /// <returns>
 /// this
 /// <c>WriterProperties</c>
 /// instance
 /// </returns>
 public virtual iText.Kernel.Pdf.WriterProperties SetPdfVersion(PdfVersion version)
 {
     this.pdfVersion = version;
     return(this);
 }
        /// <summary>Creates the certificate encryption.</summary>
        /// <remarks>
        /// Creates the certificate encryption.
        /// <para />
        /// An array of one or more public certificates must be provided together with
        /// an array of the same size for the permissions for each certificate.
        /// </remarks>
        /// <param name="certs">the public certificates to be used for the encryption</param>
        /// <param name="permissions">
        /// the user permissions for each of the certificates
        /// The open permissions for the document can be
        /// <see cref="EncryptionConstants.ALLOW_PRINTING"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_MODIFY_CONTENTS"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_COPY"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_MODIFY_ANNOTATIONS"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_FILL_IN"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_SCREENREADERS"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_ASSEMBLY"/>
        /// and
        /// <see cref="EncryptionConstants.ALLOW_DEGRADED_PRINTING"/>.
        /// The permissions can be combined by ORing them
        /// </param>
        /// <param name="encryptionType">
        /// the type of encryption. It can be one of
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_40"/>
        /// ,
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_128"/>
        /// ,
        /// <see cref="EncryptionConstants.ENCRYPTION_AES_128"/>
        /// or
        /// <see cref="EncryptionConstants.ENCRYPTION_AES_256"/>.
        /// Optionally
        /// <see cref="EncryptionConstants.DO_NOT_ENCRYPT_METADATA"/>
        /// can be ORed
        /// to output the metadata in cleartext.
        /// <see cref="EncryptionConstants.EMBEDDED_FILES_ONLY"/>
        /// can be ORed as well.
        /// Please be aware that the passed encryption types may override permissions:
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_40"/>
        /// implicitly sets
        /// <see cref="EncryptionConstants.DO_NOT_ENCRYPT_METADATA"/>
        /// and
        /// <see cref="EncryptionConstants.EMBEDDED_FILES_ONLY"/>
        /// as false;
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_128"/>
        /// implicitly sets
        /// <see cref="EncryptionConstants.EMBEDDED_FILES_ONLY"/>
        /// as false;
        /// </param>
        /// <param name="version">
        /// the
        /// <see cref="PdfVersion"/>
        /// of the target document for encryption
        /// </param>
        public PdfEncryption(X509Certificate[] certs, int[] permissions, int encryptionType, PdfVersion version)
            : base(new PdfDictionary())
        {
            if (version != null && version.CompareTo(PdfVersion.PDF_2_0) >= 0)
            {
                for (int i = 0; i < permissions.Length; i++)
                {
                    permissions[i] = FixAccessibilityPermissionPdf20(permissions[i]);
                }
            }
            int revision = SetCryptoMode(encryptionType);

            switch (revision)
            {
            case STANDARD_ENCRYPTION_40: {
                securityHandler = new PubSecHandlerUsingStandard40(this.GetPdfObject(), certs, permissions, encryptMetadata
                                                                   , embeddedFilesOnly);
                break;
            }

            case STANDARD_ENCRYPTION_128: {
                securityHandler = new PubSecHandlerUsingStandard128(this.GetPdfObject(), certs, permissions, encryptMetadata
                                                                    , embeddedFilesOnly);
                break;
            }

            case AES_128: {
                securityHandler = new PubSecHandlerUsingAes128(this.GetPdfObject(), certs, permissions, encryptMetadata, embeddedFilesOnly
                                                               );
                break;
            }

            case AES_256: {
                securityHandler = new PubSecHandlerUsingAes256(this.GetPdfObject(), certs, permissions, encryptMetadata, embeddedFilesOnly
                                                               );
                break;
            }
            }
        }