client.cpp
/*----------------------------------------------------------------------
 * do_quote()
 *
 * Generate a quote from the enclave.
 *----------------------------------------------------------------------
 * WARNING!
 *
 * DO NOT USE THIS SUBROUTINE AS A TEMPLATE FOR IMPLEMENTING REMOTE
 * ATTESTATION. do_quote() short-circuits the RA process in order 
 * to generate an enclave quote directly!
 *
 * The high-level functions provided for remote attestation take
 * care of the low-level details of quote generation for you:
 *
 *   sgx_ra_init()
 *   sgx_ra_get_msg1
 *   sgx_ra_proc_msg2
 *
 * End developers should not normally be calling these functions
 * directly when doing remote attestation: 
 *
 *    sgx_get_ps_sec_prop()
 *    sgx_get_quote()
 *    sgx_get_quote_size()
 *    sgx_calc_quote_size()
 *    sgx_get_report()
 *    sgx_init_quote()
 *
 *----------------------------------------------------------------------
 */
do_quote()
Generate a quote from the enclave.

WARNING!



The high-level functions provided for remote attestation take
care of the low-level details of quote generation for you.



End developers should not normally be calling these functions
directly when doing remote attestation.


do_quote () Generate a quote from the enclave . WARNING! The high - level functions provided for remote attestation take care of the low - level details of quote generation for you . End developers should not normally be calling these functions directly when doing remote attestation .
int do_quote(sgx_enclave_id_t eid, config_t *config)
{
	sgx_status_t status, sgxrv;
	sgx_quote_t *quote;
	sgx_report_t report;
	sgx_report_t qe_report;
	sgx_target_info_t target_info;
	sgx_epid_group_id_t epid_gid;
	uint32_t sz= 0;
	uint32_t flags= config->flags;
	sgx_quote_sign_type_t linkable= SGX_UNLINKABLE_SIGNATURE;
#ifdef _WIN32
	sgx_ps_cap_t ps_cap;
	char *pse_manifest = NULL;
	size_t pse_manifest_sz;
	LPTSTR b64quote = NULL;
	DWORD sz_b64quote = 0;
	LPTSTR b64manifest = NULL;
	DWORD sz_b64manifest = 0;
#else
	char  *b64quote= NULL;
	char *b64manifest = NULL;
#endif
 	if (OPT_ISSET(flags, OPT_LINK)) linkable= SGX_LINKABLE_SIGNATURE;
#ifdef _WIN32
	if (OPT_ISSET(flags, OPT_PSE)) {
		status = get_pse_manifest_size(eid, &pse_manifest_sz);
		if (status != SGX_SUCCESS) {
			fprintf(stderr, "get_pse_manifest_size: %08x\n",
				status);
			return 1;
		}
		pse_manifest = (char *) malloc(pse_manifest_sz);
		status = get_pse_manifest(eid, &sgxrv, pse_manifest, pse_manifest_sz);
		if (status != SGX_SUCCESS) {
			fprintf(stderr, "get_pse_manifest: %08x\n",
				status);
			return 1;
		}
		if (sgxrv != SGX_SUCCESS) {
			fprintf(stderr, "get_sec_prop_desc_ex: %08x\n",
				sgxrv);
			return 1;
		}
	}
#endif
	memset(&report, 0, sizeof(report));
	status= sgx_init_quote(&target_info, &epid_gid);
	if ( status != SGX_SUCCESS ) {
		fprintf(stderr, "sgx_init_quote: %08x\n", status);
		return 1;
	}
	if ( config->mode == MODE_EPID ) {
		printf("%08x\n", *(uint32_t *)epid_gid);
		exit(0);
	}
	status= get_report(eid, &sgxrv, &report, &target_info);
	if ( status != SGX_SUCCESS ) {
		fprintf(stderr, "get_report: %08x\n", status);
		return 1;
	}
	if ( sgxrv != SGX_SUCCESS ) {
		fprintf(stderr, "sgx_create_report: %08x\n", sgxrv);
		return 1;
	}
	if (! get_quote_size(&status, &sz)) {
		fprintf(stderr, "PSW missing sgx_get_quote_size() and sgx_calc_quote_size()\n");
		return 1;
	}
	if ( status != SGX_SUCCESS ) {
		fprintf(stderr, "SGX error while getting quote size: %08x\n", status);
		return 1;
	}
	quote= (sgx_quote_t *) malloc(sz);
	if ( quote == NULL ) {
		fprintf(stderr, "out of memory\n");
		return 1;
	}
	memset(quote, 0, sz);
	status= sgx_get_quote(&report, linkable, &config->spid,
		(OPT_ISSET(flags, OPT_NONCE)) ? &config->nonce : NULL,
		NULL, 0,
		(OPT_ISSET(flags, OPT_NONCE)) ? &qe_report : NULL, 
		quote, sz);
	if ( status != SGX_SUCCESS ) {
		fprintf(stderr, "sgx_get_quote: %08x\n", status);
		return 1;
	}
#ifdef _WIN32
	if (CryptBinaryToString((BYTE *) quote, sz, CRYPT_STRING_BASE64|CRYPT_STRING_NOCRLF, NULL, &sz_b64quote) == FALSE) {
		fprintf(stderr, "CryptBinaryToString: could not get Base64 encoded quote length\n");
		return 1;
	}
	b64quote = (LPTSTR)(malloc(sz_b64quote));
	if (b64quote == NULL) {
		perror("malloc");
		return 1;
	}
	if (CryptBinaryToString((BYTE *) quote, sz, CRYPT_STRING_BASE64|CRYPT_STRING_NOCRLF, b64quote, &sz_b64quote) == FALSE) {
		fprintf(stderr, "CryptBinaryToString: could not get Base64 encoded quote length\n");
		return 1;
	}
	if (OPT_ISSET(flags, OPT_PSE)) {
		if (CryptBinaryToString((BYTE *)pse_manifest, (uint32_t)(pse_manifest_sz), CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &sz_b64manifest) == FALSE) {
			fprintf(stderr, "CryptBinaryToString: could not get Base64 encoded manifest length\n");
			return 1;
		}
		b64manifest = (LPTSTR)(malloc(sz_b64manifest));
		if (b64manifest == NULL) {
			free(b64quote);
			perror("malloc");
			return 1;
		}
		if (CryptBinaryToString((BYTE *)pse_manifest, (uint32_t)(pse_manifest_sz), CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, b64manifest, &sz_b64manifest) == FALSE) {
			fprintf(stderr, "CryptBinaryToString: could not get Base64 encoded manifest length\n");
			return 1;
		}
	}
#else
	b64quote= base64_encode((char *) quote, sz);
	if ( b64quote == NULL ) {
		eprintf("Could not base64 encode quote\n");
		return 1;
	}
#endif
	printf("{\n");
	printf("\"isvEnclaveQuote\":\"%s\"", b64quote);
	if ( OPT_ISSET(flags, OPT_NONCE) ) {
		printf(",\n\"nonce\":\"");
		print_hexstring(stdout, &config->nonce, 16);
		printf("\"");
	}
#ifdef _WIN32
	if (OPT_ISSET(flags, OPT_PSE)) {
		printf(",\n\"pseManifest\":\"%s\"", b64manifest);	
	}
#endif
	printf("\n}\n");
#ifdef SGX_HW_SIM
	fprintf(stderr, "WARNING! Built in h/w simulation mode. This quote will not be verifiable.\n");
#endif
	free(b64quote);
#ifdef _WIN32
	if ( b64manifest != NULL ) free(b64manifest);
#endif
	return 0;
}
int do_quote ( sgx_enclave_id_t eid , config_t * config ) { sgx_status_t status , sgxrv ; sgx_quote_t * quote ; sgx_report_t report ; sgx_report_t qe_report ; sgx_target_info_t target_info ; sgx_epid_group_id_t epid_gid ; uint32_t sz = 0 ; uint32_t flags = config -> flags ; sgx_quote_sign_type_t linkable = SGX_UNLINKABLE_SIGNATURE ; #ifdef _WIN32 sgx_ps_cap_t ps_cap ; char * pse_manifest = NULL ; size_t pse_manifest_sz ; LPTSTR b64quote = NULL ; DWORD sz_b64quote = 0 ; LPTSTR b64manifest = NULL ; DWORD sz_b64manifest = 0 ; #else char * b64quote = NULL ; char * b64manifest = NULL ; #endif if ( OPT_ISSET ( flags , OPT_LINK ) ) linkable = SGX_LINKABLE_SIGNATURE ; #ifdef _WIN32 if ( OPT_ISSET ( flags , OPT_PSE ) ) { status = get_pse_manifest_size ( eid , & pse_manifest_sz ) ; if ( status != SGX_SUCCESS ) { fprintf ( stderr , " \n " , status ) ; return 1 ; } pse_manifest = ( char * ) malloc ( pse_manifest_sz ) ; status = get_pse_manifest ( eid , & sgxrv , pse_manifest , pse_manifest_sz ) ; if ( status != SGX_SUCCESS ) { fprintf ( stderr , " \n " , status ) ; return 1 ; } if ( sgxrv != SGX_SUCCESS ) { fprintf ( stderr , " \n " , sgxrv ) ; return 1 ; } } #endif memset ( & report , 0 , sizeof ( report ) ) ; status = sgx_init_quote ( & target_info , & epid_gid ) ; if ( status != SGX_SUCCESS ) { fprintf ( stderr , " \n " , status ) ; return 1 ; } if ( config -> mode == MODE_EPID ) { printf ( " \n " , * ( uint32_t * ) epid_gid ) ; exit ( 0 ) ; } status = get_report ( eid , & sgxrv , & report , & target_info ) ; if ( status != SGX_SUCCESS ) { fprintf ( stderr , " \n " , status ) ; return 1 ; } if ( sgxrv != SGX_SUCCESS ) { fprintf ( stderr , " \n " , sgxrv ) ; return 1 ; } if ( ! get_quote_size ( & status , & sz ) ) { fprintf ( stderr , " \n " ) ; return 1 ; } if ( status != SGX_SUCCESS ) { fprintf ( stderr , " \n " , status ) ; return 1 ; } quote = ( sgx_quote_t * ) malloc ( sz ) ; if ( quote == NULL ) { fprintf ( stderr , " \n " ) ; return 1 ; } memset ( quote , 0 , sz ) ; status = sgx_get_quote ( & report , linkable , & config -> spid , ( OPT_ISSET ( flags , OPT_NONCE ) ) ? & config -> nonce : NULL , NULL , 0 , ( OPT_ISSET ( flags , OPT_NONCE ) ) ? & qe_report : NULL , quote , sz ) ; if ( status != SGX_SUCCESS ) { fprintf ( stderr , " \n " , status ) ; return 1 ; } #ifdef _WIN32 if ( CryptBinaryToString ( ( BYTE * ) quote , sz , CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF , NULL , & sz_b64quote ) == FALSE ) { fprintf ( stderr , " \n " ) ; return 1 ; } b64quote = ( LPTSTR ) ( malloc ( sz_b64quote ) ) ; if ( b64quote == NULL ) { perror ( " " ) ; return 1 ; } if ( CryptBinaryToString ( ( BYTE * ) quote , sz , CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF , b64quote , & sz_b64quote ) == FALSE ) { fprintf ( stderr , " \n " ) ; return 1 ; } if ( OPT_ISSET ( flags , OPT_PSE ) ) { if ( CryptBinaryToString ( ( BYTE * ) pse_manifest , ( uint32_t ) ( pse_manifest_sz ) , CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF , NULL , & sz_b64manifest ) == FALSE ) { fprintf ( stderr , " \n " ) ; return 1 ; } b64manifest = ( LPTSTR ) ( malloc ( sz_b64manifest ) ) ; if ( b64manifest == NULL ) { free ( b64quote ) ; perror ( " " ) ; return 1 ; } if ( CryptBinaryToString ( ( BYTE * ) pse_manifest , ( uint32_t ) ( pse_manifest_sz ) , CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF , b64manifest , & sz_b64manifest ) == FALSE ) { fprintf ( stderr , " \n " ) ; return 1 ; } } #else b64quote = base64_encode ( ( char * ) quote , sz ) ; if ( b64quote == NULL ) { eprintf ( " \n " ) ; return 1 ; } #endif printf ( " \n " ) ; printf ( " \" \" \" \" " , b64quote ) ; if ( OPT_ISSET ( flags , OPT_NONCE ) ) { printf ( " \n \" \" \" " ) ; print_hexstring ( stdout , & config -> nonce , 16 ) ; printf ( " \" " ) ; } #ifdef _WIN32 if ( OPT_ISSET ( flags , OPT_PSE ) ) { printf ( " \n \" \" \" \" " , b64manifest ) ; } #endif printf ( " \n \n " ) ; #ifdef SGX_HW_SIM fprintf ( stderr , " \n " ) ; #endif free ( b64quote ) ; #ifdef _WIN32 if ( b64manifest != NULL ) free ( b64manifest ) ; #endif return 0 ; }
do_quote()
 Generate a quote from the enclave.
do_quote () Generate a quote from the enclave .
/* Platform services info. Win32 only. */ /* Get our quote */ /* Did they ask for just the EPID? */ // sgx_get_quote_size() has been deprecated, but our PSW may be too old // so use a wrapper function. /* Print our quote */ // We could also just do ((4 * sz / 3) + 3) & ~3 // but it's cleaner to use the API.