1 /* 2 * Off-the-Record Messaging Toolkit 3 * Copyright (C) 2004-2012 Ian Goldberg, Rob Smits, Chris Alexander, 4 * Nikita Borisov 5 * <otr@cypherpunks.ca> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of version 2 of the GNU General Public License as 9 * published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 #ifndef __PARSE_H__ 22 #define __PARSE_H__ 23 24 #include <gcrypt.h> 25 26 typedef struct s_KeyExchMsg { 27 unsigned char *raw; /* The base64-decoded data; must be free()d */ 28 unsigned char reply; 29 gcry_mpi_t p, q, g, e; 30 unsigned int keyid; 31 gcry_mpi_t y; 32 gcry_mpi_t r, s; 33 unsigned char *sigstart; /* Pointers into the "raw" array. Don't */ 34 unsigned char *sigend; /* free() these. */ 35 } * KeyExchMsg; 36 37 typedef struct s_DataMsg { 38 unsigned char *raw; /* The base64-decoded data; must be free()d */ 39 size_t rawlen; 40 int flags; 41 unsigned char version; 42 unsigned int sender_instance; 43 unsigned int receiver_instance; 44 unsigned int sender_keyid; 45 unsigned int rcpt_keyid; 46 gcry_mpi_t y; 47 unsigned char ctr[8]; 48 unsigned char *encmsg; /* A copy; must be free()d */ 49 size_t encmsglen; 50 unsigned char mac[20]; 51 unsigned char *mackeys; /* A copy; must be free()d */ 52 size_t mackeyslen; 53 unsigned char *macstart; /* Pointers into the "raw" array. Don't */ 54 unsigned char *macend; /* free() these. */ 55 } * DataMsg; 56 57 typedef struct s_CommitMsg { 58 unsigned char *raw; /* The base64-decoded data; must be free()d */ 59 unsigned char version; 60 unsigned int sender_instance; 61 unsigned int receiver_instance; 62 unsigned char *enckey; 63 size_t enckeylen; 64 unsigned char *hashkey; 65 size_t hashkeylen; 66 } * CommitMsg; 67 68 typedef struct s_KeyMsg { 69 unsigned char *raw; /* The base64-decoded data; must be free()d */ 70 unsigned char version; 71 unsigned int sender_instance; 72 unsigned int receiver_instance; 73 gcry_mpi_t y; 74 } * KeyMsg; 75 76 typedef struct s_RevealSigMsg { 77 unsigned char *raw; /* The base64-decoded data; must be free()d */ 78 unsigned char version; 79 unsigned int sender_instance; 80 unsigned int receiver_instance; 81 unsigned char *key; 82 size_t keylen; 83 unsigned char *encsig; 84 size_t encsiglen; 85 unsigned char mac[20]; 86 } * RevealSigMsg; 87 88 typedef struct s_SignatureMsg { 89 unsigned char *raw; /* The base64-decoded data; must be free()d */ 90 unsigned char version; 91 unsigned int sender_instance; 92 unsigned int receiver_instance; 93 unsigned char *encsig; 94 size_t encsiglen; 95 unsigned char mac[20]; 96 } * SignatureMsg; 97 98 /* Dump an unsigned int to a FILE * */ 99 void dump_int(FILE *stream, const char *title, unsigned int val); 100 101 /* Dump an mpi to a FILE * */ 102 void dump_mpi(FILE *stream, const char *title, gcry_mpi_t val); 103 104 /* Dump data to a FILE * */ 105 void dump_data(FILE *stream, const char *title, const unsigned char *data, 106 size_t datalen); 107 108 /* Parse a Key Exchange Message into a newly-allocated KeyExchMsg structure */ 109 KeyExchMsg parse_keyexch(const char *msg); 110 111 /* Deallocate a KeyExchMsg and all of the data it points to */ 112 void free_keyexch(KeyExchMsg keyexch); 113 114 /* Parse a D-H Commit Message into a newly-allocated CommitMsg structure */ 115 CommitMsg parse_commit(const char *msg); 116 117 /* Parse a Data Message into a newly-allocated DataMsg structure */ 118 DataMsg parse_datamsg(const char *msg); 119 120 /* Deallocate a CommitMsg and all of the data it points to */ 121 void free_commit(CommitMsg cmsg); 122 123 /* Parse a Reveal Signature Message into a newly-allocated RevealSigMsg 124 * structure */ 125 RevealSigMsg parse_revealsig(const char *msg); 126 127 /* Deallocate a RevealSigMsg and all of the data it points to */ 128 void free_revealsig(RevealSigMsg rmsg); 129 130 /* Parse a Signature Message into a newly-allocated SignatureMsg structure */ 131 SignatureMsg parse_signature(const char *msg); 132 133 /* Deallocate a SignatureMsg and all of the data it points to */ 134 void free_signature(SignatureMsg smsg); 135 136 /* Parse a D-H Key Message into a newly-allocated KeyMsg structure */ 137 KeyMsg parse_key(const char *msg); 138 139 /* Deallocate a KeyMsg and all of the data it points to */ 140 void free_key(KeyMsg cmsg); 141 142 /* Recalculate the MAC on the message, base64-encode the resulting MAC'd 143 * message, and put on the appropriate header and footer. Return a 144 * newly-allocated pointer to the result, which the caller will have to 145 * free(). */ 146 char *remac_datamsg(DataMsg datamsg, unsigned char mackey[20]); 147 148 /* Assemble a new Data Message from its pieces. Return a 149 * newly-allocated string containing the base64 representation. */ 150 char *assemble_datamsg(unsigned char mackey[20], 151 unsigned char version, unsigned int sender_instance, 152 unsigned int receiver_instance, int flags, unsigned int sender_keyid, 153 unsigned int rcpt_keyid, gcry_mpi_t y, 154 unsigned char ctr[8], unsigned char *encmsg, size_t encmsglen, 155 unsigned char *mackeys, size_t mackeyslen); 156 157 /* Deallocate a DataMsg and all of the data it points to */ 158 void free_datamsg(DataMsg datamsg); 159 160 /* Convert a string of hex chars to a buffer of unsigned chars. */ 161 void argv_to_buf(unsigned char **bufp, size_t *lenp, char *arg); 162 163 #endif 164