Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
wolfSSL
Date:
Sat Jul 12 07:18:23 2014 +0000
Revision:
0:1239e9b70ca2
CyaSSL 3.0.0;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:1239e9b70ca2 1 /* coding.c
wolfSSL 0:1239e9b70ca2 2 *
wolfSSL 0:1239e9b70ca2 3 * Copyright (C) 2006-2014 wolfSSL Inc.
wolfSSL 0:1239e9b70ca2 4 *
wolfSSL 0:1239e9b70ca2 5 * This file is part of CyaSSL.
wolfSSL 0:1239e9b70ca2 6 *
wolfSSL 0:1239e9b70ca2 7 * CyaSSL is free software; you can redistribute it and/or modify
wolfSSL 0:1239e9b70ca2 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:1239e9b70ca2 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:1239e9b70ca2 10 * (at your option) any later version.
wolfSSL 0:1239e9b70ca2 11 *
wolfSSL 0:1239e9b70ca2 12 * CyaSSL is distributed in the hope that it will be useful,
wolfSSL 0:1239e9b70ca2 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:1239e9b70ca2 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:1239e9b70ca2 15 * GNU General Public License for more details.
wolfSSL 0:1239e9b70ca2 16 *
wolfSSL 0:1239e9b70ca2 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:1239e9b70ca2 18 * along with this program; if not, write to the Free Software
wolfSSL 0:1239e9b70ca2 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:1239e9b70ca2 20 */
wolfSSL 0:1239e9b70ca2 21
wolfSSL 0:1239e9b70ca2 22 #ifdef HAVE_CONFIG_H
wolfSSL 0:1239e9b70ca2 23 #include <config.h>
wolfSSL 0:1239e9b70ca2 24 #endif
wolfSSL 0:1239e9b70ca2 25
wolfSSL 0:1239e9b70ca2 26 #include <cyassl/ctaocrypt/settings.h>
wolfSSL 0:1239e9b70ca2 27
wolfSSL 0:1239e9b70ca2 28 #ifndef NO_CODING
wolfSSL 0:1239e9b70ca2 29
wolfSSL 0:1239e9b70ca2 30 #include <cyassl/ctaocrypt/coding.h>
wolfSSL 0:1239e9b70ca2 31 #include <cyassl/ctaocrypt/error-crypt.h>
wolfSSL 0:1239e9b70ca2 32 #include <cyassl/ctaocrypt/logging.h>
wolfSSL 0:1239e9b70ca2 33
wolfSSL 0:1239e9b70ca2 34
wolfSSL 0:1239e9b70ca2 35 enum {
wolfSSL 0:1239e9b70ca2 36 BAD = 0xFF, /* invalid encoding */
wolfSSL 0:1239e9b70ca2 37 PAD = '=',
wolfSSL 0:1239e9b70ca2 38 PEM_LINE_SZ = 64
wolfSSL 0:1239e9b70ca2 39 };
wolfSSL 0:1239e9b70ca2 40
wolfSSL 0:1239e9b70ca2 41
wolfSSL 0:1239e9b70ca2 42 static
wolfSSL 0:1239e9b70ca2 43 const byte base64Decode[] = { 62, BAD, BAD, BAD, 63, /* + starts at 0x2B */
wolfSSL 0:1239e9b70ca2 44 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
wolfSSL 0:1239e9b70ca2 45 BAD, BAD, BAD, BAD, BAD, BAD, BAD,
wolfSSL 0:1239e9b70ca2 46 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
wolfSSL 0:1239e9b70ca2 47 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
wolfSSL 0:1239e9b70ca2 48 20, 21, 22, 23, 24, 25,
wolfSSL 0:1239e9b70ca2 49 BAD, BAD, BAD, BAD, BAD, BAD,
wolfSSL 0:1239e9b70ca2 50 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
wolfSSL 0:1239e9b70ca2 51 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
wolfSSL 0:1239e9b70ca2 52 46, 47, 48, 49, 50, 51
wolfSSL 0:1239e9b70ca2 53 };
wolfSSL 0:1239e9b70ca2 54
wolfSSL 0:1239e9b70ca2 55
wolfSSL 0:1239e9b70ca2 56 int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
wolfSSL 0:1239e9b70ca2 57 {
wolfSSL 0:1239e9b70ca2 58 word32 i = 0;
wolfSSL 0:1239e9b70ca2 59 word32 j = 0;
wolfSSL 0:1239e9b70ca2 60 word32 plainSz = inLen - ((inLen + (PEM_LINE_SZ - 1)) / PEM_LINE_SZ );
wolfSSL 0:1239e9b70ca2 61 const byte maxIdx = (byte)sizeof(base64Decode) + 0x2B - 1;
wolfSSL 0:1239e9b70ca2 62
wolfSSL 0:1239e9b70ca2 63 plainSz = (plainSz * 3 + 3) / 4;
wolfSSL 0:1239e9b70ca2 64 if (plainSz > *outLen) return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 65
wolfSSL 0:1239e9b70ca2 66 while (inLen > 3) {
wolfSSL 0:1239e9b70ca2 67 byte b1, b2, b3;
wolfSSL 0:1239e9b70ca2 68 byte e1 = in[j++];
wolfSSL 0:1239e9b70ca2 69 byte e2 = in[j++];
wolfSSL 0:1239e9b70ca2 70 byte e3 = in[j++];
wolfSSL 0:1239e9b70ca2 71 byte e4 = in[j++];
wolfSSL 0:1239e9b70ca2 72
wolfSSL 0:1239e9b70ca2 73 int pad3 = 0;
wolfSSL 0:1239e9b70ca2 74 int pad4 = 0;
wolfSSL 0:1239e9b70ca2 75
wolfSSL 0:1239e9b70ca2 76 if (e1 == 0) /* end file 0's */
wolfSSL 0:1239e9b70ca2 77 break;
wolfSSL 0:1239e9b70ca2 78 if (e3 == PAD)
wolfSSL 0:1239e9b70ca2 79 pad3 = 1;
wolfSSL 0:1239e9b70ca2 80 if (e4 == PAD)
wolfSSL 0:1239e9b70ca2 81 pad4 = 1;
wolfSSL 0:1239e9b70ca2 82
wolfSSL 0:1239e9b70ca2 83 if (e1 < 0x2B || e2 < 0x2B || e3 < 0x2B || e4 < 0x2B) {
wolfSSL 0:1239e9b70ca2 84 CYASSL_MSG("Bad Base64 Decode data, too small");
wolfSSL 0:1239e9b70ca2 85 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 86 }
wolfSSL 0:1239e9b70ca2 87
wolfSSL 0:1239e9b70ca2 88 if (e1 > maxIdx || e2 > maxIdx || e3 > maxIdx || e4 > maxIdx) {
wolfSSL 0:1239e9b70ca2 89 CYASSL_MSG("Bad Base64 Decode data, too big");
wolfSSL 0:1239e9b70ca2 90 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 91 }
wolfSSL 0:1239e9b70ca2 92
wolfSSL 0:1239e9b70ca2 93 e1 = base64Decode[e1 - 0x2B];
wolfSSL 0:1239e9b70ca2 94 e2 = base64Decode[e2 - 0x2B];
wolfSSL 0:1239e9b70ca2 95 e3 = (e3 == PAD) ? 0 : base64Decode[e3 - 0x2B];
wolfSSL 0:1239e9b70ca2 96 e4 = (e4 == PAD) ? 0 : base64Decode[e4 - 0x2B];
wolfSSL 0:1239e9b70ca2 97
wolfSSL 0:1239e9b70ca2 98 b1 = (byte)((e1 << 2) | (e2 >> 4));
wolfSSL 0:1239e9b70ca2 99 b2 = (byte)(((e2 & 0xF) << 4) | (e3 >> 2));
wolfSSL 0:1239e9b70ca2 100 b3 = (byte)(((e3 & 0x3) << 6) | e4);
wolfSSL 0:1239e9b70ca2 101
wolfSSL 0:1239e9b70ca2 102 out[i++] = b1;
wolfSSL 0:1239e9b70ca2 103 if (!pad3)
wolfSSL 0:1239e9b70ca2 104 out[i++] = b2;
wolfSSL 0:1239e9b70ca2 105 if (!pad4)
wolfSSL 0:1239e9b70ca2 106 out[i++] = b3;
wolfSSL 0:1239e9b70ca2 107 else
wolfSSL 0:1239e9b70ca2 108 break;
wolfSSL 0:1239e9b70ca2 109
wolfSSL 0:1239e9b70ca2 110 inLen -= 4;
wolfSSL 0:1239e9b70ca2 111 if (inLen && (in[j] == ' ' || in[j] == '\r' || in[j] == '\n')) {
wolfSSL 0:1239e9b70ca2 112 byte endLine = in[j++];
wolfSSL 0:1239e9b70ca2 113 inLen--;
wolfSSL 0:1239e9b70ca2 114 while (inLen && endLine == ' ') { /* allow trailing whitespace */
wolfSSL 0:1239e9b70ca2 115 endLine = in[j++];
wolfSSL 0:1239e9b70ca2 116 inLen--;
wolfSSL 0:1239e9b70ca2 117 }
wolfSSL 0:1239e9b70ca2 118 if (endLine == '\r') {
wolfSSL 0:1239e9b70ca2 119 if (inLen) {
wolfSSL 0:1239e9b70ca2 120 endLine = in[j++];
wolfSSL 0:1239e9b70ca2 121 inLen--;
wolfSSL 0:1239e9b70ca2 122 }
wolfSSL 0:1239e9b70ca2 123 }
wolfSSL 0:1239e9b70ca2 124 if (endLine != '\n') {
wolfSSL 0:1239e9b70ca2 125 CYASSL_MSG("Bad end of line in Base64 Decode");
wolfSSL 0:1239e9b70ca2 126 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 127 }
wolfSSL 0:1239e9b70ca2 128 }
wolfSSL 0:1239e9b70ca2 129 }
wolfSSL 0:1239e9b70ca2 130 *outLen = i;
wolfSSL 0:1239e9b70ca2 131
wolfSSL 0:1239e9b70ca2 132 return 0;
wolfSSL 0:1239e9b70ca2 133 }
wolfSSL 0:1239e9b70ca2 134
wolfSSL 0:1239e9b70ca2 135
wolfSSL 0:1239e9b70ca2 136 #if defined(OPENSSL_EXTRA) || defined (SESSION_CERTS) || defined(CYASSL_KEY_GEN) || defined(CYASSL_CERT_GEN) || defined(HAVE_WEBSERVER)
wolfSSL 0:1239e9b70ca2 137
wolfSSL 0:1239e9b70ca2 138 static
wolfSSL 0:1239e9b70ca2 139 const byte base64Encode[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
wolfSSL 0:1239e9b70ca2 140 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
wolfSSL 0:1239e9b70ca2 141 'U', 'V', 'W', 'X', 'Y', 'Z',
wolfSSL 0:1239e9b70ca2 142 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
wolfSSL 0:1239e9b70ca2 143 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
wolfSSL 0:1239e9b70ca2 144 'u', 'v', 'w', 'x', 'y', 'z',
wolfSSL 0:1239e9b70ca2 145 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
wolfSSL 0:1239e9b70ca2 146 '+', '/'
wolfSSL 0:1239e9b70ca2 147 };
wolfSSL 0:1239e9b70ca2 148
wolfSSL 0:1239e9b70ca2 149
wolfSSL 0:1239e9b70ca2 150 /* make sure *i (idx) won't exceed max, store and possibly escape to out,
wolfSSL 0:1239e9b70ca2 151 * raw means use e w/o decode, 0 on success */
wolfSSL 0:1239e9b70ca2 152 static int CEscape(int escaped, byte e, byte* out, word32* i, word32 max,
wolfSSL 0:1239e9b70ca2 153 int raw)
wolfSSL 0:1239e9b70ca2 154 {
wolfSSL 0:1239e9b70ca2 155 int doEscape = 0;
wolfSSL 0:1239e9b70ca2 156 word32 needed = 1;
wolfSSL 0:1239e9b70ca2 157 word32 idx = *i;
wolfSSL 0:1239e9b70ca2 158
wolfSSL 0:1239e9b70ca2 159 byte basic;
wolfSSL 0:1239e9b70ca2 160 byte plus = 0;
wolfSSL 0:1239e9b70ca2 161 byte equals = 0;
wolfSSL 0:1239e9b70ca2 162 byte newline = 0;
wolfSSL 0:1239e9b70ca2 163
wolfSSL 0:1239e9b70ca2 164 if (raw)
wolfSSL 0:1239e9b70ca2 165 basic = e;
wolfSSL 0:1239e9b70ca2 166 else
wolfSSL 0:1239e9b70ca2 167 basic = base64Encode[e];
wolfSSL 0:1239e9b70ca2 168
wolfSSL 0:1239e9b70ca2 169 /* check whether to escape */
wolfSSL 0:1239e9b70ca2 170 if (escaped) {
wolfSSL 0:1239e9b70ca2 171 switch ((char)basic) {
wolfSSL 0:1239e9b70ca2 172 case '+' :
wolfSSL 0:1239e9b70ca2 173 plus = 1;
wolfSSL 0:1239e9b70ca2 174 doEscape = 1;
wolfSSL 0:1239e9b70ca2 175 needed += 2;
wolfSSL 0:1239e9b70ca2 176 break;
wolfSSL 0:1239e9b70ca2 177 case '=' :
wolfSSL 0:1239e9b70ca2 178 equals = 1;
wolfSSL 0:1239e9b70ca2 179 doEscape = 1;
wolfSSL 0:1239e9b70ca2 180 needed += 2;
wolfSSL 0:1239e9b70ca2 181 break;
wolfSSL 0:1239e9b70ca2 182 case '\n' :
wolfSSL 0:1239e9b70ca2 183 newline = 1;
wolfSSL 0:1239e9b70ca2 184 doEscape = 1;
wolfSSL 0:1239e9b70ca2 185 needed += 2;
wolfSSL 0:1239e9b70ca2 186 break;
wolfSSL 0:1239e9b70ca2 187 default:
wolfSSL 0:1239e9b70ca2 188 /* do nothing */
wolfSSL 0:1239e9b70ca2 189 break;
wolfSSL 0:1239e9b70ca2 190 }
wolfSSL 0:1239e9b70ca2 191 }
wolfSSL 0:1239e9b70ca2 192
wolfSSL 0:1239e9b70ca2 193 /* check size */
wolfSSL 0:1239e9b70ca2 194 if ( (idx+needed) > max) {
wolfSSL 0:1239e9b70ca2 195 CYASSL_MSG("Escape buffer max too small");
wolfSSL 0:1239e9b70ca2 196 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 197 }
wolfSSL 0:1239e9b70ca2 198
wolfSSL 0:1239e9b70ca2 199 /* store it */
wolfSSL 0:1239e9b70ca2 200 if (doEscape == 0) {
wolfSSL 0:1239e9b70ca2 201 out[idx++] = basic;
wolfSSL 0:1239e9b70ca2 202 }
wolfSSL 0:1239e9b70ca2 203 else {
wolfSSL 0:1239e9b70ca2 204 out[idx++] = '%'; /* start escape */
wolfSSL 0:1239e9b70ca2 205
wolfSSL 0:1239e9b70ca2 206 if (plus) {
wolfSSL 0:1239e9b70ca2 207 out[idx++] = '2';
wolfSSL 0:1239e9b70ca2 208 out[idx++] = 'B';
wolfSSL 0:1239e9b70ca2 209 }
wolfSSL 0:1239e9b70ca2 210 else if (equals) {
wolfSSL 0:1239e9b70ca2 211 out[idx++] = '3';
wolfSSL 0:1239e9b70ca2 212 out[idx++] = 'D';
wolfSSL 0:1239e9b70ca2 213 }
wolfSSL 0:1239e9b70ca2 214 else if (newline) {
wolfSSL 0:1239e9b70ca2 215 out[idx++] = '0';
wolfSSL 0:1239e9b70ca2 216 out[idx++] = 'A';
wolfSSL 0:1239e9b70ca2 217 }
wolfSSL 0:1239e9b70ca2 218
wolfSSL 0:1239e9b70ca2 219 }
wolfSSL 0:1239e9b70ca2 220 *i = idx;
wolfSSL 0:1239e9b70ca2 221
wolfSSL 0:1239e9b70ca2 222 return 0;
wolfSSL 0:1239e9b70ca2 223 }
wolfSSL 0:1239e9b70ca2 224
wolfSSL 0:1239e9b70ca2 225
wolfSSL 0:1239e9b70ca2 226 /* internal worker, handles both escaped and normal line endings */
wolfSSL 0:1239e9b70ca2 227 static int DoBase64_Encode(const byte* in, word32 inLen, byte* out,
wolfSSL 0:1239e9b70ca2 228 word32* outLen, int escaped)
wolfSSL 0:1239e9b70ca2 229 {
wolfSSL 0:1239e9b70ca2 230 int ret = 0;
wolfSSL 0:1239e9b70ca2 231 word32 i = 0,
wolfSSL 0:1239e9b70ca2 232 j = 0,
wolfSSL 0:1239e9b70ca2 233 n = 0; /* new line counter */
wolfSSL 0:1239e9b70ca2 234
wolfSSL 0:1239e9b70ca2 235 word32 outSz = (inLen + 3 - 1) / 3 * 4;
wolfSSL 0:1239e9b70ca2 236 word32 addSz = (outSz + PEM_LINE_SZ - 1) / PEM_LINE_SZ; /* new lines */
wolfSSL 0:1239e9b70ca2 237
wolfSSL 0:1239e9b70ca2 238 if (escaped)
wolfSSL 0:1239e9b70ca2 239 addSz *= 3; /* instead of just \n, we're doing %0A triplet */
wolfSSL 0:1239e9b70ca2 240
wolfSSL 0:1239e9b70ca2 241 outSz += addSz;
wolfSSL 0:1239e9b70ca2 242
wolfSSL 0:1239e9b70ca2 243 /* if escaped we can't predetermine size for one pass encoding, but
wolfSSL 0:1239e9b70ca2 244 * make sure we have enough if no escapes are in input */
wolfSSL 0:1239e9b70ca2 245 if (outSz > *outLen) return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 246
wolfSSL 0:1239e9b70ca2 247 while (inLen > 2) {
wolfSSL 0:1239e9b70ca2 248 byte b1 = in[j++];
wolfSSL 0:1239e9b70ca2 249 byte b2 = in[j++];
wolfSSL 0:1239e9b70ca2 250 byte b3 = in[j++];
wolfSSL 0:1239e9b70ca2 251
wolfSSL 0:1239e9b70ca2 252 /* encoded idx */
wolfSSL 0:1239e9b70ca2 253 byte e1 = b1 >> 2;
wolfSSL 0:1239e9b70ca2 254 byte e2 = (byte)(((b1 & 0x3) << 4) | (b2 >> 4));
wolfSSL 0:1239e9b70ca2 255 byte e3 = (byte)(((b2 & 0xF) << 2) | (b3 >> 6));
wolfSSL 0:1239e9b70ca2 256 byte e4 = b3 & 0x3F;
wolfSSL 0:1239e9b70ca2 257
wolfSSL 0:1239e9b70ca2 258 /* store */
wolfSSL 0:1239e9b70ca2 259 ret = CEscape(escaped, e1, out, &i, *outLen, 0);
wolfSSL 0:1239e9b70ca2 260 if (ret != 0) break;
wolfSSL 0:1239e9b70ca2 261 ret = CEscape(escaped, e2, out, &i, *outLen, 0);
wolfSSL 0:1239e9b70ca2 262 if (ret != 0) break;
wolfSSL 0:1239e9b70ca2 263 ret = CEscape(escaped, e3, out, &i, *outLen, 0);
wolfSSL 0:1239e9b70ca2 264 if (ret != 0) break;
wolfSSL 0:1239e9b70ca2 265 ret = CEscape(escaped, e4, out, &i, *outLen, 0);
wolfSSL 0:1239e9b70ca2 266 if (ret != 0) break;
wolfSSL 0:1239e9b70ca2 267
wolfSSL 0:1239e9b70ca2 268 inLen -= 3;
wolfSSL 0:1239e9b70ca2 269
wolfSSL 0:1239e9b70ca2 270 if ((++n % (PEM_LINE_SZ / 4)) == 0 && inLen) {
wolfSSL 0:1239e9b70ca2 271 ret = CEscape(escaped, '\n', out, &i, *outLen, 1);
wolfSSL 0:1239e9b70ca2 272 if (ret != 0) break;
wolfSSL 0:1239e9b70ca2 273 }
wolfSSL 0:1239e9b70ca2 274 }
wolfSSL 0:1239e9b70ca2 275
wolfSSL 0:1239e9b70ca2 276 /* last integral */
wolfSSL 0:1239e9b70ca2 277 if (inLen && ret == 0) {
wolfSSL 0:1239e9b70ca2 278 int twoBytes = (inLen == 2);
wolfSSL 0:1239e9b70ca2 279
wolfSSL 0:1239e9b70ca2 280 byte b1 = in[j++];
wolfSSL 0:1239e9b70ca2 281 byte b2 = (twoBytes) ? in[j++] : 0;
wolfSSL 0:1239e9b70ca2 282
wolfSSL 0:1239e9b70ca2 283 byte e1 = b1 >> 2;
wolfSSL 0:1239e9b70ca2 284 byte e2 = (byte)(((b1 & 0x3) << 4) | (b2 >> 4));
wolfSSL 0:1239e9b70ca2 285 byte e3 = (byte)((b2 & 0xF) << 2);
wolfSSL 0:1239e9b70ca2 286
wolfSSL 0:1239e9b70ca2 287 ret = CEscape(escaped, e1, out, &i, *outLen, 0);
wolfSSL 0:1239e9b70ca2 288 if (ret == 0)
wolfSSL 0:1239e9b70ca2 289 ret = CEscape(escaped, e2, out, &i, *outLen, 0);
wolfSSL 0:1239e9b70ca2 290 if (ret == 0) {
wolfSSL 0:1239e9b70ca2 291 /* third */
wolfSSL 0:1239e9b70ca2 292 if (twoBytes)
wolfSSL 0:1239e9b70ca2 293 ret = CEscape(escaped, e3, out, &i, *outLen, 0);
wolfSSL 0:1239e9b70ca2 294 else
wolfSSL 0:1239e9b70ca2 295 ret = CEscape(escaped, '=', out, &i, *outLen, 1);
wolfSSL 0:1239e9b70ca2 296 }
wolfSSL 0:1239e9b70ca2 297 /* fourth always pad */
wolfSSL 0:1239e9b70ca2 298 if (ret == 0)
wolfSSL 0:1239e9b70ca2 299 ret = CEscape(escaped, '=', out, &i, *outLen, 1);
wolfSSL 0:1239e9b70ca2 300 }
wolfSSL 0:1239e9b70ca2 301
wolfSSL 0:1239e9b70ca2 302 if (ret == 0)
wolfSSL 0:1239e9b70ca2 303 ret = CEscape(escaped, '\n', out, &i, *outLen, 1);
wolfSSL 0:1239e9b70ca2 304
wolfSSL 0:1239e9b70ca2 305 if (i != outSz && escaped == 0 && ret == 0)
wolfSSL 0:1239e9b70ca2 306 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 307
wolfSSL 0:1239e9b70ca2 308 *outLen = i;
wolfSSL 0:1239e9b70ca2 309 return ret;
wolfSSL 0:1239e9b70ca2 310 }
wolfSSL 0:1239e9b70ca2 311
wolfSSL 0:1239e9b70ca2 312
wolfSSL 0:1239e9b70ca2 313 /* Base64 Encode, PEM style, with \n line endings */
wolfSSL 0:1239e9b70ca2 314 int Base64_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
wolfSSL 0:1239e9b70ca2 315 {
wolfSSL 0:1239e9b70ca2 316 return DoBase64_Encode(in, inLen, out, outLen, 0);
wolfSSL 0:1239e9b70ca2 317 }
wolfSSL 0:1239e9b70ca2 318
wolfSSL 0:1239e9b70ca2 319
wolfSSL 0:1239e9b70ca2 320 /* Base64 Encode, with %0A esacped line endings instead of \n */
wolfSSL 0:1239e9b70ca2 321 int Base64_EncodeEsc(const byte* in, word32 inLen, byte* out, word32* outLen)
wolfSSL 0:1239e9b70ca2 322 {
wolfSSL 0:1239e9b70ca2 323 return DoBase64_Encode(in, inLen, out, outLen, 1);
wolfSSL 0:1239e9b70ca2 324 }
wolfSSL 0:1239e9b70ca2 325
wolfSSL 0:1239e9b70ca2 326
wolfSSL 0:1239e9b70ca2 327 #endif /* defined(OPENSSL_EXTRA) || defined (SESSION_CERTS) || defined(CYASSL_KEY_GEN) || defined(CYASSL_CERT_GEN) || defined(HAVE_WEBSERVER) */
wolfSSL 0:1239e9b70ca2 328
wolfSSL 0:1239e9b70ca2 329
wolfSSL 0:1239e9b70ca2 330 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(HAVE_FIPS)
wolfSSL 0:1239e9b70ca2 331
wolfSSL 0:1239e9b70ca2 332 static
wolfSSL 0:1239e9b70ca2 333 const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
wolfSSL 0:1239e9b70ca2 334 BAD, BAD, BAD, BAD, BAD, BAD, BAD,
wolfSSL 0:1239e9b70ca2 335 10, 11, 12, 13, 14, 15, /* upper case A-F */
wolfSSL 0:1239e9b70ca2 336 BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
wolfSSL 0:1239e9b70ca2 337 BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
wolfSSL 0:1239e9b70ca2 338 BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
wolfSSL 0:1239e9b70ca2 339 BAD, BAD, /* G - ` */
wolfSSL 0:1239e9b70ca2 340 10, 11, 12, 13, 14, 15 /* lower case a-f */
wolfSSL 0:1239e9b70ca2 341 }; /* A starts at 0x41 not 0x3A */
wolfSSL 0:1239e9b70ca2 342
wolfSSL 0:1239e9b70ca2 343 int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
wolfSSL 0:1239e9b70ca2 344 {
wolfSSL 0:1239e9b70ca2 345 word32 inIdx = 0;
wolfSSL 0:1239e9b70ca2 346 word32 outIdx = 0;
wolfSSL 0:1239e9b70ca2 347
wolfSSL 0:1239e9b70ca2 348 if (inLen == 1 && *outLen && in) {
wolfSSL 0:1239e9b70ca2 349 byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
wolfSSL 0:1239e9b70ca2 350
wolfSSL 0:1239e9b70ca2 351 /* sanity check */
wolfSSL 0:1239e9b70ca2 352 if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
wolfSSL 0:1239e9b70ca2 353 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 354
wolfSSL 0:1239e9b70ca2 355 b = hexDecode[b];
wolfSSL 0:1239e9b70ca2 356
wolfSSL 0:1239e9b70ca2 357 if (b == BAD)
wolfSSL 0:1239e9b70ca2 358 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 359
wolfSSL 0:1239e9b70ca2 360 out[outIdx++] = b;
wolfSSL 0:1239e9b70ca2 361
wolfSSL 0:1239e9b70ca2 362 *outLen = outIdx;
wolfSSL 0:1239e9b70ca2 363 return 0;
wolfSSL 0:1239e9b70ca2 364 }
wolfSSL 0:1239e9b70ca2 365
wolfSSL 0:1239e9b70ca2 366 if (inLen % 2)
wolfSSL 0:1239e9b70ca2 367 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 368
wolfSSL 0:1239e9b70ca2 369 if (*outLen < (inLen / 2))
wolfSSL 0:1239e9b70ca2 370 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 371
wolfSSL 0:1239e9b70ca2 372 while (inLen) {
wolfSSL 0:1239e9b70ca2 373 byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
wolfSSL 0:1239e9b70ca2 374 byte b2 = in[inIdx++] - 0x30;
wolfSSL 0:1239e9b70ca2 375
wolfSSL 0:1239e9b70ca2 376 /* sanity checks */
wolfSSL 0:1239e9b70ca2 377 if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
wolfSSL 0:1239e9b70ca2 378 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 379 if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0]))
wolfSSL 0:1239e9b70ca2 380 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 381
wolfSSL 0:1239e9b70ca2 382 b = hexDecode[b];
wolfSSL 0:1239e9b70ca2 383 b2 = hexDecode[b2];
wolfSSL 0:1239e9b70ca2 384
wolfSSL 0:1239e9b70ca2 385 if (b == BAD || b2 == BAD)
wolfSSL 0:1239e9b70ca2 386 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 387
wolfSSL 0:1239e9b70ca2 388 out[outIdx++] = (byte)((b << 4) | b2);
wolfSSL 0:1239e9b70ca2 389 inLen -= 2;
wolfSSL 0:1239e9b70ca2 390 }
wolfSSL 0:1239e9b70ca2 391
wolfSSL 0:1239e9b70ca2 392 *outLen = outIdx;
wolfSSL 0:1239e9b70ca2 393 return 0;
wolfSSL 0:1239e9b70ca2 394 }
wolfSSL 0:1239e9b70ca2 395
wolfSSL 0:1239e9b70ca2 396
wolfSSL 0:1239e9b70ca2 397 #endif /* (OPENSSL_EXTRA) || (HAVE_WEBSERVER) || (HAVE_FIPS) */
wolfSSL 0:1239e9b70ca2 398
wolfSSL 0:1239e9b70ca2 399 #endif /* NO_CODING */
wolfSSL 0:1239e9b70ca2 400