void gtget_ssl_init(connection_t * conn) { char *clientcert = NULL; char *clientkey = NULL; const char *pers = "gtget"; sslparam_t *ssl = calloc(1, sizeof(sslparam_t)); if (!(conn->flags & GTGET_FLAG_INSECURE)) { char *cacertfile = alloca(strlen(conn->remote->host) + 5); char *servercert = NULL; strcpy(cacertfile, conn->remote->host); strcat(cacertfile, ".pem"); if (!(servercert = tryopen_alt(conn, conn->caFile, cacertfile))) servercert = tryopen("cacerts.pem"); if (!(servercert)) die(conn, "can't open cacert", NULL); if (x509_crt_parse_file(&ssl->cacert, servercert)) die(conn, "error reading cacert", servercert); } /* read and parse the client certificate if provided */ if ((clientcert = tryopen_alt(conn, conn->ccFile, "clientcert.pem"))) { if (!(clientkey = tryopen_alt(conn, conn->ckFile, "clientkey.pem"))) clientkey = clientcert; if (x509_crt_parse_file(&ssl->clicert, clientcert)) { die(conn, "error reading client certificate", clientcert); if (clientkey && pk_parse_public_keyfile(&ssl->pk, clientkey)) die(conn, "error reading client key", clientkey); } write2f("using client cert: %s\n", clientcert); write2f("using client key: %s\n", clientkey); } entropy_init(&ssl->entropy); if (0 != (ctr_drbg_init(&ssl->ctr_drbg, entropy_func, &ssl->entropy, (const unsigned char *)pers, strlen(pers)))) die(conn, "Seeding the random number generator failed", NULL); if (ssl_init(&ssl->ssl)) die(conn, "error initializing SSL", NULL); ssl_set_endpoint(&ssl->ssl, SSL_IS_CLIENT); if ((conn->flags & GTGET_FLAG_INSECURE)) { ssl_set_authmode(&ssl->ssl, SSL_VERIFY_NONE); } ssl_set_ca_chain(&ssl->ssl, &ssl->cacert, NULL, conn->remote->host); ssl_set_authmode(&ssl->ssl, SSL_VERIFY_OPTIONAL); ssl_set_verify(&ssl->ssl, verify_cb, conn); ssl_set_ciphersuites(&ssl->ssl, ssl_list_ciphersuites()); ssl_set_session(&ssl->ssl, &ssl->ssn); ssl_set_rng(&ssl->ssl, ctr_drbg_random, &ssl->ctr_drbg); conn->ssl = ssl; }
int main( int argc, char *argv[] ) { int ret = 0; pk_context pk; char buf[1024]; int i; char *p, *q; /* * Set to sane values */ pk_init( &pk ); memset( buf, 0, sizeof(buf) ); if( argc == 0 ) { usage: printf( USAGE ); goto exit; } opt.mode = DFL_MODE; opt.filename = DFL_FILENAME; opt.password = DFL_PASSWORD; opt.password_file = DFL_PASSWORD_FILE; for( i = 1; i < argc; i++ ) { p = argv[i]; if( ( q = strchr( p, '=' ) ) == NULL ) goto usage; *q++ = '\0'; if( strcmp( p, "mode" ) == 0 ) { if( strcmp( q, "private" ) == 0 ) opt.mode = MODE_PRIVATE; else if( strcmp( q, "public" ) == 0 ) opt.mode = MODE_PUBLIC; else goto usage; } else if( strcmp( p, "filename" ) == 0 ) opt.filename = q; else if( strcmp( p, "password" ) == 0 ) opt.password = q; else if( strcmp( p, "password_file" ) == 0 ) opt.password_file = q; else goto usage; } if( opt.mode == MODE_PRIVATE ) { if( strlen( opt.password ) && strlen( opt.password_file ) ) { printf( "Error: cannot have both password and password_file\n" ); goto usage; } if( strlen( opt.password_file ) ) { FILE *f; printf( "\n . Loading the password file ..." ); if( ( f = fopen( opt.password_file, "rb" ) ) == NULL ) { printf( " failed\n ! fopen returned NULL\n" ); goto exit; } fgets( buf, sizeof(buf), f ); fclose( f ); i = (int) strlen( buf ); if( buf[i - 1] == '\n' ) buf[i - 1] = '\0'; if( buf[i - 2] == '\r' ) buf[i - 2] = '\0'; opt.password = buf; } /* * 1.1. Load the key */ printf( "\n . Loading the private key ..." ); fflush( stdout ); ret = pk_parse_keyfile( &pk, opt.filename, opt.password ); if( ret != 0 ) { printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret ); goto exit; } printf( " ok\n" ); /* * 1.2 Print the key */ printf( " . Key information ...\n" ); #if defined(POLARSSL_RSA_C) if( pk_get_type( &pk ) == POLARSSL_PK_RSA ) { rsa_context *rsa = pk_rsa( pk ); mpi_write_file( "N: ", &rsa->N, 16, NULL ); mpi_write_file( "E: ", &rsa->E, 16, NULL ); mpi_write_file( "D: ", &rsa->D, 16, NULL ); mpi_write_file( "P: ", &rsa->P, 16, NULL ); mpi_write_file( "Q: ", &rsa->Q, 16, NULL ); mpi_write_file( "DP: ", &rsa->DP, 16, NULL ); mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ); mpi_write_file( "QP: ", &rsa->QP, 16, NULL ); } else #endif #if defined(POLARSSL_ECP_C) if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY ) { ecp_keypair *ecp = pk_ec( pk ); mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); mpi_write_file( "D : ", &ecp->d , 16, NULL ); } else #endif { printf("Do not know how to print key information for this type\n" ); goto exit; } } else if( opt.mode == MODE_PUBLIC ) { /* * 1.1. Load the key */ printf( "\n . Loading the public key ..." ); fflush( stdout ); ret = pk_parse_public_keyfile( &pk, opt.filename ); if( ret != 0 ) { printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret ); goto exit; } printf( " ok\n" ); printf( " . Key information ...\n" ); #if defined(POLARSSL_RSA_C) if( pk_get_type( &pk ) == POLARSSL_PK_RSA ) { rsa_context *rsa = pk_rsa( pk ); mpi_write_file( "N: ", &rsa->N, 16, NULL ); mpi_write_file( "E: ", &rsa->E, 16, NULL ); } else #endif #if defined(POLARSSL_ECP_C) if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY ) { ecp_keypair *ecp = pk_ec( pk ); mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); } else #endif { printf("Do not know how to print key information for this type\n" ); goto exit; } } else goto usage; exit: #if defined(POLARSSL_ERROR_C) polarssl_strerror( ret, buf, sizeof(buf) ); printf( " ! Last error was: %s\n", buf ); #endif pk_free( &pk ); #if defined(_WIN32) printf( " + Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( ret ); }
int main( int argc, char *argv[] ) { FILE *f; int ret = 1; size_t i; pk_context pk; unsigned char hash[20]; unsigned char buf[POLARSSL_MPI_MAX_SIZE]; char filename[512]; pk_init( &pk ); if( argc != 3 ) { printf( "usage: pk_verify <key_file> <filename>\n" ); #if defined(_WIN32) printf( "\n" ); #endif goto exit; } printf( "\n . Reading public key from '%s'", argv[1] ); fflush( stdout ); if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 ) { printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret ); goto exit; } /* * Extract the signature from the text file */ ret = 1; snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); if( ( f = fopen( filename, "rb" ) ) == NULL ) { printf( "\n ! Could not open %s\n\n", filename ); goto exit; } i = fread( buf, 1, sizeof(buf), f ); fclose( f ); /* * Compute the SHA-1 hash of the input file and compare * it with the hash decrypted from the signature. */ printf( "\n . Verifying the SHA-1 signature" ); fflush( stdout ); if( ( ret = sha1_file( argv[2], hash ) ) != 0 ) { printf( " failed\n ! Could not open or read %s\n\n", argv[2] ); goto exit; } if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA1, hash, 0, buf, i ) ) != 0 ) { printf( " failed\n ! pk_verify returned -0x%04x\n", -ret ); goto exit; } printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" ); ret = 0; exit: pk_free( &pk ); #if defined(POLARSSL_ERROR_C) polarssl_strerror( ret, (char *) buf, sizeof(buf) ); printf( " ! Last error was: %s\n", buf ); #endif #if defined(_WIN32) printf( " + Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( ret ); }
int main( int argc, char *argv[] ) { int ret = 0; pk_context key; char buf[1024]; int i; char *p, *q; /* * Set to sane values */ pk_init( &key ); memset( buf, 0, sizeof( buf ) ); if( argc == 0 ) { usage: ret = 1; polarssl_printf( USAGE ); goto exit; } opt.mode = DFL_MODE; opt.filename = DFL_FILENAME; opt.output_mode = DFL_OUTPUT_MODE; opt.output_file = DFL_OUTPUT_FILENAME; opt.output_format = DFL_OUTPUT_FORMAT; for( i = 1; i < argc; i++ ) { p = argv[i]; if( ( q = strchr( p, '=' ) ) == NULL ) goto usage; *q++ = '\0'; if( strcmp( p, "mode" ) == 0 ) { if( strcmp( q, "private" ) == 0 ) opt.mode = MODE_PRIVATE; else if( strcmp( q, "public" ) == 0 ) opt.mode = MODE_PUBLIC; else goto usage; } else if( strcmp( p, "output_mode" ) == 0 ) { if( strcmp( q, "private" ) == 0 ) opt.output_mode = OUTPUT_MODE_PRIVATE; else if( strcmp( q, "public" ) == 0 ) opt.output_mode = OUTPUT_MODE_PUBLIC; else goto usage; } else if( strcmp( p, "output_format" ) == 0 ) { #if defined(POLARSSL_PEM_WRITE_C) if( strcmp( q, "pem" ) == 0 ) opt.output_format = OUTPUT_FORMAT_PEM; else #endif if( strcmp( q, "der" ) == 0 ) opt.output_format = OUTPUT_FORMAT_DER; else goto usage; } else if( strcmp( p, "filename" ) == 0 ) opt.filename = q; else if( strcmp( p, "output_file" ) == 0 ) opt.output_file = q; else goto usage; } if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE ) { polarssl_printf( "\nCannot output a key without reading one.\n"); goto exit; } if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE ) { polarssl_printf( "\nCannot output a private key from a public key.\n"); goto exit; } if( opt.mode == MODE_PRIVATE ) { /* * 1.1. Load the key */ polarssl_printf( "\n . Loading the private key ..." ); fflush( stdout ); ret = pk_parse_keyfile( &key, opt.filename, NULL ); if( ret != 0 ) { polarssl_strerror( ret, (char *) buf, sizeof(buf) ); polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf ); goto exit; } polarssl_printf( " ok\n" ); /* * 1.2 Print the key */ polarssl_printf( " . Key information ...\n" ); #if defined(POLARSSL_RSA_C) if( pk_get_type( &key ) == POLARSSL_PK_RSA ) { rsa_context *rsa = pk_rsa( key ); mpi_write_file( "N: ", &rsa->N, 16, NULL ); mpi_write_file( "E: ", &rsa->E, 16, NULL ); mpi_write_file( "D: ", &rsa->D, 16, NULL ); mpi_write_file( "P: ", &rsa->P, 16, NULL ); mpi_write_file( "Q: ", &rsa->Q, 16, NULL ); mpi_write_file( "DP: ", &rsa->DP, 16, NULL ); mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ); mpi_write_file( "QP: ", &rsa->QP, 16, NULL ); } else #endif #if defined(POLARSSL_ECP_C) if( pk_get_type( &key ) == POLARSSL_PK_ECKEY ) { ecp_keypair *ecp = pk_ec( key ); mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); mpi_write_file( "D : ", &ecp->d , 16, NULL ); } else #endif polarssl_printf("key type not supported yet\n"); } else if( opt.mode == MODE_PUBLIC ) { /* * 1.1. Load the key */ polarssl_printf( "\n . Loading the public key ..." ); fflush( stdout ); ret = pk_parse_public_keyfile( &key, opt.filename ); if( ret != 0 ) { polarssl_strerror( ret, (char *) buf, sizeof(buf) ); polarssl_printf( " failed\n ! pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf ); goto exit; } polarssl_printf( " ok\n" ); /* * 1.2 Print the key */ polarssl_printf( " . Key information ...\n" ); #if defined(POLARSSL_RSA_C) if( pk_get_type( &key ) == POLARSSL_PK_RSA ) { rsa_context *rsa = pk_rsa( key ); mpi_write_file( "N: ", &rsa->N, 16, NULL ); mpi_write_file( "E: ", &rsa->E, 16, NULL ); } else #endif #if defined(POLARSSL_ECP_C) if( pk_get_type( &key ) == POLARSSL_PK_ECKEY ) { ecp_keypair *ecp = pk_ec( key ); mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); } else #endif polarssl_printf("key type not supported yet\n"); } else goto usage; if( opt.output_mode == OUTPUT_MODE_PUBLIC ) { write_public_key( &key, opt.output_file ); } if( opt.output_mode == OUTPUT_MODE_PRIVATE ) { write_private_key( &key, opt.output_file ); } exit: if( ret != 0 && ret != 1) { #ifdef POLARSSL_ERROR_C polarssl_strerror( ret, buf, sizeof( buf ) ); polarssl_printf( " - %s\n", buf ); #else polarssl_printf("\n"); #endif } pk_free( &key ); #if defined(_WIN32) polarssl_printf( " + Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( ret ); }
int main( int argc, char *argv[] ) { FILE *f; int ret; size_t i, olen = 0; pk_context pk; entropy_context entropy; ctr_drbg_context ctr_drbg; unsigned char input[1024]; unsigned char buf[512]; const char *pers = "pk_encrypt"; ret = 1; if( argc != 3 ) { polarssl_printf( "usage: pk_encrypt <key_file> <string of max 100 characters>\n" ); #if defined(_WIN32) polarssl_printf( "\n" ); #endif goto exit; } polarssl_printf( "\n . Seeding the random number generator..." ); fflush( stdout ); entropy_init( &entropy ); if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, (const unsigned char *) pers, strlen( pers ) ) ) != 0 ) { polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret ); goto exit; } polarssl_printf( "\n . Reading public key from '%s'", argv[1] ); fflush( stdout ); pk_init( &pk ); if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 ) { polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret ); goto exit; } if( strlen( argv[2] ) > 100 ) { polarssl_printf( " Input data larger than 100 characters.\n\n" ); goto exit; } memcpy( input, argv[2], strlen( argv[2] ) ); /* * Calculate the RSA encryption of the hash. */ polarssl_printf( "\n . Generating the encrypted value" ); fflush( stdout ); if( ( ret = pk_encrypt( &pk, input, strlen( argv[2] ), buf, &olen, sizeof(buf), ctr_drbg_random, &ctr_drbg ) ) != 0 ) { polarssl_printf( " failed\n ! pk_encrypt returned -0x%04x\n", -ret ); goto exit; } /* * Write the signature into result-enc.txt */ if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL ) { ret = 1; polarssl_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" ); goto exit; } for( i = 0; i < olen; i++ ) polarssl_fprintf( f, "%02X%s", buf[i], ( i + 1 ) % 16 == 0 ? "\r\n" : " " ); fclose( f ); polarssl_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" ); exit: ctr_drbg_free( &ctr_drbg ); entropy_free( &entropy ); #if defined(POLARSSL_ERROR_C) polarssl_strerror( ret, (char *) buf, sizeof(buf) ); polarssl_printf( " ! Last error was: %s\n", buf ); #endif #if defined(_WIN32) polarssl_printf( " + Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( ret ); }