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