codeberg.org/go-pdf/fpdf@v0.11.1/protect.go (about)

     1  // Copyright ©2023 The go-pdf Authors. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  /*
     6   * Copyright (c) 2013-2014 Kurt Jung (Gmail: kurt.w.jung)
     7   *
     8   * Permission to use, copy, modify, and distribute this software for any
     9   * purpose with or without fee is hereby granted, provided that the above
    10   * copyright notice and this permission notice appear in all copies.
    11   *
    12   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    13   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    14   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    15   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    16   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    17   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    18   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    19   */
    20  
    21  // PDF protection is adapted from the work of Klemen VODOPIVEC for the fpdf
    22  // product.
    23  
    24  package fpdf
    25  
    26  import (
    27  	"crypto/md5"
    28  	"crypto/rc4"
    29  	"encoding/binary"
    30  	"math/rand"
    31  )
    32  
    33  // Advisory bitflag constants that control document activities
    34  const (
    35  	CnProtectPrint      = 4
    36  	CnProtectModify     = 8
    37  	CnProtectCopy       = 16
    38  	CnProtectAnnotForms = 32
    39  )
    40  
    41  type protectType struct {
    42  	encrypted     bool
    43  	uValue        []byte
    44  	oValue        []byte
    45  	pValue        int
    46  	padding       []byte
    47  	encryptionKey []byte
    48  	objNum        int
    49  	rc4cipher     *rc4.Cipher
    50  	rc4n          uint32 // Object number associated with rc4 cipher
    51  }
    52  
    53  func (p *protectType) rc4(n uint32, buf *[]byte) {
    54  	if p.rc4cipher == nil || p.rc4n != n {
    55  		p.rc4cipher, _ = rc4.NewCipher(p.objectKey(n))
    56  		p.rc4n = n
    57  	}
    58  	p.rc4cipher.XORKeyStream(*buf, *buf)
    59  }
    60  
    61  func (p *protectType) objectKey(n uint32) []byte {
    62  	var nbuf, b []byte
    63  	nbuf = make([]byte, 8)
    64  	binary.LittleEndian.PutUint32(nbuf, n)
    65  	b = append(b, p.encryptionKey...)
    66  	b = append(b, nbuf[0], nbuf[1], nbuf[2], 0, 0)
    67  	s := md5.Sum(b)
    68  	return s[0:10]
    69  }
    70  
    71  func oValueGen(userPass, ownerPass []byte) (v []byte) {
    72  	var c *rc4.Cipher
    73  	tmp := md5.Sum(ownerPass)
    74  	c, _ = rc4.NewCipher(tmp[0:5])
    75  	size := len(userPass)
    76  	v = make([]byte, size)
    77  	c.XORKeyStream(v, userPass)
    78  	return
    79  }
    80  
    81  func (p *protectType) uValueGen() (v []byte) {
    82  	var c *rc4.Cipher
    83  	c, _ = rc4.NewCipher(p.encryptionKey)
    84  	size := len(p.padding)
    85  	v = make([]byte, size)
    86  	c.XORKeyStream(v, p.padding)
    87  	return
    88  }
    89  
    90  func (p *protectType) setProtection(privFlag byte, userPassStr, ownerPassStr string) {
    91  	privFlag = 192 | (privFlag & (CnProtectCopy | CnProtectModify | CnProtectPrint | CnProtectAnnotForms))
    92  	p.padding = []byte{
    93  		0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41,
    94  		0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08,
    95  		0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80,
    96  		0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A,
    97  	}
    98  	userPass := []byte(userPassStr)
    99  	var ownerPass []byte
   100  	if ownerPassStr == "" {
   101  		ownerPass = make([]byte, 8)
   102  		binary.LittleEndian.PutUint64(ownerPass, uint64(rand.Int63()))
   103  	} else {
   104  		ownerPass = []byte(ownerPassStr)
   105  	}
   106  	userPass = append(userPass, p.padding...)[0:32]
   107  	ownerPass = append(ownerPass, p.padding...)[0:32]
   108  	p.encrypted = true
   109  	p.oValue = oValueGen(userPass, ownerPass)
   110  	var buf []byte
   111  	buf = append(buf, userPass...)
   112  	buf = append(buf, p.oValue...)
   113  	buf = append(buf, privFlag, 0xff, 0xff, 0xff)
   114  	sum := md5.Sum(buf)
   115  	p.encryptionKey = sum[0:5]
   116  	p.uValue = p.uValueGen()
   117  	p.pValue = -(int(privFlag^255) + 1)
   118  }