line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/** |
2
|
|
|
|
|
|
|
* @file base64.c |
3
|
|
|
|
|
|
|
* @version 950bba4 (HEAD -> master) |
4
|
|
|
|
|
|
|
* |
5
|
|
|
|
|
|
|
* Base64 operations. |
6
|
|
|
|
|
|
|
*/ |
7
|
|
|
|
|
|
|
/* |
8
|
|
|
|
|
|
|
* Copyright (c) 2013-2017 INSIDE Secure Corporation |
9
|
|
|
|
|
|
|
* Copyright (c) PeerSec Networks, 2002-2011 |
10
|
|
|
|
|
|
|
* All Rights Reserved |
11
|
|
|
|
|
|
|
* |
12
|
|
|
|
|
|
|
* The latest version of this code is available at http://www.matrixssl.org |
13
|
|
|
|
|
|
|
* |
14
|
|
|
|
|
|
|
* This software is open source; you can redistribute it and/or modify |
15
|
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by |
16
|
|
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
17
|
|
|
|
|
|
|
* (at your option) any later version. |
18
|
|
|
|
|
|
|
* |
19
|
|
|
|
|
|
|
* This General Public License does NOT permit incorporating this software |
20
|
|
|
|
|
|
|
* into proprietary programs. If you are unable to comply with the GPL, a |
21
|
|
|
|
|
|
|
* commercial license for this software may be purchased from INSIDE at |
22
|
|
|
|
|
|
|
* http://www.insidesecure.com/ |
23
|
|
|
|
|
|
|
* |
24
|
|
|
|
|
|
|
* This program is distributed in WITHOUT ANY WARRANTY; without even the |
25
|
|
|
|
|
|
|
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
26
|
|
|
|
|
|
|
* See the GNU General Public License for more details. |
27
|
|
|
|
|
|
|
* |
28
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License |
29
|
|
|
|
|
|
|
* along with this program; if not, write to the Free Software |
30
|
|
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
31
|
|
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
32
|
|
|
|
|
|
|
*/ |
33
|
|
|
|
|
|
|
/******************************************************************************/ |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#include "../cryptoImpl.h" |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#ifdef USE_BASE64_DECODE |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
static const unsigned char map[] = { |
40
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
41
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
42
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
43
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, |
44
|
|
|
|
|
|
|
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, |
45
|
|
|
|
|
|
|
255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, |
46
|
|
|
|
|
|
|
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
47
|
|
|
|
|
|
|
19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, |
48
|
|
|
|
|
|
|
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
49
|
|
|
|
|
|
|
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
50
|
|
|
|
|
|
|
49, 50, 51 |
51
|
|
|
|
|
|
|
/* , 255, 255, 255, 255, 255, 255, 255, 255, 255, |
52
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
53
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
54
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
55
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
56
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
57
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
58
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
59
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
60
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
61
|
|
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
62
|
|
|
|
|
|
|
255, 255, 255, 255 |
63
|
|
|
|
|
|
|
*/ |
64
|
|
|
|
|
|
|
}; |
65
|
|
|
|
|
|
|
|
66
|
1628
|
|
|
|
|
|
int32_t psBase64decode(const unsigned char *in, psSize_t len, |
67
|
|
|
|
|
|
|
unsigned char *out, psSize_t *outlen) |
68
|
|
|
|
|
|
|
{ |
69
|
|
|
|
|
|
|
unsigned int t; |
70
|
|
|
|
|
|
|
unsigned char c; |
71
|
|
|
|
|
|
|
uint16_t x, y, z; |
72
|
|
|
|
|
|
|
int16_t g; |
73
|
|
|
|
|
|
|
|
74
|
1628
|
50
|
|
|
|
|
if (in == NULL || out == NULL || outlen == NULL) |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
75
|
|
|
|
|
|
|
{ |
76
|
|
|
|
|
|
|
psTraceCrypto("Arg failure to psBase64decode\n"); |
77
|
0
|
|
|
|
|
|
return PS_ARG_FAIL; |
78
|
|
|
|
|
|
|
} |
79
|
1628
|
|
|
|
|
|
g = 3; |
80
|
2261059
|
100
|
|
|
|
|
for (x = y = z = t = 0; x < len; x++) |
81
|
|
|
|
|
|
|
{ |
82
|
|
|
|
|
|
|
/* Save a little space by skipping values that would be 255 in the full map */ |
83
|
2259431
|
50
|
|
|
|
|
if (in[x] > 122) |
84
|
|
|
|
|
|
|
{ |
85
|
0
|
|
|
|
|
|
continue; |
86
|
|
|
|
|
|
|
} |
87
|
2259431
|
|
|
|
|
|
c = map[in[x]]; |
88
|
2259431
|
100
|
|
|
|
|
if (c == 255) |
89
|
|
|
|
|
|
|
{ |
90
|
34191
|
|
|
|
|
|
continue; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
/* the final '=' symbols are read and used to trim the remaining bytes */ |
93
|
2225240
|
100
|
|
|
|
|
if (c == 254) |
94
|
|
|
|
|
|
|
{ |
95
|
795
|
|
|
|
|
|
c = 0; |
96
|
|
|
|
|
|
|
/* prevent g < 0 which would potentially allow an overflow later */ |
97
|
795
|
50
|
|
|
|
|
if (--g < 0) |
98
|
|
|
|
|
|
|
{ |
99
|
|
|
|
|
|
|
psTraceCrypto("Negative g failure in psBase64decode\n"); |
100
|
0
|
|
|
|
|
|
return PS_LIMIT_FAIL; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
2224445
|
50
|
|
|
|
|
else if (g != 3) |
104
|
|
|
|
|
|
|
{ |
105
|
|
|
|
|
|
|
/* we only allow = to be at the end */ |
106
|
|
|
|
|
|
|
psTraceCrypto("g failure in psBase64decode\n"); |
107
|
0
|
|
|
|
|
|
return PS_PARSE_FAIL; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
2225240
|
|
|
|
|
|
t = (t << 6) | c; |
111
|
|
|
|
|
|
|
|
112
|
2225240
|
100
|
|
|
|
|
if (++y == 4) |
113
|
|
|
|
|
|
|
{ |
114
|
556310
|
50
|
|
|
|
|
if (z + g > *outlen) |
115
|
|
|
|
|
|
|
{ |
116
|
|
|
|
|
|
|
psTraceCrypto("outlen too small for psBase64decode\n"); |
117
|
0
|
|
|
|
|
|
return PS_LIMIT_FAIL; |
118
|
|
|
|
|
|
|
} |
119
|
556310
|
|
|
|
|
|
out[z++] = (unsigned char) ((t >> 16) & 0xFF); |
120
|
556310
|
100
|
|
|
|
|
if (g > 1) |
121
|
|
|
|
|
|
|
{ |
122
|
556090
|
|
|
|
|
|
out[z++] = (unsigned char) ((t >> 8) & 0xFF); |
123
|
|
|
|
|
|
|
} |
124
|
556310
|
100
|
|
|
|
|
if (g > 2) |
125
|
|
|
|
|
|
|
{ |
126
|
555735
|
|
|
|
|
|
out[z++] = (unsigned char) (t & 0xFF); |
127
|
|
|
|
|
|
|
} |
128
|
556310
|
|
|
|
|
|
y = t = 0; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} |
131
|
1628
|
50
|
|
|
|
|
if (y != 0) |
132
|
|
|
|
|
|
|
{ |
133
|
|
|
|
|
|
|
psTraceCrypto("y failure in psBase64decode\n"); |
134
|
0
|
|
|
|
|
|
return PS_PARSE_FAIL; |
135
|
|
|
|
|
|
|
} |
136
|
1628
|
|
|
|
|
|
*outlen = z; |
137
|
1628
|
|
|
|
|
|
return PS_SUCCESS; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
#endif /* USE_BASE64_DECODE */ |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
/******************************************************************************/ |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
/******************************************************************************/ |
145
|
|
|
|
|
|
|
|