X509_REQ_Class

8 篇文章 0 订阅
// X509_REQ_Class.h: interface for the X509_REQ_Class class.
//
//


#if !defined(AFX_X509_REQ_CLASS_H__BBC4D6D4_0067_4804_ADAA_AB39B959071E__INCLUDED_)
#define AFX_X509_REQ_CLASS_H__BBC4D6D4_0067_4804_ADAA_AB39B959071E__INCLUDED_


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


#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/x509.h>
#include <openssl/asn1.h>
#include "MYStruct.h"
#include "RSA_Class.h"
#include "EC_Class.h" // Added by ClassView






class X509_REQ_Class  
{
public:
//********验证一个REQ*******//
int VerifyReq(char *FP);
//*******得到一个秘钥匙统一结构体指针*******//
EVP_PKEY *GetEvpKeyPoint();
//**********在文件中读取一个REQ***********//
int ReadReq(char *FP);
//*********向文件中写入一个REQ********//
int WriteReq(char *mode,char *FP);
//*********设置REQ********//
int SetReq(stuCERTINFO *CertInfo);
//*************得到REQ的主题字段************//
X509_NAME *GetNamePoint();
//***********得到一个REQ拷贝**********//
X509_REQ GetReq();
//***************添加主题字段信息**************//
int AddName(stuNAMEINFO *NameInfo);
X509_REQ_Class();
virtual ~X509_REQ_Class();
private:

//**********将主体中的信息转换成宽字符***********//
void Ansi2Utf8(const LPSTR lpsrc, const int srclen, LPSTR lpdst, int& dstlen);
//**********添加主题信息函数********//
int AddNameInfo(const int iNid,const int iType,const char * lpszInput,const int iLen);
EC_Class *m_EC;//一个ECC类指针
RSA_Class *m_Rsa;//一个RSA类指针
X509_REQ *m_x509_Req;//一个REQ结构体指针
X509_REQ_INFO *m_x509_Req_Info;//一个REQ定义中信息结构体的指针
X509_NAME *m_x509_Name;//信息结构体中主体结构指针
};


#endif // !defined(AFX_X509_REQ_CLASS_H__BBC4D6D4_0067_4804_ADAA_AB39B959071E__INCLUDED_)



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


#include "stdafx.h"
#include "MYCA.h"
#include "X509_REQ_Class.h"


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


//
// Construction/Destruction
//


X509_REQ_Class::X509_REQ_Class()
{
m_Rsa=NULL;
m_x509_Req=NULL;
m_x509_Req_Info=NULL;
//m_EvpKey=NULL;
m_x509_Name=NULL;
m_EC=NULL;
}


X509_REQ_Class::~X509_REQ_Class()
{
delete m_Rsa;
if(m_EC!=NULL)
delete m_EC;
//X509_NAME_free(m_x509_Name);
X509_REQ_free(m_x509_Req);
//EVP_PKEY_free(m_EvpKey);

}




int X509_REQ_Class::AddName(stuNAMEINFO *NameInfo)
{
int name_id[]={NID_countryName,NID_stateOrProvinceName,NID_localityName,
NID_organizationName,NID_organizationalUnitName,NID_commonName,
NID_pkcs9_emailAddress};
if ((m_x509_Name=X509_NAME_new()) == NULL)
{
return 0;
}
/*添加name信息,国家,省份,城市,组织,单位,个人,Email*/
AddNameInfo(NID_countryName,V_ASN1_UTF8STRING,(char *)NameInfo->C,strlen((char *)NameInfo->C));
AddNameInfo(NID_stateOrProvinceName,V_ASN1_UTF8STRING,(char *)NameInfo->ST,strlen((char *)NameInfo->ST));
AddNameInfo(NID_localityName,V_ASN1_UTF8STRING,(char *)NameInfo->L,strlen((char *)NameInfo->L));
AddNameInfo(NID_organizationName,V_ASN1_UTF8STRING,(char *)NameInfo->O,strlen((char *)NameInfo->O));
AddNameInfo(NID_organizationalUnitName,V_ASN1_UTF8STRING,(char *)NameInfo->OU,strlen((char *)NameInfo->OU));
AddNameInfo(NID_commonName,V_ASN1_UTF8STRING,(char *)NameInfo->CN,strlen((char *)NameInfo->CN));
AddNameInfo(NID_pkcs9_emailAddress,V_ASN1_IA5STRING,(char *)NameInfo->MAIL,strlen((char *)NameInfo->MAIL));

return 1;
}


int X509_REQ_Class::AddNameInfo(const int iNid,
                   const int iType,
                   const char * lpszInput,
                   const int iLen)
{
if(NULL == lpszInput || strlen(lpszInput) == 0)
return FALSE;
if(iType == V_ASN1_UTF8STRING)
{
char lpdst[1024] = {0};
int dstlen = 0;
char input[256]={0};
strncpy(input, lpszInput, iLen);
Ansi2Utf8(input, iLen, lpdst, dstlen);//字符格式转换
X509_NAME_add_entry_by_NID(m_x509_Name,iNid,V_ASN1_UTF8STRING,(UCHAR *)lpdst,dstlen, -1, 0);//添加通过id添加函数,在x509.h
}
else
X509_NAME_add_entry_by_NID(m_x509_Name,iNid,iType,(UCHAR *)lpszInput,iLen, -1, 0);
return TRUE;


}


void X509_REQ_Class::Ansi2Utf8(const LPSTR lpsrc, const int srclen, LPSTR lpdst, int &dstlen)
{
WCHAR * pwUnicode;      
    
/*This function maps a character string to a wide-character (Unicode) string.*/
int len = MultiByteToWideChar(CP_ACP,0,(char*)lpsrc,srclen,NULL,0);//得到转换后的buffer长度      
    pwUnicode = new WCHAR[len];//建立buffer接受装换后的字符串  
    memset(pwUnicode,0,len);
    MultiByteToWideChar(CP_ACP,0 ,(char*)lpsrc,srclen,pwUnicode,len);//转换

/*This function maps a wide-character string to a new character string.*/
    dstlen = WideCharToMultiByte(CP_UTF8,0,pwUnicode,len,NULL,0,NULL,NULL);      
    WideCharToMultiByte(CP_UTF8,0,pwUnicode,len,lpdst,dstlen,NULL,NULL);      
    delete []pwUnicode;      


}


X509_REQ X509_REQ_Class::GetReq()
{
return *m_x509_Req;
}


X509_NAME *X509_REQ_Class::GetNamePoint()
{

if(m_x509_Name==NULL)
{
if(m_x509_Req==NULL)
{
return NULL;
}
if ((m_x509_Name=X509_NAME_new()) == NULL)
{
return NULL;
}
if((m_x509_Name=X509_REQ_get_subject_name(m_x509_Req))==NULL)
{
return NULL;
}

}

X509_NAME* x509_Name;
if ((x509_Name=X509_NAME_new()) == NULL)
{
return NULL;
}
*x509_Name=*m_x509_Name;

return x509_Name;
}


int X509_REQ_Class::SetReq(stuCERTINFO *CertInfo)
{
//**********初始化机构体********//
if((m_x509_Req=X509_REQ_new())==NULL)
{
return 0;
}
//*********设置版本号*********//
if(!X509_REQ_set_version(m_x509_Req,0L)) 
{
return 0;

//**********主题信息***********//
if(!X509_REQ_set_subject_name(m_x509_Req,m_x509_Name))
{
return 0;
}
//***********读取秘钥对**********//
m_Rsa=new RSA_Class;
int keykind=0;
EVP_PKEY *EvpKey;
if(m_Rsa->ReadRSA((LPSTR)(LPCTSTR)CertInfo->CAPriKeyPath))
{
keykind=1;
if((EvpKey=m_Rsa->GetEvpKeyPoint())==NULL)
{
AfxMessageBox("读取CA秘钥错误");
return 0;
}
}
if(!keykind)
{
m_EC=new EC_Class;
if(m_EC->ReadEC((LPSTR)(LPCTSTR)CertInfo->CAPriKeyPath))
{
keykind=2;
if((EvpKey=m_EC->GetEvpKeyPoint())==NULL)
{
AfxMessageBox("读取CA秘钥错误");
return 0;
}
}
}
if(keykind==0)
{
return 0;
}
//***********设置公钥*********//
if(!X509_REQ_set_pubkey(m_x509_Req,EvpKey))
{
return 0;

//***********签名REQ*********//
if(keykind==1)
{
if(!X509_REQ_sign(m_x509_Req,EvpKey,EVP_md5()))
{
return 0;

}
else if(keykind==2)
{
if(!X509_REQ_sign(m_x509_Req,EvpKey,EVP_ecdsa()))
{

return 0;
}
}
return 1;
}


int X509_REQ_Class::WriteReq(char *mode,char *FP)
{
BIO * bReq;
CString path=FP;
path=path+"\\Req."+mode;
if((bReq = BIO_new_file(path, "w"))== NULL)
{
AfxMessageBox("open "+path+" file fail");
return 0;
}
if(strcmp(mode,"der")==0)
{
if (!i2d_X509_REQ_bio(bReq,m_x509_Req))
{
AfxMessageBox("X509 REQ DER write bio fail");
return 0;
}
}
else
{
if (!PEM_write_bio_X509_REQ(bReq,m_x509_Req))
{
AfxMessageBox("X509 REQ PEM write bio fail");
return 0;
}
}

BIO_free(bReq);
return 1;
}


int X509_REQ_Class::ReadReq(char *FP)
{
BIO * bReq;
CString path=FP;
int req=0;
if(m_x509_Req==NULL)
{
if((m_x509_Req=X509_REQ_new())==NULL)
{
return 0;
}
}
if(FP!=NULL)
{
if((bReq = BIO_new_file(path, "r"))== NULL)
{
AfxMessageBox("open "+path+" file fail");
return 0;
}

if (PEM_read_bio_X509_REQ(bReq,&m_x509_Req,NULL,NULL))
{
req=1;
}
BIO_free(bReq);
}
if(!req)
{
if(FP!=NULL)
{
if((bReq = BIO_new_file(path, "r"))== NULL)
{
AfxMessageBox("open "+path+" file fail");
return 0;
}
if (d2i_X509_REQ_bio(bReq,&m_x509_Req))
{
req=1;
}
BIO_free(bReq);
}
}
return req;
}


EVP_PKEY *X509_REQ_Class::GetEvpKeyPoint()
{
EVP_PKEY* EvpKey;
if(m_x509_Req==NULL)
{
return NULL;
}
if ((EvpKey=EVP_PKEY_new()) == NULL) 
{
return NULL;
}
if((EvpKey=X509_REQ_get_pubkey(m_x509_Req))==NULL)
{
return NULL;
}

return EvpKey;
}






int X509_REQ_Class::VerifyReq(char *FP)
{
if(!ReadReq(FP))
return 0;
OpenSSL_add_all_digests();
int i=X509_REQ_verify(m_x509_Req,X509_REQ_get_pubkey(m_x509_Req));
/*
http://blog.chinaunix.net/uid-21518-id-152335.html
【X509_REQ_verify】
    与签名相对应,对签名进行验证,所以将公钥pkey传入就可以了
*/
if(i<=0)
return 0;
else
return 1;


}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值