mirror of
https://github.com/Telecominfraproject/wlan-cloud-ucentralsec.git
synced 2025-11-02 19:57:46 +00:00
Signed-off-by: stephb9959 <stephane.bourque@gmail.com>
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
//
|
||||
|
||||
#include "Poco/Path.h"
|
||||
|
||||
#include "Poco/TemporaryFile.h"
|
||||
#include "Poco/Crypto/ECKey.h"
|
||||
#include "framework/AppServiceRegistry.h"
|
||||
#include "framework/utils.h"
|
||||
|
||||
@@ -608,4 +609,154 @@ namespace OpenWifi::Utils {
|
||||
return DT.timestamp().epochTime();
|
||||
}
|
||||
|
||||
static std::string FileToString(const std::string &Filename) {
|
||||
std::ifstream ifs(Filename.c_str(),std::ios_base::in|std::ios_base::binary);
|
||||
std::ostringstream os;
|
||||
Poco::StreamCopier::copyStream(ifs,os);
|
||||
return os.str();
|
||||
}
|
||||
|
||||
bool CreateX509CSR(const CSRCreationParameters & Parameters, CSRCreationResults & Results) {
|
||||
int ret = 0;
|
||||
RSA *r = nullptr;
|
||||
BIGNUM *bne = nullptr;
|
||||
|
||||
int nVersion = 0;
|
||||
unsigned long e = RSA_F4;
|
||||
|
||||
X509_REQ *x509_req = nullptr;
|
||||
X509_NAME *x509_name = nullptr;
|
||||
EVP_PKEY *pKey = nullptr;
|
||||
// RSA *tem = nullptr;
|
||||
// BIO *bio_err = nullptr;
|
||||
|
||||
const char *szCountry = Parameters.Country.c_str();
|
||||
const char *szProvince = Parameters.Province.c_str();
|
||||
const char *szCity = Parameters.City.c_str();
|
||||
const char *szOrganization = Parameters.Organization.c_str();
|
||||
const char *szCommon = Parameters.CommonName.c_str();
|
||||
|
||||
Poco::TemporaryFile CsrPath, PubKey, PrivateKey;
|
||||
std::string Result;
|
||||
std::ifstream ifs;
|
||||
std::ostringstream ss;
|
||||
BIO *bp_public = nullptr,
|
||||
*bp_private = nullptr,
|
||||
*bp_csr = nullptr;
|
||||
|
||||
// 1. generate rsa key
|
||||
bne = BN_new();
|
||||
ret = BN_set_word(bne,e);
|
||||
if(ret != 1){
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
r = RSA_new();
|
||||
ret = RSA_generate_key_ex(r, Parameters.bits, bne, nullptr);
|
||||
if(ret != 1){
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
bp_public = BIO_new_file(PubKey.path().c_str(), "w+");
|
||||
ret = PEM_write_bio_RSAPublicKey(bp_public, r);
|
||||
if(ret != 1) {
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
bp_private = BIO_new_file(PrivateKey.path().c_str(), "w+");
|
||||
ret = PEM_write_bio_RSAPrivateKey(bp_private, r, NULL, NULL, 0, NULL, NULL);
|
||||
if(ret != 1) {
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
// 2. set version of x509 req
|
||||
x509_req = X509_REQ_new();
|
||||
ret = X509_REQ_set_version(x509_req, nVersion);
|
||||
if (ret != 1){
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
// 3. set subject of x509 req
|
||||
x509_name = X509_REQ_get_subject_name(x509_req);
|
||||
|
||||
ret = X509_NAME_add_entry_by_txt(x509_name,"C", MBSTRING_ASC, (const unsigned char*)szCountry, -1, -1, 0);
|
||||
if (ret != 1){
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
ret = X509_NAME_add_entry_by_txt(x509_name,"ST", MBSTRING_ASC, (const unsigned char*)szProvince, -1, -1, 0);
|
||||
if (ret != 1){
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
ret = X509_NAME_add_entry_by_txt(x509_name,"L", MBSTRING_ASC, (const unsigned char*)szCity, -1, -1, 0);
|
||||
if (ret != 1){
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
ret = X509_NAME_add_entry_by_txt(x509_name,"O", MBSTRING_ASC, (const unsigned char*)szOrganization, -1, -1, 0);
|
||||
if (ret != 1){
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
ret = X509_NAME_add_entry_by_txt(x509_name,"CN", MBSTRING_ASC, (const unsigned char*)szCommon, -1, -1, 0);
|
||||
if (ret != 1){
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
// 4. set public key of x509 req
|
||||
pKey = EVP_PKEY_new();
|
||||
EVP_PKEY_assign_RSA(pKey, r);
|
||||
r = nullptr; // will be free rsa when EVP_PKEY_free(pKey)
|
||||
|
||||
ret = X509_REQ_set_pubkey(x509_req, pKey);
|
||||
if (ret != 1){
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
// 5. set sign key of x509 req
|
||||
ret = X509_REQ_sign(x509_req, pKey, EVP_sha1()); // return x509_req->signature->length
|
||||
if (ret <= 0){
|
||||
goto free_all;
|
||||
}
|
||||
|
||||
bp_csr = BIO_new_file(CsrPath.path().c_str(),"w");
|
||||
ret = PEM_write_bio_X509_REQ(bp_csr, x509_req);
|
||||
|
||||
// 6. free
|
||||
free_all:
|
||||
X509_REQ_free(x509_req);
|
||||
BIO_free_all(bp_csr);
|
||||
BIO_free_all(bp_public);
|
||||
BIO_free_all(bp_private);
|
||||
|
||||
EVP_PKEY_free(pKey);
|
||||
BN_free(bne);
|
||||
if(ret==1) {
|
||||
Results.CSR = FileToString(CsrPath.path());
|
||||
Results.PrivateKey = FileToString(PrivateKey.path());
|
||||
Results.PublicKey = FileToString(PubKey.path());
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool VerifyECKey(const std::string &key) {
|
||||
try {
|
||||
Poco::TemporaryFile F;
|
||||
|
||||
std::ofstream of(F.path().c_str(), std::ios_base::trunc | std::ios_base::out | std::ios_base::binary);
|
||||
of << key;
|
||||
of.close();
|
||||
|
||||
auto Key = Poco::SharedPtr<Poco::Crypto::ECKey>(
|
||||
new Poco::Crypto::ECKey("", F.path(),""));
|
||||
|
||||
return true;
|
||||
} catch (const Poco::Exception &E) {
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace OpenWifi::Utils
|
||||
|
||||
Reference in New Issue
Block a user