|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright (c) 2007, Cameron Rich |
| 4 | + * |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * * Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * * Neither the name of the axTLS project nor the names of its contributors |
| 16 | + * may be used to endorse or promote products derived from this software |
| 17 | + * without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +/* |
| 33 | + * Enable a subset of espressif platom ssl compatible functions. We don't aim to be 100% |
| 34 | + * compatible - just to be able to do basic ports etc. |
| 35 | + * |
| 36 | + * Only really tested on mini_httpd, so I'm not too sure how extensive this |
| 37 | + * port is. |
| 38 | + */ |
| 39 | +#include "ssl_pm.h" |
| 40 | +#include "lwip/err.h" |
| 41 | +#include "openssl/ssl.h" |
| 42 | + |
| 43 | +typedef int MD5_CTX; |
| 44 | +typedef int X509_CTX; |
| 45 | + |
| 46 | +/* |
| 47 | +Sets up digest context ctx to use a digest type from ENGINE impl. |
| 48 | +Type will typically be supplied by a function such as EVP_sha1(). |
| 49 | +If impl is NULL then the default implementation of digest type is used. |
| 50 | +*/ |
| 51 | +void EVP_DigestInit(MD5_CTX* ctx, uint8* out) |
| 52 | +{ |
| 53 | + return; |
| 54 | +} |
| 55 | + |
| 56 | +/* |
| 57 | +Hashes ilen bytes of data at input into the digest context ctx. |
| 58 | +This function can be called several times on the same ctx to hash additional data. |
| 59 | +*/ |
| 60 | +void EVP_DigestUpdate(MD5_CTX* ctx, const uint8_t* input, int ilen) |
| 61 | +{ |
| 62 | + return; |
| 63 | +} |
| 64 | + |
| 65 | +/* |
| 66 | +Retrieves the digest value from ctx and places it in output. |
| 67 | +If the olen parameter is not NULL then the number of bytes of data written (i.e. the length of the digest) |
| 68 | +will be written to the integer at s, at most EVP_MAX_MD_SIZE bytes will be written. |
| 69 | +After calling EVP_DigestFinal() no additional calls to EVP_DigestUpdate() can be made, |
| 70 | +but EVP_DigestInit() can be called to initialize a new digest operation. |
| 71 | +*/ |
| 72 | +void EVP_DigestFinal(MD5_CTX* ctx, uint8_t* output, uint16* olen) |
| 73 | +{ |
| 74 | + return; |
| 75 | +} |
| 76 | + |
| 77 | +/* |
| 78 | +Return EVP_MD structures for the SHA1 digest algorithms respectively. |
| 79 | +The associated signature algorithm is RSA in each case. |
| 80 | +*/ |
| 81 | +char* EVP_sha1(void) |
| 82 | +{ |
| 83 | + return NULL; |
| 84 | +} |
| 85 | + |
| 86 | +/* |
| 87 | +cleans up EVP. |
| 88 | +*/ |
| 89 | +char* EVP_cleanup(void) |
| 90 | +{ |
| 91 | + return NULL; |
| 92 | +} |
| 93 | + |
| 94 | +static const unsigned char base64_enc_map[64] = { |
| 95 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
| 96 | + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
| 97 | + 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', |
| 98 | + 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
| 99 | + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', |
| 100 | + 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', |
| 101 | + '8', '9', '+', '/' |
| 102 | +}; |
| 103 | + |
| 104 | +/****************************************************************************** |
| 105 | + * FunctionName : base64_encode |
| 106 | + * Description : Encode Base64 data |
| 107 | + * Parameters : dst -- destination buffer |
| 108 | + * dlen -- destination buffer len |
| 109 | + * olen -- output buffer len |
| 110 | + * src -- source buffer |
| 111 | + * slen -- source buffer len |
| 112 | + * Returns : none |
| 113 | +*******************************************************************************/ |
| 114 | +int base64_encode(uint8* dst, size_t dlen, size_t* olen, |
| 115 | + const uint8_t* src, size_t slen) |
| 116 | +{ |
| 117 | + size_t i, n; |
| 118 | + int C1, C2, C3; |
| 119 | + unsigned char* p = NULL; |
| 120 | + |
| 121 | + if (slen == 0) { |
| 122 | + *olen = 0; |
| 123 | + return 0; |
| 124 | + } |
| 125 | + |
| 126 | + n = (slen << 3) / 6; |
| 127 | + |
| 128 | + switch ((slen << 3) - (n * 6)) { |
| 129 | + case 2: |
| 130 | + n += 3; |
| 131 | + break; |
| 132 | + |
| 133 | + case 4: |
| 134 | + n += 2; |
| 135 | + break; |
| 136 | + |
| 137 | + default: |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + if (dlen < (n + 1)) { |
| 142 | + *olen = n + 1; |
| 143 | + return -42; |
| 144 | + } |
| 145 | + |
| 146 | + n = (slen / 3) * 3; |
| 147 | + |
| 148 | + for (i = 0, p = dst; i < n; i += 3) { |
| 149 | + C1 = *src++; |
| 150 | + C2 = *src++; |
| 151 | + C3 = *src++; |
| 152 | + |
| 153 | + *p++ = base64_enc_map[(C1 >> 2) & 0x3F]; |
| 154 | + *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F]; |
| 155 | + *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F]; |
| 156 | + *p++ = base64_enc_map[C3 & 0x3F]; |
| 157 | + } |
| 158 | + |
| 159 | + if (i < slen) { |
| 160 | + C1 = *src++; |
| 161 | + C2 = ((i + 1) < slen) ? *src++ : 0; |
| 162 | + |
| 163 | + *p++ = base64_enc_map[(C1 >> 2) & 0x3F]; |
| 164 | + *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F]; |
| 165 | + |
| 166 | + if ((i + 1) < slen) { |
| 167 | + *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F]; |
| 168 | + } else { |
| 169 | + *p++ = '='; |
| 170 | + } |
| 171 | + |
| 172 | + *p++ = '='; |
| 173 | + |
| 174 | + *olen = p - dst; |
| 175 | + *p = 0; |
| 176 | + |
| 177 | + return 0; |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +/* |
| 182 | +Return server SSLv23 method. |
| 183 | +*/ |
| 184 | +const SSL_METHOD* SSLv23_server_method(void) |
| 185 | +{ |
| 186 | + return NULL; |
| 187 | +} |
| 188 | + |
| 189 | +/* |
| 190 | +Return client SSLv23 method. |
| 191 | +*/ |
| 192 | +const SSL_METHOD* SSLv23_client_method(void) |
| 193 | +{ |
| 194 | + return NULL; |
| 195 | +} |
| 196 | + |
| 197 | +/* |
| 198 | +Add crt file for ssl_ctx. |
| 199 | +*/ |
| 200 | +int SSL_CTX_use_certificate_chain_file(SSL_CTX* ssl_ctx, const char* file) |
| 201 | +{ |
| 202 | + return 1; |
| 203 | +} |
| 204 | + |
| 205 | +/****************************************************************************** |
| 206 | + * FunctionName : SSL_CTX_load_verify_locations |
| 207 | + * Description : load verify locations |
| 208 | + * Parameters : ctx -- espconn to set for client or server |
| 209 | + * cafile -- ca file |
| 210 | + * CApath -- no use |
| 211 | + * Returns : 1 |
| 212 | +*******************************************************************************/ |
| 213 | +int SSL_CTX_load_verify_locations(SSL_CTX* ctx, const char* CAfile, |
| 214 | + const char* CApath) |
| 215 | +{ |
| 216 | + X509* cacrt = NULL; |
| 217 | + cacrt = d2i_X509(NULL, CAfile, strlen(CAfile)); |
| 218 | + |
| 219 | + if (cacrt) { |
| 220 | + SSL_CTX_add_client_CA(ctx, cacrt); |
| 221 | + } |
| 222 | + |
| 223 | + return 1; |
| 224 | +} |
| 225 | + |
| 226 | +/* |
| 227 | +Return SSLv23 method. |
| 228 | +*/ |
| 229 | +void SSLv23_method(void) |
| 230 | +{ |
| 231 | + return; |
| 232 | +} |
| 233 | + |
| 234 | +/* |
| 235 | +Check private key in ctx. |
| 236 | +*/ |
| 237 | +int SSL_CTX_check_private_key(const SSL_CTX* ctx) |
| 238 | +{ |
| 239 | + return 1; |
| 240 | +} |
| 241 | + |
| 242 | +/* |
| 243 | +Init SSL library. |
| 244 | +*/ |
| 245 | +void SSL_library_init(void) |
| 246 | +{ |
| 247 | + return; |
| 248 | +} |
| 249 | + |
| 250 | +/* |
| 251 | +Set SSL_CTX verify paths. |
| 252 | +*/ |
| 253 | +int SSL_CTX_set_default_verify_paths(SSL_CTX* ssl_ctx) |
| 254 | +{ |
| 255 | + return 1; |
| 256 | +} |
| 257 | + |
| 258 | +/* |
| 259 | +Get current cert in x509 store ctx. |
| 260 | +*/ |
| 261 | +X509_CTX* X509_STORE_CTX_get_current_cert(X509_CTX* store) |
| 262 | +{ |
| 263 | + return NULL; |
| 264 | +} |
| 265 | + |
| 266 | +/* |
| 267 | +Prints an ASCII version of x509 ctx. |
| 268 | +*/ |
| 269 | +void X509_NAME_oneline(X509_CTX* x509_CTX) |
| 270 | +{ |
| 271 | + return; |
| 272 | +} |
| 273 | + |
| 274 | +/* |
| 275 | +Get issuer name. |
| 276 | +*/ |
| 277 | +char* X509_get_issuer_name(X509_CTX* x509_CTX) |
| 278 | +{ |
| 279 | + return NULL; |
| 280 | +} |
| 281 | + |
| 282 | +/* |
| 283 | +Get subject name. |
| 284 | +*/ |
| 285 | +char* X509_get_subject_name(X509_CTX* x509_CTX) |
| 286 | +{ |
| 287 | + return NULL; |
| 288 | +} |
| 289 | + |
| 290 | +/* |
| 291 | +Returns the depth of the error. |
| 292 | +*/ |
| 293 | +void X509_STORE_CTX_get_error_depth(X509_CTX* x509_CTX) |
| 294 | +{ |
| 295 | + return; |
| 296 | +} |
| 297 | +/* |
| 298 | +Returns the error code of ctx. |
| 299 | +*/ |
| 300 | +char* X509_STORE_CTX_get_error(X509_CTX* x509_CTX) |
| 301 | +{ |
| 302 | + return NULL; |
| 303 | +} |
| 304 | + |
| 305 | +/* |
| 306 | +Returns a human readable error string for verification error n. |
| 307 | +*/ |
| 308 | +char* X509_verify_cert_error_string(X509_CTX* x509_CTX) |
| 309 | +{ |
| 310 | + return NULL; |
| 311 | +} |
| 312 | + |
| 313 | +/* |
| 314 | +Cleanup extra crypto data. |
| 315 | +*/ |
| 316 | +void CRYPTO_cleanup_all_ex_data(void) |
| 317 | +{ |
| 318 | + return; |
| 319 | +} |
| 320 | + |
| 321 | +/* |
| 322 | +Get error number. |
| 323 | +*/ |
| 324 | +int ERR_get_error(void) |
| 325 | +{ |
| 326 | + return 0; |
| 327 | +} |
| 328 | + |
| 329 | +/* |
| 330 | +Generates a human-readable string representing the error code e, |
| 331 | +and places it at buf. buf must be at least 120 bytes long. |
| 332 | +Buf may not be NULL. |
| 333 | +*/ |
| 334 | +void ERR_error_string_n(uint32 error, char* out, uint32 olen) |
| 335 | +{ |
| 336 | + return; |
| 337 | +} |
| 338 | + |
| 339 | +/* |
| 340 | +Generates a human-readable string representing the error code e, |
| 341 | +and places it at buf. buf must be at least 120 bytes long. |
| 342 | +If buf is NULL , the error string is placed in a static buffer. |
| 343 | +*/ |
| 344 | +char* ERR_error_string(unsigned long e, char* ret) |
| 345 | +{ |
| 346 | + return; |
| 347 | +} |
| 348 | + |
| 349 | +/* |
| 350 | +Frees all previously loaded error strings. |
| 351 | +*/ |
| 352 | +void ERR_free_strings(void) |
| 353 | +{ |
| 354 | + return; |
| 355 | +} |
| 356 | + |
| 357 | +/* |
| 358 | +Convert an internal error to a string representation. |
| 359 | +*/ |
| 360 | +const char* ERR_strerror(uint32 error) |
| 361 | +{ |
| 362 | + return lwip_strerr(error); |
| 363 | +} |
0 commit comments