protected internal override void CopyContent(PdfObject from, PdfDocument document) { base.CopyContent(from, document); iText.Kernel.Pdf.PdfPrimitiveObject @object = (iText.Kernel.Pdf.PdfPrimitiveObject)from; if (@object.content != null) { content = JavaUtil.ArraysCopyOf(@object.content, @object.content.Length); } }
internal FontCacheBytesKey(byte[] fontBytes) { if (fontBytes != null) { int maxBytesNum = 10000; this.firstFontBytes = fontBytes.Length > maxBytesNum?JavaUtil.ArraysCopyOf(fontBytes, maxBytesNum) : fontBytes; this.fontLength = fontBytes.Length; } this.hashcode = CalcHashCode(); }
/// <summary>Compute layout margins.</summary> /// <returns>the float values of the margins</returns> internal virtual float[] ComputeLayoutMargins() { float[] layoutMargins = JavaUtil.ArraysCopyOf(margins, margins.Length); for (int i = 0; i < borders.Length; ++i) { float width = borders[i] != null ? borders[i].GetWidth() : 0; layoutMargins[i] += width; } for (int i = 0; i < paddings.Length; ++i) { layoutMargins[i] += paddings[i]; } return(layoutMargins); }
public virtual void ConstructMatrix(PngImageData pngImageData) { PngChromaticities pngChromaticities = pngImageData.GetPngChromaticities(); float z = pngChromaticities.GetYW() * ((pngChromaticities.GetXG() - pngChromaticities.GetXB()) * pngChromaticities .GetYR() - (pngChromaticities.GetXR() - pngChromaticities.GetXB()) * pngChromaticities.GetYG() + (pngChromaticities .GetXR() - pngChromaticities.GetXG()) * pngChromaticities.GetYB()); float YA = pngChromaticities.GetYR() * ((pngChromaticities.GetXG() - pngChromaticities.GetXB()) * pngChromaticities .GetYW() - (pngChromaticities.GetXW() - pngChromaticities.GetXB()) * pngChromaticities.GetYG() + (pngChromaticities .GetXW() - pngChromaticities.GetXG()) * pngChromaticities.GetYB()) / z; float XA = YA * pngChromaticities.GetXR() / pngChromaticities.GetYR(); float ZA = YA * ((1 - pngChromaticities.GetXR()) / pngChromaticities.GetYR() - 1); float YB = -pngChromaticities.GetYG() * ((pngChromaticities.GetXR() - pngChromaticities.GetXB()) * pngChromaticities .GetYW() - (pngChromaticities.GetXW() - pngChromaticities.GetXB()) * pngChromaticities.GetYR() + (pngChromaticities .GetXW() - pngChromaticities.GetXR()) * pngChromaticities.GetYB()) / z; float XB = YB * pngChromaticities.GetXG() / pngChromaticities.GetYG(); float ZB = YB * ((1 - pngChromaticities.GetXG()) / pngChromaticities.GetYG() - 1); float YC = pngChromaticities.GetYB() * ((pngChromaticities.GetXR() - pngChromaticities.GetXG()) * pngChromaticities .GetYW() - (pngChromaticities.GetXW() - pngChromaticities.GetXG()) * pngChromaticities.GetYW() + (pngChromaticities .GetXW() - pngChromaticities.GetXR()) * pngChromaticities.GetYG()) / z; float XC = YC * pngChromaticities.GetXB() / pngChromaticities.GetYB(); float ZC = YC * ((1 - pngChromaticities.GetXB()) / pngChromaticities.GetYB() - 1); float XW = XA + XB + XC; float YW = 1; float ZW = ZA + ZB + ZC; float[] wpa = new float[3]; wpa[0] = XW; wpa[1] = YW; wpa[2] = ZW; this.wp = JavaUtil.ArraysCopyOf(wpa, 3); float[] matrix = new float[9]; matrix[0] = XA; matrix[1] = YA; matrix[2] = ZA; matrix[3] = XB; matrix[4] = YB; matrix[5] = ZB; matrix[6] = XC; matrix[7] = YC; matrix[8] = ZC; this.matrix = JavaUtil.ArraysCopyOf(matrix, 9); }
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); } }
/// <exception cref="Org.BouncyCastle.Security.SecurityUtilityException"/> private byte[] ComputeHash(byte[] password, byte[] salt, int saltOffset, int saltLen, byte[] userKey) { IDigest mdSha256 = DigestUtilities.GetDigest("SHA-256"); mdSha256.Update(password); mdSha256.Update(salt, saltOffset, saltLen); if (userKey != null) { mdSha256.Update(userKey); } byte[] k = mdSha256.Digest(); if (isPdf2) { // See 7.6.4.3.3 "Algorithm 2.B" IDigest mdSha384 = DigestUtilities.GetDigest("SHA-384"); IDigest mdSha512 = DigestUtilities.GetDigest("SHA-512"); int userKeyLen = userKey != null ? userKey.Length : 0; int passAndUserKeyLen = password.Length + userKeyLen; // k1 repetition length int k1RepLen; int roundNum = 0; while (true) { // a) k1RepLen = passAndUserKeyLen + k.Length; byte[] k1 = new byte[k1RepLen * 64]; Array.Copy(password, 0, k1, 0, password.Length); Array.Copy(k, 0, k1, password.Length, k.Length); if (userKey != null) { Array.Copy(userKey, 0, k1, password.Length + k.Length, userKeyLen); } for (int i = 1; i < 64; ++i) { Array.Copy(k1, 0, k1, k1RepLen * i, k1RepLen); } // b) AESCipherCBCnoPad cipher = new AESCipherCBCnoPad(true, JavaUtil.ArraysCopyOf(k, 16), JavaUtil.ArraysCopyOfRange (k, 16, 32)); byte[] e = cipher.ProcessBlock(k1, 0, k1.Length); // c) IDigest md = null; BigInteger i_1 = new BigInteger(1, JavaUtil.ArraysCopyOf(e, 16)); int remainder = i_1.Remainder(BigInteger.ValueOf(3)).IntValue; switch (remainder) { case 0: { md = mdSha256; break; } case 1: { md = mdSha384; break; } case 2: { md = mdSha512; break; } } // d) k = md.Digest(e); ++roundNum; if (roundNum > 63) { // e) int condVal = e[e.Length - 1] & 0xFF; // interpreting last byte as unsigned integer if (condVal <= roundNum - 32) { break; } } } k = k.Length == 32 ? k : JavaUtil.ArraysCopyOf(k, 32); } return(k); }
private void InitKeyAndReadDictionary(PdfDictionary encryptionDictionary, byte[] password) { try { if (password == null) { password = new byte[0]; } else { if (password.Length > 127) { password = JavaUtil.ArraysCopyOf(password, 127); } } isPdf2 = encryptionDictionary.GetAsNumber(PdfName.R).GetValue() == 6; byte[] oValue = GetIsoBytes(encryptionDictionary.GetAsString(PdfName.O)); byte[] uValue = GetIsoBytes(encryptionDictionary.GetAsString(PdfName.U)); byte[] oeValue = GetIsoBytes(encryptionDictionary.GetAsString(PdfName.OE)); byte[] ueValue = GetIsoBytes(encryptionDictionary.GetAsString(PdfName.UE)); byte[] perms = GetIsoBytes(encryptionDictionary.GetAsString(PdfName.Perms)); PdfNumber pValue = (PdfNumber)encryptionDictionary.Get(PdfName.P); this.permissions = pValue.LongValue(); byte[] hash; hash = ComputeHash(password, oValue, VALIDATION_SALT_OFFSET, SALT_LENGTH, uValue); usedOwnerPassword = CompareArray(hash, oValue, 32); if (usedOwnerPassword) { hash = ComputeHash(password, oValue, KEY_SALT_OFFSET, SALT_LENGTH, uValue); AESCipherCBCnoPad ac = new AESCipherCBCnoPad(false, hash); nextObjectKey = ac.ProcessBlock(oeValue, 0, oeValue.Length); } else { hash = ComputeHash(password, uValue, VALIDATION_SALT_OFFSET, SALT_LENGTH); if (!CompareArray(hash, uValue, 32)) { throw new BadPasswordException(PdfException.BadUserPassword); } hash = ComputeHash(password, uValue, KEY_SALT_OFFSET, SALT_LENGTH); AESCipherCBCnoPad ac = new AESCipherCBCnoPad(false, hash); nextObjectKey = ac.ProcessBlock(ueValue, 0, ueValue.Length); } nextObjectKeySize = 32; AESCipherCBCnoPad ac_1 = new AESCipherCBCnoPad(false, nextObjectKey); byte[] decPerms = ac_1.ProcessBlock(perms, 0, perms.Length); if (decPerms[9] != (byte)'a' || decPerms[10] != (byte)'d' || decPerms[11] != (byte)'b') { throw new BadPasswordException(PdfException.BadUserPassword); } int permissionsDecoded = (decPerms[0] & 0xff) | ((decPerms[1] & 0xff) << 8) | ((decPerms[2] & 0xff) << 16) | ((decPerms[3] & 0xff) << 24); bool encryptMetadata = decPerms[8] == (byte)'T'; bool?encryptMetadataEntry = encryptionDictionary.GetAsBool(PdfName.EncryptMetadata); if (permissionsDecoded != permissions || encryptMetadataEntry != null && encryptMetadata != encryptMetadataEntry ) { ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Crypto.Securityhandler.StandardHandlerUsingAes256)); logger.Error(iText.IO.LogMessageConstant.ENCRYPTION_ENTRIES_P_AND_ENCRYPT_METADATA_NOT_CORRESPOND_PERMS_ENTRY ); } this.permissions = permissionsDecoded; this.encryptMetadata = encryptMetadata; } catch (BadPasswordException ex) { throw; } catch (Exception ex) { throw new PdfException(PdfException.PdfEncryption, ex); } }