x509.h
5.86 KB
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
/* Copyright (C) 2010-2012 kaosu (qiupf2000@gmail.com)
* This file is part of the Interactive Text Hooker.
* Interactive Text Hooker 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 3 of the License, or
* (at your option) any later version.
* 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/>.
*/
#ifndef ITH_X509
#define ITH_X509
//Main purpose: to extract public modulus from certificate.
//Do not verify certificate. So man-in-the-middle attack will success.
class ASN1Object
{
public:
ASN1Object() : type(0), len(0) {}
unsigned char* Parse(unsigned char* in);
unsigned char ASN1Tag()
{
return type & 0x1F;
}
bool IsConstructed()
{
return (type & 0x20) > 0;
}
unsigned char ASN1Class()
{
return type >> 6;
}
unsigned int ASN1Len()
{
return len;
}
protected:
unsigned int type, len;
//type = {
// Class : 2,
// P/C : 1,
// Tag : 5
};
class ASN1Sequence : public ASN1Object
{
public:
unsigned char* Parse(unsigned char* in);
protected:
};
class ASN1SetItem : public ASN1Object
{
public:
virtual ~ASN1SetItem();
unsigned char* Parse(unsigned char* in);
void SetNext(ASN1SetItem* s) {next = s;}
protected:
ASN1SetItem* next;
};
class ASN1Interger : public ASN1Object
{
public:
ASN1Interger()
{
value_long = 0;
}
~ASN1Interger();
unsigned char* Parse(unsigned char* in);
unsigned char* Value();
protected:
union
{
unsigned char* value_long;
unsigned char value_short[4];
};
};
class ASN1Bitstring : public ASN1Object
{
public:
unsigned char* Parse(unsigned char* in);
};
class ASN1ObjectIdentifier : public ASN1Object
{
public:
ASN1ObjectIdentifier()
{
oid_long = 0;
}
virtual ~ASN1ObjectIdentifier();
unsigned char* Parse(unsigned char* in);
unsigned char PKCS1Algorithm();
unsigned char RDNType();
protected:
union
{
unsigned char* oid_long;
unsigned char oid_short[4];
};
};
class ASN1CharacterString : public ASN1Object
{
public:
ASN1CharacterString() {str_long = 0;}
~ASN1CharacterString();
unsigned char* Parse(unsigned char* in);
protected:
union
{
unsigned char str_short[4];
unsigned char *str_long;
};
};
class CertificateVersion : ASN1Object
{
public:
CertificateVersion(int version = 0) : ver(version) {}
unsigned char* Parse(unsigned char* in);
unsigned char Version(){return ver;}
protected:
unsigned char ver;
};
/*
AlgorithmIdentifier{ALGORITHM:SupportedAlgorithms} ::= SEQUENCE {
algorithm ALGORITHM.&id ({SupportedAlgorithms}),
parameters ALGORITHM.&Type ({SupportedAlgorithms}{ @algorithm}) OPTIONAL
}
*/
class AlgorithmIdentifier : public ASN1Sequence
{
public:
unsigned char* Parse(unsigned char* in);
protected:
ASN1ObjectIdentifier algorithm;
ASN1Object param; //null = 0x5 0x0
};
class RDNSequenceItem : public ASN1Sequence
{
public:
unsigned char* Parse(unsigned char* in);
protected:
ASN1ObjectIdentifier item;
ASN1CharacterString str;
};
class RDNSequence : public ASN1SetItem
{
public:
RDNSequence() {next = 0;}
void SetNext(RDNSequence* n) {next = n;}
unsigned char* Parse(unsigned char* in);
protected:
RDNSequenceItem item;
};
class Name : public ASN1Sequence
{
public:
unsigned char* Parse(unsigned char* in);
protected:
RDNSequence head;
};
class UTCTime : ASN1Object
{
public:
unsigned char* Parse(unsigned char* in);
protected:
char utcStr[0x10]; //12 used.
};
class Validity : ASN1Sequence
{
public:
unsigned char* Parse(unsigned char* in);
protected:
UTCTime notBefore;
UTCTime notAfter;
};
class RSAKeyItem : public ASN1Sequence
{
public:
unsigned char* Parse(unsigned char* in);
unsigned int PublicKey(unsigned char* k);
protected:
ASN1Interger public_mod;
ASN1Interger public_exp;
};
class RSAKeyInfo : public ASN1Bitstring
{
public:
unsigned char* Parse(unsigned char* in);
unsigned int PublicKey(unsigned char* k);
protected:
RSAKeyItem key;
};
class SubjectPublicKeyInfo : public ASN1Sequence
{
public:
unsigned char* Parse(unsigned char* in);
unsigned int PublicKey(unsigned char* k);
protected:
AlgorithmIdentifier algorithm;
RSAKeyInfo key;
};
/* version 1.
CertificateContent ::= SEQUENCE {
version [0] Version DEFAULT v1,
serialNumber CertificateSerialNumber,
signature AlgorithmIdentifier{{SupportedAlgorithms}},
issuer Name,
validity Validity,
subject Name,
subjectPublicKeyInfo SubjectPublicKeyInfo,
}
*/
class CertificateContent : public ASN1Sequence
{
public:
unsigned char* Parse(unsigned char* in);
unsigned int PublicKey(unsigned char* k);
protected:
typedef ASN1Interger CertificateSerialNumber;
CertificateVersion version;
CertificateSerialNumber serial;
AlgorithmIdentifier signature;
Name issuer;
Validity validity;
Name subject;
SubjectPublicKeyInfo subjectPublicKeyInfo;
};
class SignatureBitstring : public ASN1Bitstring
{
public:
virtual ~SignatureBitstring();
unsigned char* Parse(unsigned char* in);
protected:
unsigned char* value;
};
class CertificateSignature : public AlgorithmIdentifier
{
public:
unsigned char* Parse(unsigned char* in);
protected:
SignatureBitstring signature;
};
//Certificate ::= SIGNED { CertificateContent }
class Certificate : public ASN1Sequence
{
public:
unsigned char* Parse(unsigned char* in);
unsigned int PublicKey(unsigned char* k);
protected:
CertificateContent certificate;
CertificateSignature signature;
};
#endif