-
Notifications
You must be signed in to change notification settings - Fork 0
/
es10b.c
1516 lines (1262 loc) · 42.8 KB
/
es10b.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "euicc.private.h"
#include "es10b.h"
#include "derutil.h"
#include "hexutil.h"
#include "base64.h"
#include "sha256.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int es10b_prepare_download_r(struct euicc_ctx *ctx, char **b64_PrepareDownloadResponse, struct es10b_prepare_download_param *param, struct es10b_prepare_download_param_user *param_user)
{
int fret = 0;
uint8_t *reqbuf = NULL;
uint32_t reqlen;
uint8_t *respbuf = NULL;
unsigned resplen;
EUICC_SHA256_CTX sha256ctx;
uint8_t hashCC[SHA256_BLOCK_SIZE];
uint8_t *smdpSigned2 = NULL, *smdpSignature2 = NULL, *smdpCertificate = NULL;
int smdpSigned2_len, smdpSignature2_len, smdpCertificate_len;
struct euicc_derutil_node n_request, n_smdpSigned2, n_smdpSignature2, n_smdpCertificate, n_hashCc, n_transactionId, n_ccRequiredFlag;
*b64_PrepareDownloadResponse = NULL;
memset(&n_request, 0, sizeof(n_request));
memset(&n_smdpSigned2, 0, sizeof(n_smdpSigned2));
memset(&n_smdpSignature2, 0, sizeof(n_smdpSignature2));
memset(&n_smdpCertificate, 0, sizeof(n_smdpCertificate));
memset(&n_hashCc, 0, sizeof(n_hashCc));
smdpSigned2 = malloc(euicc_base64_decode_len(param->b64_smdpSigned2));
if (!smdpSigned2)
{
goto err;
}
smdpSignature2 = malloc(euicc_base64_decode_len(param->b64_smdpSignature2));
if (!smdpSignature2)
{
goto err;
}
smdpCertificate = malloc(euicc_base64_decode_len(param->b64_smdpCertificate));
if (!smdpCertificate)
{
goto err;
}
if ((smdpSigned2_len = euicc_base64_decode(smdpSigned2, param->b64_smdpSigned2)) < 0)
{
goto err;
}
if ((smdpSignature2_len = euicc_base64_decode(smdpSignature2, param->b64_smdpSignature2)) < 0)
{
goto err;
}
if ((smdpCertificate_len = euicc_base64_decode(smdpCertificate, param->b64_smdpCertificate)) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_smdpSigned2, 0x30, smdpSigned2, smdpSigned2_len) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_smdpSignature2, 0x5F37, smdpSignature2, smdpSignature2_len) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_smdpCertificate, 0x30, smdpCertificate, smdpCertificate_len) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_transactionId, 0x80, n_smdpSigned2.value, n_smdpSigned2.length) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_ccRequiredFlag, 0x01, n_smdpSigned2.value, n_smdpSigned2.length) < 0)
{
goto err;
}
n_request.tag = 0xBF21;
n_request.pack.child = &n_smdpSigned2;
n_smdpSigned2.pack.next = &n_smdpSignature2;
if (euicc_derutil_convert_bin2long(n_ccRequiredFlag.value, n_ccRequiredFlag.length))
{
if ((!param_user->confirmationCode) || (strlen(param_user->confirmationCode) == 0))
{
goto err;
}
memset(&sha256ctx, 0, sizeof(sha256ctx));
euicc_sha256_init(&sha256ctx);
euicc_sha256_update(&sha256ctx, (const uint8_t *)param_user->confirmationCode, strlen(param_user->confirmationCode));
euicc_sha256_final(&sha256ctx, hashCC);
memset(&sha256ctx, 0, sizeof(sha256ctx));
euicc_sha256_init(&sha256ctx);
euicc_sha256_update(&sha256ctx, hashCC, sizeof(hashCC));
euicc_sha256_update(&sha256ctx, n_transactionId.value, n_transactionId.length);
euicc_sha256_final(&sha256ctx, hashCC);
n_hashCc.tag = 0x04;
n_hashCc.value = hashCC;
n_hashCc.length = sizeof(hashCC);
n_smdpSignature2.pack.next = &n_hashCc;
n_hashCc.pack.next = &n_smdpCertificate;
}
else
{
n_smdpSignature2.pack.next = &n_smdpCertificate;
}
if (euicc_derutil_pack_alloc(&reqbuf, &reqlen, &n_request) < 0)
{
goto err;
}
free(smdpSigned2);
smdpSigned2 = NULL;
free(smdpSignature2);
smdpSignature2 = NULL;
free(smdpCertificate);
smdpCertificate = NULL;
if (es10x_command(ctx, &respbuf, &resplen, reqbuf, reqlen) < 0)
{
goto err;
}
free(reqbuf);
reqbuf = NULL;
*b64_PrepareDownloadResponse = malloc(euicc_base64_encode_len(resplen));
if (!(*b64_PrepareDownloadResponse))
{
goto err;
}
if (euicc_base64_encode(*b64_PrepareDownloadResponse, respbuf, resplen) < 0)
{
goto err;
}
fret = 0;
goto exit;
err:
fret = -1;
free(*b64_PrepareDownloadResponse);
*b64_PrepareDownloadResponse = NULL;
exit:
free(smdpSigned2);
smdpSigned2 = NULL;
free(smdpSignature2);
smdpSignature2 = NULL;
free(smdpCertificate);
smdpCertificate = NULL;
free(reqbuf);
reqbuf = NULL;
free(respbuf);
respbuf = NULL;
return fret;
}
static int es10b_load_bound_profile_package_tx(struct euicc_ctx *ctx, struct es10b_load_bound_profile_package_result *result, const uint8_t *reqbuf, int reqbuf_len)
{
int fret = 0;
uint8_t *respbuf = NULL;
unsigned resplen;
result->bppCommandId = ES10B_BPP_COMMAND_ID_UNDEFINED;
result->errorReason = ES10B_ERROR_REASON_UNDEFINED;
if (es10x_command(ctx, &respbuf, &resplen, reqbuf, reqbuf_len) < 0)
{
goto err;
}
if (resplen > 0)
{
struct euicc_derutil_node tmpnode, n_finalResult;
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xBF37, respbuf, resplen) < 0) // ProfileInstallationResult
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xBF27, tmpnode.value, tmpnode.length) < 0) // ProfileInstallationResultData
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xA2, tmpnode.value, tmpnode.length) < 0) // finalResult
{
goto err;
}
if (euicc_derutil_unpack_first(&n_finalResult, tmpnode.value, tmpnode.length) < 0)
{
goto err;
}
switch (n_finalResult.tag)
{
case 0xA0: // SuccessResult
break;
case 0xA1: // ErrorResult
tmpnode.self.ptr = n_finalResult.value;
tmpnode.self.length = 0;
while (euicc_derutil_unpack_next(&tmpnode, &tmpnode, n_finalResult.value, n_finalResult.length) == 0)
{
int tmpint;
switch (tmpnode.tag)
{
case 0x80:
tmpint = euicc_derutil_convert_bin2long(tmpnode.value, tmpnode.length);
switch (tmpint)
{
case ES10B_BPP_COMMAND_ID_INITIALISE_SECURE_CHANNEL:
case ES10B_BPP_COMMAND_ID_CONFIGURE_ISDP:
case ES10B_BPP_COMMAND_ID_STORE_METADATA:
case ES10B_BPP_COMMAND_ID_STORE_METADATA2:
case ES10B_BPP_COMMAND_ID_REPLACE_SESSION_KEYS:
case ES10B_BPP_COMMAND_ID_LOAD_PROFILE_ELEMENTS:
result->bppCommandId = tmpint;
break;
default:
result->bppCommandId = ES10B_BPP_COMMAND_ID_UNDEFINED;
break;
}
break;
case 0x81:
tmpint = euicc_derutil_convert_bin2long(tmpnode.value, tmpnode.length);
switch (tmpint)
{
case ES10B_ERROR_REASON_INCORRECT_INPUT_VALUES:
case ES10B_ERROR_REASON_INVALID_SIGNATURE:
case ES10B_ERROR_REASON_INVALID_TRANSACTION_ID:
case ES10B_ERROR_REASON_UNSUPPORTED_CRT_VALUES:
case ES10B_ERROR_REASON_UNSUPPORTED_REMOTE_OPERATION_TYPE:
case ES10B_ERROR_REASON_UNSUPPORTED_PROFILE_CLASS:
case ES10B_ERROR_REASON_SCP03T_STRUCTURE_ERROR:
case ES10B_ERROR_REASON_SCP03T_SECURITY_ERROR:
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_ICCID_ALREADY_EXISTS_ON_EUICC:
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_INSUFFICIENT_MEMORY_FOR_PROFILE:
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_INTERRUPTION:
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_PE_PROCESSING_ERROR:
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_ICCID_MISMATCH:
case ES10B_ERROR_REASON_TEST_PROFILE_INSTALL_FAILED_DUE_TO_INVALID_NAA_KEY:
case ES10B_ERROR_REASON_PPR_NOT_ALLOWED:
case ES10B_ERROR_REASON_INSTALL_FAILED_DUE_TO_UNKNOWN_ERROR:
result->errorReason = tmpint;
break;
default:
result->errorReason = ES10B_ERROR_REASON_UNDEFINED;
break;
}
break;
}
}
goto err;
default:
goto err;
}
}
fret = 0;
goto exit;
err:
fret = -1;
exit:
free(respbuf);
respbuf = NULL;
return fret;
}
int es10b_load_bound_profile_package_r(struct euicc_ctx *ctx, struct es10b_load_bound_profile_package_result *result, const char *b64_BoundProfilePackage)
{
int fret = 0;
uint8_t *bpp = NULL;
int bpp_len;
const uint8_t *reqbuf;
int reqbuf_len;
struct euicc_derutil_node tmpnode, tmpchildnode, n_BoundProfilePackage;
bpp = malloc(euicc_base64_decode_len(b64_BoundProfilePackage));
if (!bpp)
{
goto err;
}
if ((bpp_len = euicc_base64_decode(bpp, b64_BoundProfilePackage)) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_BoundProfilePackage, 0xBF36, bpp, bpp_len) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xBF23, n_BoundProfilePackage.value, n_BoundProfilePackage.length) < 0)
{
goto err;
}
reqbuf = n_BoundProfilePackage.self.ptr;
reqbuf_len = tmpnode.self.ptr - n_BoundProfilePackage.self.ptr + tmpnode.self.length;
if (es10b_load_bound_profile_package_tx(ctx, result, reqbuf, reqbuf_len) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xA0, n_BoundProfilePackage.value, n_BoundProfilePackage.length) < 0)
{
goto err;
}
reqbuf = tmpnode.self.ptr;
reqbuf_len = tmpnode.self.length;
if (es10b_load_bound_profile_package_tx(ctx, result, reqbuf, reqbuf_len) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xA1, n_BoundProfilePackage.value, n_BoundProfilePackage.length) < 0)
{
goto err;
}
reqbuf = tmpnode.self.ptr;
reqbuf_len = tmpnode.value - tmpnode.self.ptr;
if (es10b_load_bound_profile_package_tx(ctx, result, reqbuf, reqbuf_len) < 0)
{
goto err;
}
tmpchildnode.self.ptr = tmpnode.value;
tmpchildnode.self.length = 0;
while (euicc_derutil_unpack_next(&tmpchildnode, &tmpchildnode, tmpnode.value, tmpnode.length) == 0)
{
reqbuf = tmpchildnode.self.ptr;
reqbuf_len = tmpchildnode.self.length;
if (es10b_load_bound_profile_package_tx(ctx, result, reqbuf, reqbuf_len) < 0)
{
goto err;
}
}
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xA2, n_BoundProfilePackage.value, n_BoundProfilePackage.length) == 0)
{
reqbuf = tmpnode.self.ptr;
reqbuf_len = tmpnode.self.length;
if (es10b_load_bound_profile_package_tx(ctx, result, reqbuf, reqbuf_len) < 0)
{
goto err;
}
}
if (euicc_derutil_unpack_find_tag(&tmpnode, 0xA3, n_BoundProfilePackage.value, n_BoundProfilePackage.length) < 0)
{
goto err;
}
reqbuf = tmpnode.self.ptr;
reqbuf_len = tmpnode.value - tmpnode.self.ptr;
if (es10b_load_bound_profile_package_tx(ctx, result, reqbuf, reqbuf_len) < 0)
{
goto err;
}
tmpchildnode.self.ptr = tmpnode.value;
tmpchildnode.self.length = 0;
while (euicc_derutil_unpack_next(&tmpchildnode, &tmpchildnode, tmpnode.value, tmpnode.length) == 0)
{
reqbuf = tmpchildnode.self.ptr;
reqbuf_len = tmpchildnode.self.length;
if (es10b_load_bound_profile_package_tx(ctx, result, reqbuf, reqbuf_len) < 0)
{
goto err;
}
}
goto exit;
err:
fret = -1;
exit:
free(bpp);
bpp = NULL;
return fret;
}
int es10b_get_euicc_challenge_r(struct euicc_ctx *ctx, char **b64_euiccChallenge)
{
int fret = 0;
struct euicc_derutil_node n_request = {
.tag = 0xBF2E, // GetEuiccDataRequest
};
uint32_t reqlen;
uint8_t *respbuf = NULL;
unsigned resplen;
struct euicc_derutil_node tmpnode;
reqlen = sizeof(ctx->apdu._internal.request_buffer.body);
if (euicc_derutil_pack(ctx->apdu._internal.request_buffer.body, &reqlen, &n_request))
{
goto err;
}
if (es10x_command(ctx, &respbuf, &resplen, ctx->apdu._internal.request_buffer.body, reqlen) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&tmpnode, n_request.tag, respbuf, resplen))
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&tmpnode, 0x80, tmpnode.value, tmpnode.length))
{
goto err;
}
*b64_euiccChallenge = malloc(euicc_base64_encode_len(tmpnode.length));
if (!(*b64_euiccChallenge))
{
goto err;
}
if (euicc_base64_encode(*b64_euiccChallenge, tmpnode.value, tmpnode.length) < 0)
{
goto err;
}
goto exit;
err:
fret = -1;
free(*b64_euiccChallenge);
*b64_euiccChallenge = NULL;
exit:
free(respbuf);
respbuf = NULL;
return fret;
}
int es10b_get_euicc_info_r(struct euicc_ctx *ctx, char **b64_EUICCInfo1)
{
int fret = 0;
struct euicc_derutil_node n_request = {
.tag = 0xBF20, // GetEuiccInfo1Request
};
uint32_t reqlen;
uint8_t *respbuf = NULL;
unsigned resplen;
struct euicc_derutil_node tmpnode;
reqlen = sizeof(ctx->apdu._internal.request_buffer.body);
if (euicc_derutil_pack(ctx->apdu._internal.request_buffer.body, &reqlen, &n_request))
{
goto err;
}
if (es10x_command(ctx, &respbuf, &resplen, ctx->apdu._internal.request_buffer.body, reqlen) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&tmpnode, n_request.tag, respbuf, resplen))
{
goto err;
}
*b64_EUICCInfo1 = malloc(euicc_base64_encode_len(tmpnode.self.length));
if (!(*b64_EUICCInfo1))
{
goto err;
}
if (euicc_base64_encode(*b64_EUICCInfo1, tmpnode.self.ptr, tmpnode.self.length) < 0)
{
goto err;
}
goto exit;
err:
fret = -1;
free(*b64_EUICCInfo1);
*b64_EUICCInfo1 = NULL;
exit:
free(respbuf);
respbuf = NULL;
return fret;
}
int es10b_authenticate_server_r(struct euicc_ctx *ctx, uint8_t **transaction_id, uint32_t *transaction_id_len, char **b64_AuthenticateServerResponse, struct es10b_authenticate_server_param *param, struct es10b_authenticate_server_param_user *param_user)
{
int fret = 0;
uint8_t *reqbuf = NULL;
uint32_t reqlen;
uint8_t *respbuf = NULL;
unsigned resplen;
uint8_t imei[8];
uint8_t *serverSigned1 = NULL, *serverSignature1 = NULL, *euiccCiPKIdToBeUsed = NULL, *serverCertificate = NULL;
int serverSigned1_len, serverSignature1_len, euiccCiPKIdToBeUsed_len, serverCertificate_len;
struct euicc_derutil_node n_request, n_serverSigned1, n_transactionId, n_serverSignature1, n_euiccCiPKIdToBeUsed, n_serverCertificate, n_CtxParams1, n_matchingId, n_deviceInfo, n_tac, n_deviceCapabilities, n_imei;
*transaction_id = NULL;
*transaction_id_len = 0;
*b64_AuthenticateServerResponse = NULL;
memset(&n_request, 0, sizeof(n_request));
memset(&n_serverSigned1, 0, sizeof(n_serverSigned1));
memset(&n_serverSignature1, 0, sizeof(n_serverSignature1));
memset(&n_euiccCiPKIdToBeUsed, 0, sizeof(n_euiccCiPKIdToBeUsed));
memset(&n_serverCertificate, 0, sizeof(n_serverCertificate));
memset(&n_CtxParams1, 0, sizeof(n_CtxParams1));
memset(&n_matchingId, 0, sizeof(n_matchingId));
memset(&n_deviceInfo, 0, sizeof(n_deviceInfo));
memset(&n_tac, 0, sizeof(n_tac));
memset(&n_deviceCapabilities, 0, sizeof(n_deviceCapabilities));
memset(&n_imei, 0, sizeof(n_imei));
serverSigned1 = malloc(euicc_base64_decode_len(param->b64_serverSigned1));
if (!serverSigned1)
{
goto err;
}
serverSignature1 = malloc(euicc_base64_decode_len(param->b64_serverSignature1));
if (!serverSignature1)
{
goto err;
}
euiccCiPKIdToBeUsed = malloc(euicc_base64_decode_len(param->b64_euiccCiPKIdToBeUsed));
if (!euiccCiPKIdToBeUsed)
{
goto err;
}
serverCertificate = malloc(euicc_base64_decode_len(param->b64_serverCertificate));
if (!serverCertificate)
{
goto err;
}
if ((serverSigned1_len = euicc_base64_decode(serverSigned1, param->b64_serverSigned1)) < 0)
{
goto err;
}
if ((serverSignature1_len = euicc_base64_decode(serverSignature1, param->b64_serverSignature1)) < 0)
{
goto err;
}
if ((euiccCiPKIdToBeUsed_len = euicc_base64_decode(euiccCiPKIdToBeUsed, param->b64_euiccCiPKIdToBeUsed)) < 0)
{
goto err;
}
if ((serverCertificate_len = euicc_base64_decode(serverCertificate, param->b64_serverCertificate)) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_serverSigned1, 0x30, serverSigned1, serverSigned1_len) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_transactionId, 0x80, n_serverSigned1.value, n_serverSigned1.length) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_serverSignature1, 0x5F37, serverSignature1, serverSignature1_len) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_euiccCiPKIdToBeUsed, 0x04, euiccCiPKIdToBeUsed, euiccCiPKIdToBeUsed_len) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&n_serverCertificate, 0x30, serverCertificate, serverCertificate_len) < 0)
{
goto err;
}
*transaction_id_len = n_transactionId.length;
*transaction_id = malloc(n_transactionId.length);
if (!(*transaction_id))
{
goto err;
}
memcpy(*transaction_id, n_transactionId.value, n_transactionId.length);
n_request.tag = 0xBF38;
n_request.pack.child = &n_serverSigned1;
n_serverSigned1.pack.next = &n_serverSignature1;
n_serverSignature1.pack.next = &n_euiccCiPKIdToBeUsed;
n_euiccCiPKIdToBeUsed.pack.next = &n_serverCertificate;
n_serverCertificate.pack.next = &n_CtxParams1;
n_CtxParams1.tag = 0xA0;
n_deviceInfo.tag = 0xA1;
n_deviceInfo.pack.child = &n_tac;
n_tac.tag = 0x80;
n_tac.value = imei;
n_tac.length = 4;
n_tac.pack.next = &n_deviceCapabilities;
n_deviceCapabilities.tag = 0xA1;
if (param_user->imei)
{
int imei_len;
imei_len = euicc_hexutil_gsmbcd2bin(imei, sizeof(imei), param_user->imei, 0);
if (imei_len < 0)
{
goto err;
}
n_deviceCapabilities.pack.next = &n_imei;
n_imei.tag = 0x82;
n_imei.value = imei;
n_imei.length = imei_len;
}
else
{
memcpy(imei, (uint8_t[]){0x35, 0x29, 0x06, 0x11}, 4);
}
if (param_user->matchingId)
{
n_CtxParams1.pack.child = &n_matchingId;
n_matchingId.tag = 0x80;
n_matchingId.value = (const uint8_t *)param_user->matchingId;
n_matchingId.length = strlen(param_user->matchingId);
n_matchingId.pack.next = &n_deviceInfo;
}
else
{
n_CtxParams1.pack.child = &n_deviceInfo;
}
if (euicc_derutil_pack_alloc(&reqbuf, &reqlen, &n_request) < 0)
{
goto err;
}
free(serverSigned1);
serverSigned1 = NULL;
free(serverSignature1);
serverSignature1 = NULL;
free(euiccCiPKIdToBeUsed);
euiccCiPKIdToBeUsed = NULL;
free(serverCertificate);
serverCertificate = NULL;
if (es10x_command(ctx, &respbuf, &resplen, reqbuf, reqlen) < 0)
{
goto err;
}
free(reqbuf);
reqbuf = NULL;
*b64_AuthenticateServerResponse = malloc(euicc_base64_encode_len(resplen));
if (!(*b64_AuthenticateServerResponse))
{
goto err;
}
if (euicc_base64_encode(*b64_AuthenticateServerResponse, respbuf, resplen) < 0)
{
goto err;
}
fret = 0;
goto exit;
err:
fret = -1;
free(*transaction_id);
*transaction_id = NULL;
*transaction_id_len = 0;
free(*b64_AuthenticateServerResponse);
*b64_AuthenticateServerResponse = NULL;
exit:
free(serverSigned1);
serverSigned1 = NULL;
free(serverSignature1);
serverSignature1 = NULL;
free(euiccCiPKIdToBeUsed);
euiccCiPKIdToBeUsed = NULL;
free(serverCertificate);
serverCertificate = NULL;
free(reqbuf);
reqbuf = NULL;
free(respbuf);
respbuf = NULL;
return fret;
}
int es10b_cancel_session_r(struct euicc_ctx *ctx, char **b64_CancelSessionResponse, struct es10b_cancel_session_param *param)
{
int fret = 0;
struct euicc_derutil_node n_request, n_transactionId, n_reason;
uint8_t reason_buf[sizeof(enum es10b_cancel_session_reason)];
uint32_t reason_buf_len = sizeof(reason_buf);
uint32_t reqlen;
uint8_t *respbuf = NULL;
unsigned resplen;
struct euicc_derutil_node tmpnode;
if (euicc_derutil_convert_long2bin(reason_buf, &reason_buf_len, param->reason) < 0)
{
goto err;
}
memset(&n_request, 0, sizeof(n_request));
memset(&n_transactionId, 0, sizeof(n_transactionId));
memset(&n_reason, 0, sizeof(n_reason));
n_request.tag = 0xBF41; // CancelSessionRequest
n_request.pack.child = &n_transactionId;
n_transactionId.tag = 0x80;
n_transactionId.value = (const uint8_t *)param->transactionId;
n_transactionId.length = param->transactionIdLen;
n_transactionId.pack.next = &n_reason;
n_reason.tag = 0x81;
n_reason.value = reason_buf;
n_reason.length = reason_buf_len;
reqlen = sizeof(ctx->apdu._internal.request_buffer.body);
if (euicc_derutil_pack(ctx->apdu._internal.request_buffer.body, &reqlen, &n_request))
{
goto err;
}
if (es10x_command(ctx, &respbuf, &resplen, ctx->apdu._internal.request_buffer.body, reqlen) < 0)
{
goto err;
}
if (euicc_derutil_unpack_find_tag(&tmpnode, n_request.tag, respbuf, resplen))
{
goto err;
}
*b64_CancelSessionResponse = malloc(euicc_base64_encode_len(tmpnode.self.length));
if (!(*b64_CancelSessionResponse))
{
goto err;
}
if (euicc_base64_encode(*b64_CancelSessionResponse, tmpnode.self.ptr, tmpnode.self.length) < 0)
{
goto err;
}
goto exit;
err:
fret = -1;
free(*b64_CancelSessionResponse);
*b64_CancelSessionResponse = NULL;
exit:
free(respbuf);
respbuf = NULL;
return fret;
}
void es10b_prepare_download_param_free(struct es10b_prepare_download_param *param)
{
if (!param)
{
return;
}
free(param->b64_profileMetadata);
free(param->b64_smdpCertificate);
free(param->b64_smdpSignature2);
free(param->b64_smdpSigned2);
memset(param, 0x00, sizeof(*param));
}
void es10b_authenticate_server_param_free(struct es10b_authenticate_server_param *param)
{
if (!param)
{
return;
}
free(param->b64_euiccCiPKIdToBeUsed);
free(param->b64_serverCertificate);
free(param->b64_serverSignature1);
free(param->b64_serverSigned1);
memset(param, 0x00, sizeof(*param));
}
int es10b_prepare_download(struct euicc_ctx *ctx, const char *confirmationCode)
{
int fret;
struct es10b_prepare_download_param_user param_user = {
.confirmationCode = confirmationCode,
};
if (ctx->http._internal.b64_prepare_download_response)
{
return -1;
}
if (ctx->http._internal.prepare_download_param == NULL)
{
return -1;
}
fret = es10b_prepare_download_r(ctx, &ctx->http._internal.b64_prepare_download_response, ctx->http._internal.prepare_download_param, ¶m_user);
if (fret < 0)
{
ctx->http._internal.b64_prepare_download_response = NULL;
return fret;
}
es10b_prepare_download_param_free(ctx->http._internal.prepare_download_param);
free(ctx->http._internal.prepare_download_param);
ctx->http._internal.prepare_download_param = NULL;
return fret;
}
int es10b_load_bound_profile_package(struct euicc_ctx *ctx, struct es10b_load_bound_profile_package_result *result)
{
int fret;
if (ctx->http._internal.b64_bound_profile_package == NULL)
{
return -1;
}
fret = es10b_load_bound_profile_package_r(ctx, result, ctx->http._internal.b64_bound_profile_package);
if (fret < 0)
{
return fret;
}
free(ctx->http._internal.b64_bound_profile_package);
ctx->http._internal.b64_bound_profile_package = NULL;
return fret;
}
int es10b_get_euicc_challenge_and_info(struct euicc_ctx *ctx)
{
int fret;
if (ctx->http._internal.b64_euicc_challenge)
{
return -1;
}
if (ctx->http._internal.b64_euicc_info_1)
{
return -1;
}
fret = es10b_get_euicc_challenge_r(ctx, &ctx->http._internal.b64_euicc_challenge);
if (fret < 0)
{
goto err;
}
fret = es10b_get_euicc_info_r(ctx, &ctx->http._internal.b64_euicc_info_1);
if (fret < 0)
{
goto err;
}
return fret;
err:
free(ctx->http._internal.b64_euicc_challenge);
ctx->http._internal.b64_euicc_challenge = NULL;
free(ctx->http._internal.b64_euicc_info_1);
ctx->http._internal.b64_euicc_info_1 = NULL;
return -1;
}
int es10b_authenticate_server(struct euicc_ctx *ctx, const char *matchingId, const char *imei)
{
int fret;
struct es10b_authenticate_server_param_user param_user = {
.matchingId = matchingId,
.imei = imei,
};
if (ctx->http._internal.b64_authenticate_server_response)
{
return -1;
}
if (ctx->http._internal.authenticate_server_param == NULL)
{
return -1;
}
fret = es10b_authenticate_server_r(ctx, &ctx->http._internal.transaction_id_bin, &ctx->http._internal.transaction_id_bin_len, &ctx->http._internal.b64_authenticate_server_response, ctx->http._internal.authenticate_server_param, ¶m_user);
if (fret < 0)
{
ctx->http._internal.b64_authenticate_server_response = NULL;
return fret;
}
es10b_authenticate_server_param_free(ctx->http._internal.authenticate_server_param);
free(ctx->http._internal.authenticate_server_param);
ctx->http._internal.authenticate_server_param = NULL;
return fret;
}
int es10b_cancel_session(struct euicc_ctx *ctx, enum es10b_cancel_session_reason reason)
{
int fret;
struct es10b_cancel_session_param param = {
.transactionId = ctx->http._internal.transaction_id_bin,
.transactionIdLen = ctx->http._internal.transaction_id_bin_len,
.reason = reason,
};
if (ctx->http._internal.transaction_id_bin == NULL)
{
return -1;
}
if (ctx->http._internal.transaction_id_bin_len == 0)
{
return -1;
}
if (ctx->http._internal.b64_cancel_session_response)
{
return -1;
}
fret = es10b_cancel_session_r(ctx, &ctx->http._internal.b64_cancel_session_response, ¶m);
if (fret < 0)
{
ctx->http._internal.b64_cancel_session_response = NULL;
}
return fret;
}