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