分享
 
 
 

OpenSSL CSP Engine

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

#ifndef CSPEngineH

#define CSPEngineH

#include <openssl/rsa.h>

#include <openssl/evp.h>

#include <openssl/x509.h>

#include <openssl/x509_vfy.h>

#include <windows.h>

#include <wincrypt.h>

//===========================================================================

#if defined(__cplusplus)

extern "C" {

#endif

extern X509* x509;

extern EVP_PKEY* key;

X509_STORE* X509_STORE_load_MSCryptoAPI(void);

int X509_STORE_load_CERT_STORE(X509_STORE* store, HCERTSTORE hCertStore);

EVP_PKEY* EVP_PKEY_new_CERT_CONTEXT(PCCERT_CONTEXT pCertContext, X509** cert);

X509* X509_new_CERT_CONTEXT(PCCERT_CONTEXT pCertContext);

X509_CRL* X509_CRL_new_CRL_CONTEXT(PCCRL_CONTEXT pCrlContext);

int CSP_rsa_init(

RSA* rsa

);

int CSP_rsa_finish(

RSA* rsa

);

int CSP_rsa_pub_enc(

int flen

, const unsigned char* from

, unsigned char* to

, RSA* rsa

, int padding

);

int CSP_rsa_pub_dec(

int flen

, const unsigned char* from

, unsigned char* to

, RSA* rsa

, int padding

);

int CSP_rsa_priv_enc(

int flen

, const unsigned char* from

, unsigned char* to

, RSA* rsa

, int padding

);

int CSP_rsa_priv_dec(

int flen

, const unsigned char* from

, unsigned char* to

, RSA* rsa

, int padding

);

int CSP_rsa_sign(

int type

, const unsigned char* m

, unsigned int m_length

, unsigned char* sigret

, unsigned int* siglen

, const RSA* rsa

);

int CSP_rsa_verify(

int type

, const unsigned char* m

, unsigned int m_length

, unsigned char* sigbuf

, unsigned int siglen

, const RSA* rsa

);

//---------------------------------------------------------------------------

static const RSA_METHOD CSP_rsa_method =

{

"Cryptographic RSA method" //! name

, CSP_rsa_pub_enc //! rsa_pub_enc

, CSP_rsa_pub_dec //! rsa_pub_dec

, CSP_rsa_priv_enc //! rsa_priv_enc

, CSP_rsa_priv_dec //! rsa_priv_dec

, NULL //! rsa_mod_exp

, BN_mod_exp_mont //! bn_mod_exp

, CSP_rsa_init //! init

, CSP_rsa_finish //! finish

, RSA_FLAG_SIGN_VER //! flags

, NULL //! app_data

, CSP_rsa_sign //! rsa_sign

, CSP_rsa_verify //! rsa_verify

};

#if defined(__cplusplus)

};//extern "C"

#endif

//===========================================================================

#endif//CSPEngineH

//---------------------------------------------------------------------------

#include "stdafx.h"

#include <vector>

#include <algorithm>

#include <openssl/bio.h>

#include <openssl/objects.h>

#include "CSPEngine.h"

//---------------------------------------------------------------------------

X509* x509 = NULL;

EVP_PKEY* key = NULL;

//---------------------------------------------------------------------------

X509_STORE* X509_STORE_load_MSCryptoAPI(void)

{

static X509_STORE* store = NULL;

HCERTSTORE hCertStore;

LPCTSTR lpStoreNames[] = {

TEXT("ROOT"),

TEXT("CA")

};

/* if(NULL != store) {

++store->references;

return store;

}*/

store = X509_STORE_new();

for(int i=0; i<sizeof(lpStoreNames)/sizeof(lpStoreNames[0]); ++i) {

hCertStore = ::CertOpenSystemStore(NULL, lpStoreNames[i]);

if(NULL == hCertStore) return 0;

X509_STORE_load_CERT_STORE(store, hCertStore);

}

return store;

}

//---------------------------------------------------------------------------

int X509_STORE_load_CERT_STORE(X509_STORE* store, HCERTSTORE hCertStore)

{

if(NULL == store) return 0;

if(NULL == hCertStore) return 0;

PCCERT_CONTEXT pCertContext = NULL;

while(pCertContext = ::CertEnumCertificatesInStore(hCertStore, pCertContext)) {

X509* x509;

x509 = X509_new_CERT_CONTEXT(pCertContext);

X509_STORE_add_cert(store, x509);

}

PCCRL_CONTEXT pCrlContext = NULL;

while(pCrlContext = ::CertEnumCRLsInStore(hCertStore, pCrlContext)) {

X509_CRL* crl;

crl = X509_CRL_new_CRL_CONTEXT(pCrlContext);

X509_STORE_add_crl(store, crl);

}

return 1;

}

//---------------------------------------------------------------------------

EVP_PKEY* EVP_PKEY_new_CERT_CONTEXT(PCCERT_CONTEXT pCertContext, X509** cert)

{

X509* x509;

EVP_PKEY* evp;

if(NULL == pCertContext) return NULL;

x509 = X509_new_CERT_CONTEXT(pCertContext);

if(NULL == x509) return NULL;

evp = X509_get_pubkey(x509);

if(NULL == evp) return NULL;

if(RSA* rsa = EVP_PKEY_get1_RSA(evp)) {

RSA_set_ex_data(rsa, 0, (void*)pCertContext);

RSA_set_method(rsa, &CSP_rsa_method);

rsa->flags |= CSP_rsa_method.flags;

RSA_free(rsa);

} else {

EVP_PKEY_free(evp);

evp = NULL;

}

if(evp) {

if(cert) {

*cert = x509;

} else {

X509_free(x509);

}

}

return evp;

}

//---------------------------------------------------------------------------

X509* X509_new_CERT_CONTEXT(PCCERT_CONTEXT pCertContext)

{

X509* x509 = NULL;

BIO* bio;

if(NULL == pCertContext) return NULL;

bio = BIO_new_mem_buf(pCertContext->pbCertEncoded

, pCertContext->cbCertEncoded);

if(NULL == bio) return NULL;

d2i_X509_bio(bio, &x509);

BIO_free(bio);

return x509;

}

//---------------------------------------------------------------------------

X509_CRL* X509_CRL_new_CRL_CONTEXT(PCCRL_CONTEXT pCrlContext)

{

X509_CRL* crl = NULL;

BIO* bio;

if(NULL == pCrlContext) return NULL;

bio = BIO_new_mem_buf(pCrlContext->pbCrlEncoded

, pCrlContext->cbCrlEncoded);

if(NULL == bio) return NULL;

d2i_X509_CRL_bio(bio, &crl);

BIO_free(bio);

return crl;

}

//---------------------------------------------------------------------------

int CSP_rsa_init(RSA* rsa)

{

BOOL ret;

PCERT_CONTEXT pCertContext; // 证书内容

HCRYPTPROV hCryptProv = NULL; // 密钥位置

HCRYPTKEY hCryptKey = NULL; // 私钥句柄

DWORD dwKeySpec;

BOOL fCallerFreeProv = FALSE;

pCertContext = (PCERT_CONTEXT)RSA_get_ex_data(rsa, 0);

if(NULL == pCertContext) return 0;

hCryptKey = (HCRYPTKEY )RSA_get_ex_data(rsa, 2);

if(NULL != hCryptKey) return 1;

// 得到证书相关密钥位置

ret = ::CryptAcquireCertificatePrivateKey(pCertContext

, 0, NULL, &hCryptProv, &dwKeySpec, &fCallerFreeProv);

if(!ret) goto err;

// 获得私钥句柄

ret = ::CryptGetUserKey(hCryptProv, dwKeySpec, &hCryptKey);

if(!ret) goto err;

RSA_set_ex_data(rsa, 1, (void*)hCryptProv);

RSA_set_ex_data(rsa, 2, (void*)hCryptKey);

RSA_set_ex_data(rsa, 3, (void*)dwKeySpec);

RSA_set_ex_data(rsa, 4, (void*)fCallerFreeProv);

return 1;

err:

if(hCryptKey) ::CryptDestroyKey(hCryptKey);

if(hCryptProv && fCallerFreeProv) {

::CryptReleaseContext(hCryptProv, 0);

}

return 0;

}

//---------------------------------------------------------------------------

int CSP_rsa_finish(RSA* rsa)

{

HCRYPTPROV hCryptProv = NULL;

HCRYPTKEY hCryptKey = NULL;

BOOL fCallerFreeProv = FALSE;

hCryptKey = (HCRYPTKEY )RSA_get_ex_data(rsa, 2);

::CryptDestroyKey(hCryptKey);

RSA_set_ex_data(rsa, 2, NULL);

hCryptProv = (HCRYPTPROV)RSA_get_ex_data(rsa, 1);

fCallerFreeProv = (BOOL )RSA_get_ex_data(rsa, 4);

if(hCryptProv && fCallerFreeProv) {

::CryptReleaseContext(hCryptProv, 0);

RSA_set_ex_data(rsa, 1, NULL);

}

return 1;

}

//---------------------------------------------------------------------------

int CSP_rsa_pub_enc(int flen, const unsigned char* from

, unsigned char* to, RSA* rsa, int padding)

{

return RSA_PKCS1_SSLeay()->rsa_pub_enc(flen, from, to, rsa, padding);

}

//---------------------------------------------------------------------------

int CSP_rsa_pub_dec(int flen, const unsigned char* from

, unsigned char* to, RSA* rsa, int padding)

{

return RSA_PKCS1_SSLeay()->rsa_pub_dec(flen, from, to, rsa, padding);

}

//---------------------------------------------------------------------------

int CSP_rsa_priv_enc(int flen, const unsigned char* from

, unsigned char* to, RSA* rsa, int padding)

{

return -1;

}

//---------------------------------------------------------------------------

int CSP_rsa_priv_dec(int flen, const unsigned char* from

, unsigned char* to, RSA* rsa, int padding)

{

BOOL ret;

HCRYPTKEY hCryptKey;

DWORD cbData = flen;

std::vector<BYTE> pbData;

hCryptKey = (HCRYPTKEY)RSA_get_ex_data(rsa, 2);

pbData.resize(cbData);

std::copy(from, from+flen, pbData.rbegin());

ret = ::CryptDecrypt(hCryptKey, NULL, TRUE, 0, &*pbData.begin(), &cbData);

if(!ret) return -1;

std::copy(pbData.begin(), pbData.begin()+cbData, to);

return cbData;

}

//---------------------------------------------------------------------------

ALG_ID nid2algid(int nid)

{

ALG_ID algId;

switch(nid) {

case NID_md2:

algId = CALG_MD2; break;

case NID_md4:

algId = CALG_MD4; break;

case NID_md5:

algId = CALG_MD5; break;

case NID_sha:

algId = CALG_SHA; break;

case NID_sha1:

algId = CALG_SHA1; break;

case NID_md5_sha1:

default:

algId = CALG_SSL3_SHAMD5;

break;

}

return algId;

}

//---------------------------------------------------------------------------

int CSP_rsa_sign(int type, const unsigned char *m, unsigned int m_length

, unsigned char *sigret, unsigned int *siglen, const RSA *rsa)

{

BOOL ret = FALSE;

HCRYPTPROV hCryptProv;

HCRYPTKEY hCryptKey;

DWORD dwKeySpec;

ALG_ID algId;

HCRYPTHASH hHash = NULL;

DWORD cbHash, cbHashSize;

DWORD cbData = 0;

std::vector<BYTE> pbData;

hCryptProv = (HCRYPTPROV)RSA_get_ex_data(rsa, 1);

hCryptKey = (HCRYPTKEY )RSA_get_ex_data(rsa, 2);

dwKeySpec = (DWORD )RSA_get_ex_data(rsa, 3);

if(NULL == hCryptKey) goto err;

algId = nid2algid(type);

if(-1 == algId) goto err;

ret = ::CryptCreateHash(hCryptProv, algId, 0, 0, &hHash);

if(!ret) goto err;

ret = ::CryptGetHashParam(hHash, HP_HASHSIZE, (LPBYTE)&cbHashSize, &cbHash, 0);

if(!ret) goto err;

if(m_length != cbHashSize) goto err;

ret = ::CryptSetHashParam(hHash, HP_HASHVAL, m, 0);

if(!ret) goto err;

ret = ::CryptSignHash(hHash, dwKeySpec, NULL, 0, NULL, &cbData);

if(!ret) goto err;

*siglen = cbData;

pbData.resize(cbData);

ret = ::CryptSignHash(hHash, dwKeySpec, NULL, 0, &*pbData.begin(), &cbData);

if(!ret) goto err;

std::copy(pbData.rbegin(), pbData.rend(), sigret);

err:

::CryptDestroyHash(hHash);

return ret;

}

//---------------------------------------------------------------------------

int CSP_rsa_verify(int type, const unsigned char *m, unsigned int m_length

, unsigned char *sigbuf, unsigned int siglen, const RSA *rsa)

{

BOOL ret = FALSE;

RSA* pubrsa;

pubrsa = RSAPublicKey_dup(const_cast<RSA*>(rsa));

if(NULL == pubrsa) goto err;

ret = RSA_verify(type, m, m_length, sigbuf, siglen, pubrsa);

err:

RSA_free(pubrsa);

return ret;

}

//---------------------------------------------------------------------------

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有