Menu

Error Handling

saeed atalla

As we talking about security, programmer should handle errors very carefully to ensures that every thing is OK, because this, CryptoEngine provides 3 ways to handle errors:

Note that all Exceptions handled internally, but if exception occuers inside any class Constructor, this will cause application to CRACH!, to force programmer to solve it.

1- This is preferred way: Using SIGNAL/SLOT mechanism provided by Qt-SDK:

In this simple example we will create ErrorCatcher which it's job is catching errors and print out details about it, note that class must inherits QObject class:

//Class declaration:
class ErrorCatcher : public QObject
{
    Q_OBJECT
public:
    ErrorCatcher(QObject *parent = 0);
    //Other stuff...

signals:

public slots:
    void errorHandler(const QString errString);

};

//Class definition:
ErrorCatcher::ErrorCatcher(QObject *parent) :
    QObject(parent)
{
}

void ErrorCatcher::errorHandler(const QString errString)
{
    std::cout << "\nOops! error catched:\n" << errString.toStdString() << std::endl;
    //Do other things...
}

Slot errorHandler(...) will be execute every time error occuers in any object connected to this class.

Now we can create CryptoEngine classes and connect them with above class to catch thier errors:

    //client code:

    ErrorCatcher ec;
    QByteArray plain(""), cipher;
    CELIB::SymmetricKey key(128);
    std::cout << "\nkey-string:\n" << key.toString().toStdString() << std::endl;
    QObject::connect(&key, SIGNAL(error(QString)), &ec, SLOT(errorHandler(QString)));

    CELIB::AES aes(key, CELIB::CBC);
    QObject::connect(&aes, SIGNAL(error(QString)), &ec, SLOT(errorHandler(QString)));
    std::cout << "\naes-string:\n" << aes.toString().toStdString() << std::endl;
    //Trying to encrypt empty plain text, this must fire error:
    cipher = aes.encrypt(plain, 0);

    CELIB::RSA rsa(1024);
    std::cout << "\nrsa-string:\n" << rsa.toString().toStdString() << std::endl;
    QObject::connect(&rsa, SIGNAL(error(QString)), &ec, SLOT(errorHandler(QString)));

    plain = "This a very very very vey very long plain text that can not encrypt with 128 byte key!.";
    //Trying to encrypt very long plain text with only 1024bit key, this must fire error:
    cipher = rsa.encrypt(plain, 0);

output:

key-string:
([CELIB::SymmetricKey::toString()]:
 Key-Hex:[8CFD280A733922DE8DDE0F3A36817A39],
 key-Size(Bytes): [16].)

aes-string:
([CELIB::AES::toString()]:
 Key-Hex:[!HIDDEN!],
 Key-Size(Bytes):[16],
 IV-Hex:[0968FDFFA7C389DE10EBACF32ACDA6BC],
 IV-Size(Bytes):[16],
 OPMode:[CBC].)

Oops! error catched:
Exception at CE::AES::encrypt(...): [Empty Parameter].

rsa-string:
([CELIB::RSA::toString()]:
 [Size(Bytes):[128],
 n-Hex:
 [CF8C118A44E8F62EB590CFDCC17C1FFC91413B5338A93B587BFFBF00D59E71A8332F2ADF5F5509A19EE1CA94BAAFABD0C8E938CC0053F4425D593221ADFC8CE43CF13F7BDA59B4EA984AE319345631214EEA256011A1ACDB333CE8BE0380F4AFBBF0C08EEC9723F6C956B6E4CD48C5160887FD96EA10B70FA1B03F2477C252FB],
 e-Hex:[11].]
 d:[!HIDDEN!].)

Oops! error catched:
Exception at CE::RSA::encrypt(...): [INVALID PTX Size(87) for this key size(128)!].

PTX = plaintext.

This technique also can used with Static functions (using QMetaObject system from Qt):
Note that we should supply a pointer to a class that will handle error (and it must inherit QObject), with slot function signature, supply them both to the Static Function:

    QByteArray privKey = "This is not a really private key!";
    //Trying to validate invalid encoding key using static function, this must fire error:
    CELIB::RSA::validatePrivateKey(privKey, 3 /*validation level*/, &ec, "errorHandler(QString)");

output:

Oops! error catched:
Exception at CE::RSA::validatePrivateKey(...): [BER decode error].

....................

2- Using bool argument:

In this way, we can send bool parameter (pointer) to function, and test it after function execution, if function failed, it will be false, otherwise it will be true:

    //......

    cipher = rsa.encrypt(plain, &isOk);
    if(isOk)
        std::cout << "\nEncryption successful." << std::endl;
    else
        std::cout << "\nEncryption FAILED." << std::endl;

If you don't want to send bool parameter, you can send 0;

....................

3- Using CE_DEBUG:

This is a simple & quick way that can be used when debugging, just define (CE_DEBUG) in file celib.h (line 12).
Then if any error occures, error details will be printed-out using std::cerr object.


Related

Wiki: Home
Wiki: AES Example
Wiki: RSA Example
Wiki: SHA Example

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.