#include "common.hpp"
#include "context.hpp"
#include "msg.hpp"
#include "cert.hpp"
#include "certinfo.hpp"
void CertStore::init()
{
ctx = NULL;
msg = NULL;
hstore = 0;
proto = NULL;
LOG("CertStore::init %p\n", this);
}
Cert* Cert::duplicate() throw(CSPException)
{
LOG("Cert::duplicate %p\n", pcert);
PCCERT_CONTEXT pc = CertDuplicateCertificateContext(pcert);
LOG(" into %p\n", pc);
return new Cert(pc, parent);
}
Cert::Cert(BYTE* STRING, DWORD LENGTH) throw(CSPException) : parent(NULL)
{
LOG("Cert::Cert(str)\n");
pcert = CertCreateCertificateContext(MY_ENC_TYPE, STRING, LENGTH);
LOG(" created cert: %p\n", pcert);
if (!pcert) {
throw CSPException("Cert: Couldn't decode certificate blob");
}
}
Cert *Cert::self_sign(Crypt *ctx, BYTE *STRING, DWORD LENGTH) throw(CSPException)
{
LOG("Cert::self_sign\n");
#ifdef UNIX
throw CSPException("Cert.self_sign: Self-signed certificates are not implemented on Unix", 1);
#else
CERT_NAME_BLOB issuer;
bool res;
char *subj = new char[LENGTH + 1];
strncpy(subj, (char *)STRING, LENGTH);
subj[LENGTH] = 0;
res = CertStrToName(
MY_ENC_TYPE,
(LPSTR)subj,
CERT_OID_NAME_STR | CERT_NAME_STR_REVERSE_FLAG,
NULL,
NULL,
&issuer.cbData,
NULL );
if (!res) {
delete[] subj;
throw CSPException("Cert.self_sign: Couldn't determine encoded info size");
}
issuer.pbData = (BYTE*) malloc(issuer.cbData);
res = CertStrToName(
MY_ENC_TYPE,
(LPSTR)subj,
CERT_OID_NAME_STR | CERT_NAME_STR_REVERSE_FLAG,
NULL,
issuer.pbData,
&issuer.cbData,
NULL );
if (!res) {
delete[] subj;
free(issuer.pbData);
throw CSPException("Cert.self_sign: Couldn't encode cert info");
}
delete[] subj;
CRYPT_ALGORITHM_IDENTIFIER algid;
DWORD hasi = sizeof(algid);
memset(&algid, 0, hasi);
algid.pszObjId = (char *)szOID_CP_GOST_R3411;
PCCERT_CONTEXT pc = CertCreateSelfSignCertificate(ctx->hprov,
&issuer,
0,
NULL,
&algid,
NULL,
NULL,
NULL);
if (!pc) {
free(issuer.pbData);
throw CSPException("Cert.self_sign: Couldn't acquire self-signed certificate");
}
free(issuer.pbData);
return new Cert(pc);
#endif
}
void Cert::extract(BYTE **s, DWORD *slen) throw(CSPException)
{
LOG("Cert::extract\n");
*slen = pcert->cbCertEncoded;
*s = (BYTE *)malloc(*slen);
memcpy(*s, pcert->pbCertEncoded, *slen);
}
void Cert::thumbprint(BYTE **s, DWORD *slen) throw(CSPException)
{
LOG("Cert::thumbprint\n");
if(!CertGetCertificateContextProperty(pcert, CERT_HASH_PROP_ID, NULL, slen)) {
LOG(" Error: %p\n", pcert);
throw CSPException("Cert.thumbprint: Couldn't get certificate hash size");
}
*s = (BYTE *)malloc(*slen);
if(!CertGetCertificateContextProperty(pcert, CERT_HASH_PROP_ID, (void *)*s, slen)) {
free((void *)*s);
throw CSPException("Cert.thumbprint: Couldn't get certificate thumbprint");
}
}
void Cert::subject_id(BYTE **s, DWORD *slen) throw(CSPException)
{
LOG("Cert::subject_id\n");
if(!CertGetCertificateContextProperty(pcert, CERT_KEY_IDENTIFIER_PROP_ID, NULL, slen)) {
LOG(" Error: %p\n", pcert);
throw CSPException("Cert.thumbprint: Couldn't get certificate subject key id size");
}
*s = (BYTE *)malloc(*slen);
if(!CertGetCertificateContextProperty(pcert, CERT_KEY_IDENTIFIER_PROP_ID, (void *)*s, slen)) {
free((void *)*s);
throw CSPException("Cert.thumbprint: Couldn't get certificate subject key id");
}
}
CertFind::CertFind(CertStore *p, DWORD et, DWORD ft, BYTE *STRING, DWORD LENGTH) : CertIter(p)
{
LOG("CertFind::CertFind(%p, %u, %u, %u)\n", p, et, ft, LENGTH);
enctype = et;
findtype = ft;
chb.pbData = (BYTE *)malloc(LENGTH);
memcpy(chb.pbData, STRING, LENGTH);
chb.cbData = LENGTH;
param = &chb;
}
CertFind::CertFind(CertStore *p, DWORD et, BYTE *STRING, DWORD LENGTH) : CertIter(p)
{
LOG("CertFind::CertFind(%p, %u, %s)\n", p, et);
enctype = et;
findtype = CERT_FIND_SUBJECT_STR;
param = (CRYPT_HASH_BLOB *)malloc(LENGTH + 1);
strncpy((char *)param, (const char*)STRING, LENGTH + 1);
((char *)param)[LENGTH] = 0;
}
CertStore::CertStore() throw(CSPException)
{
LOG("CertStore::CertStore()\n");
init();
hstore = CertOpenStore(CERT_STORE_PROV_MEMORY, MY_ENC_TYPE, 0, CERT_STORE_CREATE_NEW_FLAG,NULL);
if (!hstore) {
throw CSPException("CertStore: Couldn't create memory store");
}
}
CertStore::CertStore(Crypt *parent, BYTE *STRING, DWORD LENGTH) throw(CSPException)
{
LOG("CertStore::CertStore(%p)\n", parent);
HCRYPTPROV hprov = 0;
init();
if (parent) {
ctx = parent;
ctx->ref();
hprov = ctx->hprov;
}
proto = new char[LENGTH + 1];
strncpy(proto, (char *)STRING, LENGTH);
proto[LENGTH] = 0;
hstore = CertOpenStore(
CERT_STORE_PROV_SYSTEM_A, // The store provider type
0, // The encoding type is
// not needed
hprov, // Use the default HCRYPTPROV
// Set the store location in a
// registry location
CERT_STORE_NO_CRYPT_RELEASE_FLAG | CERT_SYSTEM_STORE_CURRENT_USER,
proto // The store name as a Unicode
// string
);
if (!hstore) {
throw CSPException("CertStore: Couldn't open certificate store");
}
}
Cert *CertStore::get_cert_by_info(CertInfo *ci) throw(CSPException, CSPNotFound)
{
PCCERT_CONTEXT res;
LOG("CertStore::get_cert_by_info(%p)\n", ci);
if (!ci) {
throw CSPNotFound("NULL cert info", -1);
}
res = CertGetSubjectCertificateFromStore(hstore, MY_ENC_TYPE, ci->psi);
if (!res) {
DWORD err = GetLastError();
if (err == (DWORD)CRYPT_E_NOT_FOUND) {
throw CSPNotFound("Subject cert not found", err);
}
throw CSPException("CertStore.get_cert_by_info: Error getting subject certificate from store", err);
}
return new Cert(res, this);
}
Cert *CertStore::add_cert(Cert *c) throw(CSPException)
{
LOG("CertStore::add_cert(%p)\n", c->pcert);
PCCERT_CONTEXT copy;
if (c && !CertAddCertificateContextToStore(hstore, c->pcert,
CERT_STORE_ADD_REPLACE_EXISTING, ©)) {
DWORD err = GetLastError();
switch (err) {
case CRYPT_E_EXISTS:
throw CSPException("CertStore.add_cert: Matching or newer cerificate already exist in store", err);
default:
throw CSPException("CertStore.add_cert: Couldn't add cert to store");
}
}
return new Cert(copy, this);
}
CertIter *CertStore::__iter__() throw(CSPException)
{
return new CertIter(this);
}
CertFind *CertStore::find_by_thumb(BYTE *STRING, DWORD LENGTH) throw(CSPException)
{
return new CertFind(this, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, CERT_FIND_HASH, STRING, LENGTH);
}
CertFind *CertStore::find_by_name(BYTE *STRING, DWORD LENGTH) throw(CSPException)
{
return new CertFind(this, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, STRING, LENGTH);
}
CertIter::CertIter(CertStore *p) throw (CSPException) : parent(p)
{
LOG("CertIter::CertIter(%p)\n", p);
if (parent) {
parent->ref();
}
iter = true;
pcert = NULL;
}
CertIter::~CertIter() throw (CSPException)
{
LOG("CertIter::~CertIter()\n");
if (pcert) {
CertFreeCertificateContext(pcert);
pcert = NU
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 52 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/277f6345dca0446498fbbc03843436aa_qq_38161040.jpg!1)
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)