PDF SDK Documentation

Comprehensive Guide for Developers: Features, Integration, and API Reference

Loading...
Searching...
No Matches
standard_security.h
Go to the documentation of this file.
1// Copyright (c) 2009-2025 Avanquest Software. All rights reserved.
2
3#ifndef PDFSDK_CXX_PDF_STANDARD_SECURITY_H_INCLUDED_
4#define PDFSDK_CXX_PDF_STANDARD_SECURITY_H_INCLUDED_
5
11#include <cstring>
12#include <cwchar>
13#include <string>
14
15#include <pdfsdk/core.h>
16#include <pdfsdk/cxx/exception.h>
17
18namespace PDF {
19
35
44 enum class PrintPermissions {
45 None,
48 };
49
65
74 enum class ExtractPermissions {
75 None,
77 Any
78 };
79
80 PDStdSecurityModifyFlags modifyFlags = 0;
81
93
113 static int GetDefaultVersionByCryptMethod(PDAtom cryptMethod, int keyLen);
114
133 static int GetDefaultRevisionByCryptMethod(PDAtom cryptMethod, int keyLen);
134
135 static PDStdPerms GetAllAllowedDefaultPermissions();
136
143
154 void SetPermissions(PrintPermissions printPerms, EditPermissions editPerms, ExtractPermissions extractPerms);
155
165 void SetCryptMethod(PDAtom method, int keyLen = 0);
166
175 void ResetToDefault();
176
177 bool operator==(const StdSecurityParams& that) const;
178
184 bool HasPasswords() const;
185
195 std::wstring GetUserPassword() const;
196
206 std::wstring GetOwnerPassword() const;
207
213 void SetUserPassword(const std::wstring& newUserPassword);
214
220 void SetOwnerPassword(const std::wstring& newOwnerPassword);
221};
222
224 switch (cryptMethod) {
225 case kPDAtom_V2:
226 return 16;
227
228 case kPDAtom_AESV2:
229 return 16;
230
231 case kPDAtom_AESV3:
232 return 32;
233
234 default:
235 return 5;
236 }
237}
238
239inline int StdSecurityParams::GetDefaultVersionByCryptMethod(PDAtom cryptMethod, int keyLen) {
240 switch (cryptMethod) {
241 case kPDAtom_AESV2:
242 return 4;
243
244 case kPDAtom_AESV3:
245 return 5;
246
247 default:
248 return keyLen > 5 ? 2 : 1;
249 }
250}
251
252inline int StdSecurityParams::GetDefaultRevisionByCryptMethod(PDAtom cryptMethod, int keyLen) {
253 switch (cryptMethod) {
254 case kPDAtom_AESV2: // AES 128 bit
255 return 4;
256
257 case kPDAtom_AESV3: // AES 256 bit
258 return 6;
259
260 default:
261 return keyLen > 5 ? 3 : 2;
262 }
263}
264
265inline PDStdPerms StdSecurityParams::GetAllAllowedDefaultPermissions() {
266 return 0xFFFFFFFC;
267}
268
272
295
296inline void StdSecurityParams::SetCryptMethod(PDAtom method, int keyLen) {
297 cryptMethod = method;
298 if (keyLen == 0)
299 keyLen = GetDefaultKeyLengthByCryptMethod(method);
300 keyLength = keyLen;
301 version = GetDefaultVersionByCryptMethod(method, keyLen);
303}
304
306 SetUserPassword(std::wstring{});
307 SetOwnerPassword(std::wstring{});
308 permissions = GetAllAllowedDefaultPermissions();
309 cryptMethod = kPDAtom_AESV3;
310 keyLength = 32;
311 version = 5;
312 revision = 6;
313 encryptMetadata = true;
315}
316
317inline bool StdSecurityParams::operator==(const StdSecurityParams& that) const {
318 return GetUserPassword() == that.GetUserPassword() &&
320 permissions == that.permissions &&
321 cryptMethod == that.cryptMethod &&
322 keyLength == that.keyLength &&
323 version == that.version &&
324 revision == that.revision &&
327}
328
330 return hasUserPassword || hasOwnerPassword;
331}
332
333inline std::wstring StdSecurityParams::GetUserPassword() const {
334 if (hasUserPassword)
335 return std::wstring(userPassword, wcsnlen(userPassword, kPDStdPasswordMaxChars));
336 return std::wstring{};
337}
338
339inline std::wstring StdSecurityParams::GetOwnerPassword() const {
340 if (hasOwnerPassword)
341 return std::wstring(ownerPassword, wcsnlen(ownerPassword, kPDStdPasswordMaxChars));
342 return std::wstring{};
343}
344
345inline void StdSecurityParams::SetUserPassword(const std::wstring& newUserPassword) {
346 std::memset(userPassword, 0, sizeof(PDStdPassword));
347 if (!newUserPassword.empty()) {
348 PDStdPasswordSetChars(&userPassword, newUserPassword.c_str());
349 hasUserPassword = true;
350 } else {
351 hasUserPassword = false;
352 }
353 modifyFlags |= kPDStdSecurityModifyUserPassword;
354}
355
356inline void StdSecurityParams::SetOwnerPassword(const std::wstring& newOwnerPassword) {
357 std::memset(ownerPassword, 0, sizeof(PDStdPassword));
358 if (!newOwnerPassword.empty()) {
359 PDStdPasswordSetChars(&ownerPassword, newOwnerPassword.c_str());
360 hasOwnerPassword = true;
361 } else {
362 hasOwnerPassword = false;
363 }
364 modifyFlags |= kPDStdSecurityModifyOwnerPassword;
365}
366
367} // namespace PDF
368
369#endif // PDFSDK_CXX_PDF_STANDARD_SECURITY_H_INCLUDED_
@ kPDStdPermHighPrint
Definition security.h:181
@ kPDStdPermPrint
Definition security.h:158
@ kPDStdPermFillAndSign
Definition security.h:174
@ kPDStdPermEdit
Definition security.h:159
@ kPDStdPermAccessible
Definition security.h:177
@ kPDStdPermEditNotes
Definition security.h:164
@ kPDStdPermDocAssembly
Definition security.h:179
@ kPDStdPermCopy
Definition security.h:162
Helper class that represents the data of the Standard Security Handler.
Definition standard_security.h:34
PrintPermissions
Enum representing the print permissions for the Standard Security Handler.
Definition standard_security.h:44
@ LowResolution
Low resolution print permissions.
@ HighResolution
High resolution print permissions.
void SetCryptMethod(PDAtom method, int keyLen=0)
Set the cryptographic method and key length for the Standard Security Handler.
Definition standard_security.h:296
EditPermissions
Enum representing the edit permissions for the Standard Security Handler.
Definition standard_security.h:58
@ FormFillAndSign
Filling-in form fields and signing.
@ DocumentAssembly
Creating, inserting, deleting, and rotating pages.
@ AnyButExtractPages
Any permissions except extract pages.
@ ReviewFormFillAndSign
Commenting, filling-in form fields, and signing existing signature fields.
static int GetDefaultRevisionByCryptMethod(PDAtom cryptMethod, int keyLen)
Get the default standard security handler revision based on the cryptographic method and key length.
Definition standard_security.h:252
void SetPermissions(PrintPermissions printPerms, EditPermissions editPerms, ExtractPermissions extractPerms)
Set the permissions for the Standard Security Handler.
Definition standard_security.h:273
bool HasPasswords() const
Check if the Standard Security Handler has either an owner or a user password set,...
Definition standard_security.h:329
StdSecurityParams()
Default constructor for StdSecurityParams.
Definition standard_security.h:269
std::wstring GetOwnerPassword() const
Get the owner password for the Standard Security Handler.
Definition standard_security.h:339
static int GetDefaultKeyLengthByCryptMethod(PDAtom cryptMethod)
Get the default key length of the file encryption key, based on the cryptographic method.
Definition standard_security.h:223
void ResetToDefault()
Reset the Standard Security Handler parameters to their default values.
Definition standard_security.h:305
void SetUserPassword(const std::wstring &newUserPassword)
Set the user password for the Standard Security Handler.
Definition standard_security.h:345
std::wstring GetUserPassword() const
Get the user password for the Standard Security Handler.
Definition standard_security.h:333
ExtractPermissions
Enum representing the extraction permissions for the Standard Security Handler.
Definition standard_security.h:74
@ Accessibility
Enable copying of text, images, and other content.
@ Any
Enable copying of any content.
void SetOwnerPassword(const std::wstring &newOwnerPassword)
Set the owner password for the Standard Security Handler.
Definition standard_security.h:356
static int GetDefaultVersionByCryptMethod(PDAtom cryptMethod, int keyLen)
Get the default version of the encryption algorithm used, based on the cryptographic method and key l...
Definition standard_security.h:239
Definition security.h:207
int version
Definition security.h:245
bool encryptMetadata
Definition security.h:266
bool encryptAttachmentsOnly
Definition security.h:272
PDStdPerms permissions
Definition security.h:233
PDAtom cryptMethod
Definition security.h:252
bool hasUserPassword
Definition security.h:217
int keyLength
Definition security.h:260
PDStdPassword ownerPassword
Definition security.h:222
PDStdPassword userPassword
Definition security.h:211
int revision
Definition security.h:238