Beispiel #1
0
bool
UTL_IdList::is_absolute ()
{
  return !ACE_OS::strcmp (first_component ()->get_string (), "::");
}
eap_status_e asn1_der_type_c::encode_oid_from_string(eap_const_string oid, const u32_t oid_length, eap_variable_data_c * const buffer) const
{
	if (oid == 0
		|| buffer == 0
		|| buffer->get_is_valid() == false)
	{
		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
	}

	eap_status_e status(eap_status_process_general_error);

	const eap_char * oid_char = oid;
	const eap_char * end_char = &oid[oid_length];
	u32_t remaining_length(oid_length);
	u32_t first_component(0ul);
	u32_t component_index(0ul);

	status = buffer->set_data_length(0ul);
	if (status != eap_status_ok)
	{
		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
		return EAP_STATUS_RETURN(m_am_tools, status);
	}

	while(oid_char < end_char)
	{
		// Search next dot (.).
		const eap_char * dot  = reinterpret_cast<const eap_char *>(m_am_tools->memchr(oid_char, '.', remaining_length));
		if (dot == 0)
		{
			// The last component.
			dot  = reinterpret_cast<const eap_char *>(oid_char + remaining_length);
			if (dot == 0
				|| dot != end_char)
			{
				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
				return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
			}
		}

		u32_t integer(0ul);

		status = m_am_tools->number_string_to_u32(
			reinterpret_cast<const u8_t *>(oid_char),
			dot - oid_char,
			&integer);
		if (status != eap_status_ok)
		{
			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
			return EAP_STATUS_RETURN(m_am_tools, status);
		}

		if (component_index == 0ul)
		{
			// The first component is encoded with the second component.
			first_component = integer;
		}
		else if (component_index == 1ul)
		{
			if (first_component < 2ul
				&& integer > 39ul)
			{
				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
				return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
			}
			else if (first_component > 2ul)
			{
				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
				return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
			}

			const u32_t oid_value = first_component * 40ul + integer;
			if (oid_value > 0xff)
			{
				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
				return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
			}

			const u8_t oid_octet(static_cast<u8_t>(oid_value));

			status = buffer->add_data(&oid_octet, sizeof(oid_octet));
			if (status != eap_status_ok)
			{
				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
				return EAP_STATUS_RETURN(m_am_tools, status);
			}
		}
		else
		{
			eap_variable_data_c encoded_data(m_am_tools);
			if (encoded_data.get_is_valid() == false)
			{
				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
				return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
			}

			const u32_t ENCODE_BASE = 128ul;

			while(integer > 0ul)
			{
				const u8_t oid_octet = static_cast<u8_t>(integer % ENCODE_BASE);

				// Encodes the octets to reverse order.
				status = encoded_data.add_data(&oid_octet, sizeof(oid_octet));
				if (status != eap_status_ok)
				{
					EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
					return EAP_STATUS_RETURN(m_am_tools, status);
				}

				integer = integer / ENCODE_BASE;
			} // while()

			for (u32_t ind = encoded_data.get_data_length(); ind > 0ul; --ind)
			{
				// reads the octets on reverse order.
				u8_t * oid_octet = encoded_data.get_data_offset(ind-1ul, sizeof(u8_t));
				if (oid_octet == 0)
				{
					EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
					return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
				}

				if (ind > 1ul)
				{
					// All but the last octet have high bit set.
					*oid_octet |= static_cast<u8_t>(asn1_high_bit_mask_tag);
				}

				status = buffer->add_data(oid_octet, sizeof(*oid_octet));
				if (status != eap_status_ok)
				{
					EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
					return EAP_STATUS_RETURN(m_am_tools, status);
				}
			} // for()
		}

		remaining_length -= (dot - oid_char) + 1ul;

		oid_char = dot+1ul;

		++component_index;

	} // while()

	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
	return EAP_STATUS_RETURN(m_am_tools, status);
}