Beispiel #1
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Org.BouncyCastle.Security.GeneralSecurityException"/>
        internal static DERForRecipientParams CalculateDERForRecipientParams(byte[] @in)
        {
            String s = "1.2.840.113549.3.2";
            DERForRecipientParams parameters = new DERForRecipientParams();

            byte[] outp = new byte[100];
            DerObjectIdentifier derob = new DerObjectIdentifier(s);

            byte[]          abyte0 = IVGenerator.GetIV(16); // keyp
            IBufferedCipher cf     = CipherUtilities.GetCipher(derob);
            KeyParameter    kp     = new KeyParameter(abyte0);

            byte[]           iv  = IVGenerator.GetIV(cf.GetBlockSize());
            ParametersWithIV piv = new ParametersWithIV(kp, iv);

            cf.Init(true, piv);
            int len = cf.DoFinal(@in, outp, 0);

            byte[] abyte1 = new byte[len];
            Array.Copy(outp, 0, abyte1, 0, len);

            Asn1EncodableVector ev = new Asn1EncodableVector();

            ev.Add(new DerInteger(58));
            ev.Add(new DerOctetString(iv));
            DerSequence seq = new DerSequence(ev);

            parameters.abyte0 = abyte0;
            parameters.abyte1 = abyte1;
            parameters.algorithmIdentifier = new AlgorithmIdentifier(derob, seq);
            return(parameters);
        }
Beispiel #2
0
 public OutputStreamEncryption(Stream outc, byte[] key, int off, int len, int revision)
 {
     this.outc = outc;
     aes       = (revision == AES_128 || revision == AES_256);
     if (aes)
     {
         byte[] iv   = IVGenerator.GetIV();
         byte[] nkey = new byte[len];
         System.Array.Copy(key, off, nkey, 0, len);
         cipher = new AESCipher(true, nkey, iv);
         Write(iv, 0, iv.Length);
     }
     else
     {
         arcfour = new ARCFOUREncryption();
         arcfour.PrepareARCFOURKey(key, off, len);
     }
 }
        internal static DERForRecipientParams CalculateDERForRecipientParams(byte[] @in)
        {
            /*
             * According to ISO 32000-2 (7.6.5.3 Public-key encryption algorithms) RC-2 algorithm is outdated
             * and should be replaced with a safer one 256-bit AES-CBC:
             *   The algorithms that shall be used to encrypt the enveloped data in the CMS object are:
             *   - RC4 with key lengths up to 256-bits (deprecated);
             *   - DES, Triple DES, RC2 with key lengths up to 128 bits (deprecated);
             *   - 128-bit AES in Cipher Block Chaining (CBC) mode (deprecated);
             *   - 192-bit AES in CBC mode (deprecated);
             *   - 256-bit AES in CBC mode.
             */
            String s = "1.2.840.113549.3.2";
            DERForRecipientParams parameters = new DERForRecipientParams();

            byte[] outp = new byte[100];
            DerObjectIdentifier derob = new DerObjectIdentifier(s);

            // keyp
            byte[]          abyte0 = IVGenerator.GetIV(16);
            IBufferedCipher cf     = CipherUtilities.GetCipher(derob);
            KeyParameter    kp     = new KeyParameter(abyte0);

            byte[]           iv  = IVGenerator.GetIV(cf.GetBlockSize());
            ParametersWithIV piv = new ParametersWithIV(kp, iv);

            cf.Init(true, piv);
            int len = cf.DoFinal(@in, outp, 0);

            byte[] abyte1 = new byte[len];
            Array.Copy(outp, 0, abyte1, 0, len);

            Asn1EncodableVector ev = new Asn1EncodableVector();

            ev.Add(new DerInteger(58));
            ev.Add(new DerOctetString(iv));
            DerSequence seq = new DerSequence(ev);

            parameters.abyte0 = abyte0;
            parameters.abyte1 = abyte1;
            parameters.algorithmIdentifier = new AlgorithmIdentifier(derob, seq);
            return(parameters);
        }
Beispiel #4
0
        private Asn1Object CreateDERForRecipient(byte[] inp, X509Certificate cert)
        {
            String s = "1.2.840.113549.3.2";

            byte[] outp = new byte[100];
            DerObjectIdentifier derob = new DerObjectIdentifier(s);

            byte[]          keyp = IVGenerator.GetIV(16);
            IBufferedCipher cf   = CipherUtilities.GetCipher(derob);
            KeyParameter    kp   = new KeyParameter(keyp);

            byte[]           iv  = IVGenerator.GetIV(cf.GetBlockSize());
            ParametersWithIV piv = new ParametersWithIV(kp, iv);

            cf.Init(true, piv);
            int len = cf.DoFinal(inp, outp, 0);

            byte[] abyte1 = new byte[len];
            System.Array.Copy(outp, 0, abyte1, 0, len);
            DerOctetString        deroctetstring        = new DerOctetString(abyte1);
            KeyTransRecipientInfo keytransrecipientinfo = ComputeRecipientInfo(cert, keyp);
            DerSet derset          = new DerSet(new RecipientInfo(keytransrecipientinfo));
            Asn1EncodableVector ev = new Asn1EncodableVector();

            ev.Add(new DerInteger(58));
            ev.Add(new DerOctetString(iv));
            DerSequence          seq = new DerSequence(ev);
            AlgorithmIdentifier  algorithmidentifier  = new AlgorithmIdentifier(derob, seq);
            EncryptedContentInfo encryptedcontentinfo =
                new EncryptedContentInfo(PkcsObjectIdentifiers.Data, algorithmidentifier, deroctetstring);
            Asn1Set       set = null;
            EnvelopedData env = new EnvelopedData(null, derset, encryptedcontentinfo, set);

            Org.BouncyCastle.Asn1.Cms.ContentInfo contentinfo =
                new Org.BouncyCastle.Asn1.Cms.ContentInfo(PkcsObjectIdentifiers.EnvelopedData, env);
            return(contentinfo.ToAsn1Object());
        }
Beispiel #5
0
 public PdfPublicKeySecurityHandler()
 {
     seed       = IVGenerator.GetIV(SEED_LENGTH);
     recipients = new List <PdfPublicKeyRecipient>();
 }
Beispiel #6
0
 private void InitKeyAndFillDictionary(PdfDictionary encryptionDictionary, byte[] userPassword, byte[] ownerPassword
                                       , int permissions, bool encryptMetadata, bool embeddedFilesOnly)
 {
     ownerPassword = GenerateOwnerPasswordIfNullOrEmpty(ownerPassword);
     permissions  |= PERMS_MASK_1_FOR_REVISION_3_OR_GREATER;
     permissions  &= PERMS_MASK_2;
     try {
         byte[] userKey;
         byte[] ownerKey;
         byte[] ueKey;
         byte[] oeKey;
         byte[] aes256Perms;
         if (userPassword == null)
         {
             userPassword = new byte[0];
         }
         else
         {
             if (userPassword.Length > 127)
             {
                 userPassword = JavaUtil.ArraysCopyOf(userPassword, 127);
             }
         }
         if (ownerPassword.Length > 127)
         {
             ownerPassword = JavaUtil.ArraysCopyOf(ownerPassword, 127);
         }
         // first 8 bytes are validation salt; second 8 bytes are key salt
         byte[] userValAndKeySalt  = IVGenerator.GetIV(16);
         byte[] ownerValAndKeySalt = IVGenerator.GetIV(16);
         nextObjectKey     = IVGenerator.GetIV(32);
         nextObjectKeySize = 32;
         byte[] hash;
         // Algorithm 8.1
         hash    = ComputeHash(userPassword, userValAndKeySalt, 0, 8);
         userKey = JavaUtil.ArraysCopyOf(hash, 48);
         Array.Copy(userValAndKeySalt, 0, userKey, 32, 16);
         // Algorithm 8.2
         hash = ComputeHash(userPassword, userValAndKeySalt, 8, 8);
         AESCipherCBCnoPad ac = new AESCipherCBCnoPad(true, hash);
         ueKey = ac.ProcessBlock(nextObjectKey, 0, nextObjectKey.Length);
         // Algorithm 9.1
         hash     = ComputeHash(ownerPassword, ownerValAndKeySalt, 0, 8, userKey);
         ownerKey = JavaUtil.ArraysCopyOf(hash, 48);
         Array.Copy(ownerValAndKeySalt, 0, ownerKey, 32, 16);
         // Algorithm 9.2
         hash  = ComputeHash(ownerPassword, ownerValAndKeySalt, 8, 8, userKey);
         ac    = new AESCipherCBCnoPad(true, hash);
         oeKey = ac.ProcessBlock(nextObjectKey, 0, nextObjectKey.Length);
         // Algorithm 10
         byte[] permsp = IVGenerator.GetIV(16);
         permsp[0]            = (byte)permissions;
         permsp[1]            = (byte)(permissions >> 8);
         permsp[2]            = (byte)(permissions >> 16);
         permsp[3]            = (byte)(permissions >> 24);
         permsp[4]            = (byte)(255);
         permsp[5]            = (byte)(255);
         permsp[6]            = (byte)(255);
         permsp[7]            = (byte)(255);
         permsp[8]            = encryptMetadata ? (byte)'T' : (byte)'F';
         permsp[9]            = (byte)'a';
         permsp[10]           = (byte)'d';
         permsp[11]           = (byte)'b';
         ac                   = new AESCipherCBCnoPad(true, nextObjectKey);
         aes256Perms          = ac.ProcessBlock(permsp, 0, permsp.Length);
         this.permissions     = permissions;
         this.encryptMetadata = encryptMetadata;
         SetStandardHandlerDicEntries(encryptionDictionary, userKey, ownerKey);
         SetAES256DicEntries(encryptionDictionary, oeKey, ueKey, aes256Perms, encryptMetadata, embeddedFilesOnly);
     }
     catch (Exception ex) {
         throw new PdfException(PdfException.PdfEncryption, ex);
     }
 }
Beispiel #7
0
        // gets keylength and revision and uses revison to choose the initial values for permissions
        virtual public void SetupAllKeys(byte[] userPassword, byte[] ownerPassword, int permissions)
        {
            if (ownerPassword == null || ownerPassword.Length == 0)
            {
                ownerPassword = DigestAlgorithms.Digest("MD5", CreateDocumentId());
            }
            md5.Reset();
            permissions     |= (int)((revision == STANDARD_ENCRYPTION_128 || revision == AES_128 || revision == AES_256) ? (uint)0xfffff0c0 : (uint)0xffffffc0);
            permissions     &= unchecked ((int)0xfffffffc);
            this.permissions = permissions;
            if (revision == AES_256)
            {
                if (userPassword == null)
                {
                    userPassword = new byte[0];
                }
                documentID = CreateDocumentId();
                byte[] uvs = IVGenerator.GetIV(8);
                byte[] uks = IVGenerator.GetIV(8);
                key = IVGenerator.GetIV(32);
                // Algorithm 3.8.1
                IDigest md = DigestUtilities.GetDigest("SHA-256");
                md.BlockUpdate(userPassword, 0, Math.Min(userPassword.Length, 127));
                md.BlockUpdate(uvs, 0, uvs.Length);
                userKey = new byte[48];
                md.DoFinal(userKey, 0);
                System.Array.Copy(uvs, 0, userKey, 32, 8);
                System.Array.Copy(uks, 0, userKey, 40, 8);
                // Algorithm 3.8.2
                md.BlockUpdate(userPassword, 0, Math.Min(userPassword.Length, 127));
                md.BlockUpdate(uks, 0, uks.Length);
                byte[] tempDigest = new byte[32];
                md.DoFinal(tempDigest, 0);
                AESCipherCBCnoPad ac = new AESCipherCBCnoPad(true, tempDigest);
                ueKey = ac.ProcessBlock(key, 0, key.Length);
                // Algorithm 3.9.1
                byte[] ovs = IVGenerator.GetIV(8);
                byte[] oks = IVGenerator.GetIV(8);
                md.BlockUpdate(ownerPassword, 0, Math.Min(ownerPassword.Length, 127));
                md.BlockUpdate(ovs, 0, ovs.Length);
                md.BlockUpdate(userKey, 0, userKey.Length);
                ownerKey = new byte[48];
                md.DoFinal(ownerKey, 0);
                System.Array.Copy(ovs, 0, ownerKey, 32, 8);
                System.Array.Copy(oks, 0, ownerKey, 40, 8);
                // Algorithm 3.9.2
                md.BlockUpdate(ownerPassword, 0, Math.Min(ownerPassword.Length, 127));
                md.BlockUpdate(oks, 0, oks.Length);
                md.BlockUpdate(userKey, 0, userKey.Length);
                md.DoFinal(tempDigest, 0);
                ac    = new AESCipherCBCnoPad(true, tempDigest);
                oeKey = ac.ProcessBlock(key, 0, key.Length);
                // Algorithm 3.10
                byte[] permsp = IVGenerator.GetIV(16);
                permsp[0]  = (byte)permissions;
                permsp[1]  = (byte)(permissions >> 8);
                permsp[2]  = (byte)(permissions >> 16);
                permsp[3]  = (byte)(permissions >> 24);
                permsp[4]  = (byte)(255);
                permsp[5]  = (byte)(255);
                permsp[6]  = (byte)(255);
                permsp[7]  = (byte)(255);
                permsp[8]  = encryptMetadata ? (byte)'T' : (byte)'F';
                permsp[9]  = (byte)'a';
                permsp[10] = (byte)'d';
                permsp[11] = (byte)'b';
                ac         = new AESCipherCBCnoPad(true, key);
                perms      = ac.ProcessBlock(permsp, 0, permsp.Length);
            }
            else
            {
                //PDF refrence 3.5.2 Standard Security Handler, Algorithum 3.3-1
                //If there is no owner password, use the user password instead.
                byte[] userPad  = PadPassword(userPassword);
                byte[] ownerPad = PadPassword(ownerPassword);

                this.ownerKey = ComputeOwnerKey(userPad, ownerPad);
                documentID    = CreateDocumentId();
                SetupByUserPad(this.documentID, userPad, this.ownerKey, permissions);
            }
        }
Beispiel #8
0
 internal static byte[] GenerateSeed(int seedLength)
 {
     return(IVGenerator.GetIV(seedLength));
 }
Beispiel #9
0
 private void InitKeyAndFillDictionary(PdfDictionary encryptionDictionary, byte[] userPassword, byte[] ownerPassword
                                       , int permissions, bool encryptMetadata, bool embeddedFilesOnly)
 {
     ownerPassword = GenerateOwnerPasswordIfNullOrEmpty(ownerPassword);
     permissions  |= PERMS_MASK_1_FOR_REVISION_3_OR_GREATER;
     permissions  &= PERMS_MASK_2;
     try {
         byte[] userKey;
         byte[] ownerKey;
         byte[] ueKey;
         byte[] oeKey;
         byte[] aes256Perms;
         if (userPassword == null)
         {
             userPassword = new byte[0];
         }
         byte[] uvs = IVGenerator.GetIV(8);
         byte[] uks = IVGenerator.GetIV(8);
         nextObjectKey     = IVGenerator.GetIV(32);
         nextObjectKeySize = 32;
         // Algorithm 3.8.1
         IDigest md = Org.BouncyCastle.Security.DigestUtilities.GetDigest("SHA-256");
         md.Update(userPassword, 0, Math.Min(userPassword.Length, 127));
         md.Update(uvs);
         userKey = new byte[48];
         md.Digest(userKey, 0, 32);
         System.Array.Copy(uvs, 0, userKey, 32, 8);
         System.Array.Copy(uks, 0, userKey, 40, 8);
         // Algorithm 3.8.2
         md.Update(userPassword, 0, Math.Min(userPassword.Length, 127));
         md.Update(uks);
         AESCipherCBCnoPad ac = new AESCipherCBCnoPad(true, md.Digest());
         ueKey = ac.ProcessBlock(nextObjectKey, 0, nextObjectKey.Length);
         // Algorithm 3.9.1
         byte[] ovs = IVGenerator.GetIV(8);
         byte[] oks = IVGenerator.GetIV(8);
         md.Update(ownerPassword, 0, Math.Min(ownerPassword.Length, 127));
         md.Update(ovs);
         md.Update(userKey);
         ownerKey = new byte[48];
         md.Digest(ownerKey, 0, 32);
         System.Array.Copy(ovs, 0, ownerKey, 32, 8);
         System.Array.Copy(oks, 0, ownerKey, 40, 8);
         // Algorithm 3.9.2
         md.Update(ownerPassword, 0, Math.Min(ownerPassword.Length, 127));
         md.Update(oks);
         md.Update(userKey);
         ac    = new AESCipherCBCnoPad(true, md.Digest());
         oeKey = ac.ProcessBlock(nextObjectKey, 0, nextObjectKey.Length);
         // Algorithm 3.10
         byte[] permsp = IVGenerator.GetIV(16);
         permsp[0]            = (byte)permissions;
         permsp[1]            = (byte)(permissions >> 8);
         permsp[2]            = (byte)(permissions >> 16);
         permsp[3]            = (byte)(permissions >> 24);
         permsp[4]            = (byte)(255);
         permsp[5]            = (byte)(255);
         permsp[6]            = (byte)(255);
         permsp[7]            = (byte)(255);
         permsp[8]            = encryptMetadata ? (byte)'T' : (byte)'F';
         permsp[9]            = (byte)'a';
         permsp[10]           = (byte)'d';
         permsp[11]           = (byte)'b';
         ac                   = new AESCipherCBCnoPad(true, nextObjectKey);
         aes256Perms          = ac.ProcessBlock(permsp, 0, permsp.Length);
         this.permissions     = permissions;
         this.encryptMetadata = encryptMetadata;
         SetStandardHandlerDicEntries(encryptionDictionary, userKey, ownerKey);
         SetAES256DicEntries(encryptionDictionary, oeKey, ueKey, aes256Perms, encryptMetadata, embeddedFilesOnly);
     }
     catch (Exception ex) {
         throw new PdfException(PdfException.PdfEncryption, ex);
     }
 }