Example #1
0
int main (int argc, char **argv) {
	if ((argc != 1) && (argc != 2)) {
		fprintf(stderr, "ERROR: Invalid arguments!\n");
		fprintf(stderr, "Usage: %s [output_file]\n", argv[0]);
		return EXIT_FAILURE;
	}

	FILE *output_file = NULL;
	if ((argc == 2) && (argv[1] != NULL)) {
		output_file = fopen(argv[1], "w");
		if (output_file == NULL) {
			fprintf(stderr, "ERROR: Failed to open file '%s'\n", argv[1]);;
			return EXIT_FAILURE;
		}
	}

	/* a bunch of json: */
	char *json[] = {
		"{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\", \n\"format\": {\"type\":       \"rect\", \n\"width\":      1920, \n\"height\":     1080, \n\"interlace\":  false,\"frame rate\": 24\n}\n}",

		"[\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"]",
		"[\n    [0, -1, 0],\n    [1, 0, 0],\n    [0, 0, 1]\n	]\n",
		"{\n		\"Image\": {\n			\"Width\":  800,\n			\"Height\": 600,\n			\"Title\":  \"View from 15th Floor\",\n			\"Thumbnail\": {\n				\"Url\":    \"http:/*www.example.com/image/481989943\",\n				\"Height\": 125,\n				\"Width\":  \"100\"\n			},\n			\"IDs\": [116, 943, 234, 38793]\n		}\n	}",
	"[\n	 {\n	 \"precision\": \"zip\",\n	 \"Latitude\":  37.7668,\n	 \"Longitude\": -122.3959,\n	 \"Address\":   \"\",\n	 \"City\":      \"SAN FRANCISCO\",\n	 \"State\":     \"CA\",\n	 \"Zip\":       \"94107\",\n	 \"Country\":   \"US\"\n	 },\n	 {\n	 \"precision\": \"zip\",\n	 \"Latitude\":  37.371991,\n	 \"Longitude\": -122.026020,\n	 \"Address\":   \"\",\n	 \"City\":      \"SUNNYVALE\",\n	 \"State\":     \"CA\",\n	 \"Zip\":       \"94085\",\n	 \"Country\":   \"US\"\n	 }\n	 ]"
	};

	/* Process each json textblock by parsing, then rebuilding: */
	for (size_t i = 0; i < (sizeof(json) / sizeof(char*)); i++) {
		buffer_create_with_existing_array(json_buffer, (unsigned char*)json[i], strlen(json[i]) + 1);
		int status = doit(json_buffer, output_file);
		if (status == 0) {
			fprintf(stderr, "ERROR: Failed on text %zu!\n", i);
			if (output_file != NULL) {
				fclose(output_file);
			}
			return EXIT_FAILURE;
		}
	}

	/* Now some samplecode for building objects concisely: */
	if (create_objects(output_file) != 0) {
		if (output_file != NULL) {
			fclose(output_file);
		}
		return EXIT_FAILURE;
	}

	if (output_file != NULL) {
		fclose(output_file);
	}

	return EXIT_SUCCESS;
}
Example #2
0
/*
 * Create message and header keys, encrypt header and message
 * and print them.
 */
int create_and_print_message(
		buffer_t * const packet, //needs to be 3 + crypto_aead_chacha20poly1305_NPUBBYTES + crypto_aead_chacha20poly1305_ABYTES + crypto_secretbox_NONCEBYTES + message_length + header_length + crypto_secretbox_MACBYTES + 255
		const unsigned char packet_type,
		const unsigned char current_protocol_version,
		const unsigned char highest_supported_protocol_version,
		const buffer_t * const message,
		buffer_t * const message_key, //output, crypto_secretbox_KEYBYTES
		const buffer_t * const header,
		buffer_t * const header_key) { //output, crypto_aead_chacha20poly1305_KEYBYTES
	int status;
	//create header key
	status = buffer_fill_random(header_key, crypto_aead_chacha20poly1305_KEYBYTES);
	if (status != 0) {
		buffer_clear(header_key);
		return status;
	}
	printf("Header key (%zi Bytes):\n", header_key->content_length);
	print_hex(header_key);
	putchar('\n');

	//create message key
	status = buffer_fill_random(message_key, crypto_secretbox_KEYBYTES);
	if (status != 0) {
		buffer_clear(header_key);
		buffer_clear(message_key);
		return status;
	}
	printf("Message key (%zi Bytes):\n", message_key->content_length);
	print_hex(message_key);
	putchar('\n');

	//print the header (as hex):
	printf("Header (%zi Bytes):\n", header->content_length);
	print_hex(header);
	putchar('\n');

	//print the message (as string):
	printf("Message (%zi Bytes):\n%.*s\n\n", message->content_length, (int)message->content_length, message->content);

	//now encrypt the message
	status = packet_encrypt(
			packet,
			packet_type,
			current_protocol_version,
			highest_supported_protocol_version,
			header,
			header_key,
			message,
			message_key);
	if (status != 0) {
		fprintf(stderr, "ERROR: Failed to encrypt message and header. (%i)\n", status);
		return status;
	}

	//print header nonce
	buffer_t *header_nonce = buffer_create_with_existing_array(packet->content + 3, crypto_aead_chacha20poly1305_NPUBBYTES);
	printf("Header Nonce (%zi Bytes):\n", header_nonce->content_length);
	print_hex(header_nonce);
	putchar('\n');

	//print encrypted packet
	printf("Encrypted Packet (%zi Bytes):\n", packet->content_length);
	print_hex(packet);
	putchar('\n');

	return 0;
}
Example #3
0
int main(int argc, char *args[]) {
    bool recreate = false;
    if (argc == 2) {
        if (strcmp(args[1], "--recreate") == 0) {
            recreate = true;
        }
    }
    /* don't initialize libsodium here */
    buffer_t *user_id = buffer_create_on_heap(PUBLIC_MASTER_KEY_SIZE, PUBLIC_MASTER_KEY_SIZE);
    buffer_t *backup_file = NULL; //backup to import from
    buffer_t *backup_key_file = NULL;

    unsigned char *backup = NULL;
    unsigned char *prekey_list = NULL;
    unsigned char *backup_key = malloc(BACKUP_KEY_SIZE);

    return_status status = return_status_init();

    if (sodium_init() != 0) {
        throw(INIT_ERROR, "Failed to initialize libsodium.");
    }

    if (!recreate) {
        //load the backup from a file
        status = read_file(&backup_file, "test-data/molch-init.backup");
        throw_on_error(DATA_FETCH_ERROR, "Failed to read backup from a file.");

        //load the backup key from a file
        status = read_file(&backup_key_file, "test-data/molch-init-backup.key");
        throw_on_error(DATA_FETCH_ERROR, "Failed to read backup key from a file.");
        if (backup_key_file->content_length != BACKUP_KEY_SIZE) {
            throw(INCORRECT_BUFFER_SIZE, "Backup key from file has an incorrect length.");
        }

        //try to import the backup
        status = molch_import(
                     backup_key,
                     BACKUP_KEY_SIZE,
                     backup_file->content,
                     backup_file->content_length,
                     backup_key_file->content,
                     backup_key_file->content_length);
        throw_on_error(IMPORT_ERROR, "Failed to import backup from backup.");

        //destroy again
        molch_destroy_all_users();
    }

    //create a new user
    size_t backup_length;
    size_t prekey_list_length;
    status = molch_create_user(
                 user_id->content,
                 user_id->content_length,
                 &prekey_list,
                 &prekey_list_length,
                 backup_key,
                 BACKUP_KEY_SIZE,
                 &backup,
                 &backup_length,
                 (unsigned char*)"random",
                 sizeof("random"));
    throw_on_error(CREATION_ERROR, "Failed to create user.");
    if (backup == NULL) {
        throw(EXPORT_ERROR, "Failed to export backup.");
    }

    //print the backup to a file
    buffer_create_with_existing_array(backup_buffer, backup, backup_length);
    buffer_create_with_existing_array(backup_key_buffer, backup_key, BACKUP_KEY_SIZE);
    print_to_file(backup_buffer, "molch-init.backup");
    print_to_file(backup_key_buffer, "molch-init-backup.key");

cleanup:
    buffer_destroy_from_heap_and_null_if_valid(user_id);
    free_and_null_if_valid(backup);
    free_and_null_if_valid(prekey_list);
    buffer_destroy_from_heap_and_null_if_valid(backup_file);
    buffer_destroy_from_heap_and_null_if_valid(backup_key_file);

    free_and_null_if_valid(backup_key);

    on_error {
        print_errors(&status);
        printf("NOTE: Did you change the backup format and forgot to create new molch-init.backup and molch-init-backup.key files?\n To recreate them run with --recreate. Then just copy them to the appropriate place.\n");
    }
    return_status_destroy_errors(&status);

    return status.status;
}