mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-02 12:33:40 +00:00
cc1920749e
Your redbean can now interoperate with clients that require TLS crypto. This is accomplished using a protocol polyglot that lets us distinguish between HTTP and HTTPS regardless of the port number. Certificates will be generated automatically, if none are supplied by the user. Footprint increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb - Add lseek() polyfills for ZIP executable - Automatically polyfill /tmp/FOO paths on NT - Fix readdir() / ftw() / nftw() bugs on Windows - Introduce -B flag for slower SSL that's stronger - Remove mbedtls features Cosmopolitan doesn't need - Have base64 decoder support the uri-safe alternative - Remove Truncated HMAC because it's forbidden by the IETF - Add all the mbedtls test suites and make them go 3x faster - Support opendir() / readdir() / closedir() on ZIP executable - Use Everest for ECDHE-ECDSA because it's so good it's so good - Add tinier implementation of sha1 since it's not worth the rom - Add chi-square monte-carlo mean correlation tests for getrandom() - Source entropy on Windows from the proper interface everyone uses We're continuing to outperform NGINX and other servers on raw message throughput. Using SSL means that instead of 1,000,000 qps you can get around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL handshakes, since redbean can do 2,627 per second and NGINX does 4.3k Right now, the SSL UX story works best if you give your redbean a key signing key since that can be easily generated by openssl using a one liner then redbean will do all the things that are impossibly hard to do like signing ecdsa and rsa certificates that'll work in chrome. We should integrate the let's encrypt acme protocol in the future. Live Demo: https://redbean.justine.lol/ Root Cert: https://redbean.justine.lol/redbean1.crt
192 lines
4.7 KiB
C
192 lines
4.7 KiB
C
#include "libc/str/str.h"
|
|
#include "third_party/mbedtls/common.h"
|
|
#include "third_party/mbedtls/error.h"
|
|
#include "third_party/mbedtls/hkdf.h"
|
|
#include "third_party/mbedtls/platform.h"
|
|
|
|
asm(".ident\t\"\\n\\n\
|
|
Mbed TLS (Apache 2.0)\\n\
|
|
Copyright ARM Limited\\n\
|
|
Copyright Mbed TLS Contributors\"");
|
|
asm(".include \"libc/disclaimer.inc\"");
|
|
|
|
/* clang-format off */
|
|
/*
|
|
* HKDF implementation -- RFC 5869
|
|
*
|
|
* Copyright The Mbed TLS Contributors
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
* not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt,
|
|
size_t salt_len, const unsigned char *ikm, size_t ikm_len,
|
|
const unsigned char *info, size_t info_len,
|
|
unsigned char *okm, size_t okm_len )
|
|
{
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
unsigned char prk[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, prk );
|
|
|
|
if( ret == 0 )
|
|
{
|
|
ret = mbedtls_hkdf_expand( md, prk, mbedtls_md_get_size( md ),
|
|
info, info_len, okm, okm_len );
|
|
}
|
|
|
|
mbedtls_platform_zeroize( prk, sizeof( prk ) );
|
|
|
|
return( ret );
|
|
}
|
|
|
|
int mbedtls_hkdf_extract( const mbedtls_md_info_t *md,
|
|
const unsigned char *salt, size_t salt_len,
|
|
const unsigned char *ikm, size_t ikm_len,
|
|
unsigned char *prk )
|
|
{
|
|
unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
|
|
|
|
if( salt == NULL )
|
|
{
|
|
size_t hash_len;
|
|
|
|
if( salt_len != 0 )
|
|
{
|
|
return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
|
|
}
|
|
|
|
hash_len = mbedtls_md_get_size( md );
|
|
|
|
if( hash_len == 0 )
|
|
{
|
|
return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
|
|
}
|
|
|
|
salt = null_salt;
|
|
salt_len = hash_len;
|
|
}
|
|
|
|
return( mbedtls_md_hmac( md, salt, salt_len, ikm, ikm_len, prk ) );
|
|
}
|
|
|
|
int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk,
|
|
size_t prk_len, const unsigned char *info,
|
|
size_t info_len, unsigned char *okm, size_t okm_len )
|
|
{
|
|
size_t hash_len;
|
|
size_t where = 0;
|
|
size_t n;
|
|
size_t t_len = 0;
|
|
size_t i;
|
|
int ret = 0;
|
|
mbedtls_md_context_t ctx;
|
|
unsigned char t[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
if( okm == NULL )
|
|
{
|
|
return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
|
|
}
|
|
|
|
hash_len = mbedtls_md_get_size( md );
|
|
|
|
if( prk_len < hash_len || hash_len == 0 )
|
|
{
|
|
return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
|
|
}
|
|
|
|
if( info == NULL )
|
|
{
|
|
info = (const unsigned char *) "";
|
|
info_len = 0;
|
|
}
|
|
|
|
n = okm_len / hash_len;
|
|
|
|
if( okm_len % hash_len != 0 )
|
|
{
|
|
n++;
|
|
}
|
|
|
|
/*
|
|
* Per RFC 5869 Section 2.3, okm_len must not exceed
|
|
* 255 times the hash length
|
|
*/
|
|
if( n > 255 )
|
|
{
|
|
return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
|
|
}
|
|
|
|
mbedtls_md_init( &ctx );
|
|
|
|
if( ( ret = mbedtls_md_setup( &ctx, md, 1 ) ) != 0 )
|
|
{
|
|
goto exit;
|
|
}
|
|
|
|
memset( t, 0, hash_len );
|
|
|
|
/*
|
|
* Compute T = T(1) | T(2) | T(3) | ... | T(N)
|
|
* Where T(N) is defined in RFC 5869 Section 2.3
|
|
*/
|
|
for( i = 1; i <= n; i++ )
|
|
{
|
|
size_t num_to_copy;
|
|
unsigned char c = i & 0xff;
|
|
|
|
ret = mbedtls_md_hmac_starts( &ctx, prk, prk_len );
|
|
if( ret != 0 )
|
|
{
|
|
goto exit;
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_update( &ctx, t, t_len );
|
|
if( ret != 0 )
|
|
{
|
|
goto exit;
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_update( &ctx, info, info_len );
|
|
if( ret != 0 )
|
|
{
|
|
goto exit;
|
|
}
|
|
|
|
/* The constant concatenated to the end of each T(n) is a single octet.
|
|
* */
|
|
ret = mbedtls_md_hmac_update( &ctx, &c, 1 );
|
|
if( ret != 0 )
|
|
{
|
|
goto exit;
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_finish( &ctx, t );
|
|
if( ret != 0 )
|
|
{
|
|
goto exit;
|
|
}
|
|
|
|
num_to_copy = i != n ? hash_len : okm_len - where;
|
|
memcpy( okm + where, t, num_to_copy );
|
|
where += hash_len;
|
|
t_len = hash_len;
|
|
}
|
|
|
|
exit:
|
|
mbedtls_md_free( &ctx );
|
|
mbedtls_platform_zeroize( t, sizeof( t ) );
|
|
|
|
return( ret );
|
|
}
|