From b7dcbc22deb5f04edd0fd47e9b2b0b9ffdb89101 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Wed, 19 Sep 2018 23:48:50 +0300 Subject: [PATCH] core: fix standard crypt filter name; resolves issues with Adobe Reader --- pdf/core/crypt.go | 14 +++++++++----- pdf/core/crypt_test.go | 2 +- pdf/model/writer.go | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pdf/core/crypt.go b/pdf/core/crypt.go index 211e3395..a1466d4b 100644 --- a/pdf/core/crypt.go +++ b/pdf/core/crypt.go @@ -77,6 +77,9 @@ const padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF" + "\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C" + "\xA9\xFE\x64\x53\x69\x7A" +// StandardCryptFilter is a default name for a standard crypt filter. +const StandardCryptFilter = "StdCF" + // CryptFilter represents information from a CryptFilter dictionary. // TODO (v3): Unexport. type CryptFilter struct { @@ -230,6 +233,7 @@ func (crypt *PdfCrypt) SaveCryptFilters(ed *PdfObjectDictionary) error { cf.Set(PdfObjectName(name), v) v.Set("Type", MakeName("CryptFilter")) + v.Set("AuthEvent", MakeName("DocOpen")) v.Set("CFM", MakeName(string(filter.Cfm))) v.Set("Length", MakeInteger(int64(filter.Length))) } @@ -280,7 +284,7 @@ func PdfCryptMakeNew(parser *PdfParser, ed, trailer *PdfObjectDictionary) (PdfCr crypter.V = int(*V) // Default algorithm is V2. crypter.CryptFilters = CryptFilters{} - crypter.CryptFilters["Default"] = CryptFilter{Cfm: "V2", Length: crypter.Length} + crypter.CryptFilters[StandardCryptFilter] = CryptFilter{Cfm: "V2", Length: crypter.Length} } else if *V >= 4 && *V <= 5 { crypter.V = int(*V) if err := crypter.LoadCryptFilters(ed); err != nil { @@ -766,7 +770,7 @@ func (crypt *PdfCrypt) Decrypt(obj PdfObject, parentObjNum, parentGenNum int64) dict := so.PdfObjectDictionary - streamFilter := "Default" // Default RC4. + streamFilter := StandardCryptFilter // Default RC4. if crypt.V >= 4 { streamFilter = crypt.StreamFilter common.Log.Trace("this.StreamFilter = %s", crypt.StreamFilter) @@ -821,7 +825,7 @@ func (crypt *PdfCrypt) Decrypt(obj PdfObject, parentObjNum, parentGenNum int64) if s, isString := obj.(*PdfObjectString); isString { common.Log.Trace("Decrypting string!") - stringFilter := "Default" + stringFilter := StandardCryptFilter if crypt.V >= 4 { // Currently only support Identity / RC4. common.Log.Trace("with %s filter", crypt.StringFilter) @@ -1016,7 +1020,7 @@ func (crypt *PdfCrypt) Encrypt(obj PdfObject, parentObjNum, parentGenNum int64) dict := so.PdfObjectDictionary - streamFilter := "Default" // Default RC4. + streamFilter := StandardCryptFilter // Default RC4. if crypt.V >= 4 { // For now. Need to change when we add support for more than // Identity / RC4. @@ -1073,7 +1077,7 @@ func (crypt *PdfCrypt) Encrypt(obj PdfObject, parentObjNum, parentGenNum int64) if s, isString := obj.(*PdfObjectString); isString { common.Log.Trace("Encrypting string!") - stringFilter := "Default" + stringFilter := StandardCryptFilter if crypt.V >= 4 { common.Log.Trace("with %s filter", crypt.StringFilter) if crypt.StringFilter == "Identity" { diff --git a/pdf/core/crypt_test.go b/pdf/core/crypt_test.go index 0852354f..ca9b4af3 100644 --- a/pdf/core/crypt_test.go +++ b/pdf/core/crypt_test.go @@ -149,7 +149,7 @@ func TestDecryption1(t *testing.T) { crypter.DecryptedObjects = map[PdfObject]bool{} // Default algorithm is V2 (RC4). crypter.CryptFilters = CryptFilters{} - crypter.CryptFilters["Default"] = CryptFilter{Cfm: "V2", Length: crypter.Length} + crypter.CryptFilters[StandardCryptFilter] = CryptFilter{Cfm: "V2", Length: crypter.Length} crypter.V = 2 crypter.R = 3 crypter.P = -3904 diff --git a/pdf/model/writer.go b/pdf/model/writer.go index 83ff8698..edc0cc4c 100644 --- a/pdf/model/writer.go +++ b/pdf/model/writer.go @@ -527,7 +527,7 @@ func (this *PdfWriter) Encrypt(userPass, ownerPass []byte, options *EncryptOptio return fmt.Errorf("unsupported algorithm: %v", options.Algorithm) } const ( - defaultFilter = "Default" + defaultFilter = StandardCryptFilter ) crypter.CryptFilters[defaultFilter] = cf if crypter.V >= 4 {