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 writeFile( char* filename
, unsigned char* buf
, int len
)
264 file
= fopen( filename
, "w" );
266 fwrite( buf
, len
, 1, file
);
274 unsigned char *ticketData
,
275 unsigned ticketLength
,
280 dumpBytes( "Ticket (whole)", ticketData
, ticketLength
, 0 );
281 printf( "\nBreakdown:\n" );
284 DERItem derTicket
= { .data
= ticketData
, .length
= ticketLength
};
286 DERBbTicket ticket
= {};
288 drtn
= DERDecodeBbTicket(&derTicket
, &ticket
, &ticketSize
);
289 if (drtn
!= DR_Success
) goto badTicket
;
290 fprintf( stderr
, "ticketSize=%u\n", ticketSize
);
292 // Verify signature if key file exists (we should really use the certificate or GPUK in the ticket here. */
294 int status
= rsa_verify(
296 (unsigned char *)ticket
.body
.data
,
298 (unsigned char *)ticket
.signature
.data
);
300 fprintf( stderr
, "rsa_verify failed=%d\n", status
);
302 fprintf( stdout
, "Signature verified successfully\n");
306 /* Example of how to retrive fields from ticket. */
308 drtn
= DERSetDecodeItemWithTag(&ticket
.body
,
309 ASN1_CONTEXT_SPECIFIC
| kBbSNUMTag
, &snum
);
310 if (drtn
!= DR_Success
) goto badTicket
;
312 drtn
= DERSetDecodeItemWithTag(&ticket
.body
,
313 ASN1_CONTEXT_SPECIFIC
| kBbChipIDTag
, &chipId
);
314 if (drtn
!= DR_Success
) goto badTicket
;
318 fprintf( stdout
, "Bad ticket\n");
324 const char *ticketFile
,
325 const char *keyFile
)
327 unsigned char *ticket
= NULL
;
328 unsigned ticketLength
= 0;
329 readFile( (char*)ticketFile
, &ticket
, &ticketLength
);
330 verify_bb_ticket( ticket
, ticketLength
, keyFile
, false );
334 make_sample_ap_ticket( void )
336 unsigned char chipId
[] = { 0x01, 0x02, 0x03, 0x04 };
337 unsigned char ecid
[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
339 DERApTicket ticket
= { };
341 /* Encode the signatureAlgorithm field of the ticket. */
342 DERAlgorithmId algorithmId
= { .oid
= oidSha1Rsa
, .params
= {} };
343 ticket
.signatureAlgorithm
.length
= DERLengthOfEncodedSequence(
344 ASN1_CONSTR_SEQUENCE
, &algorithmId
, DERNumAlgorithmIdItemSpecs
,
345 DERAlgorithmIdItemSpecs
);
346 ticket
.signatureAlgorithm
.data
= malloc(ticket
.signatureAlgorithm
.length
);
347 DEREncodeSequence(ASN1_CONSTR_SEQUENCE
, &algorithmId
, DERNumAlgorithmIdItemSpecs
, DERAlgorithmIdItemSpecs
, ticket
.signatureAlgorithm
.data
, &ticket
.signatureAlgorithm
.length
);
349 /* Construct ticket body. */
350 DERSize numBodyItemSpecs
= 0;
351 DERItemSpec bodyItemSpecs
[50] = {};
352 DERItem bodyItems
[50] = {};
354 /* Add tags in sorted order. */
355 bodyItemSpecs
[numBodyItemSpecs
].offset
= numBodyItemSpecs
* sizeof(DERItem
);
356 bodyItemSpecs
[numBodyItemSpecs
].tag
= ASN1_CONTEXT_SPECIFIC
| kApECIDTag
;
357 bodyItemSpecs
[numBodyItemSpecs
].options
= DER_ENC_NO_OPTS
;
358 bodyItems
[numBodyItemSpecs
].data
= ecid
;
359 bodyItems
[numBodyItemSpecs
].length
= sizeof(ecid
);
362 bodyItemSpecs
[numBodyItemSpecs
].offset
= numBodyItemSpecs
* sizeof(DERItem
);
363 bodyItemSpecs
[numBodyItemSpecs
].tag
= ASN1_CONTEXT_SPECIFIC
| kApChipIDTag
;
364 bodyItemSpecs
[numBodyItemSpecs
].options
= DER_ENC_NO_OPTS
;
365 bodyItems
[numBodyItemSpecs
].data
= chipId
;
366 bodyItems
[numBodyItemSpecs
].length
= sizeof(chipId
);
369 /* Encode ticket body. */
370 ticket
.body
.length
= DERLengthOfEncodedSequence(ASN1_CONSTR_SET
,
371 &bodyItems
, numBodyItemSpecs
, bodyItemSpecs
);
372 ticket
.body
.data
= malloc(ticket
.body
.length
);
373 DEREncodeSequence(ASN1_CONSTR_SET
, &bodyItems
, numBodyItemSpecs
,
374 bodyItemSpecs
, ticket
.body
.data
, &ticket
.body
.length
);
377 ticket
.signature
.data
= (DERByte
*)fakeSig
;
378 ticket
.signature
.length
= sizeof(fakeSig
);
381 DERItemSpec certItemSpecs
[1];
382 DERItem certItems
[1];
383 certItemSpecs
[0].offset
= 0;
384 certItemSpecs
[0].tag
= ASN1_CONSTR_SEQUENCE
;
385 certItemSpecs
[0].options
= DER_ENC_WRITE_DER
;
387 // NOTE: The Certificate should be added to the ticket by the host. I'm just simulating that here
388 // to generate the final ticket blob.
389 readFile("S5L8920_TATSU_FAC_DARWIN_DEV_CHAIN.der", &certItems
[0].data
, &certItems
[0].length
);
391 ticket
.certificates
.length
= DERLengthOfEncodedSequence(
392 ASN1_CONSTRUCTED
| ASN1_CONTEXT_SPECIFIC
| 1, &certItems
,
394 ticket
.certificates
.data
= malloc(ticket
.certificates
.length
);
395 DEREncodeSequence(ASN1_CONSTRUCTED
| ASN1_CONTEXT_SPECIFIC
| 1, &certItems
,
396 1, certItemSpecs
, ticket
.certificates
.data
, &ticket
.certificates
.length
);
398 /* Encode the entire ticket. */
399 DERSize ticketLength
= DERLengthOfEncodedSequence(ASN1_CONSTR_SEQUENCE
,
400 &ticket
, DERNumApTicketItemSpecs
, DERApTicketItemSpecs
);
401 DERByte
*ticketBytes
= malloc(ticketLength
);
402 DEREncodeSequence(ASN1_CONSTR_SEQUENCE
, &ticket
, DERNumApTicketItemSpecs
, DERApTicketItemSpecs
,
403 ticketBytes
, &ticketLength
);
405 // save ticket to file
406 writeFile("ApTicket.bin", ticketBytes
, ticketLength
);
409 free(ticket
.body
.data
);
410 free(ticket
.signatureAlgorithm
.data
);
411 free(ticket
.certificates
.data
);
416 make_sample_bb_ticket( void )
419 unsigned char chipId
[] = { 0x01, 0x02, 0x03, 0x04 };
420 unsigned char snum
[] = { 0x01, 0x02, 0x03, 0x04 };
421 unsigned char signature
[SIG_LEN
] = {};
422 DERByte
*ticketBytes
= NULL
;
424 DERBbTicket ticket
= {};
426 /* Encode the signatureAlgorithm field of the ticket. */
427 DERAlgorithmId algorithmId
= { .oid
= oidSha1Rsa
};
428 ticket
.signatureAlgorithm
.length
= DERLengthOfEncodedSequence(
429 ASN1_CONSTR_SEQUENCE
, &algorithmId
, DERNumAlgorithmIdItemSpecs
,
430 DERAlgorithmIdItemSpecs
);
431 ticket
.signatureAlgorithm
.data
= malloc(ticket
.signatureAlgorithm
.length
);
432 DEREncodeSequence(ASN1_CONSTR_SEQUENCE
, &algorithmId
,
433 DERNumAlgorithmIdItemSpecs
, DERAlgorithmIdItemSpecs
,
434 ticket
.signatureAlgorithm
.data
, &ticket
.signatureAlgorithm
.length
);
436 /* Construct ticket body. */
437 DERSize numBodyItemSpecs
= 0;
438 DERItemSpec bodyItemSpecs
[50] = {};
439 DERItem bodyItems
[50] = {};
441 /* Add tags in sorted order. */
442 bodyItemSpecs
[numBodyItemSpecs
].offset
= numBodyItemSpecs
* sizeof(DERItem
);
443 bodyItemSpecs
[numBodyItemSpecs
].tag
= ASN1_CONTEXT_SPECIFIC
| kBbSNUMTag
;
444 bodyItemSpecs
[numBodyItemSpecs
].options
= DER_ENC_NO_OPTS
;
445 bodyItems
[numBodyItemSpecs
].data
= snum
;
446 bodyItems
[numBodyItemSpecs
].length
= sizeof(snum
);
449 bodyItemSpecs
[numBodyItemSpecs
].offset
= numBodyItemSpecs
* sizeof(DERItem
);
450 bodyItemSpecs
[numBodyItemSpecs
].tag
= ASN1_CONTEXT_SPECIFIC
| kBbChipIDTag
;
451 bodyItemSpecs
[numBodyItemSpecs
].options
= DER_ENC_NO_OPTS
;
452 bodyItems
[numBodyItemSpecs
].data
= chipId
;
453 bodyItems
[numBodyItemSpecs
].length
= sizeof(chipId
);
456 /* Encode ticket body. */
457 ticket
.body
.length
= DERLengthOfEncodedSequence(ASN1_CONSTR_SET
,
458 &bodyItems
, numBodyItemSpecs
, bodyItemSpecs
);
459 ticket
.body
.data
= malloc(ticket
.body
.length
);
460 DEREncodeSequence(ASN1_CONSTR_SET
, &bodyItems
, numBodyItemSpecs
, bodyItemSpecs
,
461 ticket
.body
.data
, &ticket
.body
.length
);
463 // NOTE: In the SEE machine, the Body above will then be hashed/signed to generate signature
468 (unsigned char *)signature
);
470 fprintf( stderr
, "rsa_sign failed=%d\n", status
);
473 fprintf( stdout
, "Signed successfully\n");
480 (unsigned char *)signature
);
482 fprintf( stderr
, "rsa_verify failed=%d\n", status
);
485 fprintf( stdout
, "Signature verified successfully\n");
489 ticket
.signature
.data
= signature
;
490 ticket
.signature
.length
= SIG_LEN
;
493 ticket
.gpuk
.length
= sizeof(GoldKeyCert
);
494 ticket
.gpuk
.data
= (DERByte
*)GoldKeyCert
;
496 /* Encode the entire ticket. */
497 DERSize ticketLength
= DERLengthOfEncodedSequence(ASN1_CONSTR_SEQUENCE
,
498 &ticket
, DERNumBbTicketItemSpecs
, DERBbTicketItemSpecs
);
499 ticketBytes
= malloc(ticketLength
);
500 DEREncodeSequence(ASN1_CONSTR_SEQUENCE
, &ticket
, DERNumBbTicketItemSpecs
,
501 DERBbTicketItemSpecs
, ticketBytes
, &ticketLength
);
503 // save ticket to file
504 writeFile("BbTicket.bin", ticketBytes
, ticketLength
);
507 free(ticket
.body
.data
);
508 free(ticket
.signatureAlgorithm
.data
);
515 printf("ASN1_TAG_MASK 0x%.016qx\n", (uint64_t)ASN1_TAG_MASK
);
516 printf("ASN1_TAGNUM_MASK 0x%.016qx\n", (uint64_t)ASN1_TAGNUM_MASK
);
517 printf("ASN1_METHOD_MASK 0x%.016qx\n", (uint64_t)ASN1_METHOD_MASK
);
518 printf("ASN1_PRIMITIVE 0x%.016qx\n", (uint64_t)ASN1_PRIMITIVE
);
519 printf("ASN1_CONSTRUCTED 0x%.016qx\n", (uint64_t)ASN1_CONSTRUCTED
);
520 printf("ASN1_CLASS_MASK 0x%.016qx\n", (uint64_t)ASN1_CLASS_MASK
);
521 printf("ASN1_UNIVERSAL 0x%.016qx\n", (uint64_t)ASN1_UNIVERSAL
);
522 printf("ASN1_APPLICATION 0x%.016qx\n", (uint64_t)ASN1_APPLICATION
);
523 printf("ASN1_CONTEXT_SPECIFIC 0x%.016qx\n", (uint64_t)ASN1_CONTEXT_SPECIFIC
);
524 printf("ASN1_PRIVATE 0x%.016qx\n", (uint64_t)ASN1_PRIVATE
);
527 DERSize len
= sizeof(buf
);
529 DERTag tag
= ASN1_CONTEXT_SPECIFIC
| ASN1_TAGNUM_MASK
;
530 drtn
= DEREncodeItem(tag
, 0, 0, buf
, &len
);
533 printf("DEREncodeItem: %u\n", drtn
);
535 DERItem der
= { .data
= buf
, .length
= len
};
537 dumpBytes("tlv", buf
, len
, 0);
539 DERDecodedInfo decoded
;
540 drtn
= DERDecodeItem(&der
, &decoded
);
543 printf("DERDecodeItem: %u\n", drtn
);
545 if (decoded
.tag
!= tag
)
547 printf("DERDecodeItem tag: 0x%qx != 0x%qx\n", (uint64_t)decoded
.tag
, (uint64_t)tag
);
549 printf("DERDecodeItem tag: 0x%qx encoded in %u bytes, decoded length: %u\n",
550 (uint64_t)tag
, len
, decoded
.content
.length
);
553 int main(int argc
, char **argv
)
556 printf( "=> Making sample BB ticket...\n");
557 make_sample_bb_ticket();
558 printf( "=> Making sample AP ticket...\n");
559 make_sample_ap_ticket();
560 printf( "=> Verifying signature in bb_ticket.bin ...\n");
561 verify_ticket_file( "bb_ticket.bin", "G1_GPrK.pem" );