wxMaxima
Loading...
Searching...
No Matches
pdfencrypt.h
Go to the documentation of this file.
1/*
2** Name: pdfencrypt.h
3** Purpose: Encryption support for PDF
4** Author: Ulrich Telle
5** Created: 2005-08-16
6** Copyright: (c) 2005-2024 Ulrich Telle
7** Licence: wxWindows licence
8** SPDX-License-Identifier: LGPL-3.0+ WITH WxWindows-exception-3.1
9*/
10
12
13#ifndef _PDF_ENCRYPT_H_
14#define _PDF_ENCRYPT_H_
15
16// wxWidgets headers
17#include <wx/string.h>
18
19// wxPdfDocument headers
20#include "wx/pdfdocdef.h"
21
22// Forward declaration of exported classes
23class WXDLLIMPEXP_FWD_PDFDOC wxPdfRijndael;
24
26
29class WXDLLIMPEXP_PDFDOC wxPdfEncrypt
30{
31public:
33
37 wxPdfEncrypt(int revision = 2, int keyLength = 40);
38
40 virtual ~wxPdfEncrypt();
41
43
49 void GenerateEncryptionKey(const wxString& userPassword,
50 const wxString& ownerPassword,
51 int protection,
52 const wxString& documentId = wxEmptyString);
53
55
69 bool Authenticate(const wxString& documentID, const wxString& password,
70 const wxString& uValue, const wxString& oValue,
71 const wxString& ueValue, const wxString& oeValue, const wxString& permsValue,
72 int pValue, int lengthValue, int rValue, int vValue);
73
75
79 bool PasswordIsValid(const wxString& password) const;
80
82
85 const std::string& GetUValue() const { return m_uValue; }
86
88
91 const std::string& GetUEValue() const { return m_ue; }
92
94
97 const std::string& GetOValue() const { return m_oValue; }
98
100
103 const std::string& GetOEValue() const { return m_oe; }
104
106
109 int GetPValue() const { return m_pValue; }
110
112
115 const std::string& GetPermsValue() const { return m_permsValue; }
116
118
121 int GetRevision() const { return m_rValue; }
122
124
127 unsigned int GetKeyLength() const { return m_keyLength*8; }
128
130
135 void Encrypt(int n, int g, wxString& str);
136
138
143 void Decrypt(int n, int g, wxString& str);
144
146
152 void Encrypt(int n, int g, unsigned char* str, unsigned int len);
153
155
161 int Decrypt(int n, int g, unsigned char* str, unsigned int len);
162
164
168 size_t CalculateStreamLength(size_t length);
169
171
174 size_t CalculateStreamOffset();
175
177
180 wxString GetDocumentId() const { return m_documentId; }
181
183
186 static wxString CreateDocumentId();
187
188protected:
190 std::string PadPassword(const wxString& password);
191
193 std::string ComputeOwnerKey(const std::string& userPad, const std::string& ownerPad,
194 unsigned int keylength, int revision, bool authenticate);
195
197 std::string ComputeEncryptionKey(const wxString& documentID,
198 const std::string& userPad, const std::string& ownerKey,
199 int pValue, unsigned int keyLength, int revision);
200
202 bool CheckKey(const std::string& key1, const std::string& key2);
203
205 void RC4(unsigned char* key, unsigned int keylen,
206 const unsigned char* textin, unsigned int textlen,
207 unsigned char* textout);
208
210 void AESEncrypt(unsigned char* key, unsigned int keylen,
211 unsigned char* textin, unsigned int textlen,
212 unsigned char* textout);
213
215 int AESDecrypt(unsigned char* key, unsigned int keylen,
216 unsigned char* textin, unsigned int textlen,
217 unsigned char* textout);
218
220 void AESV3Encrypt(const unsigned char* key, unsigned int keylen,
221 const unsigned char* textin, unsigned int textlen,
222 unsigned char* textout);
223
225 int AESV3Decrypt(const unsigned char* key, unsigned int keylen,
226 unsigned char* textin, unsigned int textlen,
227 unsigned char* textout);
228
230 static void GetMD5Binary(const unsigned char* data, unsigned int length, unsigned char* digest);
231
233 static void GenerateInitialVector(unsigned char iv[16]);
234
235private:
237 void ComputeEncryptionParameters(const wxString& userPassword, const wxString& ownerPassword);
238
240 void ComputeEncryptionParametersV5(const wxString& userPassword, const wxString& ownerPassword);
241
243 std::string PreparePasswordV5(const std::string& password);
244
246 void ComputeUandUEforV5(const std::string& userPassword, const std::string& encryptionKey);
247
249 void ComputeOandOEforV5(const std::string& ownerPassword, const std::string& encryptionKey);
250
252 void ComputePermsV5(const std::string& encryptionKey);
253
255 std::string HashV5(const std::string& password, const std::string& salt, const std::string& udata);
256
258 bool CheckEncryptionParameters(const wxString& password);
259
261 bool CheckEncryptionParametersV5(const wxString& password);
262
264 bool CheckOwnerPasswordV5(const std::string& password);
265
267 bool CheckUserPasswordV5(const std::string& userPassword);
268
269 wxString m_documentId;
270 std::string m_uValue;
271 std::string m_oValue;
272 int m_pValue;
273 int m_rValue;
274 int m_vValue;
275 unsigned char m_encryptionKey[16];
276 unsigned int m_keyLength;
277 unsigned char m_rc4key[16];
278 unsigned char m_rc4last[256];
279
280 std::string m_u;
281 std::string m_ue;
282 std::string m_uValidSalt;
283 std::string m_uKeySalt;
284 std::string m_o;
285 std::string m_oe;
286 std::string m_oValidSalt;
287 std::string m_oKeySalt;
288 std::string m_permsValue;
289 std::string m_fileEncryptionKey;
290 bool m_encryptMetaData;
291
292 wxPdfRijndael* m_aes;
294};
295
298class WXDLLIMPEXP_PDFDOC wxPdfSHA2
299{
300public:
301};
302
303#endif
const wxChar * key
The name in the config file.
Definition: ToolBarSections.cpp:37
Class representing PDF encryption methods. (For internal use only)
Definition: pdfencrypt.h:30
unsigned int GetKeyLength() const
Get the key length of the encryption key in bits.
Definition: pdfencrypt.h:127
int GetRevision() const
Get the revision number of the encryption method.
Definition: pdfencrypt.h:121
const std::string & GetPermsValue() const
Get the Perms object value (protection)
Definition: pdfencrypt.h:115
const std::string & GetUValue() const
Get the U object value (user)
Definition: pdfencrypt.h:85
int GetPValue() const
Get the P object value (protection)
Definition: pdfencrypt.h:109
const std::string & GetUEValue() const
Get the UE object value (user)
Definition: pdfencrypt.h:91
const std::string & GetOEValue() const
Get the OE object value (owner)
Definition: pdfencrypt.h:103
const std::string & GetOValue() const
Get the O object value (owner)
Definition: pdfencrypt.h:97
wxString GetDocumentId() const
Get document id.
Definition: pdfencrypt.h:180
Definition: pdfencrypt.cpp:45
Class implementing the Rijndael cipher. (For internal use only)
Definition: pdfrijndael.h:104
Class representing SHA2 hashing methods. (For internal use only)
Definition: pdfencrypt.h:299
Compile time switches for the wxPdfDocument component.