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"
19 const unsigned char GoldKeyCert
[] = {
20 0x01, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
21 0x00, 0x08, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00,
22 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
23 0x03, 0x00, 0x00, 0x00, 0x31, 0x6e, 0xc6, 0x4f,
24 0xf1, 0xe8, 0x7d, 0x81, 0x38, 0x6d, 0xd8, 0xb3,
25 0x2b, 0xe4, 0xb5, 0xa0, 0x09, 0xaf, 0x74, 0xdd,
26 0xe9, 0x60, 0x27, 0x42, 0x07, 0xa3, 0xac, 0xef,
27 0xe5, 0x75, 0x07, 0xa8, 0xc2, 0x2c, 0x25, 0x56,
28 0x91, 0x37, 0xea, 0xdb, 0xdb, 0x68, 0x4a, 0x1c,
29 0xe3, 0x29, 0x61, 0x85, 0xd7, 0xd8, 0x66, 0x86,
30 0x66, 0xbf, 0xbf, 0x98, 0xae, 0xb4, 0xe9, 0x6c,
31 0x13, 0x81, 0x97, 0x78, 0x2a, 0x8d, 0xdc, 0x37,
32 0x9a, 0xf1, 0xa4, 0x0a, 0x9d, 0x74, 0xd5, 0x72,
33 0xbc, 0xb5, 0x64, 0xa6, 0x1a, 0x62, 0xd5, 0x39,
34 0xfb, 0x6f, 0xc0, 0xd1, 0xc7, 0x93, 0xc3, 0x20,
35 0xda, 0x84, 0x69, 0x1e, 0xd9, 0x96, 0x2e, 0xc1,
36 0x4f, 0x28, 0x96, 0x14, 0xa4, 0x68, 0x0a, 0x40,
37 0xe5, 0x17, 0xe7, 0xd6, 0x76, 0x4d, 0x81, 0xd8,
38 0xd2, 0xa6, 0x18, 0x82, 0x36, 0x40, 0x97, 0x31,
39 0xd9, 0x88, 0xdf, 0xaf, 0x05, 0x3a, 0x4b, 0x4e,
40 0x1b, 0x4a, 0x76, 0x6f, 0xb9, 0x6c, 0x18, 0x5d,
41 0xd5, 0x98, 0xf0, 0xf1, 0xbe, 0x0a, 0xd9, 0x57,
42 0x85, 0xc6, 0xc9, 0x63, 0xb3, 0xf5, 0x21, 0x26,
43 0x07, 0xba, 0x6a, 0x05, 0xfb, 0x5a, 0x06, 0x87,
44 0x2a, 0x30, 0x3f, 0xa9, 0xea, 0xab, 0x0e, 0x50,
45 0x70, 0x3b, 0x7e, 0xd4, 0xd2, 0x8c, 0xf3, 0xa1,
46 0xcf, 0x9a, 0x6c, 0x6b, 0xcf, 0x9b, 0x1b, 0x2a,
47 0x97, 0x6a, 0x3c, 0x38, 0x40, 0x43, 0xb1, 0x97,
48 0x19, 0x07, 0x64, 0x11, 0x94, 0x73, 0x14, 0xc9,
49 0xa3, 0xfe, 0x7f, 0xf6, 0x64, 0x23, 0x73, 0xe3,
50 0x76, 0xce, 0xf7, 0xf4, 0x2f, 0x6c, 0x9d, 0x0a,
51 0xf6, 0x39, 0xe6, 0x1d, 0xb2, 0x17, 0x29, 0x39,
52 0x98, 0x52, 0xda, 0xe0, 0x31, 0xa1, 0xfa, 0x85,
53 0x52, 0xc2, 0x60, 0xb5, 0x11, 0x42, 0xc6, 0x9b,
54 0x55, 0xd8, 0x40, 0x37, 0xf7, 0xdb, 0x01, 0x6a,
55 0xd5, 0x26, 0x3b, 0x27, 0x07, 0x20, 0xf7, 0x58,
56 0xd5, 0xa4, 0x1c, 0xe6, 0x2f, 0x74, 0x14, 0x6e,
57 0xa4, 0xe8, 0xc8, 0xe8, 0x9a, 0x39, 0x6d, 0xde,
58 0x7f, 0x67, 0x65, 0x40, 0x68, 0x26, 0x65, 0x62,
59 0x95, 0x87, 0x45, 0x62, 0x0d, 0x8d, 0x42, 0xad,
60 0x3b, 0x4f, 0xd3, 0x8f, 0x58, 0xcb, 0x61, 0x46,
61 0xc9, 0x3d, 0x7d, 0x75, 0x3c, 0x6d, 0xac, 0xdf,
62 0x53, 0xf4, 0x66, 0x9e, 0x14, 0x82, 0xc7, 0xd1,
63 0xd0, 0xec, 0x92, 0x24, 0x97, 0x1e, 0xc9, 0x7a,
64 0xfd, 0x8f, 0x75, 0xe2, 0xfd, 0x7e, 0x07, 0x44,
65 0x46, 0x56, 0x64, 0x9b, 0x1b, 0x17, 0xfa, 0xd6,
66 0xf5, 0xdb, 0xc9, 0x27, 0x3b, 0x60, 0x27, 0x2f,
67 0x84, 0xd7, 0xac, 0x7f, 0xf3, 0xa7, 0x16, 0x31,
68 0xfa, 0x19, 0x54, 0x57, 0x98, 0xb5, 0xdb, 0x9c,
69 0xc3, 0xb5, 0x55, 0x72, 0x98, 0x2f, 0x56, 0x33,
70 0x7c, 0x38, 0x1f, 0xb4, 0x8c, 0x94, 0x1a, 0x6a,
71 0x26, 0x8a, 0x84, 0xfc, 0x8d, 0xb1, 0x49, 0xbb,
72 0x6d, 0x11, 0x36, 0xc9, 0x05, 0x57, 0x87, 0xd2,
73 0xdb, 0xd3, 0xe3, 0xea, 0x08, 0xb2, 0x9f, 0x44,
74 0x85, 0xd7, 0xd4, 0x96, 0x25, 0xeb, 0x2b, 0xca,
75 0x86, 0x0f, 0x33, 0x69, 0xc4, 0xda, 0x98, 0x68,
76 0x21, 0xad, 0xd8, 0xc4, 0x4e, 0x46, 0x33, 0x43,
77 0xad, 0xe7, 0xfe, 0x58, 0x10, 0x00, 0x76, 0x3c,
78 0xd4, 0x14, 0x5a, 0x74, 0x43, 0x04, 0xc3, 0xdd,
79 0x46, 0xc3, 0xe0, 0x4b, 0x46, 0xb5, 0x84, 0xcb,
80 0xe6, 0x40, 0x71, 0xdf, 0x50, 0x16, 0x5f, 0xf0,
81 0x0f, 0xc5, 0x9c, 0x50, 0x64, 0xe0, 0x64, 0x1c,
82 0x58, 0x87, 0xae, 0x91, 0x9c, 0xb6, 0x57, 0x77,
83 0xf0, 0xc4, 0x3e, 0xcf, 0xb6, 0xc5, 0x10, 0x0c,
84 0xea, 0x5b, 0xcc, 0xaf, 0xee, 0x7b, 0x15, 0x4e,
85 0x4e, 0x3c, 0x29, 0x9c, 0xf8, 0xe6, 0x59, 0xca,
86 0xaf, 0x48, 0x12, 0x99, 0x76, 0xde, 0x54, 0xe2,
87 0x75, 0x62, 0x31, 0x17,
93 const unsigned char fakeAlgOID
[] = {
94 0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x05
97 const unsigned char fakeG3Cert
[] = {
105 0x19,0xfc,0xb6,0x7b,0x4e,0xa8,0xd7,0xb1,0xeb,0xf9,0x19,0x28,0x07,0x7f,0x47,0x4c,
106 0xe1,0x9f,0xbe,0x01,0x15,0x5c,0xea,0xda,0xc3,0xd1,0x59,0x3c,0x75,0xed,0x00,0x7b,
107 0x22,0x67,0x22,0xd6,0xd3,0xee,0xc2,0x04,0xdf,0x47,0xc4,0x85,0xc0,0x56,0xa4,0x8d,
108 0xf3,0xf0,0xcf,0x00,0x9d,0xd2,0x03,0xc1,0x23,0x3f,0xc8,0x9f,0xef,0xfe,0xea,0x50,
109 0x33,0x6b,0xbe,0x74,0x1a,0xa3,0x3c,0x13,0xa2,0xc5,0xf6,0x75,0x88,0x1d,0x3f,0xba,
110 0xff,0x0d,0x47,0xab,0xbe,0xfe,0x42,0xd3,0xea,0xe2,0xe2,0xb0,0x06,0xd7,0x88,0xf8,
111 0x1d,0x93,0xdf,0x86,0xbd,0xd8,0xa8,0x5f,0x2b,0xe3,0x97,0x41,0xa1,0xc6,0x1d,0x69,
112 0xe9,0x88,0xdf,0x87,0xbb,0x1b,0xff,0x31,0x82,0xae,0x8a,0x69,0x31,0xca,0xc3,0x90,
113 0x0f,0x0d,0x22,0xa3,0xcf,0x8d,0xcc,0x23,0x03,0x10,0xed,0x74,0x8e,0x13,0x74,0x49,
114 0x9d,0x9a,0x1c,0xf2,0x57,0x2d,0x18,0x89,0x6d,0xb8,0xcc,0xab,0xcf,0xd9,0xd8,0x0e,
115 0x46,0x68,0x98,0xa3,0x81,0x5d,0x18,0xe8,0x4d,0x03,0x96,0x14,0xc5,0xaf,0x21,0x91,
116 0xb7,0x8c,0x97,0xa1,0x85,0xde,0x85,0x78,0xa8,0xd7,0x25,0x20,0x9b,0x2b,0x98,0x36,
117 0xd5,0xfe,0x14,0x9b,0x5d,0xe3,0x78,0xf4,0xd6,0xb2,0x15,0xc9,0xfd,0x13,0x77,0x7b,
118 0x8a,0x5e,0x9e,0x85,0xff,0x53,0x6d,0x24,0x5d,0xc9,0x52,0x16,0x98,0x18,0xb1,0xaf,
119 0xe1,0x6a,0xd6,0xe8,0xa9,0x7c,0x78,0x8e,0x9f,0x79,0x21,0xa1,0xde,0xf4,0xaf,0x9c,
120 0xd4,0x61,0x52,0xf9,0xe7,0xfc,0xd7,0x10,0x1b,0x91,0x66,0x14,0x26,0xfd,0xda,0xee,
121 0xe5,0xd9,0x4c,0xb7,0x9d,0x6d,0x17,0xf8,0xc2,0x21,0xb4,0x34,0x08,0x0c,0x44,0x79,
122 0x53,0x9c,0x81,0xbf,0x1f,0x22,0x0a,0xa6,0xe7,0x22,0x5f,0x5c,0xcb,0x31,0x2e,0xf5,
123 0x0c,0x1a,0xf1,0x67,0x13,0x7f,0xe6,0xb3,0xb2,0xfe,0x6b,0x09,0xac,0xa6,0xd4,0x14,
124 0xe7,0xe9,0x11,0x0e,0x49,0x99,0x06,0x04,0xa4,0x43,0x22,0xec,0x9f,0x59,0x83,0xfb,
125 0xef,0xa3,0x8f,0x6b,0xde,0x70,0x0c,0xbb,0x89,0xe9,0x88,0xbc,0xeb,0x36,0x42,0x42,
126 0x50,0x84,0xf5,0x93,0x98,0x93,0xed,0xa2,0x1f,0x13,0x60,0x36,0xc8,0x2f,0x9f,0xd1,
127 0xc4,0x23,0xf6,0xd0,0x49,0x40,0xab,0xbe,0xf7,0x43,0x02,0x96,0xf0,0x74,0xa5,0x7d,
128 0x68,0x89,0xfa,0x58, 0xad, 0x7b, 0x2f, 0x7d, 0xe8, 0x21, 0x34, 0x5e, 0x6c, 0x20,
132 const unsigned char fakeSig
[] = {
133 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
134 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
135 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
136 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
137 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
138 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
139 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
140 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10
144 dumpBytes( const char *title
, const unsigned char *data
, int len
, int nonewline
);
149 unsigned char *plain
,
150 unsigned plainLength
,
151 unsigned char signature
[SIG_LEN
] )
156 unsigned signatureLength
= 0;
158 fp
= fopen ( (char*) keyFile
, "r" );
160 fprintf( stderr
, "failed to open file=%s\n", keyFile
);
165 fprintf( stderr
, "RSA_new() failed\n" );
168 rsa
= (RSA
*)PEM_read_RSAPrivateKey( fp
, &rsa
, NULL
, NULL
);
170 fprintf( stderr
, "PEM_read_RSAPrivateKey() failed\n" );
173 signatureLength
= SIG_LEN
;
175 (void) SHA1( plain
, plainLength
, (unsigned char*)sha1
);
176 rc
= RSA_sign( NID_sha1
, (unsigned char*)sha1
, sizeof( sha1
),
177 signature
, &signatureLength
, rsa
);
179 fprintf(stderr
, "RSA_sign failed=%d\n", rc
);
187 if ( fp
) fclose( fp
);
195 unsigned char *plain
,
196 unsigned plainLength
,
197 unsigned char signature
[SIG_LEN
] )
202 unsigned signatureLength
= 0;
204 fp
= fopen ( (char*) keyFile
, "r" );
206 fprintf( stderr
, "failed to open file=%s\n", keyFile
);
211 fprintf( stderr
, "RSA_new() failed\n" );
214 rsa
= (RSA
*)PEM_read_RSAPrivateKey( fp
, &rsa
, NULL
, NULL
);
216 fprintf( stderr
, "PEM_read_RSAPrivateKey() failed\n" );
219 signatureLength
= SIG_LEN
;
221 (void) SHA1( plain
, plainLength
, (unsigned char*)sha1
);
222 rc
= RSA_verify( NID_sha1
, (unsigned char*)sha1
, sizeof( sha1
),
223 signature
, signatureLength
, rsa
);
225 fprintf(stderr
, "RSA_verify failed=%d\n", rc
);
233 if ( fp
) fclose( fp
);
240 dumpBytes( const char *title
, const unsigned char *data
, int len
, int nonewline
)
246 multiple
= ( len
% width
== 0 );
248 printf( "[%s: %d bytes]\n", title
, len
);
249 while ( len
-- > 0 ) {
251 printf( "%02X ", *data
++ );
252 if ( line
% width
== 0 && len
> 0 && !nonewline
) {
261 readFile(char *filename
, unsigned char **data
, unsigned *len
)
265 if ((file
= fopen(filename
, "r")) == NULL
) {
266 fprintf(stderr
, "could not open file=%s", filename
);
269 fseek(file
, 0, SEEK_END
);
272 *data
= (unsigned char*)malloc(*len
);
274 fprintf(stderr
, "Out of memory");
279 (void)fread(*data
, size
, 1, file
);
284 writeFile( char* filename
, unsigned char* buf
, int len
)
287 file
= fopen( filename
, "w" );
289 fwrite( buf
, len
, 1, file
);
297 unsigned char *ticketData
,
298 unsigned ticketLength
,
303 dumpBytes( "Ticket (whole)", ticketData
, ticketLength
, 0 );
304 printf( "\nBreakdown:\n" );
307 DERItem derTicket
= { .data
= ticketData
, .length
= ticketLength
};
309 DERBbTicket ticket
= {};
311 drtn
= DERDecodeBbTicket(&derTicket
, &ticket
, &ticketSize
);
312 if (drtn
!= DR_Success
) goto badTicket
;
313 fprintf( stderr
, "ticketSize=%u\n", ticketSize
);
315 // Verify signature if key file exists (we should really use the certificate or GPUK in the ticket here. */
317 int status
= rsa_verify(
319 (unsigned char *)ticket
.body
.data
,
321 (unsigned char *)ticket
.signature
.data
);
323 fprintf( stderr
, "rsa_verify failed=%d\n", status
);
325 fprintf( stdout
, "Signature verified successfully\n");
329 /* Example of how to retrive fields from ticket. */
331 drtn
= DERSetDecodeItemWithTag(&ticket
.body
,
332 ASN1_CONTEXT_SPECIFIC
| kBbSNUMTag
, &snum
);
333 if (drtn
!= DR_Success
) goto badTicket
;
335 drtn
= DERSetDecodeItemWithTag(&ticket
.body
,
336 ASN1_CONTEXT_SPECIFIC
| kBbChipIDTag
, &chipId
);
337 if (drtn
!= DR_Success
) goto badTicket
;
341 fprintf( stdout
, "Bad ticket\n");
347 const char *ticketFile
,
348 const char *keyFile
)
350 unsigned char *ticket
= NULL
;
351 unsigned ticketLength
= 0;
352 readFile( (char*)ticketFile
, &ticket
, &ticketLength
);
353 verify_bb_ticket( ticket
, ticketLength
, keyFile
, false );
357 make_sample_ap_ticket( void )
359 unsigned char chipId
[] = { 0x01, 0x02, 0x03, 0x04 };
360 unsigned char ecid
[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
362 DERApTicket ticket
= { };
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
);
372 /* Construct ticket body. */
373 DERSize numBodyItemSpecs
= 0;
374 DERItemSpec bodyItemSpecs
[50] = {};
375 DERItem bodyItems
[50] = {};
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
);
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
);
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
);
400 ticket
.signature
.data
= (DERByte
*)fakeSig
;
401 ticket
.signature
.length
= sizeof(fakeSig
);
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
;
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
);
414 ticket
.certificates
.length
= DERLengthOfEncodedSequence(
415 ASN1_CONSTRUCTED
| ASN1_CONTEXT_SPECIFIC
| 1, &certItems
,
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
);
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
);
428 // save ticket to file
429 writeFile("ApTicket.bin", ticketBytes
, ticketLength
);
432 free(ticket
.body
.data
);
433 free(ticket
.signatureAlgorithm
.data
);
434 free(ticket
.certificates
.data
);
439 make_sample_bb_ticket( void )
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
;
447 DERBbTicket ticket
= {};
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
);
459 /* Construct ticket body. */
460 DERSize numBodyItemSpecs
= 0;
461 DERItemSpec bodyItemSpecs
[50] = {};
462 DERItem bodyItems
[50] = {};
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
);
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
);
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
);
486 // NOTE: In the SEE machine, the Body above will then be hashed/signed to generate signature
491 (unsigned char *)signature
);
493 fprintf( stderr
, "rsa_sign failed=%d\n", status
);
496 fprintf( stdout
, "Signed successfully\n");
503 (unsigned char *)signature
);
505 fprintf( stderr
, "rsa_verify failed=%d\n", status
);
508 fprintf( stdout
, "Signature verified successfully\n");
512 ticket
.signature
.data
= signature
;
513 ticket
.signature
.length
= SIG_LEN
;
516 ticket
.gpuk
.length
= sizeof(GoldKeyCert
);
517 ticket
.gpuk
.data
= (DERByte
*)GoldKeyCert
;
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
);
526 // save ticket to file
527 writeFile("BbTicket.bin", ticketBytes
, ticketLength
);
530 free(ticket
.body
.data
);
531 free(ticket
.signatureAlgorithm
.data
);
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
);
550 DERSize len
= sizeof(buf
);
552 DERTag tag
= ASN1_CONTEXT_SPECIFIC
| ASN1_TAGNUM_MASK
;
553 drtn
= DEREncodeItem(tag
, 0, 0, buf
, &len
);
556 printf("DEREncodeItem: %u\n", drtn
);
558 DERItem der
= { .data
= buf
, .length
= len
};
560 dumpBytes("tlv", buf
, len
, 0);
562 DERDecodedInfo decoded
;
563 drtn
= DERDecodeItem(&der
, &decoded
);
566 printf("DERDecodeItem: %u\n", drtn
);
568 if (decoded
.tag
!= tag
)
570 printf("DERDecodeItem tag: 0x%qx != 0x%qx\n", (uint64_t)decoded
.tag
, (uint64_t)tag
);
572 printf("DERDecodeItem tag: 0x%qx encoded in %u bytes, decoded length: %u\n",
573 (uint64_t)tag
, len
, decoded
.content
.length
);
576 int main(int argc
, char **argv
)
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" );