Preliminary main mbed library for nexpaq development
features/mbedtls/src/ssl_ticket.c@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | /* |
nexpaq | 0:6c56fb4bc5f0 | 2 | * TLS server tickets callbacks implementation |
nexpaq | 0:6c56fb4bc5f0 | 3 | * |
nexpaq | 0:6c56fb4bc5f0 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
nexpaq | 0:6c56fb4bc5f0 | 5 | * SPDX-License-Identifier: Apache-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 6 | * |
nexpaq | 0:6c56fb4bc5f0 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
nexpaq | 0:6c56fb4bc5f0 | 8 | * not use this file except in compliance with the License. |
nexpaq | 0:6c56fb4bc5f0 | 9 | * You may obtain a copy of the License at |
nexpaq | 0:6c56fb4bc5f0 | 10 | * |
nexpaq | 0:6c56fb4bc5f0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 12 | * |
nexpaq | 0:6c56fb4bc5f0 | 13 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 0:6c56fb4bc5f0 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
nexpaq | 0:6c56fb4bc5f0 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 0:6c56fb4bc5f0 | 16 | * See the License for the specific language governing permissions and |
nexpaq | 0:6c56fb4bc5f0 | 17 | * limitations under the License. |
nexpaq | 0:6c56fb4bc5f0 | 18 | * |
nexpaq | 0:6c56fb4bc5f0 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
nexpaq | 0:6c56fb4bc5f0 | 20 | */ |
nexpaq | 0:6c56fb4bc5f0 | 21 | |
nexpaq | 0:6c56fb4bc5f0 | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
nexpaq | 0:6c56fb4bc5f0 | 23 | #include "mbedtls/config.h" |
nexpaq | 0:6c56fb4bc5f0 | 24 | #else |
nexpaq | 0:6c56fb4bc5f0 | 25 | #include MBEDTLS_CONFIG_FILE |
nexpaq | 0:6c56fb4bc5f0 | 26 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 27 | |
nexpaq | 0:6c56fb4bc5f0 | 28 | #if defined(MBEDTLS_SSL_TICKET_C) |
nexpaq | 0:6c56fb4bc5f0 | 29 | |
nexpaq | 0:6c56fb4bc5f0 | 30 | #if defined(MBEDTLS_PLATFORM_C) |
nexpaq | 0:6c56fb4bc5f0 | 31 | #include "mbedtls/platform.h" |
nexpaq | 0:6c56fb4bc5f0 | 32 | #else |
nexpaq | 0:6c56fb4bc5f0 | 33 | #include <stdlib.h> |
nexpaq | 0:6c56fb4bc5f0 | 34 | #define mbedtls_calloc calloc |
nexpaq | 0:6c56fb4bc5f0 | 35 | #define mbedtls_free free |
nexpaq | 0:6c56fb4bc5f0 | 36 | #define mbedtls_time time |
nexpaq | 0:6c56fb4bc5f0 | 37 | #define mbedtls_time_t time_t |
nexpaq | 0:6c56fb4bc5f0 | 38 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 39 | |
nexpaq | 0:6c56fb4bc5f0 | 40 | #include "mbedtls/ssl_ticket.h" |
nexpaq | 0:6c56fb4bc5f0 | 41 | |
nexpaq | 0:6c56fb4bc5f0 | 42 | #include <string.h> |
nexpaq | 0:6c56fb4bc5f0 | 43 | |
nexpaq | 0:6c56fb4bc5f0 | 44 | /* Implementation that should never be optimized out by the compiler */ |
nexpaq | 0:6c56fb4bc5f0 | 45 | static void mbedtls_zeroize( void *v, size_t n ) { |
nexpaq | 0:6c56fb4bc5f0 | 46 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
nexpaq | 0:6c56fb4bc5f0 | 47 | } |
nexpaq | 0:6c56fb4bc5f0 | 48 | |
nexpaq | 0:6c56fb4bc5f0 | 49 | /* |
nexpaq | 0:6c56fb4bc5f0 | 50 | * Initialze context |
nexpaq | 0:6c56fb4bc5f0 | 51 | */ |
nexpaq | 0:6c56fb4bc5f0 | 52 | void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx ) |
nexpaq | 0:6c56fb4bc5f0 | 53 | { |
nexpaq | 0:6c56fb4bc5f0 | 54 | memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) ); |
nexpaq | 0:6c56fb4bc5f0 | 55 | |
nexpaq | 0:6c56fb4bc5f0 | 56 | #if defined(MBEDTLS_THREADING_C) |
nexpaq | 0:6c56fb4bc5f0 | 57 | mbedtls_mutex_init( &ctx->mutex ); |
nexpaq | 0:6c56fb4bc5f0 | 58 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 59 | } |
nexpaq | 0:6c56fb4bc5f0 | 60 | |
nexpaq | 0:6c56fb4bc5f0 | 61 | #define MAX_KEY_BYTES 32 /* 256 bits */ |
nexpaq | 0:6c56fb4bc5f0 | 62 | |
nexpaq | 0:6c56fb4bc5f0 | 63 | /* |
nexpaq | 0:6c56fb4bc5f0 | 64 | * Generate/update a key |
nexpaq | 0:6c56fb4bc5f0 | 65 | */ |
nexpaq | 0:6c56fb4bc5f0 | 66 | static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx, |
nexpaq | 0:6c56fb4bc5f0 | 67 | unsigned char index ) |
nexpaq | 0:6c56fb4bc5f0 | 68 | { |
nexpaq | 0:6c56fb4bc5f0 | 69 | int ret; |
nexpaq | 0:6c56fb4bc5f0 | 70 | unsigned char buf[MAX_KEY_BYTES]; |
nexpaq | 0:6c56fb4bc5f0 | 71 | mbedtls_ssl_ticket_key *key = ctx->keys + index; |
nexpaq | 0:6c56fb4bc5f0 | 72 | |
nexpaq | 0:6c56fb4bc5f0 | 73 | #if defined(MBEDTLS_HAVE_TIME) |
nexpaq | 0:6c56fb4bc5f0 | 74 | key->generation_time = (uint32_t) mbedtls_time( NULL ); |
nexpaq | 0:6c56fb4bc5f0 | 75 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 76 | |
nexpaq | 0:6c56fb4bc5f0 | 77 | if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 78 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 79 | |
nexpaq | 0:6c56fb4bc5f0 | 80 | if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 81 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 82 | |
nexpaq | 0:6c56fb4bc5f0 | 83 | /* With GCM and CCM, same context can encrypt & decrypt */ |
nexpaq | 0:6c56fb4bc5f0 | 84 | ret = mbedtls_cipher_setkey( &key->ctx, buf, |
nexpaq | 0:6c56fb4bc5f0 | 85 | mbedtls_cipher_get_key_bitlen( &key->ctx ), |
nexpaq | 0:6c56fb4bc5f0 | 86 | MBEDTLS_ENCRYPT ); |
nexpaq | 0:6c56fb4bc5f0 | 87 | |
nexpaq | 0:6c56fb4bc5f0 | 88 | mbedtls_zeroize( buf, sizeof( buf ) ); |
nexpaq | 0:6c56fb4bc5f0 | 89 | |
nexpaq | 0:6c56fb4bc5f0 | 90 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 91 | } |
nexpaq | 0:6c56fb4bc5f0 | 92 | |
nexpaq | 0:6c56fb4bc5f0 | 93 | /* |
nexpaq | 0:6c56fb4bc5f0 | 94 | * Rotate/generate keys if necessary |
nexpaq | 0:6c56fb4bc5f0 | 95 | */ |
nexpaq | 0:6c56fb4bc5f0 | 96 | static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx ) |
nexpaq | 0:6c56fb4bc5f0 | 97 | { |
nexpaq | 0:6c56fb4bc5f0 | 98 | #if !defined(MBEDTLS_HAVE_TIME) |
nexpaq | 0:6c56fb4bc5f0 | 99 | ((void) ctx); |
nexpaq | 0:6c56fb4bc5f0 | 100 | #else |
nexpaq | 0:6c56fb4bc5f0 | 101 | if( ctx->ticket_lifetime != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 102 | { |
nexpaq | 0:6c56fb4bc5f0 | 103 | uint32_t current_time = (uint32_t) mbedtls_time( NULL ); |
nexpaq | 0:6c56fb4bc5f0 | 104 | uint32_t key_time = ctx->keys[ctx->active].generation_time; |
nexpaq | 0:6c56fb4bc5f0 | 105 | |
nexpaq | 0:6c56fb4bc5f0 | 106 | if( current_time > key_time && |
nexpaq | 0:6c56fb4bc5f0 | 107 | current_time - key_time < ctx->ticket_lifetime ) |
nexpaq | 0:6c56fb4bc5f0 | 108 | { |
nexpaq | 0:6c56fb4bc5f0 | 109 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 110 | } |
nexpaq | 0:6c56fb4bc5f0 | 111 | |
nexpaq | 0:6c56fb4bc5f0 | 112 | ctx->active = 1 - ctx->active; |
nexpaq | 0:6c56fb4bc5f0 | 113 | |
nexpaq | 0:6c56fb4bc5f0 | 114 | return( ssl_ticket_gen_key( ctx, ctx->active ) ); |
nexpaq | 0:6c56fb4bc5f0 | 115 | } |
nexpaq | 0:6c56fb4bc5f0 | 116 | else |
nexpaq | 0:6c56fb4bc5f0 | 117 | #endif /* MBEDTLS_HAVE_TIME */ |
nexpaq | 0:6c56fb4bc5f0 | 118 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 119 | } |
nexpaq | 0:6c56fb4bc5f0 | 120 | |
nexpaq | 0:6c56fb4bc5f0 | 121 | /* |
nexpaq | 0:6c56fb4bc5f0 | 122 | * Setup context for actual use |
nexpaq | 0:6c56fb4bc5f0 | 123 | */ |
nexpaq | 0:6c56fb4bc5f0 | 124 | int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx, |
nexpaq | 0:6c56fb4bc5f0 | 125 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
nexpaq | 0:6c56fb4bc5f0 | 126 | mbedtls_cipher_type_t cipher, |
nexpaq | 0:6c56fb4bc5f0 | 127 | uint32_t lifetime ) |
nexpaq | 0:6c56fb4bc5f0 | 128 | { |
nexpaq | 0:6c56fb4bc5f0 | 129 | int ret; |
nexpaq | 0:6c56fb4bc5f0 | 130 | const mbedtls_cipher_info_t *cipher_info; |
nexpaq | 0:6c56fb4bc5f0 | 131 | |
nexpaq | 0:6c56fb4bc5f0 | 132 | ctx->f_rng = f_rng; |
nexpaq | 0:6c56fb4bc5f0 | 133 | ctx->p_rng = p_rng; |
nexpaq | 0:6c56fb4bc5f0 | 134 | |
nexpaq | 0:6c56fb4bc5f0 | 135 | ctx->ticket_lifetime = lifetime; |
nexpaq | 0:6c56fb4bc5f0 | 136 | |
nexpaq | 0:6c56fb4bc5f0 | 137 | cipher_info = mbedtls_cipher_info_from_type( cipher); |
nexpaq | 0:6c56fb4bc5f0 | 138 | if( cipher_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 139 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 140 | |
nexpaq | 0:6c56fb4bc5f0 | 141 | if( cipher_info->mode != MBEDTLS_MODE_GCM && |
nexpaq | 0:6c56fb4bc5f0 | 142 | cipher_info->mode != MBEDTLS_MODE_CCM ) |
nexpaq | 0:6c56fb4bc5f0 | 143 | { |
nexpaq | 0:6c56fb4bc5f0 | 144 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 145 | } |
nexpaq | 0:6c56fb4bc5f0 | 146 | |
nexpaq | 0:6c56fb4bc5f0 | 147 | if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES ) |
nexpaq | 0:6c56fb4bc5f0 | 148 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 149 | |
nexpaq | 0:6c56fb4bc5f0 | 150 | if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 || |
nexpaq | 0:6c56fb4bc5f0 | 151 | ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 152 | { |
nexpaq | 0:6c56fb4bc5f0 | 153 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 154 | } |
nexpaq | 0:6c56fb4bc5f0 | 155 | |
nexpaq | 0:6c56fb4bc5f0 | 156 | if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 || |
nexpaq | 0:6c56fb4bc5f0 | 157 | ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 158 | { |
nexpaq | 0:6c56fb4bc5f0 | 159 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 160 | } |
nexpaq | 0:6c56fb4bc5f0 | 161 | |
nexpaq | 0:6c56fb4bc5f0 | 162 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 163 | } |
nexpaq | 0:6c56fb4bc5f0 | 164 | |
nexpaq | 0:6c56fb4bc5f0 | 165 | /* |
nexpaq | 0:6c56fb4bc5f0 | 166 | * Serialize a session in the following format: |
nexpaq | 0:6c56fb4bc5f0 | 167 | * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session) |
nexpaq | 0:6c56fb4bc5f0 | 168 | * n . n+2 peer_cert length = m (0 if no certificate) |
nexpaq | 0:6c56fb4bc5f0 | 169 | * n+3 . n+2+m peer cert ASN.1 |
nexpaq | 0:6c56fb4bc5f0 | 170 | */ |
nexpaq | 0:6c56fb4bc5f0 | 171 | static int ssl_save_session( const mbedtls_ssl_session *session, |
nexpaq | 0:6c56fb4bc5f0 | 172 | unsigned char *buf, size_t buf_len, |
nexpaq | 0:6c56fb4bc5f0 | 173 | size_t *olen ) |
nexpaq | 0:6c56fb4bc5f0 | 174 | { |
nexpaq | 0:6c56fb4bc5f0 | 175 | unsigned char *p = buf; |
nexpaq | 0:6c56fb4bc5f0 | 176 | size_t left = buf_len; |
nexpaq | 0:6c56fb4bc5f0 | 177 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
nexpaq | 0:6c56fb4bc5f0 | 178 | size_t cert_len; |
nexpaq | 0:6c56fb4bc5f0 | 179 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
nexpaq | 0:6c56fb4bc5f0 | 180 | |
nexpaq | 0:6c56fb4bc5f0 | 181 | if( left < sizeof( mbedtls_ssl_session ) ) |
nexpaq | 0:6c56fb4bc5f0 | 182 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
nexpaq | 0:6c56fb4bc5f0 | 183 | |
nexpaq | 0:6c56fb4bc5f0 | 184 | memcpy( p, session, sizeof( mbedtls_ssl_session ) ); |
nexpaq | 0:6c56fb4bc5f0 | 185 | p += sizeof( mbedtls_ssl_session ); |
nexpaq | 0:6c56fb4bc5f0 | 186 | left -= sizeof( mbedtls_ssl_session ); |
nexpaq | 0:6c56fb4bc5f0 | 187 | |
nexpaq | 0:6c56fb4bc5f0 | 188 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
nexpaq | 0:6c56fb4bc5f0 | 189 | if( session->peer_cert == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 190 | cert_len = 0; |
nexpaq | 0:6c56fb4bc5f0 | 191 | else |
nexpaq | 0:6c56fb4bc5f0 | 192 | cert_len = session->peer_cert->raw.len; |
nexpaq | 0:6c56fb4bc5f0 | 193 | |
nexpaq | 0:6c56fb4bc5f0 | 194 | if( left < 3 + cert_len ) |
nexpaq | 0:6c56fb4bc5f0 | 195 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
nexpaq | 0:6c56fb4bc5f0 | 196 | |
nexpaq | 0:6c56fb4bc5f0 | 197 | *p++ = (unsigned char)( cert_len >> 16 & 0xFF ); |
nexpaq | 0:6c56fb4bc5f0 | 198 | *p++ = (unsigned char)( cert_len >> 8 & 0xFF ); |
nexpaq | 0:6c56fb4bc5f0 | 199 | *p++ = (unsigned char)( cert_len & 0xFF ); |
nexpaq | 0:6c56fb4bc5f0 | 200 | |
nexpaq | 0:6c56fb4bc5f0 | 201 | if( session->peer_cert != NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 202 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
nexpaq | 0:6c56fb4bc5f0 | 203 | |
nexpaq | 0:6c56fb4bc5f0 | 204 | p += cert_len; |
nexpaq | 0:6c56fb4bc5f0 | 205 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
nexpaq | 0:6c56fb4bc5f0 | 206 | |
nexpaq | 0:6c56fb4bc5f0 | 207 | *olen = p - buf; |
nexpaq | 0:6c56fb4bc5f0 | 208 | |
nexpaq | 0:6c56fb4bc5f0 | 209 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 210 | } |
nexpaq | 0:6c56fb4bc5f0 | 211 | |
nexpaq | 0:6c56fb4bc5f0 | 212 | /* |
nexpaq | 0:6c56fb4bc5f0 | 213 | * Unserialise session, see ssl_save_session() |
nexpaq | 0:6c56fb4bc5f0 | 214 | */ |
nexpaq | 0:6c56fb4bc5f0 | 215 | static int ssl_load_session( mbedtls_ssl_session *session, |
nexpaq | 0:6c56fb4bc5f0 | 216 | const unsigned char *buf, size_t len ) |
nexpaq | 0:6c56fb4bc5f0 | 217 | { |
nexpaq | 0:6c56fb4bc5f0 | 218 | const unsigned char *p = buf; |
nexpaq | 0:6c56fb4bc5f0 | 219 | const unsigned char * const end = buf + len; |
nexpaq | 0:6c56fb4bc5f0 | 220 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
nexpaq | 0:6c56fb4bc5f0 | 221 | size_t cert_len; |
nexpaq | 0:6c56fb4bc5f0 | 222 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
nexpaq | 0:6c56fb4bc5f0 | 223 | |
nexpaq | 0:6c56fb4bc5f0 | 224 | if( p + sizeof( mbedtls_ssl_session ) > end ) |
nexpaq | 0:6c56fb4bc5f0 | 225 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 226 | |
nexpaq | 0:6c56fb4bc5f0 | 227 | memcpy( session, p, sizeof( mbedtls_ssl_session ) ); |
nexpaq | 0:6c56fb4bc5f0 | 228 | p += sizeof( mbedtls_ssl_session ); |
nexpaq | 0:6c56fb4bc5f0 | 229 | |
nexpaq | 0:6c56fb4bc5f0 | 230 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
nexpaq | 0:6c56fb4bc5f0 | 231 | if( p + 3 > end ) |
nexpaq | 0:6c56fb4bc5f0 | 232 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 233 | |
nexpaq | 0:6c56fb4bc5f0 | 234 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
nexpaq | 0:6c56fb4bc5f0 | 235 | p += 3; |
nexpaq | 0:6c56fb4bc5f0 | 236 | |
nexpaq | 0:6c56fb4bc5f0 | 237 | if( cert_len == 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 238 | { |
nexpaq | 0:6c56fb4bc5f0 | 239 | session->peer_cert = NULL; |
nexpaq | 0:6c56fb4bc5f0 | 240 | } |
nexpaq | 0:6c56fb4bc5f0 | 241 | else |
nexpaq | 0:6c56fb4bc5f0 | 242 | { |
nexpaq | 0:6c56fb4bc5f0 | 243 | int ret; |
nexpaq | 0:6c56fb4bc5f0 | 244 | |
nexpaq | 0:6c56fb4bc5f0 | 245 | if( p + cert_len > end ) |
nexpaq | 0:6c56fb4bc5f0 | 246 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 247 | |
nexpaq | 0:6c56fb4bc5f0 | 248 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
nexpaq | 0:6c56fb4bc5f0 | 249 | |
nexpaq | 0:6c56fb4bc5f0 | 250 | if( session->peer_cert == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 251 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
nexpaq | 0:6c56fb4bc5f0 | 252 | |
nexpaq | 0:6c56fb4bc5f0 | 253 | mbedtls_x509_crt_init( session->peer_cert ); |
nexpaq | 0:6c56fb4bc5f0 | 254 | |
nexpaq | 0:6c56fb4bc5f0 | 255 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
nexpaq | 0:6c56fb4bc5f0 | 256 | p, cert_len ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 257 | { |
nexpaq | 0:6c56fb4bc5f0 | 258 | mbedtls_x509_crt_free( session->peer_cert ); |
nexpaq | 0:6c56fb4bc5f0 | 259 | mbedtls_free( session->peer_cert ); |
nexpaq | 0:6c56fb4bc5f0 | 260 | session->peer_cert = NULL; |
nexpaq | 0:6c56fb4bc5f0 | 261 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 262 | } |
nexpaq | 0:6c56fb4bc5f0 | 263 | |
nexpaq | 0:6c56fb4bc5f0 | 264 | p += cert_len; |
nexpaq | 0:6c56fb4bc5f0 | 265 | } |
nexpaq | 0:6c56fb4bc5f0 | 266 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
nexpaq | 0:6c56fb4bc5f0 | 267 | |
nexpaq | 0:6c56fb4bc5f0 | 268 | if( p != end ) |
nexpaq | 0:6c56fb4bc5f0 | 269 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 270 | |
nexpaq | 0:6c56fb4bc5f0 | 271 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 272 | } |
nexpaq | 0:6c56fb4bc5f0 | 273 | |
nexpaq | 0:6c56fb4bc5f0 | 274 | /* |
nexpaq | 0:6c56fb4bc5f0 | 275 | * Create session ticket, with the following structure: |
nexpaq | 0:6c56fb4bc5f0 | 276 | * |
nexpaq | 0:6c56fb4bc5f0 | 277 | * struct { |
nexpaq | 0:6c56fb4bc5f0 | 278 | * opaque key_name[4]; |
nexpaq | 0:6c56fb4bc5f0 | 279 | * opaque iv[12]; |
nexpaq | 0:6c56fb4bc5f0 | 280 | * opaque encrypted_state<0..2^16-1>; |
nexpaq | 0:6c56fb4bc5f0 | 281 | * opaque tag[16]; |
nexpaq | 0:6c56fb4bc5f0 | 282 | * } ticket; |
nexpaq | 0:6c56fb4bc5f0 | 283 | * |
nexpaq | 0:6c56fb4bc5f0 | 284 | * The key_name, iv, and length of encrypted_state are the additional |
nexpaq | 0:6c56fb4bc5f0 | 285 | * authenticated data. |
nexpaq | 0:6c56fb4bc5f0 | 286 | */ |
nexpaq | 0:6c56fb4bc5f0 | 287 | int mbedtls_ssl_ticket_write( void *p_ticket, |
nexpaq | 0:6c56fb4bc5f0 | 288 | const mbedtls_ssl_session *session, |
nexpaq | 0:6c56fb4bc5f0 | 289 | unsigned char *start, |
nexpaq | 0:6c56fb4bc5f0 | 290 | const unsigned char *end, |
nexpaq | 0:6c56fb4bc5f0 | 291 | size_t *tlen, |
nexpaq | 0:6c56fb4bc5f0 | 292 | uint32_t *ticket_lifetime ) |
nexpaq | 0:6c56fb4bc5f0 | 293 | { |
nexpaq | 0:6c56fb4bc5f0 | 294 | int ret; |
nexpaq | 0:6c56fb4bc5f0 | 295 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
nexpaq | 0:6c56fb4bc5f0 | 296 | mbedtls_ssl_ticket_key *key; |
nexpaq | 0:6c56fb4bc5f0 | 297 | unsigned char *key_name = start; |
nexpaq | 0:6c56fb4bc5f0 | 298 | unsigned char *iv = start + 4; |
nexpaq | 0:6c56fb4bc5f0 | 299 | unsigned char *state_len_bytes = iv + 12; |
nexpaq | 0:6c56fb4bc5f0 | 300 | unsigned char *state = state_len_bytes + 2; |
nexpaq | 0:6c56fb4bc5f0 | 301 | unsigned char *tag; |
nexpaq | 0:6c56fb4bc5f0 | 302 | size_t clear_len, ciph_len; |
nexpaq | 0:6c56fb4bc5f0 | 303 | |
nexpaq | 0:6c56fb4bc5f0 | 304 | *tlen = 0; |
nexpaq | 0:6c56fb4bc5f0 | 305 | |
nexpaq | 0:6c56fb4bc5f0 | 306 | if( ctx == NULL || ctx->f_rng == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 307 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 308 | |
nexpaq | 0:6c56fb4bc5f0 | 309 | /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag, |
nexpaq | 0:6c56fb4bc5f0 | 310 | * in addition to session itself, that will be checked when writing it. */ |
nexpaq | 0:6c56fb4bc5f0 | 311 | if( end - start < 4 + 12 + 2 + 16 ) |
nexpaq | 0:6c56fb4bc5f0 | 312 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
nexpaq | 0:6c56fb4bc5f0 | 313 | |
nexpaq | 0:6c56fb4bc5f0 | 314 | #if defined(MBEDTLS_THREADING_C) |
nexpaq | 0:6c56fb4bc5f0 | 315 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 316 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 317 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 318 | |
nexpaq | 0:6c56fb4bc5f0 | 319 | if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 320 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 321 | |
nexpaq | 0:6c56fb4bc5f0 | 322 | key = &ctx->keys[ctx->active]; |
nexpaq | 0:6c56fb4bc5f0 | 323 | |
nexpaq | 0:6c56fb4bc5f0 | 324 | *ticket_lifetime = ctx->ticket_lifetime; |
nexpaq | 0:6c56fb4bc5f0 | 325 | |
nexpaq | 0:6c56fb4bc5f0 | 326 | memcpy( key_name, key->name, 4 ); |
nexpaq | 0:6c56fb4bc5f0 | 327 | |
nexpaq | 0:6c56fb4bc5f0 | 328 | if( ( ret = ctx->f_rng( ctx->p_rng, iv, 12 ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 329 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 330 | |
nexpaq | 0:6c56fb4bc5f0 | 331 | /* Dump session state */ |
nexpaq | 0:6c56fb4bc5f0 | 332 | if( ( ret = ssl_save_session( session, |
nexpaq | 0:6c56fb4bc5f0 | 333 | state, end - state, &clear_len ) ) != 0 || |
nexpaq | 0:6c56fb4bc5f0 | 334 | (unsigned long) clear_len > 65535 ) |
nexpaq | 0:6c56fb4bc5f0 | 335 | { |
nexpaq | 0:6c56fb4bc5f0 | 336 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 337 | } |
nexpaq | 0:6c56fb4bc5f0 | 338 | state_len_bytes[0] = ( clear_len >> 8 ) & 0xff; |
nexpaq | 0:6c56fb4bc5f0 | 339 | state_len_bytes[1] = ( clear_len ) & 0xff; |
nexpaq | 0:6c56fb4bc5f0 | 340 | |
nexpaq | 0:6c56fb4bc5f0 | 341 | /* Encrypt and authenticate */ |
nexpaq | 0:6c56fb4bc5f0 | 342 | tag = state + clear_len; |
nexpaq | 0:6c56fb4bc5f0 | 343 | if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx, |
nexpaq | 0:6c56fb4bc5f0 | 344 | iv, 12, key_name, 4 + 12 + 2, |
nexpaq | 0:6c56fb4bc5f0 | 345 | state, clear_len, state, &ciph_len, tag, 16 ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 346 | { |
nexpaq | 0:6c56fb4bc5f0 | 347 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 348 | } |
nexpaq | 0:6c56fb4bc5f0 | 349 | if( ciph_len != clear_len ) |
nexpaq | 0:6c56fb4bc5f0 | 350 | { |
nexpaq | 0:6c56fb4bc5f0 | 351 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
nexpaq | 0:6c56fb4bc5f0 | 352 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 353 | } |
nexpaq | 0:6c56fb4bc5f0 | 354 | |
nexpaq | 0:6c56fb4bc5f0 | 355 | *tlen = 4 + 12 + 2 + 16 + ciph_len; |
nexpaq | 0:6c56fb4bc5f0 | 356 | |
nexpaq | 0:6c56fb4bc5f0 | 357 | cleanup: |
nexpaq | 0:6c56fb4bc5f0 | 358 | #if defined(MBEDTLS_THREADING_C) |
nexpaq | 0:6c56fb4bc5f0 | 359 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 360 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
nexpaq | 0:6c56fb4bc5f0 | 361 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 362 | |
nexpaq | 0:6c56fb4bc5f0 | 363 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 364 | } |
nexpaq | 0:6c56fb4bc5f0 | 365 | |
nexpaq | 0:6c56fb4bc5f0 | 366 | /* |
nexpaq | 0:6c56fb4bc5f0 | 367 | * Select key based on name |
nexpaq | 0:6c56fb4bc5f0 | 368 | */ |
nexpaq | 0:6c56fb4bc5f0 | 369 | static mbedtls_ssl_ticket_key *ssl_ticket_select_key( |
nexpaq | 0:6c56fb4bc5f0 | 370 | mbedtls_ssl_ticket_context *ctx, |
nexpaq | 0:6c56fb4bc5f0 | 371 | const unsigned char name[4] ) |
nexpaq | 0:6c56fb4bc5f0 | 372 | { |
nexpaq | 0:6c56fb4bc5f0 | 373 | unsigned char i; |
nexpaq | 0:6c56fb4bc5f0 | 374 | |
nexpaq | 0:6c56fb4bc5f0 | 375 | for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ ) |
nexpaq | 0:6c56fb4bc5f0 | 376 | if( memcmp( name, ctx->keys[i].name, 4 ) == 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 377 | return( &ctx->keys[i] ); |
nexpaq | 0:6c56fb4bc5f0 | 378 | |
nexpaq | 0:6c56fb4bc5f0 | 379 | return( NULL ); |
nexpaq | 0:6c56fb4bc5f0 | 380 | } |
nexpaq | 0:6c56fb4bc5f0 | 381 | |
nexpaq | 0:6c56fb4bc5f0 | 382 | /* |
nexpaq | 0:6c56fb4bc5f0 | 383 | * Load session ticket (see mbedtls_ssl_ticket_write for structure) |
nexpaq | 0:6c56fb4bc5f0 | 384 | */ |
nexpaq | 0:6c56fb4bc5f0 | 385 | int mbedtls_ssl_ticket_parse( void *p_ticket, |
nexpaq | 0:6c56fb4bc5f0 | 386 | mbedtls_ssl_session *session, |
nexpaq | 0:6c56fb4bc5f0 | 387 | unsigned char *buf, |
nexpaq | 0:6c56fb4bc5f0 | 388 | size_t len ) |
nexpaq | 0:6c56fb4bc5f0 | 389 | { |
nexpaq | 0:6c56fb4bc5f0 | 390 | int ret; |
nexpaq | 0:6c56fb4bc5f0 | 391 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
nexpaq | 0:6c56fb4bc5f0 | 392 | mbedtls_ssl_ticket_key *key; |
nexpaq | 0:6c56fb4bc5f0 | 393 | unsigned char *key_name = buf; |
nexpaq | 0:6c56fb4bc5f0 | 394 | unsigned char *iv = buf + 4; |
nexpaq | 0:6c56fb4bc5f0 | 395 | unsigned char *enc_len_p = iv + 12; |
nexpaq | 0:6c56fb4bc5f0 | 396 | unsigned char *ticket = enc_len_p + 2; |
nexpaq | 0:6c56fb4bc5f0 | 397 | unsigned char *tag; |
nexpaq | 0:6c56fb4bc5f0 | 398 | size_t enc_len, clear_len; |
nexpaq | 0:6c56fb4bc5f0 | 399 | |
nexpaq | 0:6c56fb4bc5f0 | 400 | if( ctx == NULL || ctx->f_rng == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 401 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 402 | |
nexpaq | 0:6c56fb4bc5f0 | 403 | /* See mbedtls_ssl_ticket_write() */ |
nexpaq | 0:6c56fb4bc5f0 | 404 | if( len < 4 + 12 + 2 + 16 ) |
nexpaq | 0:6c56fb4bc5f0 | 405 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 406 | |
nexpaq | 0:6c56fb4bc5f0 | 407 | #if defined(MBEDTLS_THREADING_C) |
nexpaq | 0:6c56fb4bc5f0 | 408 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 409 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 410 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 411 | |
nexpaq | 0:6c56fb4bc5f0 | 412 | if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 413 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 414 | |
nexpaq | 0:6c56fb4bc5f0 | 415 | enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1]; |
nexpaq | 0:6c56fb4bc5f0 | 416 | tag = ticket + enc_len; |
nexpaq | 0:6c56fb4bc5f0 | 417 | |
nexpaq | 0:6c56fb4bc5f0 | 418 | if( len != 4 + 12 + 2 + enc_len + 16 ) |
nexpaq | 0:6c56fb4bc5f0 | 419 | { |
nexpaq | 0:6c56fb4bc5f0 | 420 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
nexpaq | 0:6c56fb4bc5f0 | 421 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 422 | } |
nexpaq | 0:6c56fb4bc5f0 | 423 | |
nexpaq | 0:6c56fb4bc5f0 | 424 | /* Select key */ |
nexpaq | 0:6c56fb4bc5f0 | 425 | if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 426 | { |
nexpaq | 0:6c56fb4bc5f0 | 427 | /* We can't know for sure but this is a likely option unless we're |
nexpaq | 0:6c56fb4bc5f0 | 428 | * under attack - this is only informative anyway */ |
nexpaq | 0:6c56fb4bc5f0 | 429 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
nexpaq | 0:6c56fb4bc5f0 | 430 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 431 | } |
nexpaq | 0:6c56fb4bc5f0 | 432 | |
nexpaq | 0:6c56fb4bc5f0 | 433 | /* Decrypt and authenticate */ |
nexpaq | 0:6c56fb4bc5f0 | 434 | if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx, iv, 12, |
nexpaq | 0:6c56fb4bc5f0 | 435 | key_name, 4 + 12 + 2, ticket, enc_len, |
nexpaq | 0:6c56fb4bc5f0 | 436 | ticket, &clear_len, tag, 16 ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 437 | { |
nexpaq | 0:6c56fb4bc5f0 | 438 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
nexpaq | 0:6c56fb4bc5f0 | 439 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
nexpaq | 0:6c56fb4bc5f0 | 440 | |
nexpaq | 0:6c56fb4bc5f0 | 441 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 442 | } |
nexpaq | 0:6c56fb4bc5f0 | 443 | if( clear_len != enc_len ) |
nexpaq | 0:6c56fb4bc5f0 | 444 | { |
nexpaq | 0:6c56fb4bc5f0 | 445 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
nexpaq | 0:6c56fb4bc5f0 | 446 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 447 | } |
nexpaq | 0:6c56fb4bc5f0 | 448 | |
nexpaq | 0:6c56fb4bc5f0 | 449 | /* Actually load session */ |
nexpaq | 0:6c56fb4bc5f0 | 450 | if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 451 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 452 | |
nexpaq | 0:6c56fb4bc5f0 | 453 | #if defined(MBEDTLS_HAVE_TIME) |
nexpaq | 0:6c56fb4bc5f0 | 454 | { |
nexpaq | 0:6c56fb4bc5f0 | 455 | /* Check for expiration */ |
nexpaq | 0:6c56fb4bc5f0 | 456 | mbedtls_time_t current_time = mbedtls_time( NULL ); |
nexpaq | 0:6c56fb4bc5f0 | 457 | |
nexpaq | 0:6c56fb4bc5f0 | 458 | if( current_time < session->start || |
nexpaq | 0:6c56fb4bc5f0 | 459 | (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime ) |
nexpaq | 0:6c56fb4bc5f0 | 460 | { |
nexpaq | 0:6c56fb4bc5f0 | 461 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
nexpaq | 0:6c56fb4bc5f0 | 462 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 463 | } |
nexpaq | 0:6c56fb4bc5f0 | 464 | } |
nexpaq | 0:6c56fb4bc5f0 | 465 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 466 | |
nexpaq | 0:6c56fb4bc5f0 | 467 | cleanup: |
nexpaq | 0:6c56fb4bc5f0 | 468 | #if defined(MBEDTLS_THREADING_C) |
nexpaq | 0:6c56fb4bc5f0 | 469 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 470 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
nexpaq | 0:6c56fb4bc5f0 | 471 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 472 | |
nexpaq | 0:6c56fb4bc5f0 | 473 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 474 | } |
nexpaq | 0:6c56fb4bc5f0 | 475 | |
nexpaq | 0:6c56fb4bc5f0 | 476 | /* |
nexpaq | 0:6c56fb4bc5f0 | 477 | * Free context |
nexpaq | 0:6c56fb4bc5f0 | 478 | */ |
nexpaq | 0:6c56fb4bc5f0 | 479 | void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx ) |
nexpaq | 0:6c56fb4bc5f0 | 480 | { |
nexpaq | 0:6c56fb4bc5f0 | 481 | mbedtls_cipher_free( &ctx->keys[0].ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 482 | mbedtls_cipher_free( &ctx->keys[1].ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 483 | |
nexpaq | 0:6c56fb4bc5f0 | 484 | #if defined(MBEDTLS_THREADING_C) |
nexpaq | 0:6c56fb4bc5f0 | 485 | mbedtls_mutex_free( &ctx->mutex ); |
nexpaq | 0:6c56fb4bc5f0 | 486 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 487 | |
nexpaq | 0:6c56fb4bc5f0 | 488 | mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) ); |
nexpaq | 0:6c56fb4bc5f0 | 489 | } |
nexpaq | 0:6c56fb4bc5f0 | 490 | |
nexpaq | 0:6c56fb4bc5f0 | 491 | #endif /* MBEDTLS_SSL_TICKET_C */ |