ASN1_INTEGER to ASN1_STRING

The ascii hex conversion be done more simply using the built in BN_bn2hex(BIGNUM *) function

ASN1_INTEGER *serial = X509_get_serialNumber(certificateX509);
BIGNUM *bnser = ASN1_INTEGER_to_BN(serial, NULL);
char *asciiHex = BN_bn2hex(bnser);

One possibility is that you can extract the value of the ASN1_INTEGER as a normal C integer:

#include <openssl/asn1.h>
#include <stdio.h>

int main(int argc, char** argv) {
  long value;
  ASN1_INTEGER asn1int = {0};

  ASN1_INTEGER_set(&asn1int, 42);
  value = ASN1_INTEGER_get(&asn1int);
  printf("The value is %ld.\n", value);

  return 0;
}

Compiled like this:

gcc -Wall -o sploots sploots.c -lcrypto

this produces the output:

The value is 42.

To have the value as a string in an array of char, use snprintf.

I suspect there are also possibilities for using the BIO printing routines to dump the value to a BIO of some sort (perhaps a memory BIO). However, this approach seems simpler.

The way I arrived at this answer is that I looked through the OpenSSL headers for ASN1_INTEGER. After looking around for suitable APIs for a BIO-based solution, I noticed the ASN1_INTEGER_get function.

Looking around in OpenSSL header files is typically the way I learn how to use OpenSSL, since so much of the API is undocumented or incorrectly or incompletely documented.


I finally came to a solution, which may not be the most straightforward one:

 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 needs to be read as hex integer in order to retrieve logical values. I use NSMutableString to create a human 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]];
}

Tags:

C

Openssl

X509