I am using openssl to get x509 certificate information. Is there a way to convert ASN1_INTEGER to ASN1_STRING, which can be easily converted to a char array? Is there a way to convert it to any other human readable format?
EDIT: I am using openssl compiled for iOS since I have an iOS project. Here is the code I use to extract the serial number from the certificate:
ASN1_INTEGER *serial = X509_get_serialNumber(certificateX509); long value = ASN1_INTEGER_get(serial); NSLog(@"Serial %ld", value);
certificateX509 is a valid X509 object, and I managed to get other fields from it (issuer name, expiration date, etc.)
EDIT 2:
I finally came up with a solution that might not be the easiest:
ASN1_INTEGER *serial = X509_get_serialNumber(certificateX509); BIGNUM *bnser = ASN1_INTEGER_to_BN(serial, NULL); int n = BN_num_bytes(bnser); unsigned char outbuf[n]; int bin = BN_bn2bin(bnser, outbuf); char *hexbuf = (char*) outbuf;
hexBuf then contains characters whose value must be read as a hexadecimal integer to get boolean values. I use NSMutableString to create a user-readable string:
NSMutableString *str = [[NSMutableString alloc] init]; for (int i=0; i<n; i++) { NSString *temp = [NSString stringWithFormat:@"%.6x", hexbuf[i]]; [str appendString:[NSString stringWithFormat:@"%@ ", temp]]; }
If there is an easier way, I would really like to know.
Maggie
source share