OPENSSL X509證書驗證
阿新 • • 發佈:2019-01-26
步驟:
1)初始化環境
a.新建證書儲存區X509_STORE_new()
b.新建證書校驗上下文X509_STORE_CTX_new()
2)匯入根證書
a.讀取CA證書,從DER編碼格式化為X509結構d2i_X509()
b.將CA證書匯入證書儲存區X509_STORE_add_cert()
3)匯入要校驗的證書test
a.讀取證書test,從DER編碼格式化為X509結構d2i_X509()
b.在證書校驗上下文初始化證書test,X509_STORE_CTX_init()
c.校驗X509_verify_cert
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> 閱讀(10906) | 評論(0) | 轉發(1) | 給主人留下些什麼吧!~~ 評論熱議
1)初始化環境
a.新建證書儲存區X509_STORE_new()
b.新建證書校驗上下文X509_STORE_CTX_new()
2)匯入根證書
a.讀取CA證書,從DER編碼格式化為X509結構d2i_X509()
b.將CA證書匯入證書儲存區X509_STORE_add_cert()
3)匯入要校驗的證書test
a.讀取證書test,從DER編碼格式化為X509結構d2i_X509()
b.在證書校驗上下文初始化證書test,X509_STORE_CTX_init()
c.校驗X509_verify_cert
-
include <stdio.h>
-
#include
-
#include <stdlib.h>
-
#include <openssl/evp.h>
-
#include <openssl/x509.h>
-
#define CERT_PATH "/home/ckelsel/work/rc4/cert"
-
#define ROOT_CERT "ca.cer"
-
#define WIN71H "win71h.cer"
-
#define WIN71Y "win71y.cer"
-
#define GET_DEFAULT_CA_CERT(
-
#define GET_CUSTOM_CERT(str, path, name) sprintf(str, "%s/%s", path, name)
-
#define MAX_LEGTH 4096
-
int my_load_cert(unsigned char *str, unsigned long *str_len,
-
const char *verify_cert, const unsigned int cert_len)
-
{
-
FILE *fp;
-
fp = fopen(verify_cert, "rb");
-
if ( NULL == fp)
-
{
-
fprintf(stderr, "fopen fail\n");
-
return -1;
-
}
-
*str_len = fread(str, 1, cert_len, fp);
-
fclose(fp);
-
return 0;
-
}
-
X509 *der_to_x509(const unsigned char *der_str, unsigned int der_str_len)
-
{
-
X509 *x509;
-
x509 = d2i_X509(NULL, &der_str, der_str_len);
-
if ( NULL == x509 )
-
{
-
fprintf(stderr, "d2i_X509 fail\n");
-
return NULL;
-
}
-
return x509;
-
}
-
int x509_verify()
-
{
-
int ret;
-
char cert[MAX_LEGTH];
-
unsigned char user_der[MAX_LEGTH];
-
unsigned long user_der_len;
-
X509 *user = NULL;
-
unsigned char ca_der[MAX_LEGTH];
-
unsigned long ca_der_len;
-
X509 *ca = NULL;
-
X509_STORE *ca_store = NULL;
-
X509_STORE_CTX *ctx = NULL;
-
STACK_OF(X509) *ca_stack = NULL;
-
/* x509初始化 */
-
ca_store = X509_STORE_new();
-
ctx = X509_STORE_CTX_new();
-
/* root ca*/
-
GET_DEFAULT_CA_CERT(cert);
-
/* 從檔案中讀取 */
-
my_load_cert(ca_der, &ca_der_len, cert, MAX_LEGTH);
-
/* DER編碼轉X509結構 */
-
ca = der_to_x509(ca_der, ca_der_len);
-
/* 加入證書儲存區 */
-
ret = X509_STORE_add_cert(ca_store, ca);
-
if ( ret != 1 )
-
{
-
fprintf(stderr, "X509_STORE_add_cert fail, ret = %d\n", ret);
-
goto EXIT;
-
}
-
/* 需要校驗的證書 */
-
GET_CUSTOM_CERT(cert, CERT_PATH, WIN71H);
-
my_load_cert(user_der, &user_der_len, cert, MAX_LEGTH);
-
user = der_to_x509(user_der, user_der_len);
-
ret = X509_STORE_CTX_init(ctx, ca_store, user, ca_stack);
-
if ( ret != 1 )
-
{
-
fprintf(stderr, "X509_STORE_CTX_init fail, ret = %d\n", ret);
-
goto EXIT;
-
}
-
//openssl-1.0.1c/crypto/x509/x509_vfy.h
-
ret = X509_verify_cert(ctx);
-
if ( ret != 1 )
-
{
-
fprintf(stderr, "X509_verify_cert fail, ret = %d, error id = %d, %s\n",
-
ret, ctx->error, X509_verify_cert_error_string(ctx->error));
-
goto EXIT;
-
}
-
EXIT:
-
X509_free(user);
-
X509_free(ca);
-
X509_STORE_CTX_cleanup(ctx);
-
X509_STORE_CTX_free(ctx);
-
X509_STORE_free(ca_store);
-
return ret == 1 ? 0 : -1;
-
}
-
int main()
-
{
-
OpenSSL_add_all_algorithms();
-
x509_verify();
-
return 0;
- }
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> 閱讀(10906) | 評論(0) | 轉發(1) | 給主人留下些什麼吧!~~ 評論熱議