-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbadcert.cxx
108 lines (91 loc) · 3.3 KB
/
badcert.cxx
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
// badcert.cxx is a test program built in pem_create_keys.sh
// pem_create_keys.sh and pem_verify_keys.sh verify the
// PEM source files and X509Certificate class. If
// X509Certificate has a problem with a cert, like a CA cert,
// then it writes the cert to badcert.der. badcert.exe then
// reads the cert and attempts to parse it. badcert.exe is
// the prebuilt stand alone reproducer.
#include "cryptlib.h"
#include "x509cert.h"
#include "secblock.h"
#include "filters.h"
#include "files.h"
#include "osrng.h"
#include "rsa.h"
#include "sha.h"
#include "hex.h"
#include "pem.h"
#include <iostream>
#include <string>
extern const std::string pemCertificate;
int main(int argc, char* argv[])
{
using namespace CryptoPP;
FileSource fs("badcert.der", true);
X509Certificate cert;
cert.Load(fs);
const SecByteBlock& signature = cert.GetCertificateSignature();
const SecByteBlock& toBeSigned = cert.GetToBeSigned();
const X509PublicKey& publicKey = cert.GetSubjectPublicKey();
const OID &signAlgorithm = cert.GetCertificateSignatureAlgorithm();
AutoSeededRandomPool prng;
bool result1 = publicKey.Validate(prng, 3);
member_ptr<PK_Verifier> verifier;
if (signAlgorithm == id_sha1WithRSASignature)
{
verifier.reset(new RSASS<PKCS1v15, SHA1>::Verifier(publicKey));
}
else if (signAlgorithm == id_sha256WithRSAEncryption)
{
verifier.reset(new RSASS<PKCS1v15, SHA256>::Verifier(publicKey));
}
else if (signAlgorithm == id_sha384WithRSAEncryption)
{
verifier.reset(new RSASS<PKCS1v15, SHA384>::Verifier(publicKey));
}
else if (signAlgorithm == id_sha512WithRSAEncryption)
{
verifier.reset(new RSASS<PKCS1v15, SHA512>::Verifier(publicKey));
}
else if (signAlgorithm == id_ecdsaWithSHA1)
{
verifier.reset(new ECDSA<ECP, SHA1>::Verifier(publicKey));
}
else if (signAlgorithm == id_ecdsaWithSHA256)
{
verifier.reset(new ECDSA<ECP, SHA256>::Verifier(publicKey));
}
else if (signAlgorithm == id_ecdsaWithSHA384)
{
verifier.reset(new ECDSA<ECP, SHA384>::Verifier(publicKey));
}
else if (signAlgorithm == id_ecdsaWithSHA512)
{
verifier.reset(new ECDSA<ECP, SHA512>::Verifier(publicKey));
}
else
{
CRYPTOPP_ASSERT(0);
}
bool result2 = verifier->VerifyMessage(toBeSigned, toBeSigned.size(), signature, signature.size());
if (result1)
std::cout << "Verified public key" << std::endl;
else
std::cout << "Failed to verify public key" << std::endl;
if (result2)
std::cout << "Verified certificate" << std::endl;
else
std::cout << "Failed to verify certificate" << std::endl;
std::cout << std::endl;
std::cout << "Signature: ";
size_t size = std::min(signature.size(), (size_t)30);
StringSource(signature, size, true, new HexEncoder(new FileSink(std::cout)));
std::cout << "..." << std::endl;
std::cout << "To Be Signed: ";
size = std::min(signature.size(), (size_t)30);
StringSource(toBeSigned, size, true, new HexEncoder(new FileSink(std::cout)));
std::cout << "..." << std::endl;
std::cout << cert << std::endl;
std::cout << std::endl;
return 0;
}