| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qopcuakeypair.h" |
| 5 | #include "qopcuakeypair_p.h" |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | /*! |
| 10 | \class QOpcUaKeyPair |
| 11 | \inmodule QtOpcUa |
| 12 | \since 5.14 |
| 13 | |
| 14 | \brief QOpcUaKeyPair handles private and public key pairs. |
| 15 | |
| 16 | This class is currently available as a Technology Preview, and therefore the API |
| 17 | and functionality provided by the class may be subject to change at any time without |
| 18 | prior notice. |
| 19 | |
| 20 | It can generate, load and store keys for asymmetric encryption. |
| 21 | Instances of this class have to be passed to functions which need a key. |
| 22 | */ |
| 23 | |
| 24 | /*! |
| 25 | \enum QOpcUaKeyPair::RsaKeyStrength |
| 26 | |
| 27 | This enum type specifies the strength of a RSA key. |
| 28 | |
| 29 | \value Bits1024 |
| 30 | A key strength of 1024 bits. |
| 31 | \value Bits2048 |
| 32 | A key strength of 2048 bits. |
| 33 | \value Bits4096 |
| 34 | A key strength of 4096 bits. |
| 35 | */ |
| 36 | |
| 37 | /*! |
| 38 | \enum QOpcUaKeyPair::KeyType |
| 39 | |
| 40 | This enum type specifies the type of a key. |
| 41 | |
| 42 | \value Rsa |
| 43 | An RSA key |
| 44 | \value Empty |
| 45 | No key is available. |
| 46 | \value Unknown |
| 47 | The type of key is not handled. |
| 48 | */ |
| 49 | |
| 50 | /*! |
| 51 | \enum QOpcUaKeyPair::Cipher |
| 52 | |
| 53 | Ciphers for encryption of private keys. |
| 54 | |
| 55 | \value Aes128Cbc |
| 56 | Encrypting AES128 with CBC |
| 57 | \value Unencrypted |
| 58 | The Key will not be encrypted. |
| 59 | */ |
| 60 | |
| 61 | /*! |
| 62 | \fn QOpcUaKeyPair::passphraseNeeded(QString &passphrase, int maximumLength, bool writeOperation) |
| 63 | |
| 64 | This signal is emitted when a private key needs a \a passphrase for encryption or |
| 65 | decryption. |
| 66 | |
| 67 | \a writeOperation is \c true when the passphrase is needed for exporting a key, and |
| 68 | is \c false when the passphrase is needed for importing a key. |
| 69 | |
| 70 | \a maximumLength specifies the maximum length in bytes for the passphrase. |
| 71 | All characters in \a passphrase exceeding this limit will be ignored. |
| 72 | |
| 73 | In case you have use this signal crossing thread boundaries you have to connect it |
| 74 | with \c Qt::BlockingQueuedConnection. |
| 75 | */ |
| 76 | |
| 77 | /*! |
| 78 | Creates a new empty key pair with \a parent as the parent object. |
| 79 | */ |
| 80 | QOpcUaKeyPair::QOpcUaKeyPair(QObject *parent) |
| 81 | : QObject(*(new QOpcUaKeyPairPrivate()), parent) |
| 82 | { |
| 83 | } |
| 84 | /*! |
| 85 | Destroys the key pair. |
| 86 | */ |
| 87 | QOpcUaKeyPair::~QOpcUaKeyPair() |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | /*! |
| 92 | Loads a key from PEM encoded data in \a data. |
| 93 | Returns \c true on success and \c false otherwise. |
| 94 | |
| 95 | It detects from the PEM header if the data contains a private or |
| 96 | public key. Loading encrypted keys is possible by connecting a |
| 97 | function to the signal \c passphraseNeeded for provision of the |
| 98 | passphrase. |
| 99 | */ |
| 100 | bool QOpcUaKeyPair::loadFromPemData(const QByteArray &data) |
| 101 | { |
| 102 | Q_D(QOpcUaKeyPair); |
| 103 | return d->loadFromPemData(data); |
| 104 | } |
| 105 | |
| 106 | /*! |
| 107 | Returns the public key as a byte array. |
| 108 | */ |
| 109 | QByteArray QOpcUaKeyPair::publicKeyToByteArray() const |
| 110 | { |
| 111 | Q_D(const QOpcUaKeyPair); |
| 112 | return d->publicKeyToByteArray(); |
| 113 | } |
| 114 | |
| 115 | /*! |
| 116 | Returns the type of the current key. |
| 117 | */ |
| 118 | QOpcUaKeyPair::KeyType QOpcUaKeyPair::type() const |
| 119 | { |
| 120 | Q_D(const QOpcUaKeyPair); |
| 121 | return d->keyType(); |
| 122 | } |
| 123 | |
| 124 | /*! |
| 125 | Returns \c true if the current key contains a private key, otherwise \c false. |
| 126 | */ |
| 127 | bool QOpcUaKeyPair::hasPrivateKey() const |
| 128 | { |
| 129 | Q_D(const QOpcUaKeyPair); |
| 130 | return d->hasPrivateKey(); |
| 131 | } |
| 132 | |
| 133 | /*! |
| 134 | Returns the PEM encoded private key. |
| 135 | In case there is no private key, an empty byte array is returned. |
| 136 | |
| 137 | The encryption of the key has to be specified using the parameters |
| 138 | \a cipher and \a password. In order to store the key unencrypted |
| 139 | the cipher \c Cipher::Unencrypted has to be used. |
| 140 | */ |
| 141 | QByteArray QOpcUaKeyPair::privateKeyToByteArray(Cipher cipher, const QString &password) const |
| 142 | { |
| 143 | Q_D(const QOpcUaKeyPair); |
| 144 | return d->privateKeyToByteArray(cipher, password); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /*! |
| 149 | Generates a new asymmetric RSA key pair. |
| 150 | |
| 151 | The length of the key is specified by \a strength. |
| 152 | */ |
| 153 | void QOpcUaKeyPair::generateRsaKey(QOpcUaKeyPair::RsaKeyStrength strength) |
| 154 | { |
| 155 | Q_D(QOpcUaKeyPair); |
| 156 | d->generateRsaKey(strength); |
| 157 | } |
| 158 | |
| 159 | QT_END_NAMESPACE |
| 160 | |