libcdoc 0.5.0
libcdoc::CDocReader Class Referenceabstract

Provides decryption interface. More...

#include <CDocReader.h>

Public Member Functions

virtual ~CDocReader () noexcept=default
virtual const std::vector< Lock > & getLocks ()=0
 Get decryption locks in given document.
virtual result_t getLockForCert (const std::vector< uint8_t > &cert)=0
 Finds the lock index for given certificate.
virtual result_t getFMK (std::vector< uint8_t > &fmk, unsigned int lock_idx)=0
 Obtain FMK of given lock.
virtual result_t beginDecryption (const std::vector< uint8_t > &fmk)=0
 Start decrypting container.
virtual result_t nextFile (std::string &name, int64_t &size)=0
 Go to the next file in container.
virtual result_t readData (uint8_t *dst, size_t size)=0
 Read data from the current file.
virtual result_t finishDecryption ()=0
 Finish decrypting container.
result_t nextFile (FileInfo &info)
 Go to the next file in container.
virtual result_t decrypt (const std::vector< uint8_t > &fmk, MultiDataConsumer *consumer)=0
 Decrypt document in one step.
std::string getLastErrorStr () const
 Get the error text of the last failed operation.

Static Public Member Functions

static int getCDocFileVersion (const std::string &path)
 Try to determine the cdoc file version.
static int getCDocFileVersion (DataSource *src)
 Try to determine the cdoc file version.
static CDocReadercreateReader (DataSource *src, bool take_ownership, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network)
 Create CDoc document reader.
static CDocReadercreateReader (const std::string &path, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network)
 Create CDoc document reader.
static CDocReadercreateReader (std::istream &ifs, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network)
 Create CDoc document reader.

Public Attributes

const int version
 The container version (1 or 2).

Protected Member Functions

 CDocReader (int _version)
void setLastError (const std::string &message)

Protected Attributes

std::string last_error
Configurationconf = nullptr
CryptoBackendcrypto = nullptr
NetworkBackendnetwork = nullptr

Detailed Description

Provides decryption interface.

An abstract base class of CDoc1 and CDoc2 readers. Provides unified interface for loading and decryption of containers.

Constructor & Destructor Documentation

◆ ~CDocReader()

virtual libcdoc::CDocReader::~CDocReader ( )
virtualdefaultnoexcept

◆ CDocReader()

libcdoc::CDocReader::CDocReader ( int _version)
inlineexplicitprotected

References version.

Referenced by createReader(), createReader(), and createReader().

Member Function Documentation

◆ beginDecryption()

virtual result_t libcdoc::CDocReader::beginDecryption ( const std::vector< uint8_t > & fmk)
pure virtual

Start decrypting container.

Starts decryption of the container. This may involve parsing and decrypting headers, checking file and key integrity etc.

Parameters
fmkFile Master Key of the document
Returns
error code or OK

References beginDecryption().

Referenced by beginDecryption().

◆ createReader() [1/3]

CDocReader * libcdoc::CDocReader::createReader ( const std::string & path,
Configuration * conf,
CryptoBackend * crypto,
NetworkBackend * network )
static

Create CDoc document reader.

Creates a new document reader if file is a valid CDoc container (either version 1 or 2) Configuration and NetworkBackend may be null if keyservers are not used.

Parameters
paththe path to file
confa configuration object
cryptoa cryptographic backend implementation
networka network backend implementation
Returns
a new CDocReader or null

References CDocReader(), conf, crypto, and network.

◆ createReader() [2/3]

CDocReader * libcdoc::CDocReader::createReader ( DataSource * src,
bool take_ownership,
Configuration * conf,
CryptoBackend * crypto,
NetworkBackend * network )
static

Create CDoc document reader.

Creates a new document reader if source is a valid CDoc container (either version 1 or 2). Configuration and NetworkBackend may be null if keyservers are not used. If take_ownership is true, the source is deleted by the reader destructor. If src is not a valid CDoc file, the source is deleted before returning null.

Parameters
srcthe container source
take_ownershipif true the source is deleted in reader destructor
confa configuration object
cryptoa cryptographic backend implementation
networka network backend implementation
Returns
a new CDocReader or null

References CDocReader(), conf, crypto, and network.

◆ createReader() [3/3]

CDocReader * libcdoc::CDocReader::createReader ( std::istream & ifs,
Configuration * conf,
CryptoBackend * crypto,
NetworkBackend * network )
static

Create CDoc document reader.

Creates a new document reader if inputstream is a valid CDoc container (either version 1 or 2) Configuration and NetworkBackend may be null if keyservers are not used.

Parameters
ifsthe input stream
confa configuration object
cryptoa cryptographic backend implementation
networka network backend implementation
Returns
a new CDocReader or null

References CDocReader(), conf, crypto, and network.

◆ decrypt()

virtual result_t libcdoc::CDocReader::decrypt ( const std::vector< uint8_t > & fmk,
MultiDataConsumer * consumer )
pure virtual

Decrypt document in one step.

Decrypts the encrypted content and writes files to provided output object.

Parameters
fmkThe FMK of the document
consumera consumer of decrypted files
Returns
error code or OK

◆ finishDecryption()

virtual result_t libcdoc::CDocReader::finishDecryption ( )
pure virtual

Finish decrypting container.

Finishes the decryption of the container. This may onvolve releasing buffers, closing hardware keys etc.

Returns
error code or OK

References finishDecryption().

Referenced by finishDecryption().

◆ getCDocFileVersion() [1/2]

int libcdoc::CDocReader::getCDocFileVersion ( const std::string & path)
static

Try to determine the cdoc file version.

Tries to open the file and find CDoc format descriptors inside it.

Parameters
patha path to file
Returns
version or error code if not a readable CDoc file

◆ getCDocFileVersion() [2/2]

int libcdoc::CDocReader::getCDocFileVersion ( DataSource * src)
static

Try to determine the cdoc file version.

Tries to read the source and find CDoc format descriptors inside it.

Parameters
srcthe container source
Returns
version or error code if not a readable CDoc file

◆ getFMK()

virtual result_t libcdoc::CDocReader::getFMK ( std::vector< uint8_t > & fmk,
unsigned int lock_idx )
pure virtual

Obtain FMK of given lock.

Obtains FMK (File Master Key) of the lock with given index. Depending on the lock type it uses a relevant CryptoBackend and/or NetworkBackend methods to either fetch secret and derive key or perform external decryption of encrypted KEK.

Parameters
fmkThe FMK of the document
lock_idxthe index of a lock (in the document lock list)
Returns
error code or OK

References getFMK().

Referenced by getFMK().

◆ getLastErrorStr()

std::string libcdoc::CDocReader::getLastErrorStr ( ) const
inline

Get the error text of the last failed operation.

Get the error message of the last failed operation. It should be called immediately after getting error code as certain methods may reset the error.

Returns
error description, empty string if no errors

References last_error.

◆ getLockForCert()

virtual result_t libcdoc::CDocReader::getLockForCert ( const std::vector< uint8_t > & cert)
pure virtual

Finds the lock index for given certificate.

Returns the first lock that can be opened by the private key of the certificate holder.

Parameters
certa x509 certificate (der)
Returns
lock index or error code

References getLockForCert().

Referenced by getLockForCert().

◆ getLocks()

virtual const std::vector< Lock > & libcdoc::CDocReader::getLocks ( )
pure virtual

Get decryption locks in given document.

Returns
a vector of locks

References getLocks().

Referenced by getLocks().

◆ nextFile() [1/2]

result_t libcdoc::CDocReader::nextFile ( FileInfo & info)
inline

Go to the next file in container.

Begins decrypting the next file in container. On success the FileInfo struct is filled and the method returns OK. If there are no more file in the document, END_OF_STREAM is returned. It is OK to call nextFile before reading the whole data from the previous one.

Parameters
infoa FileInfo structure
Returns
error code, OK or END_OF_STREAM

References nextFile(), and nextFile().

Referenced by nextFile().

◆ nextFile() [2/2]

virtual result_t libcdoc::CDocReader::nextFile ( std::string & name,
int64_t & size )
pure virtual

Go to the next file in container.

Begins decrypting the next file in container. On success the file name and size are filled and the method returns OK. If there are no more file in the document, END_OF_STREAM is returned. It is OK to call nextFile before reading the whole data from the previous one. It has to be called always (even for single-file container) immediately after beginDecryption to get access to the first file.

Parameters
namethe name of the next file
sizethe size of the next file
Returns
error code, OK or END_OF_STREAM

References nextFile().

Referenced by nextFile(), and nextFile().

◆ readData()

virtual result_t libcdoc::CDocReader::readData ( uint8_t * dst,
size_t size )
pure virtual

Read data from the current file.

Read bytes from the current file (opened with nextFile) inside of the container into the buffer. The number of bytes read is always the requested number, unless end of file is reached or error occurs. Thus the end of file is marked by returning 0.

Parameters
dstdestination byte buffer
sizethe number of bytes to read
Returns
the number of bytes actually read or error code

References readData().

Referenced by readData().

◆ setLastError()

void libcdoc::CDocReader::setLastError ( const std::string & message)
inlineprotected

References last_error.

Member Data Documentation

◆ conf

Configuration* libcdoc::CDocReader::conf = nullptr
protected

◆ crypto

CryptoBackend* libcdoc::CDocReader::crypto = nullptr
protected

◆ last_error

std::string libcdoc::CDocReader::last_error
protected

Referenced by getLastErrorStr(), and setLastError().

◆ network

NetworkBackend* libcdoc::CDocReader::network = nullptr
protected

◆ version

const int libcdoc::CDocReader::version

The container version (1 or 2).

Referenced by CDocReader().


The documentation for this class was generated from the following file:
  • CDocReader.h