1/*
2 * Licensed Materials - Property of IBM
3 *
4 * trousers - An open source TCG Software Stack
5 *
6 * (C) Copyright International Business Machines Corp. 2006
7 *
8 */
9
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13#include <errno.h>
14
15#include "trousers/tss.h"
16#include "spi_internal_types.h"
17#include "spi_utils.h"
18#include "obj.h"
19// #include "tcslog.h"
20#include "bi.h"
21#include "daa_parameter.h"
22#include "issuer.h"
23
24static char *DEFAULT_FILENAME = "issuer.txt";
25static char *DEFAULT_ISSUER = "IBM-Issuer";
26
27static const int DEFAULT_ISSUER_ATTRIBUTES = 2;	// A1 A2
28static const int DEFAULT_RECEIVER_ATTRIBUTES = 3;	// A3 A4 A5
29
30int print_usage(char *cmd) {
31	fprintf(stderr, "usage: %s\n", cmd);
32	fprintf(stderr, "	\t-npa,\t--nb_platform_attr\tnumber of attributes that the\
33 Platform can choose and which will not be visible to the Issuer (default: %d)\n",
34			DEFAULT_ISSUER_ATTRIBUTES);
35	fprintf(stderr, "	\t-nia,\t--nb_issuer_attr\tnumber of attributes that the issuer\
36 can choose and which will be visible to both the Platform and the Issuer(default: %d)\n",
37			DEFAULT_RECEIVER_ATTRIBUTES);
38	fprintf(stderr, "	\t-if,\t--issuer_file\tthe file that will contain all key pair\
39 and proof to be used by the issuer (default: %s)\n",
40			DEFAULT_FILENAME);
41	fprintf(stderr, "	\t-i,\t--issuer\tissuer identity (default: %s)\n",
42			DEFAULT_ISSUER);
43	return -1;
44}
45
46int main(int argc, char *argv[]) {
47	int nb_platform_attr = DEFAULT_ISSUER_ATTRIBUTES;
48	int nb_issuer_attr = DEFAULT_RECEIVER_ATTRIBUTES;
49	char *filename = DEFAULT_FILENAME;
50	char *issuer = DEFAULT_ISSUER;
51	int i;
52	char *param;
53	TSS_HCONTEXT hContext;
54	TSS_DAA_KEY_PAIR *key_pair;
55	TSS_DAA_PK_PROOF *public_keyproof;
56	TSS_RESULT result;
57	TSS_HDAA hDAA;
58	TSS_DAA_PK_PROOF_internal *public_keyproof_internal;
59	TSS_DAA_PK_internal *pk;
60	TSS_DAA_PRIVATE_KEY *private_key;
61	DAA_PRIVATE_KEY_internal *private_key_internal;
62	KEY_PAIR_WITH_PROOF_internal *key_pair_with_proof;
63
64	printf("Issuer Setup (%s:%s,%s)\n", argv[0], __DATE__, __TIME__);
65	i = 1;
66	while( i < argc) {
67		param = argv[ i];
68		if         ( strcmp( param, "-if") == 0 || strcmp( param, "--issuer_file")) {
69			i++;
70			if( i == argc) return print_usage( argv[0]);
71			filename = argv[i];
72		} else if( strcmp( param, "-npa") == 0 || strcmp( param, "--nb_platform_attr")) {
73			i++;
74			if( i == argc) return print_usage( argv[0]);
75			nb_platform_attr = atoi( argv[i]);
76		} else if( strcmp( param, "-nia") == 0 || strcmp( param, "--nb_issuer_attr")) {
77			i++;
78			if( i == argc) return print_usage( argv[0]);
79			nb_issuer_attr = atoi(argv[i]);
80		} else if( strcmp( param, "-i") == 0 || strcmp( param, "--issuer")) {
81			i++;
82			if( i == argc) return print_usage( argv[0]);
83			issuer = argv[i];
84		} else {
85			fprintf(stderr, 	"%s:unrecognized option `%s'\n", argv[0], param);
86			return print_usage( argv[0]);
87		}
88		i++;
89	}
90	bi_init( NULL);
91	// Create Context
92	printf("Create Context\n");
93	result = Tspi_Context_Create( &hContext );
94	if ( result != TSS_SUCCESS )
95	{
96		fprintf( stderr, "Tspi_Context_Create %d\n", result );
97		exit( result );
98	}
99
100	// Connect to Context
101	printf("Connect to the context\n");
102	result = Tspi_Context_Connect( hContext, NULL );
103	if ( result != TSS_SUCCESS )
104	{
105		fprintf( stderr, "Tspi_Context_Connect error:%d\n", result );
106		Tspi_Context_FreeMemory( hContext, NULL );
107		Tspi_Context_Close( hContext );
108		exit( result );
109	}
110	//Create Object
111	result = obj_daa_add( hContext, &hDAA);
112	if (result != TSS_SUCCESS) {
113		goto close;
114	}
115	result = Tspi_DAA_IssueSetup(
116		hDAA,	// in
117		strlen( issuer),	// in
118		(BYTE *)issuer,	// in
119		nb_platform_attr,	// in
120		nb_issuer_attr,	// in
121		(TSS_HKEY *)&key_pair,	// out
122		&public_keyproof);	// out
123	if( result != TSS_SUCCESS) goto close;
124
125	// TSS_DAA_KEY_PAIR_internal *key_pair_internal = DAA_KEY_PAIR_2_internal( key_pair);
126	public_keyproof_internal = e_2_i_TSS_DAA_PK_PROOF( public_keyproof);
127	pk = e_2_i_TSS_DAA_PK( key_pair->public_key);
128	private_key = key_pair->private_key;
129	private_key_internal = e_2_i_TSS_DAA_PRIVATE_KEY( private_key);
130	key_pair_with_proof =
131		(KEY_PAIR_WITH_PROOF_internal *)malloc( sizeof(KEY_PAIR_WITH_PROOF_internal));
132	if( key_pair_with_proof == NULL) {
133		fprintf("malloc of %d bytes failed", sizeof(KEY_PAIR_WITH_PROOF_internal));
134		goto close;
135	}
136	key_pair_with_proof->pk = pk;
137	key_pair_with_proof->private_key = private_key_internal;
138	key_pair_with_proof->proof = public_keyproof_internal;
139
140	printf("Saving key pair with proof  -> 	\'%s\'", filename);
141	FILE *file = fopen( filename, "w");
142	if( file == NULL) {
143		fprintf( stderr, "%s: Error when saving \'%s\': %s\n",
144			argv[0],
145			filename,
146			strerror( errno));
147		return -1;
148	}
149	if( save_KEY_PAIR_WITH_PROOF( file, key_pair_with_proof) != 0) {
150		fprintf( stderr, "%s: Error when saving \'%s\': %s\n",
151			argv[0],
152			filename,
153			strerror( errno));
154		return -1;
155	}
156	fclose( file);
157	printf("\nDone.\n");
158close:
159	obj_daa_remove( hDAA, hContext);
160	printf("Closing the context\n");
161	Tspi_Context_FreeMemory( hContext, NULL );
162	Tspi_Context_Close( hContext );
163	bi_release();
164	printf("Result: %d", result);
165	return result;
166}
167