forked from rileytestut/AltServer-Windows
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCertificateRequest.cpp
More file actions
executable file
·171 lines (138 loc) · 4.25 KB
/
CertificateRequest.cpp
File metadata and controls
executable file
·171 lines (138 loc) · 4.25 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
//
// CertificateRequest.cpp
// AltSign-Windows
//
// Created by Riley Testut on 8/12/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
#include "CertificateRequest.hpp"
#include "Error.hpp"
#include <optional>
#include <openssl/pem.h>
// Based on https://www.codepool.biz/how-to-use-openssl-to-generate-x-509-certificate-request.html
CertificateRequest::CertificateRequest()
{
std::optional<std::vector<unsigned char>> outputData = std::nullopt;
std::optional<std::vector<unsigned char>> outputPrivateKey = std::nullopt;
BIGNUM *bignum = NULL;
RSA *rsa = NULL;
X509_REQ *request = NULL;
EVP_PKEY *publicKey = NULL;
BIO *csr = NULL;
BIO *privateKey = NULL;
auto finish = [this, &bignum, &rsa, &request, &publicKey, &csr, &privateKey, &outputData, &outputPrivateKey](void) {
if (publicKey != NULL)
{
// Also frees rsa, so we check if non-nil to prevent double free.
EVP_PKEY_free(publicKey);
}
else
{
RSA_free(rsa);
}
BN_free(bignum);
X509_REQ_free(request);
BIO_free_all(csr);
BIO_free_all(privateKey);
if (!outputData.has_value() || !outputPrivateKey.has_value())
{
throw APIError(APIErrorCode::InvalidCertificateRequest);
}
else
{
this->_data = *outputData;
this->_privateKey = *outputPrivateKey;
}
};
/* Generate RSA Key */
bignum = BN_new();
if (BN_set_word(bignum, RSA_F4) != 1)
{
finish();
return;
}
rsa = RSA_new();
if (RSA_generate_key_ex(rsa, 2048, bignum, NULL) != 1)
{
finish();
return;
}
/* Generate request */
const char *country = "US";
const char *state = "CA";
const char *city = "Los Angeles";
const char *organization = "AltSign";
const char *commonName = "AltSign";
request = X509_REQ_new();
if (X509_REQ_set_version(request, 1) != 1)
{
finish();
return;
}
// Subject
X509_NAME *subject = X509_REQ_get_subject_name(request);
X509_NAME_add_entry_by_txt(subject, "C", MBSTRING_ASC, (const unsigned char *)country, -1, -1, 0);
X509_NAME_add_entry_by_txt(subject, "ST", MBSTRING_ASC, (const unsigned char*)state, -1, -1, 0);
X509_NAME_add_entry_by_txt(subject, "L", MBSTRING_ASC, (const unsigned char*)city, -1, -1, 0);
X509_NAME_add_entry_by_txt(subject, "O", MBSTRING_ASC, (const unsigned char*)organization, -1, -1, 0);
X509_NAME_add_entry_by_txt(subject, "CN", MBSTRING_ASC, (const unsigned char*)commonName, -1, -1, 0);
// Public Key
publicKey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(publicKey, rsa);
if (X509_REQ_set_pubkey(request, publicKey) != 1)
{
finish();
return;
}
// Sign request
if (X509_REQ_sign(request, publicKey, EVP_sha1()) <= 0)
{
finish();
return;
}
// Output
csr = BIO_new(BIO_s_mem());
if (PEM_write_bio_X509_REQ(csr, request) != 1)
{
finish();
return;
}
privateKey = BIO_new(BIO_s_mem());
if (PEM_write_bio_RSAPrivateKey(privateKey, rsa, NULL, NULL, 0, NULL, NULL) != 1)
{
finish();
return;
}
/* Return values */
char *csrData = NULL;
long csrLength = BIO_get_mem_data(csr, &csrData);
char *privateKeyBuffer = NULL;
long privateKeyLength = BIO_get_mem_data(privateKey, &privateKeyBuffer);
std::vector<unsigned char> requestData;
requestData.reserve(csrLength);
for (int i = 0; i < csrLength; i++)
{
requestData.push_back(csrData[i]);
}
outputData = requestData;
std::vector<unsigned char> privateKeyData;
privateKeyData.reserve(privateKeyLength);
for (int i = 0; i < privateKeyLength; i++)
{
privateKeyData.push_back(privateKeyBuffer[i]);
}
outputPrivateKey = privateKeyData;
finish();
}
CertificateRequest::~CertificateRequest()
{
}
#pragma mark - Getters -
std::vector<unsigned char> CertificateRequest::data() const
{
return _data;
}
std::vector<unsigned char> CertificateRequest::privateKey() const
{
return _privateKey;
}