core: fix standard crypt filter name; resolves issues with Adobe Reader

This commit is contained in:
Denys Smirnov 2018-09-19 23:48:50 +03:00
parent eac1e899dc
commit b7dcbc22de
3 changed files with 11 additions and 7 deletions

View File

@ -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" + "\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C" +
"\xA9\xFE\x64\x53\x69\x7A" "\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. // CryptFilter represents information from a CryptFilter dictionary.
// TODO (v3): Unexport. // TODO (v3): Unexport.
type CryptFilter struct { type CryptFilter struct {
@ -230,6 +233,7 @@ func (crypt *PdfCrypt) SaveCryptFilters(ed *PdfObjectDictionary) error {
cf.Set(PdfObjectName(name), v) cf.Set(PdfObjectName(name), v)
v.Set("Type", MakeName("CryptFilter")) v.Set("Type", MakeName("CryptFilter"))
v.Set("AuthEvent", MakeName("DocOpen"))
v.Set("CFM", MakeName(string(filter.Cfm))) v.Set("CFM", MakeName(string(filter.Cfm)))
v.Set("Length", MakeInteger(int64(filter.Length))) v.Set("Length", MakeInteger(int64(filter.Length)))
} }
@ -280,7 +284,7 @@ func PdfCryptMakeNew(parser *PdfParser, ed, trailer *PdfObjectDictionary) (PdfCr
crypter.V = int(*V) crypter.V = int(*V)
// Default algorithm is V2. // Default algorithm is V2.
crypter.CryptFilters = CryptFilters{} 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 { } else if *V >= 4 && *V <= 5 {
crypter.V = int(*V) crypter.V = int(*V)
if err := crypter.LoadCryptFilters(ed); err != nil { if err := crypter.LoadCryptFilters(ed); err != nil {
@ -766,7 +770,7 @@ func (crypt *PdfCrypt) Decrypt(obj PdfObject, parentObjNum, parentGenNum int64)
dict := so.PdfObjectDictionary dict := so.PdfObjectDictionary
streamFilter := "Default" // Default RC4. streamFilter := StandardCryptFilter // Default RC4.
if crypt.V >= 4 { if crypt.V >= 4 {
streamFilter = crypt.StreamFilter streamFilter = crypt.StreamFilter
common.Log.Trace("this.StreamFilter = %s", 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 { if s, isString := obj.(*PdfObjectString); isString {
common.Log.Trace("Decrypting string!") common.Log.Trace("Decrypting string!")
stringFilter := "Default" stringFilter := StandardCryptFilter
if crypt.V >= 4 { if crypt.V >= 4 {
// Currently only support Identity / RC4. // Currently only support Identity / RC4.
common.Log.Trace("with %s filter", crypt.StringFilter) common.Log.Trace("with %s filter", crypt.StringFilter)
@ -1016,7 +1020,7 @@ func (crypt *PdfCrypt) Encrypt(obj PdfObject, parentObjNum, parentGenNum int64)
dict := so.PdfObjectDictionary dict := so.PdfObjectDictionary
streamFilter := "Default" // Default RC4. streamFilter := StandardCryptFilter // Default RC4.
if crypt.V >= 4 { if crypt.V >= 4 {
// For now. Need to change when we add support for more than // For now. Need to change when we add support for more than
// Identity / RC4. // Identity / RC4.
@ -1073,7 +1077,7 @@ func (crypt *PdfCrypt) Encrypt(obj PdfObject, parentObjNum, parentGenNum int64)
if s, isString := obj.(*PdfObjectString); isString { if s, isString := obj.(*PdfObjectString); isString {
common.Log.Trace("Encrypting string!") common.Log.Trace("Encrypting string!")
stringFilter := "Default" stringFilter := StandardCryptFilter
if crypt.V >= 4 { if crypt.V >= 4 {
common.Log.Trace("with %s filter", crypt.StringFilter) common.Log.Trace("with %s filter", crypt.StringFilter)
if crypt.StringFilter == "Identity" { if crypt.StringFilter == "Identity" {

View File

@ -149,7 +149,7 @@ func TestDecryption1(t *testing.T) {
crypter.DecryptedObjects = map[PdfObject]bool{} crypter.DecryptedObjects = map[PdfObject]bool{}
// Default algorithm is V2 (RC4). // Default algorithm is V2 (RC4).
crypter.CryptFilters = CryptFilters{} 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.V = 2
crypter.R = 3 crypter.R = 3
crypter.P = -3904 crypter.P = -3904

View File

@ -527,7 +527,7 @@ func (this *PdfWriter) Encrypt(userPass, ownerPass []byte, options *EncryptOptio
return fmt.Errorf("unsupported algorithm: %v", options.Algorithm) return fmt.Errorf("unsupported algorithm: %v", options.Algorithm)
} }
const ( const (
defaultFilter = "Default" defaultFilter = StandardCryptFilter
) )
crypter.CryptFilters[defaultFilter] = cf crypter.CryptFilters[defaultFilter] = cf
if crypter.V >= 4 { if crypter.V >= 4 {