PDF SDK Documentation

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

Loading...
Searching...
No Matches
standard_security.h
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
6#include <cstring>
7#include <cwchar>
8#include <string>
9
10#include <pdfsdk/core.h>
11#include <pdfsdk/cxx/exception.h>
12
13namespace PDF {
14
30
39 enum class PrintPermissions {
40 None,
43 };
44
60
69 enum class ExtractPermissions {
70 None,
72 Any
73 };
74
75 PDStdSecurityModifyFlags modifyFlags = 0;
76
88
108 static int GetDefaultVersionByCryptMethod(PDAtom cryptMethod, int keyLen);
109
128 static int GetDefaultRevisionByCryptMethod(PDAtom cryptMethod, int keyLen);
129
130 static PDStdPerms GetAllAllowedDefaultPermissions();
131
138
149 void SetPermissions(PrintPermissions printPerms, EditPermissions editPerms, ExtractPermissions extractPerms);
150
160 void SetCryptMethod(PDAtom method, int keyLen = 0);
161
170 void ResetToDefault();
171
172 bool operator==(const StdSecurityParams& that) const;
173
179 bool HasPasswords() const;
180
190 std::wstring GetUserPassword() const;
191
201 std::wstring GetOwnerPassword() const;
202
208 void SetUserPassword(const std::wstring& newUserPassword);
209
215 void SetOwnerPassword(const std::wstring& newOwnerPassword);
216};
217
219 switch (cryptMethod) {
220 case kPDAtom_V2:
221 return 16;
222
223 case kPDAtom_AESV2:
224 return 16;
225
226 case kPDAtom_AESV3:
227 return 32;
228
229 default:
230 return 5;
231 }
232}
233
234inline int StdSecurityParams::GetDefaultVersionByCryptMethod(PDAtom cryptMethod, int keyLen) {
235 switch (cryptMethod) {
236 case kPDAtom_AESV2:
237 return 4;
238
239 case kPDAtom_AESV3:
240 return 5;
241
242 default:
243 return keyLen > 5 ? 2 : 1;
244 }
245}
246
247inline int StdSecurityParams::GetDefaultRevisionByCryptMethod(PDAtom cryptMethod, int keyLen) {
248 switch (cryptMethod) {
249 case kPDAtom_AESV2: // AES 128 bit
250 return 4;
251
252 case kPDAtom_AESV3: // AES 256 bit
253 return 6;
254
255 default:
256 return keyLen > 5 ? 3 : 2;
257 }
258}
259
260inline PDStdPerms StdSecurityParams::GetAllAllowedDefaultPermissions() {
261 return 0xFFFFFFFC;
262}
263
267
269 permissions = 0xFFFFF0C0;
270
271 if (printPerms == PrintPermissions::LowResolution)
272 permissions |= kPDStdPermPrint;
273 else if (printPerms == PrintPermissions::HighResolution)
274 permissions |= kPDStdPermPrint | kPDStdPermHighPrint;
275
276 if (editPerms == EditPermissions::DocumentAssembly)
277 permissions |= kPDStdPermDocAssembly;
278 else if (editPerms == EditPermissions::FormFillAndSign)
279 permissions |= kPDStdPermFillAndSign;
280 else if (editPerms == EditPermissions::ReviewFormFillAndSign)
281 permissions |= kPDStdPermFillAndSign | kPDStdPermEditNotes;
282 else if (editPerms == EditPermissions::AnyButExtractPages)
283 permissions |= kPDStdPermFillAndSign | kPDStdPermEditNotes | kPDStdPermEdit;
284
285 if (extractPerms == ExtractPermissions::Any)
286 permissions |= kPDStdPermAccessible | kPDStdPermCopy;
287 else if (extractPerms == ExtractPermissions::Accessibility)
288 permissions |= kPDStdPermAccessible;
289}
290
291inline void StdSecurityParams::SetCryptMethod(PDAtom method, int keyLen) {
292 cryptMethod = method;
293 if (keyLen == 0)
294 keyLen = GetDefaultKeyLengthByCryptMethod(method);
295 keyLength = keyLen;
296 version = GetDefaultVersionByCryptMethod(method, keyLen);
298}
299
301 SetUserPassword(std::wstring{});
302 SetOwnerPassword(std::wstring{});
303 permissions = GetAllAllowedDefaultPermissions();
304 cryptMethod = kPDAtom_AESV3;
305 keyLength = 32;
306 version = 5;
307 revision = 6;
308 encryptMetadata = true;
310}
311
312inline bool StdSecurityParams::operator==(const StdSecurityParams& that) const {
313 return GetUserPassword() == that.GetUserPassword() &&
315 permissions == that.permissions &&
316 cryptMethod == that.cryptMethod &&
317 keyLength == that.keyLength &&
318 version == that.version &&
319 revision == that.revision &&
322}
323
325 return hasUserPassword || hasOwnerPassword;
326}
327
328inline std::wstring StdSecurityParams::GetUserPassword() const {
329 if (hasUserPassword)
330 return std::wstring(userPassword, wcsnlen(userPassword, kPDStdPasswordMaxChars));
331 return std::wstring{};
332}
333
334inline std::wstring StdSecurityParams::GetOwnerPassword() const {
335 if (hasOwnerPassword)
336 return std::wstring(ownerPassword, wcsnlen(ownerPassword, kPDStdPasswordMaxChars));
337 return std::wstring{};
338}
339
340inline void StdSecurityParams::SetUserPassword(const std::wstring& newUserPassword) {
341 std::memset(userPassword, 0, sizeof(PDStdPassword));
342 if (!newUserPassword.empty()) {
343 PDStdPasswordSetChars(&userPassword, newUserPassword.c_str());
344 hasUserPassword = true;
345 } else {
346 hasUserPassword = false;
347 }
348 modifyFlags |= kPDStdSecurityModifyUserPassword;
349}
350
351inline void StdSecurityParams::SetOwnerPassword(const std::wstring& newOwnerPassword) {
352 std::memset(ownerPassword, 0, sizeof(PDStdPassword));
353 if (!newOwnerPassword.empty()) {
354 PDStdPasswordSetChars(&ownerPassword, newOwnerPassword.c_str());
355 hasOwnerPassword = true;
356 } else {
357 hasOwnerPassword = false;
358 }
359 modifyFlags |= kPDStdSecurityModifyOwnerPassword;
360}
361
362} // namespace PDF
363
364#endif // PDFSDK_CXX_PDF_STANDARD_SECURITY_H_INCLUDED_
Helper class that represents the data of the Standard Security Handler.
Definition standard_security.h:29
PrintPermissions
Enum representing the print permissions for the Standard Security Handler.
Definition standard_security.h:39
@ 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:291
EditPermissions
Enum representing the edit permissions for the Standard Security Handler.
Definition standard_security.h:53
@ 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:247
void SetPermissions(PrintPermissions printPerms, EditPermissions editPerms, ExtractPermissions extractPerms)
Set the permissions for the Standard Security Handler.
Definition standard_security.h:268
bool HasPasswords() const
Check if the Standard Security Handler has either an owner or a user password set,...
Definition standard_security.h:324
StdSecurityParams()
Default constructor for StdSecurityParams.
Definition standard_security.h:264
std::wstring GetOwnerPassword() const
Get the owner password for the Standard Security Handler.
Definition standard_security.h:334
static int GetDefaultKeyLengthByCryptMethod(PDAtom cryptMethod)
Get the default key length of the file encryption key, based on the cryptographic method.
Definition standard_security.h:218
void ResetToDefault()
Reset the Standard Security Handler parameters to their default values.
Definition standard_security.h:300
void SetUserPassword(const std::wstring &newUserPassword)
Set the user password for the Standard Security Handler.
Definition standard_security.h:340
std::wstring GetUserPassword() const
Get the user password for the Standard Security Handler.
Definition standard_security.h:328
ExtractPermissions
Enum representing the extraction permissions for the Standard Security Handler.
Definition standard_security.h:69
@ 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:351
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:234
Definition security.h:137
int version
Definition security.h:175
bool encryptMetadata
Definition security.h:196
bool encryptAttachmentsOnly
Definition security.h:202
PDStdPerms permissions
Definition security.h:163
PDAtom cryptMethod
Definition security.h:182
bool hasUserPassword
Definition security.h:147
int keyLength
Definition security.h:190
PDStdPassword ownerPassword
Definition security.h:152
PDStdPassword userPassword
Definition security.h:141
int revision
Definition security.h:168