forked from keepassxreboot/keepassxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASN1Key.cpp
172 lines (143 loc) · 4.37 KB
/
ASN1Key.cpp
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
/*
* Copyright (C) 2018 Toni Spets <[email protected]>
* Copyright (C) 2018 KeePassXC Team <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ASN1Key.h"
#include "BinaryStream.h"
#include "OpenSSHKey.h"
namespace
{
constexpr quint8 TAG_INT = 0x02;
constexpr quint8 TAG_SEQUENCE = 0x30;
constexpr quint8 KEY_ZERO = 0x0;
bool nextTag(BinaryStream& stream, quint8& tag, quint32& len)
{
stream.read(tag);
quint8 lenByte;
stream.read(lenByte);
if (lenByte & 0x80) {
quint32 bytes = lenByte & ~0x80;
if (bytes == 1) {
stream.read(lenByte);
len = lenByte;
} else if (bytes == 2) {
quint16 lenShort;
stream.read(lenShort);
len = lenShort;
} else if (bytes == 4) {
stream.read(len);
} else {
return false;
}
} else {
len = lenByte;
}
return true;
}
bool parsePrivateHeader(BinaryStream& stream, quint8 wantedType)
{
quint8 tag;
quint32 len;
nextTag(stream, tag, len);
if (tag != TAG_SEQUENCE) {
return false;
}
nextTag(stream, tag, len);
if (tag != TAG_INT || len != 1) {
return false;
}
quint8 keyType;
stream.read(keyType);
return (keyType == wantedType);
}
bool readInt(BinaryStream& stream, QByteArray& target)
{
quint8 tag;
quint32 len;
nextTag(stream, tag, len);
if (tag != TAG_INT) {
return false;
}
target.resize(len);
stream.read(target);
return true;
}
} // namespace
bool ASN1Key::parseDSA(QByteArray& ba, OpenSSHKey& key)
{
BinaryStream stream(&ba);
if (!parsePrivateHeader(stream, KEY_ZERO)) {
return false;
}
QByteArray p, q, g, y, x;
readInt(stream, p);
readInt(stream, q);
readInt(stream, g);
readInt(stream, y);
readInt(stream, x);
QByteArray publicData;
BinaryStream publicDataStream(&publicData);
publicDataStream.writeString(p);
publicDataStream.writeString(q);
publicDataStream.writeString(g);
publicDataStream.writeString(y);
QByteArray privateData;
BinaryStream privateDataStream(&privateData);
privateDataStream.writeString(p);
privateDataStream.writeString(q);
privateDataStream.writeString(g);
privateDataStream.writeString(y);
privateDataStream.writeString(x);
key.setType("ssh-dss");
key.setPublicData(publicData);
key.setPrivateData(privateData);
key.setComment("");
return true;
}
bool ASN1Key::parseRSA(QByteArray& ba, OpenSSHKey& key)
{
BinaryStream stream(&ba);
if (!parsePrivateHeader(stream, KEY_ZERO)) {
return false;
}
QByteArray n, e, d, p, q, dp, dq, qinv;
readInt(stream, n);
readInt(stream, e);
readInt(stream, d);
readInt(stream, p);
readInt(stream, q);
readInt(stream, dp);
readInt(stream, dq);
readInt(stream, qinv);
// Note: To properly calculate the key fingerprint, e and n are reversed per RFC 4253
QByteArray publicData;
BinaryStream publicDataStream(&publicData);
publicDataStream.writeString(e);
publicDataStream.writeString(n);
QByteArray privateData;
BinaryStream privateDataStream(&privateData);
privateDataStream.writeString(n);
privateDataStream.writeString(e);
privateDataStream.writeString(d);
privateDataStream.writeString(qinv);
privateDataStream.writeString(p);
privateDataStream.writeString(q);
key.setType("ssh-rsa");
key.setPublicData(publicData);
key.setPrivateData(privateData);
key.setComment("");
return true;
}