Esempio n. 1
0
/**
 * Get the subject name (or the issuer) of a certificate.
 */
int asn1_name(const uint8_t *cert, int *offset, char *dn[])
{
    int ret = X509_NOT_OK;
    int dn_type;
    char *tmp;

    if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0)
        goto end_name;

    while (asn1_next_obj(cert, offset, ASN1_SET) >= 0)
    {
        int i, found = 0;

        if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 ||
               (dn_type = asn1_get_oid_x520(cert, offset)) < 0)
            goto end_name;

        tmp = NULL;

        if (asn1_get_printable_str(cert, offset, &tmp) < 0)
        {
            free(tmp);
            goto end_name;
        }

        /* find the distinguished named type */
        for (i = 0; i < X509_NUM_DN_TYPES; i++)
        {
            if (dn_type == g_dn_types[i])
            {
                if (dn[i] == NULL)
                {
                    dn[i] = tmp;
                    found = 1;
                    break;
                }
            }
        }

        if (found == 0) /* not found so get rid of it */
        {
            free(tmp);
        }
    }

    ret = X509_OK;
end_name:
    return ret;
}
Esempio n. 2
0
/**
 * Get the subject name (or the issuer) of a certificate.
 */
int asn1_name(const uint8_t *cert, int *offset, char *dn[])
{
    int ret = X509_NOT_OK;
    int dn_type = 0;
    char *name = NULL;
    char* name_prefix = NULL; /* GBG */
    
    if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0)
        goto end_name;

    while (asn1_next_obj(cert, offset, ASN1_SET) >= 0)
    {
        int i, found = 0;

        if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0) {  /* GBG */
            goto end_name;
        }
        /* get the oid */
        {
            int len = asn1_next_obj(cert, offset, ASN1_OID);
            int oid_offset = *offset;
            
            if (len < 0) goto end_name;
            if (len == 3 && cert[oid_offset] == 0x55 && cert[oid_offset+1] == 0x04) {
                dn_type = cert[oid_offset+2];
            } else {
                /* convert the OID to a string */
                name_prefix = asn1_oid_to_string(cert+oid_offset, len);
                if (name_prefix == NULL) goto end_name;
            }
            *offset += len;
        }
        if (asn1_get_printable_str(cert, offset, &name) < 0) {
            free(name);
            if (name_prefix) free(name_prefix);
            goto end_name;
        }
        /* add the prefix if there is one */
        if (name_prefix) {
            int name_prefix_len = (int)strlen(name_prefix);
            int name_len        = (int)strlen(name);
            char* compound = malloc(name_prefix_len+name_len+2);
            memcpy(compound, name_prefix, name_prefix_len);
            compound[name_prefix_len] = '=';
            memcpy(compound+name_prefix_len+1, name, name_len+1);
            free(name);
            free(name_prefix);
            name = compound;
            name_prefix = NULL;
        }

        /* find the distinguished named type */
        for (i = 0; i < X509_NUM_DN_TYPES; i++)
        {
            if (dn_type == g_dn_types[i])
            {
                if (dn[i] == NULL)
                {
                    dn[i] = name;
                    found = 1;
                    break;
                }
            }
        }

        if (found == 0) /* not found so get rid of it */
        {
            free(name);
        }
    }

    ret = X509_OK;
end_name:
    return ret;
}