mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 11:37:35 +00:00
398f0c16fb
This change makes SSL virtual hosting possible. You can now load
multiple certificates for multiple domains and redbean will just
figure out which one to use, even if you only have 1 ip address.
You can also use a jumbo certificate that lists all your domains
in the the subject alternative names.
This change also makes performance improvements to MbedTLS. Here
are some benchmarks vs. cc1920749e
BEFORE AFTER (microsecs)
suite_ssl.com 2512881 191738 13.11x faster
suite_pkparse.com 36291 3295 11.01x faster
suite_x509parse.com 854669 120293 7.10x faster
suite_pkwrite.com 6549 1265 5.18x faster
suite_ecdsa.com 53347 18778 2.84x faster
suite_pk.com 49051 18717 2.62x faster
suite_ecdh.com 19535 9502 2.06x faster
suite_shax.com 15848 7965 1.99x faster
suite_rsa.com 353257 184828 1.91x faster
suite_x509write.com 162646 85733 1.90x faster
suite_ecp.com 20503 11050 1.86x faster
suite_hmac_drbg.no_reseed.com 19528 11417 1.71x faster
suite_hmac_drbg.nopr.com 12460 8010 1.56x faster
suite_mpi.com 687124 442661 1.55x faster
suite_hmac_drbg.pr.com 11890 7752 1.53x faster
There aren't any special tricks to the performance imporvements.
It's mostly due to code cleanup, assembly and intel instructions
like mulx, adox, and adcx.
395 lines
14 KiB
C
395 lines
14 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright The Mbed TLS Contributors │
|
|
│ │
|
|
│ 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. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "third_party/mbedtls/asn1write.h"
|
|
#include "third_party/mbedtls/common.h"
|
|
#include "third_party/mbedtls/error.h"
|
|
#include "third_party/mbedtls/oid.h"
|
|
#include "third_party/mbedtls/x509.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 */
|
|
/*
|
|
* X.509 base functions for creating certificates / CSRs
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
#if defined(MBEDTLS_X509_CREATE_C)
|
|
|
|
/* Structure linking OIDs for X.509 DN AttributeTypes to their
|
|
* string representations and default string encodings used by Mbed TLS. */
|
|
typedef struct {
|
|
const char *name; /* String representation of AttributeType, e.g.
|
|
* "CN" or "emailAddress". */
|
|
size_t name_len; /* Length of 'name', without trailing 0 byte. */
|
|
const char *oid; /* String representation of OID of AttributeType,
|
|
* as per RFC 5280, Appendix A.1. */
|
|
int default_tag; /* The default character encoding used for the
|
|
* given attribute type, e.g.
|
|
* MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
|
|
} x509_attr_descriptor_t;
|
|
|
|
#define ADD_STRLEN( s ) s, sizeof( s ) - 1
|
|
|
|
/* X.509 DN attributes from RFC 5280, Appendix A.1. */
|
|
static const x509_attr_descriptor_t x509_attrs[] =
|
|
{
|
|
{ ADD_STRLEN( "CN" ),
|
|
MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "commonName" ),
|
|
MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "C" ),
|
|
MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
|
|
{ ADD_STRLEN( "countryName" ),
|
|
MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
|
|
{ ADD_STRLEN( "O" ),
|
|
MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "organizationName" ),
|
|
MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "L" ),
|
|
MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "locality" ),
|
|
MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "R" ),
|
|
MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
|
|
{ ADD_STRLEN( "OU" ),
|
|
MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "organizationalUnitName" ),
|
|
MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "ST" ),
|
|
MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "stateOrProvinceName" ),
|
|
MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "emailAddress" ),
|
|
MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
|
|
{ ADD_STRLEN( "serialNumber" ),
|
|
MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
|
|
{ ADD_STRLEN( "postalAddress" ),
|
|
MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
|
|
{ ADD_STRLEN( "postalCode" ),
|
|
MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
|
|
{ ADD_STRLEN( "dnQualifier" ),
|
|
MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
|
|
{ ADD_STRLEN( "title" ),
|
|
MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "surName" ),
|
|
MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "SN" ),
|
|
MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "givenName" ),
|
|
MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "GN" ),
|
|
MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "initials" ),
|
|
MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "pseudonym" ),
|
|
MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "generationQualifier" ),
|
|
MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
|
|
{ ADD_STRLEN( "domainComponent" ),
|
|
MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
|
|
{ ADD_STRLEN( "DC" ),
|
|
MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
|
|
{ NULL, 0, NULL, MBEDTLS_ASN1_NULL }
|
|
};
|
|
|
|
static const x509_attr_descriptor_t *x509_attr_descr_from_name( const char *name, size_t name_len )
|
|
{
|
|
const x509_attr_descriptor_t *cur;
|
|
|
|
for( cur = x509_attrs; cur->name != NULL; cur++ )
|
|
if( cur->name_len == name_len &&
|
|
strncmp( cur->name, name, name_len ) == 0 )
|
|
break;
|
|
|
|
if ( cur->name == NULL )
|
|
return( NULL );
|
|
|
|
return( cur );
|
|
}
|
|
|
|
int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
|
|
{
|
|
int ret = 0;
|
|
const char *s = name, *c = s;
|
|
const char *end = s + strlen( s );
|
|
const char *oid = NULL;
|
|
const x509_attr_descriptor_t* attr_descr = NULL;
|
|
int in_tag = 1;
|
|
char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
|
|
char *d = data;
|
|
|
|
/* Clear existing chain if present */
|
|
mbedtls_asn1_free_named_data_list( head );
|
|
|
|
while( c <= end )
|
|
{
|
|
if( in_tag && *c == '=' )
|
|
{
|
|
if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL )
|
|
{
|
|
ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
|
|
goto exit;
|
|
}
|
|
|
|
oid = attr_descr->oid;
|
|
s = c + 1;
|
|
in_tag = 0;
|
|
d = data;
|
|
}
|
|
|
|
if( !in_tag && *c == '\\' && c != end )
|
|
{
|
|
c++;
|
|
|
|
/* Check for valid escaped characters */
|
|
if( c == end || *c != ',' )
|
|
{
|
|
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
|
goto exit;
|
|
}
|
|
}
|
|
else if( !in_tag && ( *c == ',' || c == end ) )
|
|
{
|
|
mbedtls_asn1_named_data* cur =
|
|
mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
|
|
(unsigned char *) data,
|
|
d - data );
|
|
|
|
if(cur == NULL )
|
|
{
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
|
}
|
|
|
|
// set tagType
|
|
cur->val.tag = attr_descr->default_tag;
|
|
|
|
while( c < end && *(c + 1) == ' ' )
|
|
c++;
|
|
|
|
s = c + 1;
|
|
in_tag = 1;
|
|
}
|
|
|
|
if( !in_tag && s != c + 1 )
|
|
{
|
|
*(d++) = *c;
|
|
|
|
if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
|
|
{
|
|
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
|
goto exit;
|
|
}
|
|
}
|
|
|
|
c++;
|
|
}
|
|
|
|
exit:
|
|
|
|
return( ret );
|
|
}
|
|
|
|
/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
|
|
* to store the critical boolean for us
|
|
*/
|
|
int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
|
|
int critical, const unsigned char *val, size_t val_len )
|
|
{
|
|
mbedtls_asn1_named_data *cur;
|
|
|
|
if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
|
|
NULL, val_len + 1 ) ) == NULL )
|
|
{
|
|
return( MBEDTLS_ERR_X509_ALLOC_FAILED );
|
|
}
|
|
|
|
cur->val.p[0] = critical;
|
|
memcpy( cur->val.p + 1, val, val_len );
|
|
|
|
return( 0 );
|
|
}
|
|
|
|
/*
|
|
* RelativeDistinguishedName ::=
|
|
* SET OF AttributeTypeAndValue
|
|
*
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
* type AttributeType,
|
|
* value AttributeValue }
|
|
*
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
*
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
*/
|
|
static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name)
|
|
{
|
|
int ret = MBEDTLS_ERR_THIS_CORRUPTION;
|
|
size_t len = 0;
|
|
const char *oid = (const char*)cur_name->oid.p;
|
|
size_t oid_len = cur_name->oid.len;
|
|
const unsigned char *name = cur_name->val.p;
|
|
size_t name_len = cur_name->val.len;
|
|
|
|
// Write correct string tag and value
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tagged_string( p, start,
|
|
cur_name->val.tag,
|
|
(const char *) name,
|
|
name_len ) );
|
|
// Write OID
|
|
//
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid,
|
|
oid_len ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
MBEDTLS_ASN1_SET ) );
|
|
|
|
return( (int) len );
|
|
}
|
|
|
|
int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
|
|
mbedtls_asn1_named_data *first )
|
|
{
|
|
int ret = MBEDTLS_ERR_THIS_CORRUPTION;
|
|
size_t len = 0;
|
|
mbedtls_asn1_named_data *cur = first;
|
|
|
|
while( cur != NULL )
|
|
{
|
|
MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) );
|
|
cur = cur->next;
|
|
}
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
|
|
|
return( (int) len );
|
|
}
|
|
|
|
int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
|
|
const char *oid, size_t oid_len,
|
|
unsigned char *sig, size_t size )
|
|
{
|
|
int ret = MBEDTLS_ERR_THIS_CORRUPTION;
|
|
size_t len = 0;
|
|
|
|
if( *p < start || (size_t)( *p - start ) < size )
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
|
|
|
len = size;
|
|
(*p) -= len;
|
|
memcpy( *p, sig, len );
|
|
|
|
if( *p - start < 1 )
|
|
return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
|
|
|
|
*--(*p) = 0;
|
|
len += 1;
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
|
|
|
|
// Write OID
|
|
//
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
|
|
oid_len, 0 ) );
|
|
|
|
return( (int) len );
|
|
}
|
|
|
|
static int x509_write_extension( unsigned char **p, unsigned char *start,
|
|
mbedtls_asn1_named_data *ext )
|
|
{
|
|
int ret = MBEDTLS_ERR_THIS_CORRUPTION;
|
|
size_t len = 0;
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
|
|
ext->val.len - 1 ) );
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
|
|
|
|
if( ext->val.p[0] != 0 )
|
|
{
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
|
|
}
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
|
|
ext->oid.len ) );
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
|
|
|
return( (int) len );
|
|
}
|
|
|
|
/*
|
|
* Extension ::= SEQUENCE {
|
|
* extnID OBJECT IDENTIFIER,
|
|
* critical BOOLEAN DEFAULT FALSE,
|
|
* extnValue OCTET STRING
|
|
* -- contains the DER encoding of an ASN.1 value
|
|
* -- corresponding to the extension type identified
|
|
* -- by extnID
|
|
* }
|
|
*/
|
|
int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
|
|
mbedtls_asn1_named_data *first )
|
|
{
|
|
int ret = MBEDTLS_ERR_THIS_CORRUPTION;
|
|
size_t len = 0;
|
|
mbedtls_asn1_named_data *cur_ext = first;
|
|
|
|
while( cur_ext != NULL )
|
|
{
|
|
MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
|
|
cur_ext = cur_ext->next;
|
|
}
|
|
|
|
return( (int) len );
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_CREATE_C */
|