Ejemplo n.º 1
0
stringer_t * imap_build_array_isliteral(placer_t data) {

	size_t length;
	chr_t *holder, buffer[32];

	if (pl_empty(data)) {
		return NULL;
	}

	length = pl_length_get(data);
	holder = pl_data_get(data);

	// Look for illegal characters.
	for (size_t i = 0; i < length; i++) {
		if ((*holder >= '*' && *holder <= '~') || *holder == ' ' || *holder == '_'	|| *holder == '!' || (*holder >= '#' && *holder <= '&')) {
			holder++;
		}
		else {
			snprintf(buffer, 32, "{%zu}\r\n", length);
			return st_merge("ns", buffer, &data);

		}
	}

	return NULL;
}
Ejemplo n.º 2
0
EC_KEY * deprecated_ecies_key_private(uint64_t format, placer_t data) {

	EC_KEY *key = NULL;
	BIGNUM *number = NULL;

	if (!(key = deprecated_ecies_key_alloc())) {
		log_info("Unable to allocate an empty key context.");
		return NULL;
	}

	// Process a key in binary format.
	if (format & ECIES_PRIVATE_BINARY) {

		if (!(number = BN_bin2bn_d(pl_data_get(data), pl_length_get(data), NULL))) {
			log_info("An error occurred while parsing the binary elliptical curve point data used to represent the private key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
			EC_KEY_free_d(key);
			return NULL;
		}

	}
	// Process a key in hex.
	else if (format & ECIES_PRIVATE_HEX) {

		if (!(BN_hex2bn_d(&number, pl_char_get(data)))) {
			log_info("An error occurred while parsing the binary elliptical curve point data used to represent the private key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
			EC_KEY_free_d(key);
			return NULL;
		}

	}
	// Invalid format!
	else {
		log_info("The private key data is using an unrecognized format.");
		EC_KEY_free_d(key);
		return NULL;
	}

	// Assign the point to our empty key instance.
	if (EC_KEY_set_private_key_d(key, number) != 1) {
		log_info("The provided point data does not represent a valid public key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_KEY_free_d(key);
		BN_free_d(number);
		return NULL;
	}

	// The above function call duplicates the point so the local copy is no longer needed.
	BN_free_d(number);

	return key;
}
Ejemplo n.º 3
0
EC_KEY * deprecated_ecies_key_public(uint64_t format, placer_t data) {

	EC_KEY *key = NULL;
	EC_POINT *point = NULL;

	if (!(key = deprecated_ecies_key_alloc())) {
		log_info("Unable to allocate an empty key context.");
		return NULL;
	}

	// Process a key in binary format.
	if (format & ECIES_PUBLIC_BINARY) {

		// Generate the empty point context we'll be assigning to below.
		if (!(point = EC_POINT_new_d(EC_KEY_get0_group_d(key)))) {
			log_info("An error occurred while allocate the elliptical curve point. {%s}", ssl_error_string(MEMORYBUF(256), 256));
			EC_KEY_free_d(key);
			return NULL;
		}
		else if (EC_POINT_oct2point_d(EC_KEY_get0_group_d(key), point, pl_data_get(data), pl_length_get(data), NULL) != 1) {
			log_info("An error occurred while parsing the binary elliptical curve point data used to represent the public key. {%s}",
					ssl_error_string(MEMORYBUF(256), 256));
			EC_POINT_free_d(point);
			EC_KEY_free_d(key);
			return NULL;
		}

	}
	// Process a key in hex.
	else if (format & ECIES_PUBLIC_HEX) {

		if (!(point = EC_POINT_hex2point_d(EC_KEY_get0_group_d(key), pl_char_get(data), NULL, NULL))) {
			log_info("An error occurred while parsing the binary elliptical curve point data used to represent the public key. {%s}",
					ssl_error_string(MEMORYBUF(256), 256));
			EC_KEY_free_d(key);
			return NULL;
		}

	}
	// Invalid format!
	else {
		log_info("The public key data is using an unrecognized format.");
		EC_POINT_free_d(point);
		EC_KEY_free_d(key);
		return NULL;
	}

	// Assign the point to our empty key instance.
	if (EC_KEY_set_public_key_d(key, point) != 1) {
		log_info("The provided point data does not represent a valid public key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_POINT_free_d(point);
		EC_KEY_free_d(key);
		return NULL;
	}

	// The above function call duplicates the point so the local copy is no longer needed.
	EC_POINT_free_d(point);

	// Ensures the provided point is along the active elliptical curve and therefore represents a valid public key.
	if (EC_KEY_check_key_d(key) != 1) {
		log_info("The provided point data does not represent a valid public key. {%s}", ssl_error_string(MEMORYBUF(256), 256));
		EC_KEY_free_d(key);
		return NULL;
	}

	return key;
}
Ejemplo n.º 4
0
/**
 * @brief	Retrieve a specified token from a placer.
 * @see		token_get_ns()
 * @param	string		a pointer to the placer to be tokenized.
 * @param	token		the token character that will be used to split the placer.
 * @param	fragment	the zero-indexed token number to be extracted from the placer.
 * @param	value		a pointer to a placer that will receive the value of the extracted token on success, or pl_null() on failure.
 * @return	-1 on failure, 0 on success, or 1 if the token was extracted successfully, but was the last one found in the placer.
 */
int tok_get_pl(placer_t string, char token, uint64_t fragment, placer_t *value) {

	return tok_get_bl(pl_data_get(string), pl_length_get(string), token, fragment, value);
}
Ejemplo n.º 5
0
Archivo: orgs.c Proyecto: lavabit/magma
prime_org_signet_t * org_signet_set(stringer_t *org) {

	prime_field_t *field = NULL;
	prime_object_t *object = NULL;
	prime_org_signet_t *result = NULL;

	if (!(object = prime_unpack(org))) {
		log_pedantic("Unable to parse the PRIME organizational signet.");
		return NULL;
	}
	else if (object->type != PRIME_ORG_SIGNET) {
		log_pedantic("The object passed in was not an organizational signet.");
		prime_object_free(object);
		return NULL;
	}

	else if (!(result = org_signet_alloc())) {
		log_pedantic("Unable to allocate a PRIME organizational signet.");
		prime_object_free(object);
		return NULL;
	}

	// Public signing key, verify the length and import the ed25519 public key.
	else if (!(field = prime_field_get(object, 1)) || st_length_get(&(field->payload)) != 32 ||
		!(result->signing = ed25519_public_set(&(field->payload)))) {
		log_pedantic("Unable to parse the PRIME organizational signing key.");
		prime_object_free(object);
		org_signet_free(result);
		return NULL;
	}

	// Public encryption key, verify the length and import the compressed secp256k1 public key.
	else if (!(field = prime_field_get(object, 3)) || st_length_get(&(field->payload)) != 33 ||
		!(result->encryption = secp256k1_public_set(&(field->payload)))) {
		log_pedantic("Unable to parse the PRIME organizational encryption key.");
		prime_object_free(object);
		org_signet_free(result);
		return NULL;
	}

	// Self signature taken over the cryptographic fields, verify the length and import the ed25519 signature.
	else if (!(field = prime_field_get(object, 4)) || st_length_get(&(field->payload)) != 64 ||
		!(result->signature = st_import(pl_data_get(field->payload), pl_length_get(field->payload)))) {
		log_pedantic("Unable to parse the PRIME organizational signet signature.");
		prime_object_free(object);
		org_signet_free(result);
		return NULL;
	}

	// We don't need the packed object context any more.
	prime_object_free(object);

	// Verify the signature.
	if (!org_signet_verify(result)) {
		log_pedantic("The PRIME organizational signet signature is invalid.");
		org_signet_free(result);
		return NULL;
	}

	return result;
}
Ejemplo n.º 6
0
stringer_t * imap_parse_address_part(placer_t input) {

	chr_t *stream;
	int_t mode = 0;
	placer_t box, dom;
	stringer_t *address = NULL, *name = NULL, *output = NULL;
	size_t zero_len = 0, one_len = 0, two_len = 0, length;

	length = pl_length_get(input);
	stream = pl_data_get(input);

	while (length > 0) {

		if (mode == 0 && *stream == '\"') {
			mode = 1;
		}
		else if (mode == 0 && *stream == '<') {
			mode = 2;
		}

		else if (mode == 1 && *stream == '\"') {
			mode = 0;
		}
		else if (mode == 2 && *stream == '>') {
			mode = 0;
		}

		else if (mode == 0 && *stream != ' ') {
			zero_len++;
		}
		else if (mode == 1) {
			one_len++;
		}
		else if (mode == 2) {
			two_len++;
		}

		length--;
		stream++;
	}

	/*if (two_len != 0) {
		if ((address = st_alloc(two_len)) == NULL) {
			return NULL;
		}
	}
	if (one_len != 0) {
		if ((name = st_alloc(one_len + zero_len)) == NULL) {
			return NULL;
		}
	}
	if (zero_len != 0 && two_len == 0 && one_len == 0) {
		if ((address = st_alloc(zero_len)) == NULL) {
			return NULL;
		}
	}*/

	if ((address = st_alloc(pl_length_get(input))) == NULL) {
		return NULL;
	}
	else if ((name = st_alloc(pl_length_get(input))) == NULL) {
		if (address) st_free(address);
		return NULL;

	}

	mode = 0;
	length = pl_length_get(input);
	stream = pl_data_get(input);

	while (length > 0) {

		if (mode == 0 && *stream == '\"') {
			mode = 1;
		}
		else if (mode == 0 && *stream == '<') {
			mode = 2;
		}

		else if (mode == 1 && *stream == '\"') {
			mode = 0;
		}
		else if (mode == 2 && *stream == '>') {
			mode = 0;
		}

		// No quoted part, no <> part.
		else if ((mode == 0 && one_len == 0 && two_len == 0) ||
			// If there is a quoted part, but no <>, use the quoted part for name, and the <> for address, and ignore everything outside that.
			(mode == 0 && one_len != 0 && two_len == 0) ||
			// Or the data is inside the <>.
			mode == 2) {

			// Email addresses shouldn't have spaces.
			if (*stream != ' ')
				imap_parse_address_put(address, *stream);
		}
		else if (mode == 0 || mode == 1) {
			// Keeps any leading spaces from being added.
			if (st_length_get(name) != 0 || (st_length_get(name) == 0 && *stream != ' '))
				imap_parse_address_put(name, *stream);
		}

		length--;
		stream++;
	}

	// A simple trimmer.
	while (st_length_get(name) != 0 && *(st_char_get(name) + st_length_get(name) - 1) == ' ') {
		st_length_set(name, st_length_get(name) - 1);
	}

	// In situations where a name couldn't be parsed, reset the pointer to NULL so that "NIL" is returned.
	if (st_length_get(name) == 0) {
		st_free(name);
		name = NULL;
	}

	tok_get_st(address, '@', 0, &box);
	tok_get_st(address, '@', 1, &dom);
	output = imap_build_array("snss", name, NULL, &box, &dom);
	if (name != NULL) {
		st_free(name);
	}
	if (address != NULL) {
		st_free(address);
	}

	return output;
}