【问题标题】:Failed to read serial number(ASN1_INTEGER) from x509 certiticate using openssl使用 openssl 从 x509 证书读取序列号(ASN1_INTEGER)失败
【发布时间】:2011-12-21 15:35:41
【问题描述】:

如何使用 Openssl 从 x509 证书中读取证书详细信息(序列号、颁发者、主题详细信息)。

我使用 PKCS12_parse() 解析 P12 文件,然后从获得的 x509 证书中检索 ASN1_INTEGER 格式的序列号。但是我如何解析它以便可以读取它。

【问题讨论】:

  • 你解决了吗?
  • 是的,我可以检索证书的一些详细信息以及序列号、颁发者和主题详细信息。
  • 你是怎么得到序列号的?

标签: openssl


【解决方案1】:

我试过这种方式..并且可以读取证书的值。

   bio_out=BIO_new_fp(stdout,BIO_NOCLOSE);   //here instead of stdout, a file pointer can also be given
   x509 = sk_X509_value(certs,0);
   X509_NAME_print_ex(bio_out,X509_get_issuer_name(x509), XN_FLAG_COMPAT, X509_FLAG_COMPAT);


//Issuer Name
BIO_printf(bio_out,"\n");
unsigned long nmflag = 0;   
CryptoUtility *cryptoU = [[CryptoUtility alloc] init];
[cryptoU print_name:bio_out title:"Verify : issuer= " x509name:X509_get_issuer_name(x509) flag:nmflag];
BIO_printf(bio_out,"\n");

//Subject Name
BIO_printf(bio_out,"\n");
[cryptoU print_name:bio_out title:"Verify : subject= " x509name:X509_get_subject_name(x509) flag:nmflag];
BIO_printf(bio_out,"\n");

//Serial NO
BIO_printf(bio_out,"\n");
BIO_printf(bio_out,"Verify : serial=");
i2a_ASN1_INTEGER(bio_out, X509_get_serialNumber(x509));
BIO_printf(bio_out,"\n");
BIO_printf(bio_out,"\n");
//NSLog(@"Issuer name %@",X509_get_issuer_name(x509));

    //Common Name
char peer_CN[256];
X509_NAME_get_text_by_NID(X509_get_subject_name(x509),NID_commonName, peer_CN, 256);
NSLog(@"Verify : comman name %s",peer_CN);

我希望这会有所帮助。

【讨论】:

  • 而不是文件指针如何从 BIO 结构中获取值..?我想使用 Memory Bio 结构。而不是文件 BIO 结构。
【解决方案2】:

创建内存 BIO:

 BIO *mem = BIO_new(BIO_s_mem());
 //pass this mem BIO to hold the data

 Extract the BUF_MEM structure from a memory BIO and then free up the BIO:

 BUF_MEM *bptr;
 BIO_get_mem_ptr(mem, &bptr);
 BIO_set_close(mem, BIO_NOCLOSE); /* So BIO_free() leaves BUF_MEM alone */

 char *buff = (char *)malloc(bptr->length);       //converting BUF_MEM  to Char * 
 memcpy(buff, bptr->data, bptr->length-1);        //to be used later as you needed
 buff[bptr->length-1] = 0;
 NSLog(@"--------------------------->%s",buff);
 BIO_free(mem);

buff 可以进一步用于逻辑......希望这会有所帮助:)

【讨论】:

  • 嗨@Balamurugan,这是我的第一个答案,如果他们真的对你有帮助,为什么不接受他们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
相关资源
最近更新 更多