📄 复件 vc1.txt
字号:
void CTianDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE);
dlg.m_ofn.lpstrFilter="X.509Certificate files(*.cer)\0*.cer\0All Files(*.*)\0*.*";
dlg.DoModal();
CString szp=dlg.GetPathName();
CButton*btPh=(CButton*)GetDlgItem(IDC_path);
btPh->SetWindowText(szp.GetBuffer(szp.GetLength())) ;
}
OCSP_REQUEST* CTianDlg::gencrateNewRequest(char *certpath)
{ OCSP_REQUEST *ret=NULL;
if((ret=(OCSP_REQUEST*)OPENSSL_malloc(sizeof(OCSP_REQUEST)))==NULL)
{ printf("mallet failed!");
return NULL;
}
ret->tbsRequest = OCSP_REQINFO_new();
ret->optionalSignature=NULL;
BIO *in =NULL;
in=BIO_new(BIO_s_file());
if (in == NULL)
{ printf("创建I/O文件出错!");
return NULL;
}
/*从文件中导入一个证书*/
if(BIO_read_filename(in,certpath)<=0)
{ printf("无法导入OCSP证书\n");
return NULL;
}
X509*X509_cert;
X509_cert=PEM_read_bio_X509(in,NULL,NULL,NULL);
if(X509_cert==NULL)
{ printf("无法导入OCSP证书\n");
return NULL;
}
/*从P-被传递的itiE书中得到Subject Name */
X509_NAME *name=NULL;
name=X509_get_subject_name(X509_cert);
if(name==NULL)
{ printf("name = NULLt\n");
return NULL;
}
if(!(ret->tbsRequest->requestorName=GENERAL_NAME_new()))
{ printf("GENERAL-NAME少ew failed");
return NULL;
}
ret->tbsRequest->requestorName->type=GEN_DIRNAME;
ret->tbsRequest->requestorName->d.dirn=X509_NAME_dup(name);
if(!(ret->tbsRequest->requestList=sk_new(NULL)))
{printf("sk_new_failed");
return NULL;
}
//把cert_id力1{入到请求列表Request list中从certsfile中读入证书
BIO *bio_certs=NULL;
X509 *cl =NULL;
OCSP_CERTID *cid;
bio_certs=BIO_new(BIO_s_file());
bio_certs = BIO_new_file(certpath, "r" );
cl =PEM_read_bio_X509(bio_certs, NULL,NULL, NULL);
if(cl==NULL)
{ printf("c 1=NULLr\n");
return NULL;
}
EVP_MD *ocsp_digest=NULL;
ocsp_digest=(EVP_MD*)EVP_get_digestbyname("md5");
if(ocsp_digest==NULL)
{ printf("ocsp digest=NULLn");
return NULL;
}
if (!(cid=OCSP_cert_id_new(ocsp_digest,X509_get_issuer_name(cl),cl->cert_info->key->public_key,
X509_get_serialNumber(cl))))
{printf("OCSP一 ce电i走new");
return NULL;
}
return ret;
}
int i2d_OCSP_CERTID(OCSP_CERTID *a, unsigned char **pp)
{ M_ASN1_I2D_vars(a);
M_ASN1_I2D_len(a->hashAlgorithm,i2d_X509_ALGOR);
M_ASN1_I2D_len(a->issuerNameHash,i2d_ASN1_OCTET_STRING);
M_ASN1_I2D_len(a->issuerKeyHash, i2d_ASN1_OCTET_STRING);
M_ASN1_I2D_len(a->serialNumber, i2d_ASN1_INTEGER);
M_ASN1_I2D_seq_total();
M_ASN1_I2D_put(a->hashAlgorithm, i2d_X509_ALGOR);
M_ASN1_I2D_put(a->issuerNameHash,i2d_ASN1_OCTET_STRING);
M_ASN1_I2D_put(a->issuerKeyHash, i2d_ASN1_OCTET_STRING);
M_ASN1_I2D_put(a->serialNumber, i2d_ASN1_INTEGER);
M_ASN1_I2D_finish();
}
OCSP_RESPONSE* CTianDlg::OCSP_sendreq_bio(BIO *b, char *path, OCSP_REQUEST *req)
{
BIO*mem=NULL;
char tmpbuf[1024]="\0";
OCSP_RESPONSE*resp=NULL;
char*p,*q,*r;
int len,retcode;
static char req_txt[]="POST%s HTTP/1.0\r\n\content-Type:application/ocsp-request\r\n\content-Length:%d\r\n\r\n";
len=i2d_OCSP_REQUEST(req,NULL);
if(BIO_printf(b,req_txt,path,len)<0)goto err;
//if(i2d_OCSP_REQUEST_bio(b,req)<=0)goto err;
if(!(mem=BIO_new(BIO_s_mem()))) goto err;
/*把响应复制到BIO灯*/
while((len=BIO_read(b,tmpbuf,1024)))
{ if(len<0) break;
BIO_write(mem,tmpbuf,len);
}
if(tmpbuf[0]==0) goto err;
printf("%s\n",tmpbuf);
if(BIO_gets(mem,tmpbuf,512)<=0) goto err;
for(p=tmpbuf;*p&&!isspace((unsigned char)*p);p++)continue;
if(*p) goto err;
while(*p&&isspace((unsigned char)*p))p++;
if(*p) goto err;
for(q=p;*q&&!isspace((unsigned char)*q);q++)continue;
if(*q) goto err;
*q++=0;
retcode=strtoul(p,&r,10);
if(*r)goto err;
while(*q&&isspace((unsigned char)*q))q++;
if(*q) goto err;
for(r=q+strlen(q)-1;isspace((unsigned char)*r);r--)*r=0;
if(retcode!=200)
{ ERR_add_error_data(4,"Code=",p,",Reason=",q);
goto err;
}
while(BIO_gets(mem,tmpbuf,512)>0)
{
for(p=tmpbuf;*p&&isspace((unsigned char)*p); p++) continue;
if(*p) break;
}
if(*p) goto err;
if(!(resp=d2i_OCSP_RESPONSE_bio(mem,NULL))) goto err;
err:
BIO_free(mem);
return resp;
}
BIO * CTianDlg::makeConnect(char *host, char *port, char *path)
{ BIO *ebio=NULL;
ebio=BIO_new_connect(host);
if (!ebio)
{ printf("BIO_ newconnect eroft ");
return NULL;
}
if (port) BIO_set_conn_port(ebio, port);
if(BIO_do_connect(ebio)<=0)
{
printf("B10夕ew connect errorhn");
return NULL;
}
return ebio;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -