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