summaryrefslogtreecommitdiff
path: root/plugins/MirOTR/libotr/src/b64.c
blob: a7d53aa2b1fec607f47d71ab4215b8fb211b3ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
62
63
64
65
66
67
68
69
70
71
72
73
74
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 *  Off-the-Record Messaging library
 *  Copyright (C) 2004-2012  Ian Goldberg, Chris Alexander, Willy Lew,
 *  			     Nikita Borisov
 *                           <otr@cypherpunks.ca>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of version 2.1 of the GNU Lesser General
 *  Public License as published by the Free Software Foundation.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/* Modified from: */

/*********************************************************************\

MODULE NAME:    b64.c

AUTHOR:         Bob Trower 08/04/01

LICENCE:        Copyright (c) 2001 Bob Trower, Trantor Standard Systems Inc.

		Permission is hereby granted, free of charge, to any person
		obtaining a copy of this software and associated
		documentation files (the "Software"), to deal in the
		Software without restriction, including without limitation
		the rights to use, copy, modify, merge, publish, distribute,
		sublicense, and/or sell copies of the Software, and to
		permit persons to whom the Software is furnished to do so,
		subject to the following conditions:

		The above copyright notice and this permission notice shall
		be included in all copies or substantial portions of the
		Software.

		THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
		KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
		WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
		PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
		OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
		OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
		OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
		SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

VERSION HISTORY:
		Bob Trower 08/04/01 -- Create Version 0.00.00B

\******************************************************************* */

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

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

/*
** Translation Table as described in RFC1113
*/
static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/*
** Translation Table to decode (created by author)
*/
static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";

/*
** encodeblock
**
** encode up to 3 8-bit binary bytes as 4 '6-bit' characters.
** len must be 1, 2, or 3.
*/
static void encodeblock( char *out, const unsigned char *in, size_t len )
{
    unsigned char in0, in1, in2;
    in0 = in[0];
    in1 = len > 1 ? in[1] : 0;
    in2 = len > 2 ? in[2] : 0;

    out[0] = cb64[ in0 >> 2 ];
    out[1] = cb64[ ((in0 & 0x03) << 4) | ((in1 & 0xf0) >> 4) ];
    out[2] = len > 1 ? cb64[ ((in1 & 0x0f) << 2) | ((in2 & 0xc0) >> 6) ]
		     : '=';
    out[3] = len > 2 ? cb64[ in2 & 0x3f ]
		     : '=';
}

/*
 * base64 encode data.  Insert no linebreaks or whitespace.
 *
 * The buffer base64data must contain at least ((datalen+2)/3)*4 bytes of
 * space.  This function will return the number of bytes actually used.
 */
size_t otrl_base64_encode(char *base64data, const unsigned char *data,
	size_t datalen)
{
    size_t base64len = 0;

    while(datalen > 2) {
	encodeblock(base64data, data, 3);
	base64data += 4;
	base64len += 4;
	data += 3;
	datalen -= 3;
    }
    if (datalen > 0) {
	encodeblock(base64data, data, datalen);
	base64len += 4;
    }

    return base64len;
}

static size_t decode(unsigned char *out, const char *in, size_t b64len)
{
    size_t written = 0;
    unsigned char c = 0;

    if (b64len > 0) {
	c = in[0] << 2;
    }
    if (b64len > 1) {
	out[0] = c | in[1] >> 4;
	written = 1;
	c = in[1] << 4;
    }
    if (b64len > 2) {
	out[1] = c | in[2] >> 2;
	written = 2;
	c = in[2] << 6;
    }
    if (b64len > 3) {
	out[2] = c | in[3];
	written = 3;
    }
    return written;
}

/*
 * base64 decode data.  Skip non-base64 chars, and terminate at the
 * first '=', or the end of the buffer.
 *
 * The buffer data must contain at least ((base64len+3) / 4) * 3 bytes
 * of space.  This function will return the number of bytes actually
 * used.
 */
size_t otrl_base64_decode(unsigned char *data, const char *base64data,
	size_t base64len)
{
    size_t datalen = 0;
    char b64[4];
    size_t b64accum = 0;

    while(base64len > 0) {
	char b = *base64data;
	unsigned char bdecode;
	++base64data;
	--base64len;
	if (b < '+' || b > 'z') continue;  /* Skip non-base64 chars */
	if (b == '=') {
	    /* Force termination */
	    datalen += decode(data, b64, b64accum);
	    base64len = 0;
	} else {
	    bdecode = cd64[b-'+'];
	    if (bdecode == '$') continue;  /* Skip non-base64 chars */
	    b64[b64accum++] = bdecode-'>';
	    if (b64accum == 4) {
		/* We have a complete block; decode it. */
		size_t written = decode(data, b64, b64accum);
		data += written;
		datalen += written;
		b64accum = 0;
	    }
	}
    }

    /* Just discard any short block at the end. */

    return datalen;
}

/*
 * Base64-encode a block of data, stick "?OTR:" and "." around it, and
 * return the result, or NULL in the event of a memory error.  The
 * caller must free() the return value.
 */
char *otrl_base64_otr_encode(const unsigned char *buf, size_t buflen)
{
    char *base64buf;
    size_t base64len;
    const size_t HALF_MAX_SIZE_T = ((size_t)-1) >> 1;

    if (buflen > HALF_MAX_SIZE_T) {
	/* You somehow have a buffer that's of size more than half of
	 * all addressable memory, and you now want a base64 version in
	 * a new buffer 33% larger?  Not going to happen.  Exit now,
	 * rather in the malloc below, to avoid integer overflowing the
	 * computation of base64len. */
	 return NULL;
    }

    /* Make the base64-encoding. */
    base64len = ((buflen + 2) / 3) * 4;
    base64buf = malloc(5 + base64len + 1 + 1);
    if (base64buf == NULL) {
	return NULL;
    }
    memmove(base64buf, "?OTR:", 5);
    otrl_base64_encode(base64buf+5, buf, buflen);
    base64buf[5 + base64len] = '.';
    base64buf[5 + base64len + 1] = '\0';

    return base64buf;
}

/*
 * Base64-decode the portion of the given message between "?OTR:" and
 * ".".  Set *bufp to the decoded data, and set *lenp to its length.
 * The caller must free() the result.  Return 0 on success, -1 on a
 * memory error, or -2 on invalid input.
 */
int otrl_base64_otr_decode(const char *msg, unsigned char **bufp,
	size_t *lenp)
{
    char *otrtag, *endtag;
    size_t msglen, rawlen;
    unsigned char *rawmsg;

    otrtag = strstr(msg, "?OTR:");
    if (!otrtag) {
	return -2;
    }

    endtag = strchr(otrtag, '.');
    if (endtag) {
	msglen = endtag-otrtag;
    } else {
	return -2;
    }

    /* Skip over the "?OTR:" */
    otrtag += 5;
    msglen -= 5;

    /* Base64-decode the message */
    rawlen = OTRL_B64_MAX_DECODED_SIZE(msglen);   /* maximum possible */
    rawmsg = malloc(rawlen);
    if (!rawmsg && rawlen > 0) {
	return -1;
    }

    rawlen = otrl_base64_decode(rawmsg, otrtag, msglen);  /* actual size */

    *bufp = rawmsg;
    *lenp = rawlen;

    return 0;
}