Ejemplo n.º 1
0
/**
 * Write an EC domain parameter identifier as defined in RFC 5656
 */
static void write_ec_identifier(bio_writer_t *writer, char *prefix, int oid,
								chunk_t enc)
{
	char *curve, identifier[128];

	switch (oid)
	{
		case OID_PRIME256V1:
			curve = strdup("nistp256");
			break;
		case OID_SECT384R1:
			curve = strdup("nistp384");
			break;
		case OID_SECT521R1:
			curve = strdup("nistp521");
			break;
		default:
			curve = asn1_oid_to_string(enc);
			break;
	}
	if (curve && snprintf(identifier, sizeof(identifier), "%s%s", prefix,
						  curve) < sizeof(identifier))
	{
		writer->write_data32(writer, chunk_from_str(identifier));
	}
	free(curve);
}
Ejemplo n.º 2
0
END_TEST

/*******************************************************************************
 * oid_to_string
 */

START_TEST(test_asn1_oid_to_string)
{
	typedef struct {
		char *string;
		chunk_t oid;
	} testdata_t;

	testdata_t test[] = {
		{  NULL,  chunk_empty },
		{ "0.2.262.1", chunk_from_chars(
			0x02, 0x82, 0x06, 0x01) },
		{ "1.2.840.10045.4.1", chunk_from_chars(
			0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
		{ "1.3.6.1.4.1.36906.1", chunk_from_chars(
			0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa0, 0x2a, 0x01) },
		{ "2.16.840.1.101.3.4.2.1", chunk_from_chars(
			0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01) },
		{ "0.10.100.1000.10000.100000.1000000.10000000.100000000.268435455",
			chunk_from_chars( 0x0a, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d,
			0x20, 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00,
			0xaf, 0xd7, 0xc2, 0x00, 0xff, 0xff, 0xff, 0x7f) },
		{ NULL, chunk_from_chars(
			0x0a, 0x02, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d, 0x20,
			0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00, 0xaf, 0xd7, 0xc2, 0x00,
		    0xff, 0xff, 0xff, 0x7f) },
		{ NULL, chunk_from_chars(0x0a, 0x87) }
	};

	int i;
	char *string = NULL;

	for (i = 0; i < countof(test); i++)
	{
		string = asn1_oid_to_string(test[i].oid);
		if (test[i].string == NULL)
		{
			ck_assert(string == NULL);
		}
		else
		{
			ck_assert(streq(string, test[i].string));
			free(string);
		}
	}
}
Ejemplo n.º 3
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;
}