/* * Checkup routine */ int mbedtls_dhm_self_test( int verbose ) { int ret; mbedtls_dhm_context dhm; mbedtls_dhm_init( &dhm ); if( verbose != 0 ) mbedtls_printf( " DHM parameter load: " ); if( ( ret = mbedtls_dhm_parse_dhm( &dhm, (const unsigned char *) mbedtls_test_dhm_params, mbedtls_test_dhm_params_len ) ) != 0 ) { if( verbose != 0 ) mbedtls_printf( "failed\n" ); ret = 1; goto exit; } if( verbose != 0 ) mbedtls_printf( "passed\n\n" ); exit: mbedtls_dhm_free( &dhm ); return( ret ); }
/* * X.509 cert verification callback * * XXX This is a stub to get you started, just printing some information. */ static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags ) { char buf[1024]; struct vrfy_state *state = (struct vrfy_state *) data; mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth ); mbedtls_x509_crt_info( buf, sizeof( buf ), " ", crt ); mbedtls_printf( "%s", buf ); x509_crt_pkhash( crt, buf, sizeof( buf ) ); mbedtls_printf( " Certificate public key hash: %s\n", buf ); // TODO: check certificate against pins (void) state; if ( ( *flags ) == 0 ) mbedtls_printf( " This certificate has no issues\n" ); else { mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags ); mbedtls_printf( "%s\n", buf ); } return( 0 ); }
/* * Checkup routine */ int mbedtls_md2_self_test( int verbose ) { int i; unsigned char md2sum[16]; for( i = 0; i < 7; i++ ) { if( verbose != 0 ) mbedtls_printf( " MD2 test #%d: ", i + 1 ); mbedtls_md2( (unsigned char *) md2_test_str[i], strlen( md2_test_str[i] ), md2sum ); if( memcmp( md2sum, md2_test_sum[i], 16 ) != 0 ) { if( verbose != 0 ) mbedtls_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) mbedtls_printf( "passed\n" ); } if( verbose != 0 ) mbedtls_printf( "\n" ); return( 0 ); }
/* * Checkup routine for HMAC_DRBG with SHA-1 */ int mbedtls_hmac_drbg_self_test( int verbose ) { mbedtls_hmac_drbg_context ctx; unsigned char buf[OUTPUT_LEN]; const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ); mbedtls_hmac_drbg_init( &ctx ); /* * PR = True */ if( verbose != 0 ) mbedtls_printf( " HMAC_DRBG (PR = True) : " ); test_offset = 0; CHK( mbedtls_hmac_drbg_seed( &ctx, md_info, hmac_drbg_self_test_entropy, (void *) entropy_pr, NULL, 0 ) ); mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON ); CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); CHK( memcmp( buf, result_pr, OUTPUT_LEN ) ); mbedtls_hmac_drbg_free( &ctx ); mbedtls_hmac_drbg_free( &ctx ); if( verbose != 0 ) mbedtls_printf( "passed\n" ); /* * PR = False */ if( verbose != 0 ) mbedtls_printf( " HMAC_DRBG (PR = False) : " ); mbedtls_hmac_drbg_init( &ctx ); test_offset = 0; CHK( mbedtls_hmac_drbg_seed( &ctx, md_info, hmac_drbg_self_test_entropy, (void *) entropy_nopr, NULL, 0 ) ); CHK( mbedtls_hmac_drbg_reseed( &ctx, NULL, 0 ) ); CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); CHK( memcmp( buf, result_nopr, OUTPUT_LEN ) ); mbedtls_hmac_drbg_free( &ctx ); mbedtls_hmac_drbg_free( &ctx ); if( verbose != 0 ) mbedtls_printf( "passed\n" ); if( verbose != 0 ) mbedtls_printf( "\n" ); return( 0 ); }
static int write_ssl_and_get_response( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len ) { int ret; unsigned char data[128]; char code[4]; size_t i, idx = 0; mbedtls_printf("\n%s", buf); while( len && ( ret = mbedtls_ssl_write( ssl, buf, len ) ) <= 0 ) { if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret ); return -1; } } do { len = sizeof( data ) - 1; memset( data, 0, sizeof( data ) ); ret = mbedtls_ssl_read( ssl, data, len ); if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE ) continue; if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ) return -1; if( ret <= 0 ) { mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret ); return -1; } mbedtls_printf("\n%s", data); len = ret; for( i = 0; i < len; i++ ) { if( data[i] != '\n' ) { if( idx < 4 ) code[ idx++ ] = data[i]; continue; } if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' ) { code[3] = '\0'; return atoi( code ); } idx = 0; } } while( 1 ); }
static void exit_usage( const char *name, const char *value ) { if( value == NULL ) mbedtls_printf( " unknown option or missing value: %s\n", name ); else mbedtls_printf( " option %s: illegal value: %s\n", name, value ); mbedtls_printf( USAGE ); exit( 1 ); }
/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */ void print_packet( const packet *p, const char *why ) { if( why == NULL ) mbedtls_printf( " %05lu %s %s (%u bytes)\n", ellapsed_time(), p->way, p->type, p->len ); else mbedtls_printf( " %s %s (%u bytes): %s\n", p->way, p->type, p->len, why ); fflush( stdout ); }
/* * Checkup routine */ int mbedtls_ctr_drbg_self_test( int verbose ) { mbedtls_ctr_drbg_context ctx; unsigned char buf[16]; mbedtls_ctr_drbg_init( &ctx ); /* * Based on a NIST CTR_DRBG test vector (PR = True) */ if( verbose != 0 ) mbedtls_printf( " CTR_DRBG (PR = TRUE) : " ); test_offset = 0; CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy, (void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) ); mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON ); CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) ); CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) ); CHK( memcmp( buf, result_pr, MBEDTLS_CTR_DRBG_BLOCKSIZE ) ); mbedtls_ctr_drbg_free( &ctx ); if( verbose != 0 ) mbedtls_printf( "passed\n" ); /* * Based on a NIST CTR_DRBG test vector (PR = FALSE) */ if( verbose != 0 ) mbedtls_printf( " CTR_DRBG (PR = FALSE): " ); mbedtls_ctr_drbg_init( &ctx ); test_offset = 0; CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy, (void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) ); CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) ); CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) ); CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) ); CHK( memcmp( buf, result_nopr, 16 ) ); mbedtls_ctr_drbg_free( &ctx ); if( verbose != 0 ) mbedtls_printf( "passed\n" ); if( verbose != 0 ) mbedtls_printf( "\n" ); return( 0 ); }
int send_packet( const packet *p, const char *why ) { int ret; mbedtls_net_context *dst = p->dst; /* insert corrupted ApplicationData record? */ if( opt.bad_ad && strcmp( p->type, "ApplicationData" ) == 0 ) { unsigned char buf[MAX_MSG_SIZE]; memcpy( buf, p->buf, p->len ); if( p->len <= 13 ) { mbedtls_printf( " ! can't corrupt empty AD record" ); } else { ++buf[13]; print_packet( p, "corrupted" ); } if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 ) { mbedtls_printf( " ! dispatch returned %d\n", ret ); return( ret ); } } print_packet( p, why ); if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 ) { mbedtls_printf( " ! dispatch returned %d\n", ret ); return( ret ); } /* Don't duplicate Application Data, only handshake covered */ if( opt.duplicate != 0 && strcmp( p->type, "ApplicationData" ) != 0 && rand() % opt.duplicate == 0 ) { print_packet( p, "duplicated" ); if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 ) { mbedtls_printf( " ! dispatch returned %d\n", ret ); return( ret ); } } return( 0 ); }
int mbedtls_pkcs5_self_test( int verbose ) { mbedtls_md_context_t sha1_ctx; const mbedtls_md_info_t *info_sha1; int ret, i; unsigned char key[64]; mbedtls_md_init( &sha1_ctx ); info_sha1 = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ); if( info_sha1 == NULL ) { ret = 1; goto exit; } if( ( ret = mbedtls_md_setup( &sha1_ctx, info_sha1, 1 ) ) != 0 ) { ret = 1; goto exit; } for( i = 0; i < MAX_TESTS; i++ ) { if( verbose != 0 ) mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i ); ret = mbedtls_pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i], slen[i], it_cnt[i], key_len[i], key ); if( ret != 0 || memcmp( result_key[i], key, key_len[i] ) != 0 ) { if( verbose != 0 ) mbedtls_printf( "failed\n" ); ret = 1; goto exit; } if( verbose != 0 ) mbedtls_printf( "passed\n" ); } if( verbose != 0 ) mbedtls_printf( "\n" ); exit: mbedtls_md_free( &sha1_ctx ); return( ret ); }
static int do_handshake( mbedtls_ssl_context *ssl ) { int ret; uint32_t flags; unsigned char buf[1024]; memset(buf, 0, 1024); /* * 4. Handshake */ mbedtls_printf( " . Performing the SSL/TLS handshake..." ); fflush( stdout ); while( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) { if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { #if defined(MBEDTLS_ERROR_C) mbedtls_strerror( ret, (char *) buf, 1024 ); #endif mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned %d: %s\n\n", ret, buf ); return( -1 ); } } mbedtls_printf( " ok\n [ Ciphersuite is %s ]\n", mbedtls_ssl_get_ciphersuite( ssl ) ); /* * 5. Verify the server certificate */ mbedtls_printf( " . Verifying peer X.509 certificate..." ); /* In real life, we probably want to bail out when ret != 0 */ if( ( flags = mbedtls_ssl_get_verify_result( ssl ) ) != 0 ) { char vrfy_buf[512]; mbedtls_printf( " failed\n" ); mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags ); mbedtls_printf( "%s\n", vrfy_buf ); } else mbedtls_printf( " ok\n" ); mbedtls_printf( " . Peer certificate information ...\n" ); mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ", mbedtls_ssl_get_peer_cert( ssl ) ); mbedtls_printf( "%s\n", buf ); return( 0 ); }
static int write_and_get_response( mbedtls_net_context *sock_fd, unsigned char *buf, size_t len ) { int ret; unsigned char data[128]; char code[4]; size_t i, idx = 0; mbedtls_printf("\n%s", buf); if( len && ( ret = mbedtls_net_send( sock_fd, buf, len ) ) <= 0 ) { mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret ); return -1; } do { len = sizeof( data ) - 1; memset( data, 0, sizeof( data ) ); ret = mbedtls_net_recv( sock_fd, data, len ); if( ret <= 0 ) { mbedtls_printf( "failed\n ! read returned %d\n\n", ret ); return -1; } data[len] = '\0'; mbedtls_printf("\n%s", data); len = ret; for( i = 0; i < len; i++ ) { if( data[i] != '\n' ) { if( idx < 4 ) code[ idx++ ] = data[i]; continue; } if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' ) { code[3] = '\0'; return atoi( code ); } idx = 0; } } while( 1 ); }
int main( int argc, char *argv[] ) { FILE *f; int i, k, ret = 1; int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_entropy_context entropy; unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; if( argc < 2 ) { mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] ); return( exit_code ); } if( ( f = fopen( argv[1], "wb+" ) ) == NULL ) { mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] ); return( exit_code ); } mbedtls_entropy_init( &entropy ); for( i = 0, k = 768; i < k; i++ ) { ret = mbedtls_entropy_func( &entropy, buf, sizeof( buf ) ); if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_entropy_func returned -%04X\n", ret ); goto cleanup; } fwrite( buf, 1, sizeof( buf ), f ); mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \ "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k ); fflush( stdout ); } exit_code = MBEDTLS_EXIT_SUCCESS; cleanup: mbedtls_printf( "\n" ); fclose( f ); mbedtls_entropy_free( &entropy ); return( exit_code ); }
static int ctx_buffer_append( ctx_buffer *buf, const unsigned char * data, size_t len ) { int ret; if( len > (size_t) INT_MAX ) return( -1 ); if( len > sizeof( buf->data ) ) { mbedtls_printf( " ! buffer size %u too large (max %u)\n", (unsigned) len, (unsigned) sizeof( buf->data ) ); return( -1 ); } if( sizeof( buf->data ) - buf->len < len ) { if( ( ret = ctx_buffer_flush( buf ) ) <= 0 ) return( ret ); } memcpy( buf->data + buf->len, data, len ); buf->len += len; if( ++buf->num_datagrams == 1 ) buf->packet_lifetime = ellapsed_time(); return( (int) len ); }
int main( void ) { mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or " "MBEDTLS_GENPRIME not defined.\n"); return( 0 ); }
int main( void ) { mbedtls_printf( "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or " "MBEDTLS_NET_C and/or MBEDTLS_SSL_CLI_C and/or UNIX " "not defined.\n"); return( 0 ); }
int mbedtls_pkcs5_self_test( int verbose ) { if( verbose != 0 ) mbedtls_printf( " PBKDF2 (SHA1): skipped\n\n" ); return( 0 ); }
int pkencrypt_main( void ) { mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or " "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or " "MBEDTLS_CTR_DRBG_C not defined.\n"); return( 0 ); }
int main( void ) { mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or " "MBEDTLS_MD_C and/or " "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n"); return( 0 ); }
static int write_ssl_data( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len ) { int ret; mbedtls_printf("\n%s", buf); while( len && ( ret = mbedtls_ssl_write( ssl, buf, len ) ) <= 0 ) { if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret ); return -1; } } return( 0 ); }
int main( void ) { mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or " "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C " "not defined.\n" ); return( 0 ); }
int main( void ) { mbedtls_printf("MBEDTLS_RSA_C and/or MBEDTLS_X509_CRT_PARSE_C " "MBEDTLS_FS_IO and/or MBEDTLS_X509_CRL_PARSE_C " "not defined.\n"); return( 0 ); }
int main( void ) { mbedtls_printf( "MBEDTLS_ECDH_C and/or " "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or " "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C " "not defined\n" ); return( 0 ); }
int main( void ) { mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or " "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or " "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or " "MBEDTLS_CTR_DRBG_C not defined.\n"); return( 0 ); }
void mbedtls_param_failed( const char *failure_condition, const char *file, int line ) { mbedtls_printf( "%s:%i: Input param failed - %s\n", file, line, failure_condition ); mbedtls_exit( MBEDTLS_EXIT_FAILURE ); }
int main( void ) { mbedtls_printf( "MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or " "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or " "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " "MBEDTLS_ERROR_C not defined.\n"); return( 0 ); }
int main( void ) { mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C " "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or " "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or " "MBEDTLS_CTR_DRBG_C not defined.\n"); return( 0 ); }
/* * A test to ensure hat the entropy sources are functioning correctly * and there is no obvious failure. The test performs the following checks: * - The entropy source is not providing only 0s (all bits unset) or 1s (all * bits set). * - The entropy source is not providing values in a pattern. Because the * hardware could be providing data in an arbitrary length, this check polls * the hardware entropy source twice and compares the result to ensure they * are not equal. * - The error code returned by the entropy source is not an error. */ int mbedtls_entropy_source_self_test(int verbose) { int ret = 0; unsigned char buf0[2 * sizeof(unsigned long long int)]; unsigned char buf1[2 * sizeof(unsigned long long int)]; if (verbose != 0) { mbedtls_printf(" ENTROPY_BIAS test: "); } memset(buf0, 0x00, sizeof(buf0)); memset(buf1, 0x00, sizeof(buf1)); if ((ret = mbedtls_entropy_source_self_test_gather(buf0, sizeof(buf0))) != 0) { goto cleanup; } if ((ret = mbedtls_entropy_source_self_test_gather(buf1, sizeof(buf1))) != 0) { goto cleanup; } /* Make sure that the returned values are not all 0 or 1 */ if ((ret = mbedtls_entropy_source_self_test_check_bits(buf0, sizeof(buf0))) != 0) { goto cleanup; } if ((ret = mbedtls_entropy_source_self_test_check_bits(buf1, sizeof(buf1))) != 0) { goto cleanup; } /* Make sure that the entropy source is not returning values in a * pattern */ ret = memcmp(buf0, buf1, sizeof(buf0)) == 0; cleanup: if (verbose != 0) { if (ret != 0) { mbedtls_printf("failed\n"); } else { mbedtls_printf("passed\n"); } mbedtls_printf("\n"); } return (ret != 0); }
int main( void ) { mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or " "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or " "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C " "not defined.\n"); return( 0 ); }
int dtlsclient_main( void ) { mbedtls_printf( "MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or " "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or " "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or " "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" ); return( 0 ); }