1
///////////////////////////////////////////////////////////////////////////////
4
// Author: Ulrich Telle
7
// Copyright: (c) Ulrich Telle
8
// Licence: wxWindows licence
9
///////////////////////////////////////////////////////////////////////////////
11
/// \file pdfencrypt.h Interface of the wxPdfFont class
13
#ifndef _PDFENCRYPT_H_
14
#define _PDFENCRYPT_H_
17
#include <wx/string.h>
18
#include <wx/wfstream.h>
20
#include "wx/pdfdocdef.h"
21
#include "wx/pdfrijndael.h"
23
/// Class representing PDF encryption methods. (For internal use only)
24
class WXDLLIMPEXP_PDFDOC wxPdfEncrypt
27
/// Default constructor
28
wxPdfEncrypt(int revision = 2, int keyLength = 40);
30
/// Default destructor
31
virtual ~wxPdfEncrypt();
33
/// Generate encryption key from user and owner passwords and protection key
34
void GenerateEncryptionKey(const wxString& userPassword,
35
const wxString& ownerPassword,
38
bool Authenticate(const wxString& documentID, const wxString& password,
39
const wxString& uValue, const wxString& oValue,
40
int pValue, int lengthValue, int rValue);
42
/// Get the U object value (user)
43
const unsigned char* GetUValue() const { return m_uValue; }
45
/// Get the O object value (owner)
46
const unsigned char* GetOValue() const { return m_oValue; }
48
/// Get the P object value (protection)
49
int GetPValue() const { return m_pValue; }
51
/// Get the revision number of the encryption method
52
int GetRevision() const { return m_rValue; }
54
/// Get the key length of the encryption key in bits
55
int GetKeyLength() const { return m_keyLength*8; }
57
/// Encrypt a wxString
58
void Encrypt(int n, int g, wxString& str);
60
/// Encrypt a character string
61
void Encrypt(int n, int g, unsigned char* str, int len);
63
/// Calculate stream size
64
int CalculateStreamLength(int length);
66
/// Calculate stream offset
67
int CalculateStreamOffset();
69
/// Create document id
70
wxString CreateDocumentId();
73
wxString GetDocumentId() const { return m_documentId; }
76
/// Pad a password to 32 characters
77
void PadPassword(const wxString& password, unsigned char pswd[32]);
80
void ComputeOwnerKey(unsigned char userPad[32], unsigned char ownerPad[32],
81
int keylength, int revision, bool authenticate,
82
unsigned char ownerKey[32]);
84
/// Compute encryption key and user key
85
void ComputeEncryptionKey(const wxString& documentID,
86
unsigned char userPad[32], unsigned char ownerKey[32],
87
int pValue, int keyLength, int revision,
88
unsigned char userKey[32]);
90
/// Check two keys for equality
91
bool CheckKey(unsigned char key1[32], unsigned char key2[32]);
94
void RC4(unsigned char* key, int keylen,
95
unsigned char* textin, int textlen,
96
unsigned char* textout);
98
/// Calculate the binary MD5 message digest of the given data
99
void GetMD5Binary(const unsigned char* data, int length, unsigned char* digest);
102
void AES(unsigned char* key, int WXUNUSED(keylen),
103
unsigned char* textin, int textlen,
104
unsigned char* textout);
106
/// Generate initial vector
107
void GenerateInitialVector(unsigned char iv[16]);
110
wxString m_documentId; ///< Document ID
111
unsigned char m_uValue[32]; ///< U entry in pdf document
112
unsigned char m_oValue[32]; ///< O entry in pdf document
113
int m_pValue; ///< P entry in pdf document
114
int m_rValue; ///< Revision
115
unsigned char m_encryptionKey[16]; ///< Encryption key
116
int m_keyLength; ///< Length of encryption key
117
unsigned char m_rc4key[16]; ///< last RC4 key
118
unsigned char m_rc4last[256]; ///< last RC4 state table
120
wxPdfRijndael* m_aes; ///< AES encryptor