-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (75 loc) · 3.31 KB
/
Copy pathmain.cpp
File metadata and controls
86 lines (75 loc) · 3.31 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
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include "mbedtls/x509_crt.h"
static const std::map<int, const char*> errors = {
{-0x006E, "This is a bug in the library"},
{-0x2080, "Unavailable feature, e.g. RSA hashing/encryption combination."},
{-0x2100, "Requested OID is unknown."},
{-0x2180, "The CRT/CRL/CSR format is invalid, e.g. different type expected."},
{-0x2200, "The CRT/CRL/CSR version element is invalid."},
{-0x2280, "The serial tag or value is invalid."},
{-0x2300, "The algorithm tag or value is invalid."},
{-0x2380, "The name tag or value is invalid."},
{-0x2400, "The date tag or value is invalid."},
{-0x2480, "The signature tag or value invalid."},
{-0x2500, "The extension tag or value is invalid."},
{-0x2580, "CRT/CRL/CSR has an unsupported version number."},
{-0x2600, "Signature algorithm (oid) is unsupported."},
{-0x2680, "Signature algorithms do not match. (see mbedtls_x509_crt sig_oid)"},
{-0x2700, "Certificate verification failed, e.g. CRL, CA or signature check failed."},
{-0x2780, "Format not recognized as DER or PEM."},
{-0x2800, "Input invalid."},
{-0x2880, "Allocation of memory failed."},
{-0x2900, "Read/write of file failed."},
{-0x2980, "Destination buffer is too small."},
{-0x3000, "A fatal error occurred, eg the chain is too long or the vrfy callback failed."},
{-0x3F80, "Memory allocation failed."},
{-0x3F00, "Type mismatch, eg attempt to encrypt with an ECDSA key"},
{-0x3E80, "Bad input parameters to function."},
{-0x3E00, "Read/write of file failed."},
{-0x3D80, "Unsupported key version"},
{-0x3D00, "Invalid key tag or value."},
{-0x3C80, "Key algorithm is unsupported (only RSA and EC are supported)."},
{-0x3C00, "Private key password can't be empty."},
{-0x3B80, "Given private key password does not allow for correct decryption."},
{-0x3B00, "The pubkey tag or value is invalid (only RSA and EC are supported)."},
{-0x3A80, "The algorithm tag or value is invalid."},
{-0x3A00, "Elliptic curve is unsupported (only NIST curves are supported)."},
{-0x3980, "Unavailable feature, e.g. RSA disabled for RSA key."},
{-0x3900, "The buffer contains a valid signature followed by more data."}
};
int main() {
std::ifstream file("cert.der", std::ios::binary | std::ios::ate);
if (!file.is_open()) {
std::cerr << "failed to open der-file" << std::endl;
return 1;
}
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> buf(size);
if (!file.read(reinterpret_cast<char*>(buf.data()), size))
{
std::cerr << "something went wrong\n";
return 2;
}
std::cout << size << " bytes were read" << std::endl;
mbedtls_x509_crt crt;
mbedtls_x509_crt_init(&crt);
int res = mbedtls_x509_crt_parse_der(&crt, buf.data(), buf.size());
if (res < 0) {
std::string reason;
auto it = errors.find(res);
if (it != errors.end()) {
reason = it->second;
} else {
reason = "reason not found";
}
std::cerr << "failed to parse der-file: " << reason << "\n";
} else {
std::cout << "certificate is parsed successfully" << std::endl;
}
mbedtls_x509_crt_free(&crt);
return 0;
}