/* Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its hex representation @@ (Contents of buffer are always kept in ASCII, also on EBCDIC machines)  */
char* hex_to_string(const unsigned char *buffer, long len)
/* Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its hex representation @@ (Contents of buffer are always kept in ASCII, also on EBCDIC machines)  */
char* hex_to_string(const unsigned char *buffer, long len)
{
    char *tmp, *q;
    const unsigned char *p;
    int i;
    static const char hexdig[] = "0123456789ABCDEF";
    if (!buffer || !len)
        return NULL;
    if (!(tmp = OPENSSL_malloc(len * 3 + 1))) {
        OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
        return NULL;
    }
    q = tmp;
    for (i = 0, p = buffer; i < len; i++, p++) {
        *q++ = hexdig[(*p >> 4) & 0xf];
        *q++ = hexdig[*p & 0xf];
        *q++ = ':';
    }
    q[-1] = 0;
    return tmp;
}