1#include <stdlib.h> 2#include <stdio.h> 3#include <stdbool.h> 4#include <openssl/pem.h> 5#include <openssl/rsa.h> 6#include <openssl/engine.h> 7#include <openssl/bio.h> 8#include <openssl/x509.h> 9#include <openssl/sha.h> 10#include "AppleMobilePersonalizedTicket.h" 11#include <libDER/libDER.h> 12#include <libDER/asn1Types.h> 13#include <libDER/DER_Decode.h> 14#include <libDER/DER_Encode.h> 15#include <libDER/DER_Keys.h> 16#include <libDER/oids.h> 17#include "DER_Ticket.h" 18 19const unsigned char GoldKeyCert[] = { 200x01, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 210x00, 0x08, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 220x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 230x03, 0x00, 0x00, 0x00, 0x31, 0x6e, 0xc6, 0x4f, 240xf1, 0xe8, 0x7d, 0x81, 0x38, 0x6d, 0xd8, 0xb3, 250x2b, 0xe4, 0xb5, 0xa0, 0x09, 0xaf, 0x74, 0xdd, 260xe9, 0x60, 0x27, 0x42, 0x07, 0xa3, 0xac, 0xef, 270xe5, 0x75, 0x07, 0xa8, 0xc2, 0x2c, 0x25, 0x56, 280x91, 0x37, 0xea, 0xdb, 0xdb, 0x68, 0x4a, 0x1c, 290xe3, 0x29, 0x61, 0x85, 0xd7, 0xd8, 0x66, 0x86, 300x66, 0xbf, 0xbf, 0x98, 0xae, 0xb4, 0xe9, 0x6c, 310x13, 0x81, 0x97, 0x78, 0x2a, 0x8d, 0xdc, 0x37, 320x9a, 0xf1, 0xa4, 0x0a, 0x9d, 0x74, 0xd5, 0x72, 330xbc, 0xb5, 0x64, 0xa6, 0x1a, 0x62, 0xd5, 0x39, 340xfb, 0x6f, 0xc0, 0xd1, 0xc7, 0x93, 0xc3, 0x20, 350xda, 0x84, 0x69, 0x1e, 0xd9, 0x96, 0x2e, 0xc1, 360x4f, 0x28, 0x96, 0x14, 0xa4, 0x68, 0x0a, 0x40, 370xe5, 0x17, 0xe7, 0xd6, 0x76, 0x4d, 0x81, 0xd8, 380xd2, 0xa6, 0x18, 0x82, 0x36, 0x40, 0x97, 0x31, 390xd9, 0x88, 0xdf, 0xaf, 0x05, 0x3a, 0x4b, 0x4e, 400x1b, 0x4a, 0x76, 0x6f, 0xb9, 0x6c, 0x18, 0x5d, 410xd5, 0x98, 0xf0, 0xf1, 0xbe, 0x0a, 0xd9, 0x57, 420x85, 0xc6, 0xc9, 0x63, 0xb3, 0xf5, 0x21, 0x26, 430x07, 0xba, 0x6a, 0x05, 0xfb, 0x5a, 0x06, 0x87, 440x2a, 0x30, 0x3f, 0xa9, 0xea, 0xab, 0x0e, 0x50, 450x70, 0x3b, 0x7e, 0xd4, 0xd2, 0x8c, 0xf3, 0xa1, 460xcf, 0x9a, 0x6c, 0x6b, 0xcf, 0x9b, 0x1b, 0x2a, 470x97, 0x6a, 0x3c, 0x38, 0x40, 0x43, 0xb1, 0x97, 480x19, 0x07, 0x64, 0x11, 0x94, 0x73, 0x14, 0xc9, 490xa3, 0xfe, 0x7f, 0xf6, 0x64, 0x23, 0x73, 0xe3, 500x76, 0xce, 0xf7, 0xf4, 0x2f, 0x6c, 0x9d, 0x0a, 510xf6, 0x39, 0xe6, 0x1d, 0xb2, 0x17, 0x29, 0x39, 520x98, 0x52, 0xda, 0xe0, 0x31, 0xa1, 0xfa, 0x85, 530x52, 0xc2, 0x60, 0xb5, 0x11, 0x42, 0xc6, 0x9b, 540x55, 0xd8, 0x40, 0x37, 0xf7, 0xdb, 0x01, 0x6a, 550xd5, 0x26, 0x3b, 0x27, 0x07, 0x20, 0xf7, 0x58, 560xd5, 0xa4, 0x1c, 0xe6, 0x2f, 0x74, 0x14, 0x6e, 570xa4, 0xe8, 0xc8, 0xe8, 0x9a, 0x39, 0x6d, 0xde, 580x7f, 0x67, 0x65, 0x40, 0x68, 0x26, 0x65, 0x62, 590x95, 0x87, 0x45, 0x62, 0x0d, 0x8d, 0x42, 0xad, 600x3b, 0x4f, 0xd3, 0x8f, 0x58, 0xcb, 0x61, 0x46, 610xc9, 0x3d, 0x7d, 0x75, 0x3c, 0x6d, 0xac, 0xdf, 620x53, 0xf4, 0x66, 0x9e, 0x14, 0x82, 0xc7, 0xd1, 630xd0, 0xec, 0x92, 0x24, 0x97, 0x1e, 0xc9, 0x7a, 640xfd, 0x8f, 0x75, 0xe2, 0xfd, 0x7e, 0x07, 0x44, 650x46, 0x56, 0x64, 0x9b, 0x1b, 0x17, 0xfa, 0xd6, 660xf5, 0xdb, 0xc9, 0x27, 0x3b, 0x60, 0x27, 0x2f, 670x84, 0xd7, 0xac, 0x7f, 0xf3, 0xa7, 0x16, 0x31, 680xfa, 0x19, 0x54, 0x57, 0x98, 0xb5, 0xdb, 0x9c, 690xc3, 0xb5, 0x55, 0x72, 0x98, 0x2f, 0x56, 0x33, 700x7c, 0x38, 0x1f, 0xb4, 0x8c, 0x94, 0x1a, 0x6a, 710x26, 0x8a, 0x84, 0xfc, 0x8d, 0xb1, 0x49, 0xbb, 720x6d, 0x11, 0x36, 0xc9, 0x05, 0x57, 0x87, 0xd2, 730xdb, 0xd3, 0xe3, 0xea, 0x08, 0xb2, 0x9f, 0x44, 740x85, 0xd7, 0xd4, 0x96, 0x25, 0xeb, 0x2b, 0xca, 750x86, 0x0f, 0x33, 0x69, 0xc4, 0xda, 0x98, 0x68, 760x21, 0xad, 0xd8, 0xc4, 0x4e, 0x46, 0x33, 0x43, 770xad, 0xe7, 0xfe, 0x58, 0x10, 0x00, 0x76, 0x3c, 780xd4, 0x14, 0x5a, 0x74, 0x43, 0x04, 0xc3, 0xdd, 790x46, 0xc3, 0xe0, 0x4b, 0x46, 0xb5, 0x84, 0xcb, 800xe6, 0x40, 0x71, 0xdf, 0x50, 0x16, 0x5f, 0xf0, 810x0f, 0xc5, 0x9c, 0x50, 0x64, 0xe0, 0x64, 0x1c, 820x58, 0x87, 0xae, 0x91, 0x9c, 0xb6, 0x57, 0x77, 830xf0, 0xc4, 0x3e, 0xcf, 0xb6, 0xc5, 0x10, 0x0c, 840xea, 0x5b, 0xcc, 0xaf, 0xee, 0x7b, 0x15, 0x4e, 850x4e, 0x3c, 0x29, 0x9c, 0xf8, 0xe6, 0x59, 0xca, 860xaf, 0x48, 0x12, 0x99, 0x76, 0xde, 0x54, 0xe2, 870x75, 0x62, 0x31, 0x17, 88}; 89 90#define SIG_LEN 128 91#define HASH_LEN 20 92 93const unsigned char fakeAlgOID[] = { 940x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x05 95}; 96 97const unsigned char fakeG3Cert[] = { 980x01,0x00,0x00,0x00, 990x1c,0x00,0x00,0x00, 1000x01,0x08,0x00,0x00, 1010x10,0x0a,0x00,0x00, 1020x00,0x00,0x00,0x00, 1030x00,0x04,0x00,0x00, 1040x01,0x00,0x01,0x00, 1050x19,0xfc,0xb6,0x7b,0x4e,0xa8,0xd7,0xb1,0xeb,0xf9,0x19,0x28,0x07,0x7f,0x47,0x4c, 1060xe1,0x9f,0xbe,0x01,0x15,0x5c,0xea,0xda,0xc3,0xd1,0x59,0x3c,0x75,0xed,0x00,0x7b, 1070x22,0x67,0x22,0xd6,0xd3,0xee,0xc2,0x04,0xdf,0x47,0xc4,0x85,0xc0,0x56,0xa4,0x8d, 1080xf3,0xf0,0xcf,0x00,0x9d,0xd2,0x03,0xc1,0x23,0x3f,0xc8,0x9f,0xef,0xfe,0xea,0x50, 1090x33,0x6b,0xbe,0x74,0x1a,0xa3,0x3c,0x13,0xa2,0xc5,0xf6,0x75,0x88,0x1d,0x3f,0xba, 1100xff,0x0d,0x47,0xab,0xbe,0xfe,0x42,0xd3,0xea,0xe2,0xe2,0xb0,0x06,0xd7,0x88,0xf8, 1110x1d,0x93,0xdf,0x86,0xbd,0xd8,0xa8,0x5f,0x2b,0xe3,0x97,0x41,0xa1,0xc6,0x1d,0x69, 1120xe9,0x88,0xdf,0x87,0xbb,0x1b,0xff,0x31,0x82,0xae,0x8a,0x69,0x31,0xca,0xc3,0x90, 1130x0f,0x0d,0x22,0xa3,0xcf,0x8d,0xcc,0x23,0x03,0x10,0xed,0x74,0x8e,0x13,0x74,0x49, 1140x9d,0x9a,0x1c,0xf2,0x57,0x2d,0x18,0x89,0x6d,0xb8,0xcc,0xab,0xcf,0xd9,0xd8,0x0e, 1150x46,0x68,0x98,0xa3,0x81,0x5d,0x18,0xe8,0x4d,0x03,0x96,0x14,0xc5,0xaf,0x21,0x91, 1160xb7,0x8c,0x97,0xa1,0x85,0xde,0x85,0x78,0xa8,0xd7,0x25,0x20,0x9b,0x2b,0x98,0x36, 1170xd5,0xfe,0x14,0x9b,0x5d,0xe3,0x78,0xf4,0xd6,0xb2,0x15,0xc9,0xfd,0x13,0x77,0x7b, 1180x8a,0x5e,0x9e,0x85,0xff,0x53,0x6d,0x24,0x5d,0xc9,0x52,0x16,0x98,0x18,0xb1,0xaf, 1190xe1,0x6a,0xd6,0xe8,0xa9,0x7c,0x78,0x8e,0x9f,0x79,0x21,0xa1,0xde,0xf4,0xaf,0x9c, 1200xd4,0x61,0x52,0xf9,0xe7,0xfc,0xd7,0x10,0x1b,0x91,0x66,0x14,0x26,0xfd,0xda,0xee, 1210xe5,0xd9,0x4c,0xb7,0x9d,0x6d,0x17,0xf8,0xc2,0x21,0xb4,0x34,0x08,0x0c,0x44,0x79, 1220x53,0x9c,0x81,0xbf,0x1f,0x22,0x0a,0xa6,0xe7,0x22,0x5f,0x5c,0xcb,0x31,0x2e,0xf5, 1230x0c,0x1a,0xf1,0x67,0x13,0x7f,0xe6,0xb3,0xb2,0xfe,0x6b,0x09,0xac,0xa6,0xd4,0x14, 1240xe7,0xe9,0x11,0x0e,0x49,0x99,0x06,0x04,0xa4,0x43,0x22,0xec,0x9f,0x59,0x83,0xfb, 1250xef,0xa3,0x8f,0x6b,0xde,0x70,0x0c,0xbb,0x89,0xe9,0x88,0xbc,0xeb,0x36,0x42,0x42, 1260x50,0x84,0xf5,0x93,0x98,0x93,0xed,0xa2,0x1f,0x13,0x60,0x36,0xc8,0x2f,0x9f,0xd1, 1270xc4,0x23,0xf6,0xd0,0x49,0x40,0xab,0xbe,0xf7,0x43,0x02,0x96,0xf0,0x74,0xa5,0x7d, 1280x68,0x89,0xfa,0x58, 0xad, 0x7b, 0x2f, 0x7d, 0xe8, 0x21, 0x34, 0x5e, 0x6c, 0x20, 1290x97, 0x9e 130}; 131 132const unsigned char fakeSig[] = { 1330x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10, 1340x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10, 1350x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10, 1360x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10, 1370x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10, 1380x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10, 1390x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10, 1400x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10 141}; 142 143static void 144dumpBytes( const char *title, const unsigned char *data, int len, int nonewline ); 145 146static int 147rsa_sign( 148 const char *keyFile, 149 unsigned char *plain, 150 unsigned plainLength, 151 unsigned char signature[SIG_LEN] ) 152{ 153 int rc = -1; 154 FILE *fp = NULL; 155 RSA *rsa = NULL; 156 unsigned signatureLength = 0; 157 158 fp = fopen ( (char*) keyFile, "r" ); 159 if ( !fp ) { 160 fprintf( stderr, "failed to open file=%s\n", keyFile ); 161 goto cleanup; 162 } 163 rsa = RSA_new(); 164 if ( !rsa ) { 165 fprintf( stderr, "RSA_new() failed\n" ); 166 goto cleanup; 167 } 168 rsa = (RSA*)PEM_read_RSAPrivateKey( fp, &rsa, NULL, NULL ); 169 if ( !rsa ) { 170 fprintf( stderr, "PEM_read_RSAPrivateKey() failed\n" ); 171 goto cleanup; 172 } 173 signatureLength = SIG_LEN; 174 char sha1[20] = {}; 175 (void) SHA1( plain, plainLength, (unsigned char*)sha1 ); 176 rc = RSA_sign( NID_sha1, (unsigned char*)sha1, sizeof( sha1), 177 signature, &signatureLength, rsa ); 178 if ( rc != 1 ) { 179 fprintf(stderr, "RSA_sign failed=%d\n", rc ); 180 rc = -1; 181 goto cleanup; 182 } else { 183 rc = 0; 184 } 185 186cleanup: 187 if ( fp ) fclose( fp ); 188 RSA_free( rsa ); 189 return rc; 190} 191 192static int 193rsa_verify( 194 const char *keyFile, 195 unsigned char *plain, 196 unsigned plainLength, 197 unsigned char signature[SIG_LEN] ) 198{ 199 int rc = -1; 200 FILE *fp = NULL; 201 RSA *rsa = NULL; 202 unsigned signatureLength = 0; 203 204 fp = fopen ( (char*) keyFile, "r" ); 205 if ( !fp ) { 206 fprintf( stderr, "failed to open file=%s\n", keyFile ); 207 goto cleanup; 208 } 209 rsa = RSA_new(); 210 if ( !rsa ) { 211 fprintf( stderr, "RSA_new() failed\n" ); 212 goto cleanup; 213 } 214 rsa = (RSA*)PEM_read_RSAPrivateKey( fp, &rsa, NULL, NULL ); 215 if ( !rsa ) { 216 fprintf( stderr, "PEM_read_RSAPrivateKey() failed\n" ); 217 goto cleanup; 218 } 219 signatureLength = SIG_LEN; 220 char sha1[20] = {}; 221 (void) SHA1( plain, plainLength, (unsigned char*)sha1 ); 222 rc = RSA_verify( NID_sha1, (unsigned char*)sha1, sizeof( sha1 ), 223 signature, signatureLength, rsa ); 224 if ( rc != 1 ) { 225 fprintf(stderr, "RSA_verify failed=%d\n", rc ); 226 rc = -1; 227 goto cleanup; 228 } else { 229 rc = 0; 230 } 231 232cleanup: 233 if ( fp ) fclose( fp ); 234 RSA_free( rsa ); 235 return rc; 236 return rc; 237} 238 239static void 240dumpBytes( const char *title, const unsigned char *data, int len, int nonewline ) 241{ 242 int width = 16; 243 int line = 0; 244 int multiple = 0; 245 246 multiple = ( len % width == 0 ); 247 248 printf( "[%s: %d bytes]\n", title, len ); 249 while ( len-- > 0 ) { 250 line++; 251 printf( "%02X ", *data++ ); 252 if ( line % width == 0 && len > 0 && !nonewline ) { 253 printf( "\n" ); 254 } 255 } 256 257 printf("\n"); 258} 259 260static void 261readFile(char *filename, unsigned char **data, unsigned *len) 262{ 263 int size = 0; 264 FILE *file = NULL; 265 if ((file = fopen(filename, "r")) == NULL) { 266 fprintf(stderr, "could not open file=%s", filename); 267 return; 268 } 269 fseek(file, 0, SEEK_END); 270 size = ftell(file); 271 *len = size; 272 *data = (unsigned char*)malloc(*len); 273 if (!*data) { 274 fprintf(stderr, "Out of memory"); 275 fclose(file); 276 return; 277 } 278 rewind(file); 279 (void)fread(*data, size, 1, file); 280 fclose(file); 281} 282 283static void 284writeFile( char* filename, unsigned char* buf, int len ) 285{ 286 FILE *file = NULL; 287 file = fopen( filename, "w" ); 288 if ( file ) { 289 fwrite( buf, len, 1, file ); 290 fclose( file ); 291 } 292} 293 294 295static void 296verify_bb_ticket( 297 unsigned char *ticketData, 298 unsigned ticketLength, 299 const char *keyFile, 300 bool doPrint ) 301{ 302 if ( doPrint ) { 303 dumpBytes( "Ticket (whole)", ticketData, ticketLength, 0 ); 304 printf( "\nBreakdown:\n" ); 305 } 306 307 DERItem derTicket = { .data = ticketData, .length = ticketLength }; 308 DERReturn drtn; 309 DERBbTicket ticket = {}; 310 DERSize ticketSize; 311 drtn = DERDecodeBbTicket(&derTicket, &ticket, &ticketSize); 312 if (drtn != DR_Success) goto badTicket; 313 fprintf( stderr, "ticketSize=%u\n", ticketSize ); 314 315 // Verify signature if key file exists (we should really use the certificate or GPUK in the ticket here. */ 316 if ( keyFile ) { 317 int status = rsa_verify( 318 keyFile, 319 (unsigned char *)ticket.body.data, 320 ticket.body.length, 321 (unsigned char *)ticket.signature.data ); 322 if ( status ) { 323 fprintf( stderr, "rsa_verify failed=%d\n", status ); 324 } else { 325 fprintf( stdout, "Signature verified successfully\n"); 326 } 327 } 328 329 /* Example of how to retrive fields from ticket. */ 330 DERItem snum; 331 drtn = DERSetDecodeItemWithTag(&ticket.body, 332 ASN1_CONTEXT_SPECIFIC | kBbSNUMTag, &snum); 333 if (drtn != DR_Success) goto badTicket; 334 DERItem chipId; 335 drtn = DERSetDecodeItemWithTag(&ticket.body, 336 ASN1_CONTEXT_SPECIFIC | kBbChipIDTag, &chipId); 337 if (drtn != DR_Success) goto badTicket; 338 339 return; 340badTicket: 341 fprintf( stdout, "Bad ticket\n"); 342 return; 343} 344 345static void 346verify_ticket_file( 347 const char *ticketFile, 348 const char *keyFile ) 349{ 350 unsigned char *ticket = NULL; 351 unsigned ticketLength = 0; 352 readFile( (char*)ticketFile, &ticket, &ticketLength ); 353 verify_bb_ticket( ticket, ticketLength, keyFile, false ); 354} 355 356static void 357make_sample_ap_ticket( void ) 358{ 359 unsigned char chipId[] = { 0x01, 0x02, 0x03, 0x04 }; 360 unsigned char ecid[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; 361 362 DERApTicket ticket = { }; 363 364 /* Encode the signatureAlgorithm field of the ticket. */ 365 DERAlgorithmId algorithmId = { .oid = oidSha1Rsa, .params = {} }; 366 ticket.signatureAlgorithm.length = DERLengthOfEncodedSequence( 367 ASN1_CONSTR_SEQUENCE, &algorithmId, DERNumAlgorithmIdItemSpecs, 368 DERAlgorithmIdItemSpecs); 369 ticket.signatureAlgorithm.data = malloc(ticket.signatureAlgorithm.length); 370 DEREncodeSequence(ASN1_CONSTR_SEQUENCE, &algorithmId, DERNumAlgorithmIdItemSpecs, DERAlgorithmIdItemSpecs, ticket.signatureAlgorithm.data, &ticket.signatureAlgorithm.length); 371 372 /* Construct ticket body. */ 373 DERSize numBodyItemSpecs = 0; 374 DERItemSpec bodyItemSpecs[50] = {}; 375 DERItem bodyItems[50] = {}; 376 377 /* Add tags in sorted order. */ 378 bodyItemSpecs[numBodyItemSpecs].offset = numBodyItemSpecs * sizeof(DERItem); 379 bodyItemSpecs[numBodyItemSpecs].tag = ASN1_CONTEXT_SPECIFIC | kApECIDTag; 380 bodyItemSpecs[numBodyItemSpecs].options = DER_ENC_NO_OPTS; 381 bodyItems[numBodyItemSpecs].data = ecid; 382 bodyItems[numBodyItemSpecs].length = sizeof(ecid); 383 numBodyItemSpecs++; 384 385 bodyItemSpecs[numBodyItemSpecs].offset = numBodyItemSpecs * sizeof(DERItem); 386 bodyItemSpecs[numBodyItemSpecs].tag = ASN1_CONTEXT_SPECIFIC | kApChipIDTag; 387 bodyItemSpecs[numBodyItemSpecs].options = DER_ENC_NO_OPTS; 388 bodyItems[numBodyItemSpecs].data = chipId; 389 bodyItems[numBodyItemSpecs].length = sizeof(chipId); 390 numBodyItemSpecs++; 391 392 /* Encode ticket body. */ 393 ticket.body.length = DERLengthOfEncodedSequence(ASN1_CONSTR_SET, 394 &bodyItems, numBodyItemSpecs, bodyItemSpecs); 395 ticket.body.data = malloc(ticket.body.length); 396 DEREncodeSequence(ASN1_CONSTR_SET, &bodyItems, numBodyItemSpecs, 397 bodyItemSpecs, ticket.body.data, &ticket.body.length); 398 399 // Signature 400 ticket.signature.data = (DERByte *)fakeSig; 401 ticket.signature.length = sizeof(fakeSig); 402 403 // Certificates 404 DERItemSpec certItemSpecs[1]; 405 DERItem certItems[1]; 406 certItemSpecs[0].offset = 0; 407 certItemSpecs[0].tag = ASN1_CONSTR_SEQUENCE; 408 certItemSpecs[0].options = DER_ENC_WRITE_DER; 409 410 // NOTE: The Certificate should be added to the ticket by the host. I'm just simulating that here 411 // to generate the final ticket blob. 412 readFile("S5L8920_TATSU_FAC_DARWIN_DEV_CHAIN.der", &certItems[0].data, &certItems[0].length); 413 414 ticket.certificates.length = DERLengthOfEncodedSequence( 415 ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC | 1, &certItems, 416 1, certItemSpecs); 417 ticket.certificates.data = malloc(ticket.certificates.length); 418 DEREncodeSequence(ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC | 1, &certItems, 419 1, certItemSpecs, ticket.certificates.data, &ticket.certificates.length); 420 421 /* Encode the entire ticket. */ 422 DERSize ticketLength = DERLengthOfEncodedSequence(ASN1_CONSTR_SEQUENCE, 423 &ticket, DERNumApTicketItemSpecs, DERApTicketItemSpecs); 424 DERByte *ticketBytes = malloc(ticketLength); 425 DEREncodeSequence(ASN1_CONSTR_SEQUENCE, &ticket, DERNumApTicketItemSpecs, DERApTicketItemSpecs, 426 ticketBytes, &ticketLength); 427 428 // save ticket to file 429 writeFile("ApTicket.bin", ticketBytes, ticketLength); 430 431//cleanup: 432 free(ticket.body.data); 433 free(ticket.signatureAlgorithm.data); 434 free(ticket.certificates.data); 435 free(ticketBytes); 436} 437 438static void 439make_sample_bb_ticket( void ) 440{ 441 int status = 0; 442 unsigned char chipId[] = { 0x01, 0x02, 0x03, 0x04 }; 443 unsigned char snum[] = { 0x01, 0x02, 0x03, 0x04 }; 444 unsigned char signature[SIG_LEN] = {}; 445 DERByte *ticketBytes = NULL; 446 447 DERBbTicket ticket = {}; 448 449 /* Encode the signatureAlgorithm field of the ticket. */ 450 DERAlgorithmId algorithmId = { .oid = oidSha1Rsa }; 451 ticket.signatureAlgorithm.length = DERLengthOfEncodedSequence( 452 ASN1_CONSTR_SEQUENCE, &algorithmId, DERNumAlgorithmIdItemSpecs, 453 DERAlgorithmIdItemSpecs); 454 ticket.signatureAlgorithm.data = malloc(ticket.signatureAlgorithm.length); 455 DEREncodeSequence(ASN1_CONSTR_SEQUENCE, &algorithmId, 456 DERNumAlgorithmIdItemSpecs, DERAlgorithmIdItemSpecs, 457 ticket.signatureAlgorithm.data, &ticket.signatureAlgorithm.length); 458 459 /* Construct ticket body. */ 460 DERSize numBodyItemSpecs = 0; 461 DERItemSpec bodyItemSpecs[50] = {}; 462 DERItem bodyItems[50] = {}; 463 464 /* Add tags in sorted order. */ 465 bodyItemSpecs[numBodyItemSpecs].offset = numBodyItemSpecs * sizeof(DERItem); 466 bodyItemSpecs[numBodyItemSpecs].tag = ASN1_CONTEXT_SPECIFIC | kBbSNUMTag; 467 bodyItemSpecs[numBodyItemSpecs].options = DER_ENC_NO_OPTS; 468 bodyItems[numBodyItemSpecs].data = snum; 469 bodyItems[numBodyItemSpecs].length = sizeof(snum); 470 numBodyItemSpecs++; 471 472 bodyItemSpecs[numBodyItemSpecs].offset = numBodyItemSpecs * sizeof(DERItem); 473 bodyItemSpecs[numBodyItemSpecs].tag = ASN1_CONTEXT_SPECIFIC | kBbChipIDTag; 474 bodyItemSpecs[numBodyItemSpecs].options = DER_ENC_NO_OPTS; 475 bodyItems[numBodyItemSpecs].data = chipId; 476 bodyItems[numBodyItemSpecs].length = sizeof(chipId); 477 numBodyItemSpecs++; 478 479 /* Encode ticket body. */ 480 ticket.body.length = DERLengthOfEncodedSequence(ASN1_CONSTR_SET, 481 &bodyItems, numBodyItemSpecs, bodyItemSpecs); 482 ticket.body.data = malloc(ticket.body.length); 483 DEREncodeSequence(ASN1_CONSTR_SET, &bodyItems, numBodyItemSpecs, bodyItemSpecs, 484 ticket.body.data, &ticket.body.length); 485 486 // NOTE: In the SEE machine, the Body above will then be hashed/signed to generate signature 487 status = rsa_sign( 488 "G1_GPrK.pem", 489 ticket.body.data, 490 ticket.body.length, 491 (unsigned char *)signature ); 492 if ( status ) { 493 fprintf( stderr, "rsa_sign failed=%d\n", status ); 494 goto cleanup; 495 } else { 496 fprintf( stdout, "Signed successfully\n"); 497 } 498 499 status = rsa_verify( 500 "G1_GPrK.pem", 501 ticket.body.data, 502 ticket.body.length, 503 (unsigned char *)signature ); 504 if ( status ) { 505 fprintf( stderr, "rsa_verify failed=%d\n", status ); 506 goto cleanup; 507 } else { 508 fprintf( stdout, "Signature verified successfully\n"); 509 } 510 511 // Signature 512 ticket.signature.data = signature; 513 ticket.signature.length = SIG_LEN; 514 515 // Certificates 516 ticket.gpuk.length = sizeof(GoldKeyCert); 517 ticket.gpuk.data = (DERByte *)GoldKeyCert; 518 519 /* Encode the entire ticket. */ 520 DERSize ticketLength = DERLengthOfEncodedSequence(ASN1_CONSTR_SEQUENCE, 521 &ticket, DERNumBbTicketItemSpecs, DERBbTicketItemSpecs); 522 ticketBytes = malloc(ticketLength); 523 DEREncodeSequence(ASN1_CONSTR_SEQUENCE, &ticket, DERNumBbTicketItemSpecs, 524 DERBbTicketItemSpecs, ticketBytes, &ticketLength); 525 526 // save ticket to file 527 writeFile("BbTicket.bin", ticketBytes, ticketLength); 528 529cleanup: 530 free(ticket.body.data); 531 free(ticket.signatureAlgorithm.data); 532 free(ticketBytes); 533} 534 535static void 536long_tag_test(void) 537{ 538 printf("ASN1_TAG_MASK 0x%.016qx\n", (uint64_t)ASN1_TAG_MASK); 539 printf("ASN1_TAGNUM_MASK 0x%.016qx\n", (uint64_t)ASN1_TAGNUM_MASK); 540 printf("ASN1_METHOD_MASK 0x%.016qx\n", (uint64_t)ASN1_METHOD_MASK); 541 printf("ASN1_PRIMITIVE 0x%.016qx\n", (uint64_t)ASN1_PRIMITIVE); 542 printf("ASN1_CONSTRUCTED 0x%.016qx\n", (uint64_t)ASN1_CONSTRUCTED); 543 printf("ASN1_CLASS_MASK 0x%.016qx\n", (uint64_t)ASN1_CLASS_MASK); 544 printf("ASN1_UNIVERSAL 0x%.016qx\n", (uint64_t)ASN1_UNIVERSAL); 545 printf("ASN1_APPLICATION 0x%.016qx\n", (uint64_t)ASN1_APPLICATION); 546 printf("ASN1_CONTEXT_SPECIFIC 0x%.016qx\n", (uint64_t)ASN1_CONTEXT_SPECIFIC); 547 printf("ASN1_PRIVATE 0x%.016qx\n", (uint64_t)ASN1_PRIVATE); 548 549 DERByte buf[10]; 550 DERSize len = sizeof(buf); 551 DERReturn drtn; 552 DERTag tag = ASN1_CONTEXT_SPECIFIC | ASN1_TAGNUM_MASK; 553 drtn = DEREncodeItem(tag, 0, 0, buf, &len); 554 if (drtn) 555 { 556 printf("DEREncodeItem: %u\n", drtn); 557 } 558 DERItem der = { .data = buf, .length = len }; 559 560 dumpBytes("tlv", buf, len, 0); 561 562 DERDecodedInfo decoded; 563 drtn = DERDecodeItem(&der, &decoded); 564 if (drtn) 565 { 566 printf("DERDecodeItem: %u\n", drtn); 567 } 568 if (decoded.tag != tag) 569 { 570 printf("DERDecodeItem tag: 0x%qx != 0x%qx\n", (uint64_t)decoded.tag, (uint64_t)tag); 571 } 572 printf("DERDecodeItem tag: 0x%qx encoded in %u bytes, decoded length: %u\n", 573 (uint64_t)tag, len, decoded.content.length); 574} 575 576int main(int argc, char **argv) 577{ 578 long_tag_test(); 579 printf( "=> Making sample BB ticket...\n"); 580 make_sample_bb_ticket(); 581 printf( "=> Making sample AP ticket...\n"); 582 make_sample_ap_ticket(); 583 printf( "=> Verifying signature in bb_ticket.bin ...\n"); 584 verify_ticket_file( "bb_ticket.bin", "G1_GPrK.pem" ); 585 return 0; 586} 587