CRL_Class

来源:互联网 发布:黑暗之魂3低配置优化 编辑:程序博客网 时间:2024/03/29 00:31
// CRL_Class.h: interface for the CRL_Class class.
//

//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CRL_CLASS_H__704A9F6C_E660_454A_9F93_112B4A36BF83__INCLUDED_)

#define AFX_CRL_CLASS_H__704A9F6C_E660_454A_9F93_112B4A36BF83__INCLUDED_


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include "openssl/ssl.h"
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/x509.h>
#include "MYStruct.h"


class CRL_Class  
{


public:
//*********在文件中读取CRL********//
int ReadCrl(char *FP);
//**********向文件中写入*********//
int WriteCrl(char *mode,char *FP);
//************设置CRL主体**************//
int SetCrl(stuCRLINFO *CrlInfo);
CRL_Class();
virtual ~CRL_Class();


private:
X509_CRL *m_x509_crl;
};


#endif // !defined(AFX_CRL_CLASS_H__704A9F6C_E660_454A_9F93_112B4A36BF83__INCLUDED_)



// CRL_Class.cpp: implementation of the CRL_Class class.
//
//////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "MYCA.h"
#include "CRL_Class.h"
#include "X509_Cert_Class.h"




#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


CRL_Class::CRL_Class()
{
m_x509_crl=NULL;
}


CRL_Class::~CRL_Class()
{
X509_CRL_free(m_x509_crl);
}






int CRL_Class::SetCrl(stuCRLINFO *CrlInfo)
{
//**********初始换结构体********//
if((m_x509_crl=X509_CRL_new())==NULL)
return 0;
//***********设置版本号*********//
if(!X509_CRL_set_version(m_x509_crl,1))
return 0;
//*************设置发行者信息************8//
X509_Cert_Class x509_Cert;
if(!x509_Cert.ReadX509((LPSTR)(LPCTSTR)CrlInfo->CACertFile))
return 0;
X509_NAME *x509_Name=x509_Cert.GetNamePoint();
if(!X509_CRL_set_issuer_name(m_x509_crl,x509_Name))
return 0;

//**********设置更新时间**********//
m_x509_crl->crl->lastUpdate = ASN1_TIME_new();//初始化
X509_gmtime_adj(m_x509_crl->crl->lastUpdate,0);//设置最后更新时间

m_x509_crl->crl->nextUpdate = ASN1_TIME_new();
X509_gmtime_adj(m_x509_crl->crl->nextUpdate,(long)60*60*24*CrlInfo->CrlDate);//设置下次更新时间

//*******添加注销列表堆栈********//
m_x509_crl->crl->revoked = sk_X509_REVOKED_new_null();//初始化废除堆栈
X509_REVOKED *x509_revoked = NULL;
stuCRLREVOKE *p=CrlInfo->lCrlRevoke;
while(p!=NULL)
{
x509_revoked = X509_REVOKED_new();//初始化废除结构体
X509_gmtime_adj(x509_revoked->revocationDate,p->RevokeDate);//设置证书废除时间
ASN1_INTEGER *serial;
serial=M_ASN1_INTEGER_new();
ASN1_INTEGER_set(serial, p->Serial);
X509_REVOKED_set_serialNumber(x509_revoked, serial);//设置废除证书的序列号
sk_X509_REVOKED_push(m_x509_crl->crl->revoked,x509_revoked);//压入废除堆栈
p=p->pCrlRevoke;
//AfxMessageBox("dddddddd");
}

//****************签名**************//
int keykind=0;
RSA_Class CA_Rsa;
EVP_PKEY *EvpKey;
if(CA_Rsa.ReadRSA((LPSTR)(LPCTSTR)CrlInfo->CAPriKeyFile))
{
keykind=1;
if((EvpKey=CA_Rsa.GetEvpKeyPoint())==NULL)
{
AfxMessageBox("读取CA秘钥错误");
return 0;
}
}
else
{
AfxMessageBox("读取CA秘钥错误");
return 0;
}
X509_CRL_sign(m_x509_crl,EvpKey,EVP_md5());


return 1;
}


int CRL_Class::WriteCrl(char *mode,char *FP)
{
BIO * bCert;
CString path=FP;
path=path+"\\CA.crl";

if((bCert = BIO_new_file(path, "wb"))== NULL)
{
AfxMessageBox("open CA.crl fail");
return 0;
}
if(strcmp(mode,"der")==0)
{
if (!i2d_X509_CRL_bio(bCert,m_x509_crl))
{
AfxMessageBox("X509 DER write bio fail");
return 0;
}
}
else
{
if (!PEM_write_bio_X509_CRL(bCert,m_x509_crl))
{
AfxMessageBox("X509 PEM write bio fail");
return 0;
}
}
BIO_free(bCert);
return 1;
}


int CRL_Class::ReadCrl(char *FP)
{
return 1;
}



原创粉丝点击