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