1 /* 2 * Copyright (C) 2017 Toni Spets <toni.spets@iki.fi> 3 * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org> 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 2 or (at your option) 8 * version 3 of the License. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #ifndef KEEPASSXC_OPENSSHKEY_H 20 #define KEEPASSXC_OPENSSHKEY_H 21 22 #include <QtCore> 23 24 class BinaryStream; 25 26 class OpenSSHKey : public QObject 27 { 28 Q_OBJECT 29 public: 30 static OpenSSHKey generate(bool secure = true); 31 32 explicit OpenSSHKey(QObject* parent = nullptr); 33 OpenSSHKey(const OpenSSHKey& other); 34 bool operator==(const OpenSSHKey& other) const; 35 36 bool parsePKCS1PEM(const QByteArray& in); 37 bool encrypted() const; 38 bool openKey(const QString& passphrase = QString()); 39 40 const QString cipherName() const; 41 const QString type() const; 42 int keyLength() const; 43 const QString fingerprint(QCryptographicHash::Algorithm algo = QCryptographicHash::Sha256) const; 44 const QString comment() const; 45 const QString publicKey() const; 46 const QString privateKey() const; 47 const QString errorString() const; 48 49 void setType(const QString& type); 50 void setPublicData(const QList<QByteArray>& data); 51 void setPrivateData(const QList<QByteArray>& data); 52 void setComment(const QString& comment); 53 54 void clearPrivate(); 55 56 bool readPublic(BinaryStream& stream); 57 bool readPrivate(BinaryStream& stream); 58 bool writePublic(BinaryStream& stream); 59 bool writePrivate(BinaryStream& stream); 60 61 QList<QByteArray> publicParts() const; 62 QList<QByteArray> privateParts() const; 63 const QString& privateType() const; 64 65 static const QString TYPE_DSA_PRIVATE; 66 static const QString TYPE_RSA_PRIVATE; 67 static const QString TYPE_RSA_PUBLIC; 68 static const QString TYPE_OPENSSH_PRIVATE; 69 70 enum Type 71 { 72 Public, 73 Private 74 }; 75 76 static OpenSSHKey restoreFromBinary(Type eType, const QByteArray& serialized); 77 static QByteArray serializeToBinary(Type eType, const OpenSSHKey& key); 78 79 private: 80 bool extractPEM(const QByteArray& in, QByteArray& out); 81 82 QString m_type; 83 QString m_cipherName; 84 QByteArray m_cipherIV; 85 QString m_kdfName; 86 QByteArray m_kdfOptions; 87 88 QString m_rawType; 89 QByteArray m_rawData; 90 QList<QByteArray> m_rawPublicData; 91 QList<QByteArray> m_rawPrivateData; 92 QString m_comment; 93 QString m_error; 94 }; 95 96 uint qHash(const OpenSSHKey& key); 97 98 #endif // KEEPASSXC_OPENSSHKEY_H 99