int gnutls_x509_crt_get_key_purpose_oid(gnutls_x509_crt_t cert,
    int indx, void *oid, size_t * sizeof_oid, unsigned int *critical)
{
    char counter[MAX_INT_DIGITS];
    char tmpstr[64];
    int result, len;
    gnutls_datum_t id;
    ASN1_TYPE c2 = ASN1_TYPE_EMPTY;

    if (cert == NULL) {
	gnutls_assert();
	return GNUTLS_E_INVALID_REQUEST;
    }

    if (oid)
	memset(oid, 0, *sizeof_oid);
    else
	*sizeof_oid = 0;

    if ((result =
	 _gnutls_x509_crt_get_extension(cert, "2.5.29.37", 0, &id,
					critical)) < 0) {
	return result;
    }

    if (id.size == 0 || id.data == NULL) {
	gnutls_assert();
	return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
    }

    result = asn1_create_element
	(_gnutls_get_pkix(), "PKIX1.ExtKeyUsageSyntax", &c2);
    if (result != ASN1_SUCCESS) {
	gnutls_assert();
	_gnutls_free_datum(&id);
	return _gnutls_asn2err(result);
    }

    result = asn1_der_decoding(&c2, id.data, id.size, NULL);
    _gnutls_free_datum(&id);

    if (result != ASN1_SUCCESS) {
	gnutls_assert();
	asn1_delete_structure(&c2);
	return _gnutls_asn2err(result);
    }

    indx++;
    /* create a string like "?1"
     */
    _gnutls_int2str(indx, counter);
    _gnutls_str_cpy(tmpstr, sizeof(tmpstr), "?");
    _gnutls_str_cat(tmpstr, sizeof(tmpstr), counter);

    len = *sizeof_oid;
    result = asn1_read_value(c2, tmpstr, oid, &len);

    *sizeof_oid = len;
    asn1_delete_structure(&c2);

    if (result == ASN1_VALUE_NOT_FOUND || result == ASN1_ELEMENT_NOT_FOUND) {
	return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
    }

    if (result != ASN1_SUCCESS) {
	gnutls_assert();
	return _gnutls_asn2err(result);
    }

    return 0;

}
int gnutls_x509_crt_get_key_purpose_oid(gnutls_x509_crt_t cert,
    int indx, void *oid, size_t * sizeof_oid, unsigned int *critical)
{
    char counter[MAX_INT_DIGITS];
    char tmpstr[64];
    int result, len;
    gnutls_datum_t id;
    ASN1_TYPE c2 = ASN1_TYPE_EMPTY;

    if (cert == NULL) {
	gnutls_assert();
	return GNUTLS_E_INVALID_REQUEST;
    }

    if (oid)
	memset(oid, 0, *sizeof_oid);
    else
	*sizeof_oid = 0;

    if ((result =
	 _gnutls_x509_crt_get_extension(cert, "2.5.29.37", 0, &id,
					critical)) < 0) {
	return result;
    }

    if (id.size == 0 || id.data == NULL) {
	gnutls_assert();
	return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
    }

    result = asn1_create_element
	(_gnutls_get_pkix(), "PKIX1.ExtKeyUsageSyntax", &c2);
    if (result != ASN1_SUCCESS) {
	gnutls_assert();
	_gnutls_free_datum(&id);
	return _gnutls_asn2err(result);
    }

    result = asn1_der_decoding(&c2, id.data, id.size, NULL);
    _gnutls_free_datum(&id);

    if (result != ASN1_SUCCESS) {
	gnutls_assert();
	asn1_delete_structure(&c2);
	return _gnutls_asn2err(result);
    }

    indx++;
     
    _gnutls_int2str(indx, counter);
    _gnutls_str_cpy(tmpstr, sizeof(tmpstr), "?");
    _gnutls_str_cat(tmpstr, sizeof(tmpstr), counter);

    len = *sizeof_oid;
    result = asn1_read_value(c2, tmpstr, oid, &len);

    *sizeof_oid = len;
    asn1_delete_structure(&c2);

    if (result == ASN1_VALUE_NOT_FOUND || result == ASN1_ELEMENT_NOT_FOUND) {
	return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
    }

    if (result != ASN1_SUCCESS) {
	gnutls_assert();
	return _gnutls_asn2err(result);
    }

    return 0;

}