otr_readforge.c 3.55 KB
Newer Older
cypherpunk's avatar
cypherpunk committed
1 2
/*
 *  Off-the-Record Messaging Toolkit
Rob Smits's avatar
Rob Smits committed
3
 *  Copyright (C) 2004-2012  Ian Goldberg, Chris Alexander, Nikita Borisov
cypherpunk's avatar
cypherpunk committed
4 5 6 7 8 9 10 11 12 13 14 15 16
 *                           <otr@cypherpunks.ca>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of version 2 of the GNU General Public License as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
Rob Smits's avatar
Rob Smits committed
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cypherpunk's avatar
cypherpunk committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
 */

/* system headers */
#include <stdio.h>
#include <stdlib.h>

/* libotr headers */
#include "proto.h"

/* toolkit headers */
#include "readotr.h"
#include "parse.h"
#include "sesskeys.h"
#include "sha1hmac.h"
#include "ctrmode.h"

static void usage(const char *progname)
{
    fprintf(stderr, "Usage: %s aeskey [new_message]\n"
"Read an OTR Data Message from stdin.  Use the given AES key to\n"
"verify its MAC and decrypt the message to stdout.  If new_message\n"
"is given, output a new OTR Data Message with the same fields as the\n"
"original, but with the message replaced by new_message\n", progname);
    exit(1);
}

int main(int argc, char **argv)
{
    unsigned char *aeskey;
    unsigned char mackey[20];
    unsigned char macval[20];
    size_t aeskeylen;
    unsigned char *plaintext, *ciphertext;
    char *otrmsg = NULL;
    DataMsg datamsg;

    if (argc != 2 && argc != 3) {
	usage(argv[0]);
    }

    argv_to_buf(&aeskey, &aeskeylen, argv[1]);
    if (!aeskey) {
	usage(argv[0]);
    }
Rob Smits's avatar
Rob Smits committed
62

cypherpunk's avatar
cypherpunk committed
63 64 65 66 67 68 69 70 71 72
    if (aeskeylen != 16) {
	fprintf(stderr, "The AES key must be 32 hex chars long.\n");
	usage(argv[0]);
    }

    otrmsg = readotr(stdin);
    if (otrmsg == NULL) {
	fprintf(stderr, "No OTR Data Message found on stdin.\n");
	exit(1);
    }
Rob Smits's avatar
Rob Smits committed
73

74
    if (otrl_proto_message_type(otrmsg) != OTRL_MSGTYPE_DATA) {
cypherpunk's avatar
cypherpunk committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	fprintf(stderr, "OTR Non-Data Message found on stdin.\n");
	exit(1);
    }

    datamsg = parse_datamsg(otrmsg);
    free(otrmsg);
    if (datamsg == NULL) {
	fprintf(stderr, "Invalid OTR Data Message found on stdin.\n");
	exit(1);
    }

    /* Create the MAC key */
    sesskeys_make_mac(mackey, aeskey);

    /* Check the MAC */
    sha1hmac(macval, mackey, datamsg->macstart,
	    datamsg->macend - datamsg->macstart);
    if (memcmp(macval, datamsg->mac, 20)) {
	fprintf(stderr, "MAC does not verify: wrong AES key?\n");
    } else {
	/* Decrypt the message */
	plaintext = malloc(datamsg->encmsglen+1);
	if (!plaintext) {
	    fprintf(stderr, "Out of memory!\n");
	    exit(1);
	}
	aes_ctr_crypt(plaintext, datamsg->encmsg, datamsg->encmsglen,
		aeskey, datamsg->ctr);
	plaintext[datamsg->encmsglen] = '\0';
	printf("Plaintext: ``%s''\n", plaintext);
	free(plaintext);
    }

    /* Do we want to forge a message? */
    if (argv[2] != NULL) {
	char *newdatamsg;
	size_t newlen = strlen(argv[2]);
	ciphertext = malloc(newlen);
	if (!ciphertext && newlen > 0) {
	    fprintf(stderr, "Out of memory!\n");
	    exit(1);
	}
cypherpunk's avatar
cypherpunk committed
117 118
	aes_ctr_crypt(ciphertext, (const unsigned char *)argv[2], newlen,
		aeskey, datamsg->ctr);
cypherpunk's avatar
cypherpunk committed
119 120 121
	free(datamsg->encmsg);
	datamsg->encmsg = ciphertext;
	datamsg->encmsglen = newlen;
Rob Smits's avatar
Rob Smits committed
122

cypherpunk's avatar
cypherpunk committed
123 124 125 126 127 128 129 130 131 132 133
	newdatamsg = remac_datamsg(datamsg, mackey);

	printf("%s\n", newdatamsg);
	free(newdatamsg);
    }

    free_datamsg(datamsg);
    free(aeskey);
    fflush(stdout);
    return 0;
}