forked from chris2511/xca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpki_key.h
271 lines (253 loc) · 6.02 KB
/
pki_key.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright (C) 2001 - 2020 Christian Hohnstaedt.
*
* All rights reserved.
*/
#ifndef __PKI_KEY_H
#define __PKI_KEY_H
#include <QString>
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <openssl/pem.h>
#include <openssl/evp.h>
#include "pki_base.h"
//#include "pki_export.h"
#include "builtin_curves.h"
#define PEM_STRING_OPENSSH_KEY "OPENSSH PRIVATE KEY"
#define DEFAULT_KEY_LENGTH 2048
#define ED25519_KEYLEN 32
#define VIEW_public_keys_type 6
#define VIEW_public_keys_len 7
#define VIEW_public_keys_public 8
extern builtin_curves builtinCurves;
class keytype
{
public:
static QList<keytype> types()
{
return QList<keytype> {
keytype(EVP_PKEY_RSA, "RSA", CKM_RSA_PKCS_KEY_PAIR_GEN,
false, true),
keytype(EVP_PKEY_DSA, "DSA", CKM_DSA_KEY_PAIR_GEN,
false, true),
#ifndef OPENSSL_NO_EC
keytype(EVP_PKEY_EC, "EC", CKM_EC_KEY_PAIR_GEN,
true, false),
#ifdef EVP_PKEY_ED25519
keytype(EVP_PKEY_ED25519, "ED25519", CKM_VENDOR_DEFINED, false, false),
#endif
#endif
};
}
int type;
QString name;
CK_MECHANISM_TYPE mech;
bool curve, length;
keytype(int t, const QString &n, CK_MECHANISM_TYPE m, bool c, bool l)
: type(t), name(n), mech(m), curve(c), length(l) { }
keytype() : type(-1), name(QString()), mech(0),
curve(false), length(true) { }
bool isValid()
{
return type != -1;
}
QString traditionalPemName() const
{
return name + " PRIVATE KEY";
}
static const keytype byType(int type)
{
foreach(const keytype t, types()) {
if (t.type == type)
return t;
}
return keytype();
}
static const keytype byMech(CK_MECHANISM_TYPE mech)
{
foreach(const keytype t, types()) {
if (t.mech == mech)
return t;
}
return keytype();
}
static const keytype byName(const QString &name)
{
foreach(const keytype t, types()) {
if (t.name == name.toUpper())
return t;
}
return keytype();
}
static const keytype byPKEY(EVP_PKEY *pkey)
{
return byType(EVP_PKEY_type(EVP_PKEY_id(pkey)));
}
};
class keyjob
{
public:
static keyjob defaultjob;
keytype ktype;
int size;
int ec_nid;
slotid slot;
keyjob() {
size = DEFAULT_KEY_LENGTH;
ktype = keytype::byName("RSA");
ec_nid = NID_undef;
slot = slotid();
}
keyjob(const QString &desc)
{
QStringList sl = desc.split(':');
if (sl.size() == 1)
sl += "";
if (sl.size() != 2)
return;
ktype = keytype::byName(sl[0]);
size = DEFAULT_KEY_LENGTH;
ec_nid = NID_undef;
if (isEC())
ec_nid = OBJ_txt2nid(sl[1].toLatin1());
else if (!isED25519())
size = sl[1].toInt();
slot = slotid();
}
QString toString()
{
if (isED25519())
return ktype.name;
return QString("%1:%2").arg(ktype.name)
.arg(isEC() ?
OBJ_obj2QString(OBJ_nid2obj(ec_nid)) :
QString::number(size));
}
bool isToken() const
{
return slot.lib != NULL;
}
bool isEC() const
{
return ktype.type == EVP_PKEY_EC;
}
bool isED25519() const
{
#ifdef EVP_PKEY_ED25519
return ktype.type == EVP_PKEY_ED25519;
#else
return false;
#endif
}
bool isValid()
{
if (!ktype.isValid())
return false;
if (isED25519())
return true;
if (isEC() && builtinCurves.containNid(ec_nid))
return true;
if (!isEC() && size > 0)
return true;
return false;
}
};
class pki_key: public pki_base
{
Q_OBJECT
friend class pki_x509super;
public:
enum passType { ptCommon, ptPrivate, ptBogus, ptPin };
protected:
enum passType ownPass;
int key_size;
bool isPub;
EVP_PKEY *key;
QString BN2QString(const BIGNUM *bn) const;
QString BNoneLine(BIGNUM *bn) const;
QByteArray SSH2publicQByteArray(bool raw=false) const;
QByteArray X509_PUBKEY_public_key() const;
QByteArray PEM_comment() const;
void collect_properties(QMap<QString, QString> &prp) const;
BIGNUM *ssh_key_data2bn(QByteArray *ba) const;
void ssh_key_check_chunk(QByteArray *ba, const char *expect) const;
QByteArray ssh_key_next_chunk(QByteArray *ba) const;
void ssh_key_QBA2data(const QByteArray &ba,
QByteArray *data) const;
void ssh_key_bn2data(const BIGNUM *bn, QByteArray *data) const;
private:
mutable int useCount; // usage counter
public:
pki_key(const QString &name = QString());
pki_key(const pki_key *pk);
virtual ~pki_key();
void autoIntName(const QString &file);
QString length() const;
QString comboText() const;
QString getKeyTypeString(void) const;
virtual EVP_PKEY *decryptKey() const = 0;
virtual bool isToken();
virtual QString getTypeString(void) const;
virtual QList<int> possibleHashNids();
QString getMsg(msg_type msg) const;
void writePublic(XFile &file, bool pem) const;
bool compare(const pki_base *ref) const;
int getKeyType() const;
bool isPrivKey() const;
bool verify(EVP_PKEY *pkey) const;
virtual bool verify_priv(EVP_PKEY *pkey) const;
int getUcount() const;
void setUcount(int c)
{
useCount = c;
}
enum passType getOwnPass(void)
{
return ownPass;
}
EVP_PKEY *getPubKey()
{
return key;
}
bool isPubKey() const
{
return isPub;
}
virtual void generate(const keyjob &)
{
qFatal("generate in pki_key");
}
bool pem(BioByteArray &);
QVariant column_data(const dbheader *hd) const;
QString modulus() const;
QString pubEx() const;
QString subprime() const;
QString pubkey() const;
#ifndef OPENSSL_NO_EC
int ecParamNid() const;
QString ecPubKey() const;
QByteArray ed25519PubKey() const;
QByteArray ed25519PrivKey(const EVP_PKEY *pkey) const;
BIGNUM *ecPubKeyBN() const;
#endif
void d2i(QByteArray &ba);
void d2i_old(QByteArray &ba, int type);
QByteArray i2d() const;
EVP_PKEY *load_ssh2_key(const QByteArray &ba);
void writeSSH2public(XFile &file) const;
void writeSSH2private(XFile &file, pem_password_cb *cb) const;
QString fingerprint(const QString &format) const;
bool SSH2_compatible() const;
void write_SSH2_ed25519_private(BIO *b,
const EVP_PKEY *pkey, const EVP_CIPHER *enc) const;
void print(BioByteArray &b, enum print_opt opt) const;
void resetUcount()
{
useCount = -1;
}
QSqlError insertSqlData();
QSqlError deleteSqlData();
void restoreSql(const QSqlRecord &rec);
};
Q_DECLARE_METATYPE(pki_key *);
#endif