获取 X509 数字证书里面的使用者信息 (openssl)

 
  
#define ENTRY_COUNT  7

struct entry {
 	int key;
	char *name;
};
static const struct entry nids[ENTRY_COUNT] = {
	{NID_countryName, "countryName"},
	{NID_stateOrProvinceName, "stateOrProvinceName"},
	{NID_localityName, "localityName"},
	{NID_organizationName, "organiationName"},
	{NID_organizationalUnitName, "organizationalUnitName"},
	{NID_commonName, "commonName"},
	{NID_pkcs9_emailAddress, "emailAddress"},
};
static int  x509_get_subject(X509 *x)
{
	X509_NAME *xn = NULL;
	ASN1_STRING *d = NULL;
	
	int j = 0;
	int pos = -1;
	unsigned i = 0;
	xn = X509_get_subject_name(x);
	j = X509_NAME_entry_count(xn);
	
	for (i = 0; i < ENTRY_COUNT; i++) {
		pos = -1;
		for (;;) {
			pos = X509_NAME_get_index_by_NID(xn, nids[i].key, pos);
			if (pos >=0 && pos <= j) {   // pos != -1
				d = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(xn, pos));
				printf("%s = %s [%d]\n", nids[i].name, d->data, d->length);
			}
		}
	}
	return 0;
}
 
  
 
  
 
 

你可能感兴趣的:(openssl)