10 #include "PdfString.h"
11 #include "PdfReference.h"
14 #ifndef PODOFO_CRYPT_CTX
15 #define PODOFO_CRYPT_CTX void
55 #ifdef PODOFO_HAVE_LIBIDN
95 #ifdef PODOFO_HAVE_LIBIDN
109 class PdfEncryptContext;
123 friend class PdfEncryptMD5Base;
124 friend class PdfEncryptAESV3;
144 static std::unique_ptr<PdfEncrypt>
Create(
const std::string_view& userPassword,
145 const std::string_view& ownerPassword,
161 static std::unique_ptr<PdfEncrypt> CreateFromObject(
const PdfObject& obj);
189 void EnsureEncryptionInitialized(
const PdfString& documentId, PdfEncryptContext& context);
221 PdfEncryptContext& context,
const PdfReference& objref)
const = 0;
234 PdfEncryptContext& context,
const PdfReference& objref)
const = 0;
250 bool IsOwnerPasswordSet()
const;
259 bool IsPrintAllowed()
const;
268 bool IsEditAllowed()
const;
277 bool IsCopyAllowed()
const;
286 bool IsEditNotesAllowed()
const;
295 bool IsFillAndSignAllowed()
const;
304 bool IsAccessibilityAllowed()
const;
313 bool IsDocAssemblyAllowed()
const;
322 bool IsHighPrintAllowed()
const;
343 unsigned GetKeyLengthBytes()
const;
366 inline bool IsMetadataEncrypted()
const {
return m_EncryptMetadata; }
368 inline bool IsParsed()
const {
return m_IsParsed; }
371 inline const unsigned char* GetUValueRaw()
const {
return m_uValue; }
373 inline const unsigned char* GetOValueRaw()
const {
return m_oValue; }
375 inline const std::string& GetUserPassword()
const {
return m_userPass; }
377 inline const std::string& GetOwnerPassword()
const {
return m_ownerPass; }
379 int64_t GetPValueForSerialization()
const;
387 bool encryptedMetadata);
393 void InitFromScratch(
const std::string_view& userPassword,
const std::string_view& ownerPassword,
397 virtual void Decrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
398 const PdfReference& objref,
char* outStr,
size_t& outLen)
const = 0;
400 virtual void Encrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
401 const PdfReference& objref,
char* outStr,
size_t outLen)
const = 0;
403 virtual PdfAuthResult Authenticate(
const std::string_view& password,
const std::string_view& documentId,
404 PODOFO_CRYPT_CTX* ctx,
unsigned char encryptionKey[32])
const = 0;
406 virtual void GenerateEncryptionKey(
407 const std::string_view& documentId,
PdfAuthResult authResult, PODOFO_CRYPT_CTX* ctx,
408 unsigned char uValue[48],
unsigned char oValue[48],
unsigned char encryptionKey[32]) = 0;
411 bool CheckKey(
const unsigned char key1[32],
const unsigned char key2[32])
const;
413 enum class PdfRC4Revision
419 #ifdef PODOFO_HAVE_LIBIDN
420 enum class PdfAESV3Revision
430 PdfEncrypt(
const PdfEncrypt& rhs) =
default;
432 PdfEncrypt& operator=(PdfEncrypt& rhs) =
delete;
435 static std::unique_ptr<PdfEncrypt> CreateFromEncrypt(
const PdfEncrypt& rhs);
437 void clearSensitiveInfo();
441 unsigned char m_rValue;
444 unsigned char m_uValue[48];
445 unsigned char m_oValue[48];
446 unsigned char m_uValueSize;
447 unsigned char m_oValueSize;
448 bool m_EncryptMetadata;
451 std::string m_userPass;
452 std::string m_ownerPass;
456 class PODOFO_API PdfEncryptContext final
458 friend class PdfEncrypt;
459 friend class PdfEncryptRC4;
460 friend class PdfEncryptAESV2;
461 friend class PdfEncryptAESV3;
466 ~PdfEncryptContext();
468 PdfEncryptContext(
const PdfEncryptContext&);
470 PdfEncryptContext& operator=(
const PdfEncryptContext&);
473 inline PdfAuthResult GetAuthResult() {
return m_AuthResult; }
475 inline const std::string GetDocumentId() {
return m_documentId; }
477 bool IsAuthenticated()
const;
480 inline const unsigned char* GetEncryptionKey()
const {
return m_encryptionKey; }
482 PODOFO_CRYPT_CTX* GetCryptCtx();
484 template <
typename T>
487 if (m_customCtx ==
nullptr)
489 m_customCtx = ::operator
new(
sizeof(T));
490 m_customCtxSize =
sizeof(T);
493 return *(T*)m_customCtx;
497 unsigned char m_encryptionKey[32];
498 std::string m_documentId;
500 PODOFO_CRYPT_CTX* m_cryptCtx;
502 size_t m_customCtxSize;
505 #ifdef PODOFO_HAVE_LIBIDN
516 class PdfEncryptMD5Base :
public PdfEncrypt
518 friend class PdfEncryptRC4;
519 friend class PdfEncryptAESV2;
524 PdfEncryptMD5Base(
const PdfEncryptMD5Base& rhs);
527 void CreateEncryptionDictionary(PdfDictionary& dictionary)
const override;
531 PODOFO_CRYPT_CTX* ctx,
unsigned char encryptionKey[32])
const override = 0;
535 static void ComputeOwnerKey(
const unsigned char userPad[32],
const unsigned char ownerPad[32],
536 unsigned keylength,
unsigned revision,
bool authenticate, PODOFO_CRYPT_CTX* ctx,
unsigned char ownerKey[32]);
539 static void PadPassword(
const std::string_view& password,
unsigned char pswd[32]);
542 static void ComputeEncryptionKey(
const std::string_view& documentID,
543 const unsigned char userPad[32],
const unsigned char ownerKey[32],
545 bool encryptMetadata, PODOFO_CRYPT_CTX* ctx,
546 unsigned char userKey[32],
unsigned char encryptionKey[32]);
553 void CreateObjKey(
unsigned char objkey[16],
unsigned& pnKeyLen,
554 const unsigned char m_encryptionKey[32],
const PdfReference& objref)
const;
569 PdfEncryptAESV2(
const std::string_view& userPassword,
const std::string_view& ownerPassword,
575 PdfEncryptContext& context,
const PdfReference& objref)
const override;
577 PdfEncryptContext& context,
const PdfReference& objref)
const override;
584 void Encrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
585 const PdfReference& objref,
char* outStr,
size_t outLen)
const override;
586 void Decrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
587 const PdfReference& objref,
char* outStr,
size_t& outLen)
const override;
589 void GenerateEncryptionKey(
const std::string_view& documentId,
PdfAuthResult authResult, PODOFO_CRYPT_CTX* ctx,
590 unsigned char uValue[48],
unsigned char oValue[48],
unsigned char encryptionKey[32])
override;
592 PdfAuthResult Authenticate(
const std::string_view& password,
const std::string_view& documentId,
593 PODOFO_CRYPT_CTX* ctx,
unsigned char encryptionKey[32])
const override;
596 void generateInitialVector(
const std::string_view& documentId,
unsigned char iv[])
const;
599 #ifdef PODOFO_HAVE_LIBIDN
607 class PdfEncryptAESV3 final :
public PdfEncrypt
614 PdfEncryptAESV3(
const std::string_view& userPassword,
const std::string_view& ownerPassword,
616 PdfEncryptAESV3(
const PdfEncryptAESV3& rhs);
619 std::unique_ptr<InputStream> CreateEncryptionInputStream(
InputStream& inputStream,
size_t inputLen,
620 PdfEncryptContext& context,
const PdfReference& objref)
const override;
621 std::unique_ptr<OutputStream> CreateEncryptionOutputStream(
OutputStream& outputStream,
622 PdfEncryptContext& context,
const PdfReference& objref)
const override;
624 size_t CalculateStreamOffset()
const override;
626 size_t CalculateStreamLength(
size_t length)
const override;
628 void CreateEncryptionDictionary(
PdfDictionary& dictionary)
const override;
641 void Encrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
642 const PdfReference& objref,
char* outStr,
size_t outLen)
const override;
643 void Decrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
644 const PdfReference& objref,
char* outStr,
size_t& outLen)
const override;
646 PdfAuthResult Authenticate(
const std::string_view& password,
const std::string_view& documentId,
647 PODOFO_CRYPT_CTX* ctx,
unsigned char encryptionKey[32])
const override;
649 void GenerateEncryptionKey(
const std::string_view& documentId,
PdfAuthResult authResult, PODOFO_CRYPT_CTX* ctx,
650 unsigned char uValue[48],
unsigned char oValue[48],
unsigned char encryptionKey[32])
override;
654 static void generateInitialVector(
unsigned char iv[]);
658 static void preprocessPassword(
const std::string_view& password,
unsigned char* outBuf,
unsigned& len);
661 static void computeEncryptionKey(
unsigned keyLength,
unsigned char encryptionKey[32]);
664 static void computeHash(
const unsigned char* pswd,
unsigned pswdLen,
unsigned revision,
665 const unsigned char salt[8],
const unsigned char uValue[48],
unsigned char hashValue[32]);
668 static void computeUserKey(
const unsigned char* userpswd,
unsigned len,
unsigned revision,
669 unsigned keyLength,
const unsigned char encryptionKey[32],
670 unsigned char uValue[48],
unsigned char ueValue[32]);
673 static void computeOwnerKey(
const unsigned char* userpswd,
unsigned len,
unsigned revision,
674 unsigned keyLength,
const unsigned char encryptionKey[32],
const unsigned char uValue[48],
675 unsigned char oValue[48],
unsigned char oeValue[32]);
678 unsigned char m_ueValue[32];
679 unsigned char m_oeValue[32];
680 unsigned char m_permsValue[16];
698 unsigned keyLength,
bool encryptMetadata);
699 PdfEncryptRC4(
const std::string_view& userPassword,
const std::string_view& ownerPassword,
707 PdfEncryptContext& context,
const PdfReference& objref)
const override;
710 PdfEncryptContext& context,
const PdfReference& objref)
const override;
717 void Encrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
718 const PdfReference& objref,
char* outStr,
size_t outLen)
const override;
720 void Decrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
721 const PdfReference& objref,
char* outStr,
size_t& outLen)
const override;
723 void GenerateEncryptionKey(
const std::string_view& documentId,
PdfAuthResult authResult, PODOFO_CRYPT_CTX* ctx,
724 unsigned char uValue[48],
unsigned char oValue[48],
unsigned char encryptionKey[32])
override;
726 PdfAuthResult Authenticate(
const std::string_view& password,
const std::string_view& documentId,
727 PODOFO_CRYPT_CTX* ctx,
unsigned char encryptionKey[32])
const override;
730 static unsigned normalizeKeyLength(
unsigned keyLength);
An interface for writing blocks of data to a data source.
Definition: OutputStream.h:18
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition: PdfDictionary.h:82
A class that is used to encrypt a PDF file (AES-128)
Definition: PdfEncrypt.h:564
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition: PdfEncrypt.cpp:1402
std::unique_ptr< OutputStream > CreateEncryptionOutputStream(OutputStream &outputStream, PdfEncryptContext &context, const PdfReference &objref) const override
Create an OutputStream that encrypts all data written to it using the current settings of the PdfEncr...
Definition: PdfEncrypt.cpp:1420
std::unique_ptr< InputStream > CreateEncryptionInputStream(InputStream &inputStream, size_t inputLen, PdfEncryptContext &context, const PdfReference &objref) const override
Create an InputStream that decrypts all data read from it using the current settings of the PdfEncryp...
Definition: PdfEncrypt.cpp:1411
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition: PdfEncrypt.cpp:1343
A class that is used to encrypt a PDF file (RC4 40-bit and 128-bit)
Definition: PdfEncrypt.h:692
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition: PdfEncrypt.cpp:1091
std::unique_ptr< OutputStream > CreateEncryptionOutputStream(OutputStream &outputStream, PdfEncryptContext &context, const PdfReference &objref) const override
Create an OutputStream that encrypts all data written to it using the current settings of the PdfEncr...
Definition: PdfEncrypt.cpp:1209
std::unique_ptr< InputStream > CreateEncryptionInputStream(InputStream &inputStream, size_t inputLen, PdfEncryptContext &context, const PdfReference &objref) const override
Create an InputStream that decrypts all data read from it using the current settings of the PdfEncryp...
Definition: PdfEncrypt.cpp:1117
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition: PdfEncrypt.cpp:1096
A bundle of the encrypt object together a context.
Definition: PdfEncryptSession.h:18
A class that is used to encrypt a PDF file and set document permissions on the PDF file.
Definition: PdfEncrypt.h:122
virtual std::unique_ptr< InputStream > CreateEncryptionInputStream(InputStream &inputStream, size_t inputLen, PdfEncryptContext &context, const PdfReference &objref) const =0
Create an InputStream that decrypts all data read from it using the current settings of the PdfEncryp...
virtual size_t CalculateStreamLength(size_t length) const =0
Calculate stream size.
unsigned GetRevision() const
Get the revision number of the encryption method.
Definition: PdfEncrypt.h:364
virtual size_t CalculateStreamOffset() const =0
Calculate stream offset.
virtual std::unique_ptr< OutputStream > CreateEncryptionOutputStream(OutputStream &outputStream, PdfEncryptContext &context, const PdfReference &objref) const =0
Create an OutputStream that encrypts all data written to it using the current settings of the PdfEncr...
void Authenticate(const std::string_view &password, const PdfString &documentId, PdfEncryptContext &context) const
Tries to authenticate a user using either the user or owner password.
PdfPermissions GetPValue() const
Get the P object value (protection)
Definition: PdfEncrypt.h:360
virtual void CreateEncryptionDictionary(PdfDictionary &dictionary) const =0
Fill all keys into a encryption dictionary.
PdfKeyLength GetKeyLength() const
Get the length of the encryption key in bits.
Definition: PdfEncrypt.h:356
PdfEncryptionAlgorithm GetEncryptAlgorithm() const
Get the encryption algorithm of this object.
Definition: PdfEncrypt.h:239
This class represents a PDF indirect Object in memory.
Definition: PdfObject.h:35
A reference is a pointer to a object in the PDF file of the form "4 0 R", where 4 is the object numbe...
Definition: PdfReference.h:24
A string that can be written to a PDF document.
Definition: PdfString.h:24
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition: basetypes.h:38
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16
PdfEncryptionAlgorithm
The encryption algorithm.
Definition: PdfEncrypt.h:90
@ AESV2
AES encryption with a 128 bit key (PDF1.6)
@ RC4V1
RC4 Version 1 encryption using a 40bit key.
@ RC4V2
RC4 Version 2 encryption using a key with 40-128bit.
@ None
Do not add a default appearrance.
@ Create
Create a new file or truncate existing one for writing/reading.
cspan< char > bufferview
Convenient read-only char buffer span.
Definition: basetypes.h:19
PdfPermissions
Set user permissions/restrictions on a document.
Definition: PdfEncrypt.h:66
@ Print
Allow printing the document.
@ FillAndSign
Fill in existing form or signature fields.
@ HighPrint
Print a high resolution version of the document.
@ Copy
Allow text and graphic extraction.
@ DocAssembly
Assemble the document: insert, create, rotate delete pages or add bookmarks.
@ Edit
Allow modifying the document besides annotations, form fields or changing pages.
@ EditNotes
Add or modify text annotations or form fields (if PdfPermissions::Edit is set also allow to create in...
@ Accessible
Extract text and graphics to support user with disabilities.
PdfKeyLength
A enum specifying a valid keylength for a PDF encryption key.
Definition: PdfEncrypt.h:41
PdfAuthResult
Definition: PdfEncrypt.h:102
@ User
Success authenticating a user for this PDF.
@ Owner
Success authenticating the owner for this PDF.
@ Failed
Failed to authenticate to this PDF.