1 /* Ricochet - https://ricochet.im/
2  * Copyright (C) 2014, John Brooks <john.brooks@dereferenced.net>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *    * Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  *
11  *    * Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following disclaimer
13  *      in the documentation and/or other materials provided with the
14  *      distribution.
15  *
16  *    * Neither the names of the copyright owners nor the names of its
17  *      contributors may be used to endorse or promote products derived from
18  *      this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef CRYPTOKEY_H
34 #define CRYPTOKEY_H
35 
36 #include <QString>
37 #include <QSharedData>
38 #include <QExplicitlySharedDataPointer>
39 
40 class CryptoKey
41 {
42 public:
43     enum KeyType {
44         PrivateKey,
45         PublicKey
46     };
47 
48     enum KeyFormat {
49         PEM,
50         DER
51     };
52 
53     CryptoKey();
CryptoKey(const CryptoKey & other)54     CryptoKey(const CryptoKey &other) : d(other.d) { }
55     ~CryptoKey();
56 
57     bool loadFromData(const QByteArray &data, KeyType type, KeyFormat format = PEM);
58     bool loadFromFile(const QString &path, KeyType type, KeyFormat format = PEM);
59     void clear();
60 
isLoaded()61     bool isLoaded() const { return d.data() && d->key != 0; }
62     bool isPrivate() const;
63 
64     QByteArray publicKeyDigest() const;
65     QByteArray encodedPublicKey(KeyFormat format) const;
66     QByteArray encodedPrivateKey(KeyFormat format) const;
67     QString torServiceID() const;
68     int bits() const;
69 
70     // Calculate and sign SHA-256 digest of data using this key and PKCS #1 v2.0 padding
71     QByteArray signData(const QByteArray &data) const;
72     // Verify a signature as per signData
73     bool verifyData(const QByteArray &data, QByteArray signature) const;
74 
75     // Sign the input SHA-256 digest using this key and PKCS #1 v2.0 padding
76     QByteArray signSHA256(const QByteArray &digest) const;
77     // Verify a signature as per signSHA256
78     bool verifySHA256(const QByteArray &digest, QByteArray signature) const;
79 
80 private:
81     struct Data : public QSharedData
82     {
83         typedef struct rsa_st RSA;
84         RSA *key;
85 
keyData86         Data(RSA *k = 0) : key(k) { }
87         ~Data();
88     };
89 
90     QExplicitlySharedDataPointer<Data> d;
91 };
92 
93 QByteArray torControlHashedPassword(const QByteArray &password);
94 
95 #endif // CRYPTOKEY_H
96