cya_u
Fork of CyaSSL-forEncrypt by
coding.c@0:5045d2638c29, 2011-02-05 (annotated)
- Committer:
- toddouska
- Date:
- Sat Feb 05 01:09:17 2011 +0000
- Revision:
- 0:5045d2638c29
Beta Version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
toddouska | 0:5045d2638c29 | 1 | /* coding.c |
toddouska | 0:5045d2638c29 | 2 | * |
toddouska | 0:5045d2638c29 | 3 | * Copyright (C) 2006-2009 Sawtooth Consulting Ltd. |
toddouska | 0:5045d2638c29 | 4 | * |
toddouska | 0:5045d2638c29 | 5 | * This file is part of CyaSSL. |
toddouska | 0:5045d2638c29 | 6 | * |
toddouska | 0:5045d2638c29 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
toddouska | 0:5045d2638c29 | 8 | * it under the terms of the GNU General Public License as published by |
toddouska | 0:5045d2638c29 | 9 | * the Free Software Foundation; either version 2 of the License, or |
toddouska | 0:5045d2638c29 | 10 | * (at your option) any later version. |
toddouska | 0:5045d2638c29 | 11 | * |
toddouska | 0:5045d2638c29 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
toddouska | 0:5045d2638c29 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
toddouska | 0:5045d2638c29 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
toddouska | 0:5045d2638c29 | 15 | * GNU General Public License for more details. |
toddouska | 0:5045d2638c29 | 16 | * |
toddouska | 0:5045d2638c29 | 17 | * You should have received a copy of the GNU General Public License |
toddouska | 0:5045d2638c29 | 18 | * along with this program; if not, write to the Free Software |
toddouska | 0:5045d2638c29 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
toddouska | 0:5045d2638c29 | 20 | */ |
toddouska | 0:5045d2638c29 | 21 | |
toddouska | 0:5045d2638c29 | 22 | |
toddouska | 0:5045d2638c29 | 23 | #include "coding.h" |
toddouska | 0:5045d2638c29 | 24 | |
toddouska | 0:5045d2638c29 | 25 | |
toddouska | 0:5045d2638c29 | 26 | enum { |
toddouska | 0:5045d2638c29 | 27 | BAD = 0xFF, /* invalid encoding */ |
toddouska | 0:5045d2638c29 | 28 | PAD = '=', |
toddouska | 0:5045d2638c29 | 29 | PEM_LINE_SZ = 64 |
toddouska | 0:5045d2638c29 | 30 | }; |
toddouska | 0:5045d2638c29 | 31 | |
toddouska | 0:5045d2638c29 | 32 | |
toddouska | 0:5045d2638c29 | 33 | static |
toddouska | 0:5045d2638c29 | 34 | const byte base64Decode[] = { 62, BAD, BAD, BAD, 63, /* + starts at 0x2B */ |
toddouska | 0:5045d2638c29 | 35 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, |
toddouska | 0:5045d2638c29 | 36 | BAD, BAD, BAD, BAD, BAD, BAD, BAD, |
toddouska | 0:5045d2638c29 | 37 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
toddouska | 0:5045d2638c29 | 38 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
toddouska | 0:5045d2638c29 | 39 | 20, 21, 22, 23, 24, 25, |
toddouska | 0:5045d2638c29 | 40 | BAD, BAD, BAD, BAD, BAD, BAD, |
toddouska | 0:5045d2638c29 | 41 | 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, |
toddouska | 0:5045d2638c29 | 42 | 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, |
toddouska | 0:5045d2638c29 | 43 | 46, 47, 48, 49, 50, 51 |
toddouska | 0:5045d2638c29 | 44 | }; |
toddouska | 0:5045d2638c29 | 45 | |
toddouska | 0:5045d2638c29 | 46 | |
toddouska | 0:5045d2638c29 | 47 | int Base64Decode(const byte* in, word32 inLen, byte* out, word32* outLen) |
toddouska | 0:5045d2638c29 | 48 | { |
toddouska | 0:5045d2638c29 | 49 | word32 i = 0; |
toddouska | 0:5045d2638c29 | 50 | word32 j = 0; |
toddouska | 0:5045d2638c29 | 51 | word32 plainSz = inLen - ((inLen + (PEM_LINE_SZ - 1)) / PEM_LINE_SZ ); |
toddouska | 0:5045d2638c29 | 52 | |
toddouska | 0:5045d2638c29 | 53 | plainSz = (plainSz * 3 + 3) / 4; |
toddouska | 0:5045d2638c29 | 54 | if (plainSz > *outLen) return -1; |
toddouska | 0:5045d2638c29 | 55 | |
toddouska | 0:5045d2638c29 | 56 | while (inLen > 3) { |
toddouska | 0:5045d2638c29 | 57 | byte b1, b2, b3; |
toddouska | 0:5045d2638c29 | 58 | byte e1 = in[j++]; |
toddouska | 0:5045d2638c29 | 59 | byte e2 = in[j++]; |
toddouska | 0:5045d2638c29 | 60 | byte e3 = in[j++]; |
toddouska | 0:5045d2638c29 | 61 | byte e4 = in[j++]; |
toddouska | 0:5045d2638c29 | 62 | |
toddouska | 0:5045d2638c29 | 63 | int pad3 = 0; |
toddouska | 0:5045d2638c29 | 64 | int pad4 = 0; |
toddouska | 0:5045d2638c29 | 65 | |
toddouska | 0:5045d2638c29 | 66 | if (e1 == 0) /* end file 0's */ |
toddouska | 0:5045d2638c29 | 67 | break; |
toddouska | 0:5045d2638c29 | 68 | if (e3 == PAD) |
toddouska | 0:5045d2638c29 | 69 | pad3 = 1; |
toddouska | 0:5045d2638c29 | 70 | if (e4 == PAD) |
toddouska | 0:5045d2638c29 | 71 | pad4 = 1; |
toddouska | 0:5045d2638c29 | 72 | |
toddouska | 0:5045d2638c29 | 73 | e1 = base64Decode[e1 - 0x2B]; |
toddouska | 0:5045d2638c29 | 74 | e2 = base64Decode[e2 - 0x2B]; |
toddouska | 0:5045d2638c29 | 75 | e3 = (e3 == PAD) ? 0 : base64Decode[e3 - 0x2B]; |
toddouska | 0:5045d2638c29 | 76 | e4 = (e4 == PAD) ? 0 : base64Decode[e4 - 0x2B]; |
toddouska | 0:5045d2638c29 | 77 | |
toddouska | 0:5045d2638c29 | 78 | b1 = (e1 << 2) | (e2 >> 4); |
toddouska | 0:5045d2638c29 | 79 | b2 = ((e2 & 0xF) << 4) | (e3 >> 2); |
toddouska | 0:5045d2638c29 | 80 | b3 = ((e3 & 0x3) << 6) | e4; |
toddouska | 0:5045d2638c29 | 81 | |
toddouska | 0:5045d2638c29 | 82 | out[i++] = b1; |
toddouska | 0:5045d2638c29 | 83 | if (!pad3) |
toddouska | 0:5045d2638c29 | 84 | out[i++] = b2; |
toddouska | 0:5045d2638c29 | 85 | if (!pad4) |
toddouska | 0:5045d2638c29 | 86 | out[i++] = b3; |
toddouska | 0:5045d2638c29 | 87 | else |
toddouska | 0:5045d2638c29 | 88 | break; |
toddouska | 0:5045d2638c29 | 89 | |
toddouska | 0:5045d2638c29 | 90 | inLen -= 4; |
toddouska | 0:5045d2638c29 | 91 | if (in[j] == ' ' || in[j] == '\r' || in[j] == '\n') { |
toddouska | 0:5045d2638c29 | 92 | byte endLine = in[j++]; |
toddouska | 0:5045d2638c29 | 93 | inLen--; |
toddouska | 0:5045d2638c29 | 94 | while (endLine == ' ') { /* allow trailing whitespace */ |
toddouska | 0:5045d2638c29 | 95 | endLine = in[j++]; |
toddouska | 0:5045d2638c29 | 96 | inLen--; |
toddouska | 0:5045d2638c29 | 97 | } |
toddouska | 0:5045d2638c29 | 98 | if (endLine == '\r') { |
toddouska | 0:5045d2638c29 | 99 | endLine = in[j++]; |
toddouska | 0:5045d2638c29 | 100 | inLen--; |
toddouska | 0:5045d2638c29 | 101 | } |
toddouska | 0:5045d2638c29 | 102 | if (endLine != '\n') |
toddouska | 0:5045d2638c29 | 103 | return -1; |
toddouska | 0:5045d2638c29 | 104 | } |
toddouska | 0:5045d2638c29 | 105 | } |
toddouska | 0:5045d2638c29 | 106 | *outLen = i; |
toddouska | 0:5045d2638c29 | 107 | |
toddouska | 0:5045d2638c29 | 108 | return 0; |
toddouska | 0:5045d2638c29 | 109 | } |
toddouska | 0:5045d2638c29 | 110 | |
toddouska | 0:5045d2638c29 | 111 | |
toddouska | 0:5045d2638c29 | 112 | #if defined(OPENSSL_EXTRA) || defined (SESSION_CERTS) || defined(CYASSL_KEY_GEN) || defined(CYASSL_CERT_GEN) |
toddouska | 0:5045d2638c29 | 113 | |
toddouska | 0:5045d2638c29 | 114 | static |
toddouska | 0:5045d2638c29 | 115 | const byte base64Encode[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
toddouska | 0:5045d2638c29 | 116 | 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
toddouska | 0:5045d2638c29 | 117 | 'U', 'V', 'W', 'X', 'Y', 'Z', |
toddouska | 0:5045d2638c29 | 118 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
toddouska | 0:5045d2638c29 | 119 | 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', |
toddouska | 0:5045d2638c29 | 120 | 'u', 'v', 'w', 'x', 'y', 'z', |
toddouska | 0:5045d2638c29 | 121 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
toddouska | 0:5045d2638c29 | 122 | '+', '/' |
toddouska | 0:5045d2638c29 | 123 | }; |
toddouska | 0:5045d2638c29 | 124 | |
toddouska | 0:5045d2638c29 | 125 | |
toddouska | 0:5045d2638c29 | 126 | /* porting assistance from yaSSL by Raphael HUCK */ |
toddouska | 0:5045d2638c29 | 127 | int Base64Encode(const byte* in, word32 inLen, byte* out, word32* outLen) |
toddouska | 0:5045d2638c29 | 128 | { |
toddouska | 0:5045d2638c29 | 129 | word32 i = 0, |
toddouska | 0:5045d2638c29 | 130 | j = 0, |
toddouska | 0:5045d2638c29 | 131 | n = 0; /* new line counter */ |
toddouska | 0:5045d2638c29 | 132 | |
toddouska | 0:5045d2638c29 | 133 | word32 outSz = (inLen + 3 - 1) / 3 * 4; |
toddouska | 0:5045d2638c29 | 134 | outSz += (outSz + PEM_LINE_SZ - 1) / PEM_LINE_SZ; /* new lines */ |
toddouska | 0:5045d2638c29 | 135 | |
toddouska | 0:5045d2638c29 | 136 | if (outSz > *outLen) return -1; |
toddouska | 0:5045d2638c29 | 137 | |
toddouska | 0:5045d2638c29 | 138 | while (inLen > 2) { |
toddouska | 0:5045d2638c29 | 139 | byte b1 = in[j++]; |
toddouska | 0:5045d2638c29 | 140 | byte b2 = in[j++]; |
toddouska | 0:5045d2638c29 | 141 | byte b3 = in[j++]; |
toddouska | 0:5045d2638c29 | 142 | |
toddouska | 0:5045d2638c29 | 143 | /* encoded idx */ |
toddouska | 0:5045d2638c29 | 144 | byte e1 = b1 >> 2; |
toddouska | 0:5045d2638c29 | 145 | byte e2 = ((b1 & 0x3) << 4) | (b2 >> 4); |
toddouska | 0:5045d2638c29 | 146 | byte e3 = ((b2 & 0xF) << 2) | (b3 >> 6); |
toddouska | 0:5045d2638c29 | 147 | byte e4 = b3 & 0x3F; |
toddouska | 0:5045d2638c29 | 148 | |
toddouska | 0:5045d2638c29 | 149 | /* store */ |
toddouska | 0:5045d2638c29 | 150 | out[i++] = base64Encode[e1]; |
toddouska | 0:5045d2638c29 | 151 | out[i++] = base64Encode[e2]; |
toddouska | 0:5045d2638c29 | 152 | out[i++] = base64Encode[e3]; |
toddouska | 0:5045d2638c29 | 153 | out[i++] = base64Encode[e4]; |
toddouska | 0:5045d2638c29 | 154 | |
toddouska | 0:5045d2638c29 | 155 | inLen -= 3; |
toddouska | 0:5045d2638c29 | 156 | |
toddouska | 0:5045d2638c29 | 157 | if ((++n % (PEM_LINE_SZ / 4)) == 0 && inLen) |
toddouska | 0:5045d2638c29 | 158 | out[i++] = '\n'; |
toddouska | 0:5045d2638c29 | 159 | } |
toddouska | 0:5045d2638c29 | 160 | |
toddouska | 0:5045d2638c29 | 161 | /* last integral */ |
toddouska | 0:5045d2638c29 | 162 | if (inLen) { |
toddouska | 0:5045d2638c29 | 163 | int twoBytes = (inLen == 2); |
toddouska | 0:5045d2638c29 | 164 | |
toddouska | 0:5045d2638c29 | 165 | byte b1 = in[j++]; |
toddouska | 0:5045d2638c29 | 166 | byte b2 = (twoBytes) ? in[j++] : 0; |
toddouska | 0:5045d2638c29 | 167 | |
toddouska | 0:5045d2638c29 | 168 | byte e1 = b1 >> 2; |
toddouska | 0:5045d2638c29 | 169 | byte e2 = ((b1 & 0x3) << 4) | (b2 >> 4); |
toddouska | 0:5045d2638c29 | 170 | byte e3 = (b2 & 0xF) << 2; |
toddouska | 0:5045d2638c29 | 171 | |
toddouska | 0:5045d2638c29 | 172 | out[i++] = base64Encode[e1]; |
toddouska | 0:5045d2638c29 | 173 | out[i++] = base64Encode[e2]; |
toddouska | 0:5045d2638c29 | 174 | out[i++] = (twoBytes) ? base64Encode[e3] : PAD; |
toddouska | 0:5045d2638c29 | 175 | out[i++] = PAD; |
toddouska | 0:5045d2638c29 | 176 | } |
toddouska | 0:5045d2638c29 | 177 | |
toddouska | 0:5045d2638c29 | 178 | out[i++] = '\n'; |
toddouska | 0:5045d2638c29 | 179 | if (i != outSz) |
toddouska | 0:5045d2638c29 | 180 | return -1; |
toddouska | 0:5045d2638c29 | 181 | *outLen = outSz; |
toddouska | 0:5045d2638c29 | 182 | |
toddouska | 0:5045d2638c29 | 183 | return 0; |
toddouska | 0:5045d2638c29 | 184 | } |
toddouska | 0:5045d2638c29 | 185 | |
toddouska | 0:5045d2638c29 | 186 | |
toddouska | 0:5045d2638c29 | 187 | static |
toddouska | 0:5045d2638c29 | 188 | const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
toddouska | 0:5045d2638c29 | 189 | BAD, BAD, BAD, BAD, BAD, BAD, BAD, |
toddouska | 0:5045d2638c29 | 190 | 10, 11, 12, 13, 14, 15 |
toddouska | 0:5045d2638c29 | 191 | }; /* A starts at 0x41 not 0x3A */ |
toddouska | 0:5045d2638c29 | 192 | |
toddouska | 0:5045d2638c29 | 193 | int Base16Decode(const byte* in, word32 inLen, byte* out, word32* outLen) |
toddouska | 0:5045d2638c29 | 194 | { |
toddouska | 0:5045d2638c29 | 195 | word32 inIdx = 0; |
toddouska | 0:5045d2638c29 | 196 | word32 outIdx = 0; |
toddouska | 0:5045d2638c29 | 197 | |
toddouska | 0:5045d2638c29 | 198 | if (inLen % 2) |
toddouska | 0:5045d2638c29 | 199 | return -1; |
toddouska | 0:5045d2638c29 | 200 | |
toddouska | 0:5045d2638c29 | 201 | if (*outLen < (inLen / 2)) |
toddouska | 0:5045d2638c29 | 202 | return -1; |
toddouska | 0:5045d2638c29 | 203 | |
toddouska | 0:5045d2638c29 | 204 | while (inLen) { |
toddouska | 0:5045d2638c29 | 205 | byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */ |
toddouska | 0:5045d2638c29 | 206 | byte b2 = in[inIdx++] - 0x30; |
toddouska | 0:5045d2638c29 | 207 | |
toddouska | 0:5045d2638c29 | 208 | /* sanity checks */ |
toddouska | 0:5045d2638c29 | 209 | if (b >= sizeof(hexDecode)/sizeof(hexDecode[0])) |
toddouska | 0:5045d2638c29 | 210 | return -1; |
toddouska | 0:5045d2638c29 | 211 | if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0])) |
toddouska | 0:5045d2638c29 | 212 | return -1; |
toddouska | 0:5045d2638c29 | 213 | |
toddouska | 0:5045d2638c29 | 214 | b = hexDecode[b]; |
toddouska | 0:5045d2638c29 | 215 | b2 = hexDecode[b2]; |
toddouska | 0:5045d2638c29 | 216 | |
toddouska | 0:5045d2638c29 | 217 | if (b == BAD || b2 == BAD) |
toddouska | 0:5045d2638c29 | 218 | return -1; |
toddouska | 0:5045d2638c29 | 219 | |
toddouska | 0:5045d2638c29 | 220 | out[outIdx++] = (b << 4) | b2; |
toddouska | 0:5045d2638c29 | 221 | inLen -= 2; |
toddouska | 0:5045d2638c29 | 222 | } |
toddouska | 0:5045d2638c29 | 223 | |
toddouska | 0:5045d2638c29 | 224 | *outLen = outIdx; |
toddouska | 0:5045d2638c29 | 225 | return 0; |
toddouska | 0:5045d2638c29 | 226 | } |
toddouska | 0:5045d2638c29 | 227 | |
toddouska | 0:5045d2638c29 | 228 | |
toddouska | 0:5045d2638c29 | 229 | #endif /* OPENSSL_EXTRA */ |