/// <summary>
        /// Overides the default implementaton to output the Encryption object before calling the base method
        /// </summary>
        /// <param name="documentid"></param>
        /// <remarks>A reference to the written encrypt object is held so that it can be output on the trailer dictionary later on</remarks>
        public override void CloseDocument(PDFDocumentID documentid)
        {
            PDFObjectRef enc = this.Security.WriteTo(this);

            _encryptionobject = enc;
            base.CloseDocument(documentid);
        }
        /// <summary>
        /// Overrides the base implementation to write the Encrypt reference to the trailer dictionary.
        /// </summary>
        /// <param name="documentid"></param>
        /// <remarks>The reference comes from the Close method which should have already been called</remarks>
        protected override void WriteTrailerDictionaryEntries(PDFDocumentID documentid)
        {
            base.WriteTrailerDictionaryEntries(documentid);

            //Append the encrypt entry in the document trailer
            if (null != _encryptionobject)
            {
                this.WriteDictionaryObjectRefEntry("Encrypt", _encryptionobject);
            }
        }
        //implementation methods

        protected override PDFWriter DoGetInstance(Document forDoc, Stream stream, int generation, PDFDocumentRenderOptions options, PDFTraceLog log)
        {
            if (null == forDoc.DocumentID)
            {
                forDoc.DocumentID = PDFDocumentID.Create();
            }

            PDFEncrypterFactory factory = this.GetEncrypterFactory();

            IDocumentPasswordSettings settings;
            string          path = forDoc.LoadedSource;
            SecureString    owner;
            SecureString    user;
            PermissionFlags protection;

            if (Options.SecurityOptions.TryGetPasswordSettings(path, out settings))
            {
                owner      = settings.OwnerPassword;
                user       = settings.UserPassword;
                protection = settings.DefaultPermissions;

                if (settings.AllowDocumentOverrides)
                {
                    if (null != this.OwnerPassword)
                    {
                        owner = this.OwnerPassword;
                    }
                    if (null != this.UserPassword)
                    {
                        user = this.UserPassword;
                    }
                    protection |= this.GetRestrictions();
                }
            }
            else
            {
                protection = this.GetRestrictions();
                owner      = this.OwnerPassword;
                user       = this.UserPassword;
            }

            PDFEncryter       enc    = factory.InitEncrypter(owner, user, forDoc.DocumentID, protection);
            PDFSecureWriter14 writer = new PDFSecureWriter14(stream, log, enc);

            return(writer);
        }
        /// <summary>
        /// Overrides the base implementation to output the document
        /// </summary>
        /// <param name="context"></param>
        /// <param name="writer"></param>
        /// <returns></returns>
        protected override PDFObjectRef DoOutputToPDF(PDFRenderContext context, PDFWriter writer)
        {
            context.TraceLog.Begin(TraceLevel.Message, "Layout Document", "Outputting document to the PDFWriter");
            writer.OpenDocument();
            PDFObjectRef catalog = this.WriteCatalog(context, writer);

            this.WriteInfo(context, writer);

            PDFDocumentID id = this.DocumentComponent.DocumentID;

            if (null == id)
            {
                id = PDFDocumentID.Create();
            }

            writer.CloseDocument(id);

            context.TraceLog.End(TraceLevel.Message, "Layout Document", "Completed output of the document to the PDFWriter");

            return(catalog);
        }
Example #5
0
        internal PDFEncryter InitEncrypter(string ownerpassword, string userpassword, PDFDocumentID documentid, PermissionFlags protection, PDFPerformanceMonitor monitor)
        {
            IDisposable dur = null;

            if (string.IsNullOrEmpty(ownerpassword))
            {
                throw new ArgumentNullException("ownerpassword", "As a minimum the owner password is required");
            }

            byte[]      owner = null;
            byte[]      user  = null;
            PDFEncryter info;

            try
            {
                if (null != monitor)
                {
                    dur = monitor.Record(PerformanceMonitorType.Encrypting_Streams, "Key creation");
                }

                owner = ConvertToPaddedBytes(ownerpassword);
                user  = ConvertToPaddedBytes(userpassword);
                info  = this.InitEncrypter(owner, user, documentid, protection);
            }
            catch (Exception ex)
            {
                throw new PDFSecurityException("Could not create the encrypter information", ex);
            }
            finally
            {
                if (null != owner)
                {
                    ZeroArray(owner);
                }
                if (null != user)
                {
                    ZeroArray(user);
                }

                if (null != dur)
                {
                    dur.Dispose();
                }
            }

            return(info);
        }
Example #6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="paddedownerpassword">The 32 byte padded owner password</param>
 /// <param name="paddeduserpassword"></param>
 /// <param name="documentid"></param>
 /// <returns></returns>
 protected abstract PDFEncryter InitEncrypter(byte[] paddedownerpassword, byte[] paddeduserpassword, PDFDocumentID documentid, PermissionFlags protection);
        protected override PDFEncryter InitEncrypter(byte[] paddedownerpassword, byte[] paddeduserpassword, PDFDocumentID documentid, PermissionFlags protection)
        {
            if (null == documentid || null == documentid.One || documentid.One.Length == 0)
            {
                throw new ArgumentNullException("documentid");
            }

            //TODO: Revision3 copyin
            byte[] O = GetOwnerValue(paddedownerpassword, paddeduserpassword, Rev2RC4Length);

            byte[] key = CreateEncryptionKey(paddedownerpassword, paddeduserpassword, Rev2RC4Length, (int)protection, documentid.One);

            //User key
            byte[] U = GetUserValue(key);


            PDFEncryter info = new PDFEncrypterV1R2(O, U, key, protection);

            return(info);
        }
        protected override PDFEncryter InitEncrypter(byte[] paddedownerpassword, byte[] paddeduserpassword, PDFDocumentID documentid, PermissionFlags protection)
        {
            if (null == documentid || null == documentid.One || documentid.One.Length == 0)
            {
                throw new ArgumentNullException("documentid");
            }

            byte[] O   = GetOwnerValue(paddedownerpassword, paddeduserpassword, KeyLengthBytes);
            byte[] key = CreateEncryptionKey(O, paddeduserpassword, KeyLengthBytes, (int)protection, documentid.One);

            byte[] U = GetUserValue(key, documentid.One);

            PDFEncryter info = new PDFEncrypterV2R3(O, U, this.KeyLengthBytes, key, protection);

            return(info);
        }
        internal PDFEncryter InitEncrypter(string ownerpassword, string userpassword, PDFDocumentID documentid, PermissionFlags protection)
        {
            if (string.IsNullOrEmpty(ownerpassword))
            {
                throw new ArgumentNullException("ownerpassword", Errors.OwnerPasswordIsRequired);
            }

            byte[]      owner = null;
            byte[]      user  = null;
            PDFEncryter info;

            try
            {
                owner = ConvertToPaddedBytes(ownerpassword);
                user  = ConvertToPaddedBytes(userpassword);
                info  = this.InitEncrypter(owner, user, documentid, protection);
            }
            catch (Exception ex)
            {
                throw new PDFSecurityException(Errors.CouldNotCreateTheSecurityInfo, ex);
            }
            finally
            {
                if (null != owner)
                {
                    ZeroArray(owner);
                }
                if (null != user)
                {
                    ZeroArray(user);
                }
            }

            return(info);
        }