/
decrypt_cenc_isma.c
2542 lines (2255 loc) · 73.8 KB
/
decrypt_cenc_isma.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
/*
* GPAC - Multimedia Framework C SDK
*
* Authors: Jean Le Feuvre
* Copyright (c) Telecom ParisTech 2018-2023
* All rights reserved
*
* This file is part of GPAC / CENC and ISMA decrypt filter
*
* GPAC is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GPAC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <gpac/filters.h>
#include <gpac/constants.h>
#include <gpac/crypt_tools.h>
#include <gpac/crypt.h>
#include <gpac/base_coding.h>
#include <gpac/download.h>
#include <gpac/network.h>
#include <gpac/internal/media_dev.h>
#if !defined(GPAC_DISABLE_CRYPTO) && !defined(GPAC_DISABLE_CDCRYPT)
//#define OLD_KEY_FETCHERS
//old key fetchers require sync download
#if defined(GPAC_CONFIG_EMSCRIPTEN) && defined(OLD_KEY_FETCHERS)
#undef OLD_KEY_FETCHERS
#endif
enum
{
DECRYPT_STATE_ERROR=1,
DECRYPT_STATE_SETUP,
DECRYPT_STATE_PLAY,
};
enum
{
DECRYPT_FULL=0,
DECRYPT_NOKEY,
DECRYPT_SKIP,
DECRYPT_PAD0,
DECRYPT_PAD1,
DECRYPT_PADSC,
};
typedef struct
{
GF_Crypt *crypt;
bin128 key;
u32 key_valid;
} CENCDecKey;
typedef struct
{
const char *cfile;
u32 decrypt;
GF_PropUIntList drop_keys;
GF_PropStringList kids;
GF_PropStringList keys;
GF_CryptInfo *cinfo;
Bool hls_cenc_patch_iv;
GF_Filter *filter;
GF_List *streams;
GF_BitStream *bs_r;
GF_DownloadManager *dm;
u32 pending_keys;
} GF_CENCDecCtx;
typedef struct
{
GF_CENCDecCtx *ctx;
GF_FilterPid *ipid, *opid;
u32 state;
u32 pssh_crc;
u32 scheme_type, scheme_version;
GF_Err key_error;
CENCDecKey *crypts;
u32 nb_crypts;
//ISMA & OMA
char salt[8];
u64 last_IV;
u32 nb_allow_play;
Bool is_oma, is_adobe;
u32 preview_range;
Bool is_nalu;
Bool selective_encryption;
u32 IV_length, KI_length;
/*CENC*/
Bool is_cenc;
Bool is_cbc;
u32 KID_count;
Bool inband_keys;
bin128 *KIDs;
bin128 *keys;
const GF_PropertyValue *cenc_ki;
u32 multikey;
const GF_PropertyValue *cenc_pattern;
/*adobe and CENC*/
Bool crypt_init;
Bool is_hls;
bin128 hls_IV;
char *hls_key_url;
Bool is_hls_saes;
u32 codec_id;
Bool force_hls_iv;
Bool gpac_master_leaf;
bin128 master_key;
u32 clearkey_crc;
char *body;
u32 res_size;
Bool hdr_done;
GF_DownloadSession *sess;
u32 rep_crc, per_crc, as_id;
} GF_CENCDecStream;
#ifdef OLD_KEY_FETCHERS
static void cenc_dec_kms_netio(void *cbck, GF_NETIO_Parameter *par)
{
}
static GF_Err gf_ismacryp_gpac_get_info(u32 stream_id, char *drm_file, char *key, char *salt)
{
GF_Err e;
u32 i, count;
GF_CryptInfo *info;
e = GF_OK;
info = gf_crypt_info_load(drm_file, &e);
if (!info) return e;
count = gf_list_count(info->tcis);
for (i=0; i<count; i++) {
GF_TrackCryptInfo *tci = (GF_TrackCryptInfo *) gf_list_get(info->tcis, i);
if ((info->has_common_key && !tci->trackID) || (tci->trackID == stream_id) ) {
if (tci->KID_count)
memcpy(key, tci->keys[0], sizeof(char)*16);
memcpy(salt, tci->first_IV, sizeof(char)*8);
e = GF_OK;
break;
}
}
gf_crypt_info_del(info);
return e;
}
static GF_Err cenc_dec_get_gpac_kms(GF_CENCDecCtx *ctx, GF_CENCDecStream *cstr, const char *kms_url)
{
const GF_PropertyValue *prop;
FILE *t;
u32 id = 0;
GF_FilterPid *pid = cstr->ipid;
GF_Err e;
GF_DownloadSession * sess;
prop = gf_filter_pid_get_property(pid, GF_PROP_PID_ID);
if (!prop) prop = gf_filter_pid_get_property(pid, GF_PROP_PID_ESID);
if (prop) id = prop->value.uint;
if (!strnicmp(kms_url, "(ipmp)", 6)) return GF_NOT_SUPPORTED;
else if (!strnicmp(kms_url, "(uri)", 5)) kms_url += 5;
else if (!strnicmp(kms_url, "file://", 7)) kms_url += 7;
/*try local*/
t = (strstr(kms_url, "://") == NULL) ? gf_fopen(kms_url, "rb") : NULL;
if (t) {
gf_fclose(t);
return gf_ismacryp_gpac_get_info(id, (char *)kms_url, cstr->key, cstr->salt);
}
/*note that gpac doesn't have TLS support -> not really useful. As a general remark, ISMACryp
is supported as a proof of concept, crypto and IPMP being the last priority on gpac...*/
GF_LOG(GF_LOG_INFO, GF_LOG_MEDIA, ("[CENC/ISMA] Fetching ISMACryp key for channel %d\n", id) );
sess = gf_dm_sess_new(ctx->dm, kms_url, GF_NETIO_SESSION_NOT_THREADED, cenc_dec_kms_netio, ctx, NULL);
if (!sess) return GF_IO_ERR;
while (1) {
GF_NetIOStatus status;
e = gf_dm_sess_process(sess);
if (e) break;
gf_dm_sess_get_stats(sess, NULL, NULL, NULL, NULL, NULL, &status);
if (status>=GF_NETIO_DATA_TRANSFERED) break;
}
if (e >= GF_EOS) {
e = gf_ismacryp_gpac_get_info(id, (char *) gf_dm_sess_get_cache_name(sess), cstr->key, cstr->salt);
}
gf_dm_sess_del(sess);
return e;
}
static Bool gf_ismacryp_mpeg4ip_get_info(char *kms_uri, char *key, char *salt)
{
char szPath[1024], catKey[24], line[101];
u32 i, x;
Bool got_it;
FILE *kms;
strcpy(szPath, getenv("HOME"));
strcat(szPath , "/.kms_data");
got_it = 0;
kms = gf_fopen(szPath, "rt");
while (kms && !gf_feof(kms)) {
if (!gf_fgets(szPath, 1024, kms)) break;
szPath[strlen(szPath) - 1] = 0;
if (stricmp(szPath, kms_uri)) continue;
gf_fgets(line, 1, 100, kms);
line[100] = 0;
for (i=0; i<24; i++) {
char szV[3];
szV[0] = line[2*i];
szV[1] = line[2*i + 1];
szV[2] = 0;
if (!sscanf(szV, "%x", &x)) break;
catKey[i] = x;
}
if (i==24) got_it = 1;
break;
}
if (kms) gf_fclose(kms);
if (got_it) {
/*watchout, MPEG4IP stores SALT|KEY, NOT KEY|SALT*/
memcpy(key, catKey+8, sizeof(char)*16);
memcpy(salt, catKey, sizeof(char)*8);
return 1;
}
return 0;
}
#endif
static GF_Err cenc_dec_setup_isma(GF_CENCDecCtx *ctx, GF_CENCDecStream *cstr, u32 scheme_type, u32 scheme_version, const char *scheme_uri, const char *kms_uri)
{
u32 kms_crc=0;
const GF_PropertyValue *p;
cstr->state = DECRYPT_STATE_ERROR;
if (scheme_type != GF_ISOM_ISMACRYP_SCHEME) return GF_NOT_SUPPORTED;
if (scheme_version != 1) return GF_NOT_SUPPORTED;
if (!kms_uri) return GF_NON_COMPLIANT_BITSTREAM;
kms_crc = gf_crc_32(kms_uri, (u32) strlen(kms_uri));
if (cstr->pssh_crc == kms_crc) return GF_OK;
cstr->pssh_crc = kms_crc;
/*try to fetch the keys*/
/*base64 inband encoding*/
if (!strnicmp(kms_uri, "(key)", 5)) {
char data[100];
gf_base64_decode((char*) kms_uri+5, (u32)strlen(kms_uri)-5, data, 100);
memcpy(cstr->crypts[0].key, data, sizeof(char)*16);
memcpy(cstr->salt, data+16, sizeof(char)*8);
}
/*hexadecimal inband encoding*/
else if (!strnicmp(kms_uri, "(key-hexa)", 10)) {
u32 v;
char szT[3], *k;
u32 i;
szT[2] = 0;
if (strlen(kms_uri) < 10+32+16) return GF_NON_COMPLIANT_BITSTREAM;
k = (char *)kms_uri + 10;
for (i=0; i<16; i++) {
szT[0] = k[2*i];
szT[1] = k[2*i + 1];
sscanf(szT, "%X", &v);
cstr->crypts[0].key[i] = v;
}
k = (char *)kms_uri + 10 + 32;
for (i=0; i<8; i++) {
szT[0] = k[2*i];
szT[1] = k[2*i + 1];
sscanf(szT, "%X", &v);
cstr->salt[i] = v;
}
}
/*MPEG4-IP KMS*/
else if (!stricmp(kms_uri, "AudioKey") || !stricmp(kms_uri, "VideoKey")) {
#ifdef OLD_KEY_FETCHERS
if (!gf_ismacryp_mpeg4ip_get_info((char *) kms_uri, cstr->key, cstr->salt)) {
return GF_BAD_PARAM;
}
#else
return GF_NOT_SUPPORTED;
#endif
}
/*gpac default scheme is used, fetch file from KMS and load keys*/
else if (scheme_uri && !stricmp(scheme_uri, "urn:gpac:isma:encryption_scheme")) {
#ifdef OLD_KEY_FETCHERS
e = cenc_dec_get_gpac_kms(ctx, cstr, kms_uri);
if (e) return e;
#else
return GF_NOT_SUPPORTED;
#endif
}
/*hardcoded keys*/
else {
static u8 mysalt[] = { 8,7,6,5,4,3,2,1, 0,0,0,0,0,0,0,0 };
static u8 mykey[][16] = {
{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
}
};
memcpy(cstr->salt, mysalt, sizeof(char)*8);
memcpy(cstr->crypts[0].key, mykey, sizeof(char)*16);
}
cstr->state = DECRYPT_STATE_SETUP;
p = gf_filter_pid_get_property(cstr->ipid, GF_PROP_PID_ISMA_SELECTIVE_ENC);
if (p) cstr->selective_encryption = p->value.boolean;
p = gf_filter_pid_get_property(cstr->ipid, GF_PROP_PID_ISMA_IV_LENGTH);
if (p) cstr->IV_length = p->value.uint;
p = gf_filter_pid_get_property(cstr->ipid, GF_PROP_PID_ISMA_KI_LENGTH);
if (p) cstr->KI_length = p->value.uint;
//ctx->nb_allow_play = 1;
return GF_OK;
}
static GF_Err cenc_dec_access_isma(GF_CENCDecCtx *ctx, GF_CENCDecStream *cstr, Bool is_play)
{
GF_Err e;
if (is_play) {
char IV[16];
if (cstr->state != DECRYPT_STATE_SETUP)
return GF_SERVICE_ERROR;
gf_assert(!cstr->crypts[0].crypt);
//if (!ctx->nb_allow_play) return GF_AUTHENTICATION_FAILURE;
//ctx->nb_allow_play--;
/*init decrypter*/
cstr->crypts[0].crypt = gf_crypt_open(GF_AES_128, GF_CTR);
if (!cstr->crypts[0].crypt) return GF_IO_ERR;
memset(IV, 0, sizeof(char)*16);
memcpy(IV, cstr->salt, sizeof(char)*8);
e = gf_crypt_init(cstr->crypts[0].crypt, cstr->crypts[0].key, IV);
if (e) return e;
cstr->state = DECRYPT_STATE_PLAY;
return GF_OK;
} else {
if (cstr->state != DECRYPT_STATE_PLAY)
return GF_SERVICE_ERROR;
if (cstr->crypts[0].crypt) gf_crypt_close(cstr->crypts[0].crypt);
cstr->crypts[0].crypt = NULL;
cstr->state = DECRYPT_STATE_SETUP;
return GF_OK;
}
return GF_BAD_PARAM;
}
static GF_Err cenc_dec_process_isma(GF_CENCDecCtx *ctx, GF_CENCDecStream *cstr, GF_FilterPacket *in_pck)
{
u32 data_size;
const char *in_data;
u8 *out_data;
u64 isma_BSO = 0;
u32 offset=0;
GF_FilterPacket *out_pck;
Bool is_encrypted=GF_FALSE;
if (!cstr->crypts[0].crypt)
return GF_SERVICE_ERROR;
if (! gf_filter_pck_get_crypt_flags(in_pck)) {
out_pck = gf_filter_pck_new_ref(cstr->opid, 0, 0, in_pck);
if (!out_pck) return GF_OUT_OF_MEM;
gf_filter_pck_merge_properties(in_pck, out_pck);
gf_filter_pck_set_crypt_flags(out_pck, 0);
gf_filter_pck_send(out_pck);
return GF_OK;
}
in_data = gf_filter_pck_get_data(in_pck, &data_size);
gf_bs_reassign_buffer(ctx->bs_r, in_data, data_size);
if (cstr->selective_encryption) {
if (gf_bs_read_int(ctx->bs_r, 1)) is_encrypted=GF_TRUE;
gf_bs_read_int(ctx->bs_r, 7);
offset = 1;
} else {
is_encrypted=GF_TRUE;
}
if (is_encrypted) {
if (cstr->IV_length != 0) {
isma_BSO = gf_bs_read_long_int(ctx->bs_r, 8*cstr->IV_length);
offset += cstr->IV_length;
}
if (cstr->KI_length) {
offset += cstr->KI_length;
}
}
/*resync IV*/
if (!cstr->last_IV || (cstr->last_IV != isma_BSO)) {
char IV[17];
u64 count;
u32 remain;
GF_BitStream *bs;
count = isma_BSO / 16;
remain = (u32) (isma_BSO % 16);
/*format IV to begin of counter*/
bs = gf_bs_new(IV, 17, GF_BITSTREAM_WRITE);
gf_bs_write_u8(bs, 0); /*begin of counter*/
gf_bs_write_data(bs, cstr->salt, 8);
gf_bs_write_u64(bs, (s64) count);
gf_bs_del(bs);
gf_crypt_set_IV(cstr->crypts[0].crypt, IV, 17);
/*decrypt remain bytes*/
if (remain) {
char dummy[20];
gf_crypt_decrypt(cstr->crypts[0].crypt, dummy, remain);
}
cstr->last_IV = isma_BSO;
}
in_data += offset;
data_size -= offset;
out_pck = gf_filter_pck_new_alloc(cstr->opid, data_size, &out_data);
if (!out_pck) return GF_OUT_OF_MEM;
memcpy(out_data, in_data, data_size);
/*decrypt*/
gf_crypt_decrypt(cstr->crypts[0].crypt, out_data, data_size);
cstr->last_IV += data_size;
/*replace AVC start codes (0x00000001) by nalu size*/
if (cstr->is_nalu) {
u32 nalu_size;
u32 remain = data_size;
char *start, *end;
start = out_data;
end = start + 4;
while (remain>4) {
if (!end[0] && !end[1] && !end[2] && (end[3]==0x01)) {
nalu_size = (u32) (end - start - 4);
start[0] = (nalu_size>>24)&0xFF;
start[1] = (nalu_size>>16)&0xFF;
start[2] = (nalu_size>>8)&0xFF;
start[3] = (nalu_size)&0xFF;
start = end;
end = start+4;
remain -= 4;
continue;
}
end++;
remain--;
}
nalu_size = (u32) (end - start - 4);
start[0] = (nalu_size>>24)&0xFF;
start[1] = (nalu_size>>16)&0xFF;
start[2] = (nalu_size>>8)&0xFF;
start[3] = (nalu_size)&0xFF;
}
gf_filter_pck_merge_properties(in_pck, out_pck);
gf_filter_pck_set_crypt_flags(out_pck, 0);
gf_filter_pck_send(out_pck);
return GF_OK;
}
#ifdef OLD_KEY_FETCHERS
static GF_Err cenc_dec_setup_oma(GF_CENCDecCtx *ctx, GF_CENCDecStream *cstr, u32 scheme_type, u32 scheme_version, const char *scheme_uri, const char *kms_uri)
{
const GF_PropertyValue *prop;
GF_FilterPid *pid = cstr->ipid;
cstr->state = DECRYPT_STATE_ERROR;
if (scheme_type != GF_ISOM_OMADRM_SCHEME) return GF_NOT_SUPPORTED;
if (scheme_version != 0x00000200) return GF_NOT_SUPPORTED;
cstr->is_oma = GF_TRUE;
prop = gf_filter_pid_get_property(pid, GF_PROP_PID_OMA_PREVIEW_RANGE);
if (prop) cstr->preview_range = (u32) prop->value.longuint;
/*TODO: call DRM agent, fetch keys*/
if (!kms_uri) return GF_NON_COMPLIANT_BITSTREAM;
cstr->state = DECRYPT_STATE_SETUP;
/*we have preview*/
if (cstr->preview_range) return GF_OK;
return GF_NOT_SUPPORTED;
}
#endif
static GF_Err cenc_dec_load_keys(GF_CENCDecCtx *ctx, GF_CENCDecStream *cstr)
{
bin128 blank_KID;
const u8 *key_info;
u32 i, j, kpos=3, nb_keys;
cstr->crypt_init = GF_FALSE;
//this can be NULL when set per sample and not at init
if (!cstr->cenc_ki)
return GF_OK;
memset(blank_KID, 0, 16);
key_info = cstr->cenc_ki->value.data.ptr;
if (key_info[0]) {
nb_keys = key_info[1];
nb_keys<<=8;
nb_keys |= key_info[2];
} else {
nb_keys = 1;
}
for (i=0; i<nb_keys; i++) {
Bool found = GF_FALSE;
u8 iv_size = key_info[kpos];
const u8 *KID = key_info + kpos+1;
kpos += 17;
if (!iv_size) {
iv_size = key_info[kpos];
kpos += 1 + iv_size;
}
cstr->crypts[i].key_valid = GF_TRUE;
if (ctx->drop_keys.nb_items) {
for (j=0; j<ctx->drop_keys.nb_items; j++) {
if (ctx->drop_keys.vals[j] == i+1) {
cstr->crypts[i].key_valid = GF_FALSE;
break;
}
}
}
if (ctx->kids.nb_items) {
char szKID[33];
szKID[0] = 0;
for (j=0; j<16; j++) {
char szC[3];
sprintf(szC, "%02X", KID[j]);
strcat(szKID, szC);
}
for (j=0; j<ctx->kids.nb_items; j++) {
char *kid_d = ctx->kids.vals[j];
if (!strncmp(kid_d, "0x", 2)) kid_d+=2;
if (stricmp(szKID, kid_d)) continue;
//no global key, disable key
if (!ctx->keys.nb_items) {
cstr->crypts[i].key_valid = GF_FALSE;
}
//use global keys
else {
u32 len;
bin128 key;
char *key_str = ctx->keys.vals[j];
if (!strncmp(key_str, "0x", 2)) key_str+= 2;
len = (u32) strlen(key_str);
if (len!=32) {
cstr->crypts[i].key_valid = GF_FALSE;
break;
}
for (j=0; j<16; j++) {
u32 val;
char szV[3];
szV[0] = key_str[2*j];
szV[1] = key_str[2*j+1];
szV[2] = 0;
sscanf(szV, "%02X", &val);
key[j] = val;
}
memcpy(cstr->crypts[i].key, key, 16);
found = GF_TRUE;
}
break;
}
}
if (!cstr->crypts[i].key_valid) continue;
if (found) continue;
for (j=0; j<cstr->KID_count; j++) {
Bool match = GF_FALSE;
if (cstr->is_hls) {
match = GF_TRUE;
} else if (!memcmp(KID, cstr->KIDs[j], 16) || !memcmp(blank_KID, cstr->KIDs[j], 16) ) {
match = GF_TRUE;
}
if (match) {
memcpy(cstr->crypts[i].key, cstr->keys[j], 16);
found = GF_TRUE;
if (ctx->decrypt>=DECRYPT_SKIP) {
cstr->crypts[i].key_valid = GF_FALSE;
} else {
cstr->crypts[i].key_valid = GF_TRUE;
}
break;
}
}
if (!found) {
char szKID[33];
//key not yet delivered
if (cstr->gpac_master_leaf) {
cstr->crypts[i].key_valid = GF_FALSE;
continue;
}
szKID[0] = 0;
for (j=0; j<16; j++) {
char szV[3];
sprintf(szV, "%02X", KID[j]);
strcat(szKID, szV);
}
if (ctx->decrypt==DECRYPT_FULL) {
GF_LOG(GF_LOG_ERROR, GF_LOG_MEDIA, ("[CENC] Cannot locate key #%d for given KID 0x%s, aborting !\n\tUse '--decrypt=nokey' to force decrypting\n", i+1, szKID));
return cstr->key_error = GF_SERVICE_ERROR;
}
GF_LOG(GF_LOG_WARNING, GF_LOG_MEDIA, ("[CENC] Cannot locate key #%d for given KID 0x%s, will leave data encrypted\n", i+1, szKID));
cstr->crypts[i].key_valid = GF_FALSE;
}
}
cstr->key_error = GF_OK;
cstr->crypt_init = GF_FALSE;
return GF_OK;
}
static GF_Err rfmt_dec_b64(u8 *data, u8 *output, u32 osize)
{
u32 len, i=0;
while (data[i]) {
if (data[i] == '-') data[i] = '+';
else if (data[i] == '_') data[i] = '/';
i++;
}
len = (u32) strlen(data);
switch (len%4) {
case 0: break;
case 2: strcat(data, "=="); break;
case 3: strcat(data, "="); break;
default: return GF_NON_COMPLIANT_BITSTREAM;
}
len = gf_base64_decode(data, (u32) strlen(data), output, osize);
if (len != 16) return GF_NON_COMPLIANT_BITSTREAM;
return GF_OK;
}
#ifdef GPAC_USE_DOWNLOADER
static void ck_http_io(void *usr_cbk, GF_NETIO_Parameter *par)
{
GF_CENCDecStream *cstr = usr_cbk;
switch (par->msg_type) {
case GF_NETIO_GET_METHOD:
par->name = "POST";
break;
case GF_NETIO_GET_HEADER:
if (!cstr->hdr_done) {
cstr->hdr_done = GF_TRUE;
par->name = "Content-Type";
par->value = "application/json";
}
break;
case GF_NETIO_GET_CONTENT:
par->data = cstr->body;
par->size = (u32) strlen(cstr->body);
cstr->res_size = 0;
break;
case GF_NETIO_DATA_EXCHANGE:
if (!cstr->res_size) {
if (cstr->body) gf_free(cstr->body);
cstr->body = NULL;
}
cstr->body = gf_realloc(cstr->body, (cstr->res_size + par->size + 1));
if (!cstr->body) {
cstr->state = DECRYPT_STATE_ERROR;
break;
}
memcpy(cstr->body + cstr->res_size, par->data, par->size);
cstr->res_size+=par->size;
cstr->body[cstr->res_size] = 0;
break;
case GF_NETIO_DATA_TRANSFERED:
{
//extract kids and keys from json
GF_Err e = GF_OK;
u32 key_idx=0;
char *ptr = cstr->body;
while (1) {
char *k = strstr(ptr, "\"k\"");
char *kid = strstr(ptr, "\"kid\"");
if (!k || !kid) break;
k+=3;
kid+=5;
while (k[0] && (k[0] != '"')) k++;
while (kid[0] && (kid[0] != '"')) kid++;
if ((k[0] != '"') || (kid[0] != '"')) {
e = GF_NON_COMPLIANT_BITSTREAM;
break;
}
k++;
kid++;
char *s1 = strchr(k, '"');
char *s2 = strchr(kid, '"');
if (!s1 || !s2) {
e = GF_NON_COMPLIANT_BITSTREAM;
break;
}
s1[0] = 0;
s2[0] = 0;
u8 key_val[20], kid_val[20];
e = rfmt_dec_b64(k, key_val, 20);
if (e) break;
e = rfmt_dec_b64(kid, kid_val, 20);
if (e) break;
cstr->keys = gf_realloc(cstr->keys, sizeof(bin128)*(key_idx+1));
cstr->KIDs = gf_realloc(cstr->KIDs, sizeof(bin128)*(key_idx+1));
memcpy(cstr->keys[key_idx], key_val, sizeof(bin128));
memcpy(cstr->KIDs[key_idx], kid_val, sizeof(bin128));
key_idx++;
if (s1 < s2) ptr = s2+1;
else ptr = s1+1;
}
if (!e) {
cstr->KID_count = key_idx;
if (!cstr->crypts) {
cstr->crypts = gf_malloc(sizeof(CENCDecKey));
memset(cstr->crypts, 0, sizeof(CENCDecKey));
}
memcpy(cstr->crypts[0].key, cstr->keys[0], sizeof(bin128));
cstr->crypts[0].key_valid = 1;
} else {
cstr->state = DECRYPT_STATE_ERROR;
}
if (cstr->body) gf_free(cstr->body);
cstr->body = NULL;
gf_dm_sess_del(cstr->sess);
cstr->sess = NULL;
cstr->ctx->pending_keys--;
}
break;
case GF_NETIO_STATE_ERROR:
if (cstr->sess) {
gf_dm_sess_del(cstr->sess);
cstr->sess = NULL;
cstr->ctx->pending_keys--;
if (cstr->body) gf_free(cstr->body);
cstr->body = NULL;
}
break;
default:
break;
}
}
#endif
static GF_Err cenc_dec_set_clearkey(GF_CENCDecCtx *ctx, GF_CENCDecStream *cstr, char *ck_url, u8 *ck_kid)
{
GF_Err e;
char data64[32];
u32 i, cklen, res = gf_base64_encode(ck_kid, 16, data64, 32);
data64[res]=0;
for (i=0; i<res; i++) {
if (data64[i]=='+') data64[i] = '-';
else if (data64[i]=='/') data64[i] = '_';
}
while (data64[res-1]=='=') {
data64[res-1]=0;
res--;
if (!res) break;
}
if (cstr->body) gf_free(cstr->body);
cstr->body = NULL;
gf_dynstrcat(&cstr->body, "{\"kids\": [\"", NULL);
gf_dynstrcat(&cstr->body, data64, NULL);
gf_dynstrcat(&cstr->body, "\"], \"type\":\"temporary\"}", NULL);
cklen = (u32) strlen(cstr->body);
u32 crc = gf_crc_32(cstr->body, cklen);
if (cstr->clearkey_crc == crc) return GF_OK;
#ifdef GPAC_USE_DOWNLOADER
GF_DownloadManager *dm = gf_filter_get_download_manager(ctx->filter);
cstr->sess = gf_dm_sess_new(dm, ck_url, 0, ck_http_io, cstr, &e);
if (e) return e;
ctx->pending_keys++;
return gf_dm_sess_process(cstr->sess);
#else
return GF_NOT_SUPPORTED;
#endif
}
#ifdef GPAC_USE_DOWNLOADER
static void hls_kms_io(void *usr_cbk, GF_NETIO_Parameter *par)
{
GF_CENCDecStream *cstr = (GF_CENCDecStream *)usr_cbk;
if (par->msg_type==GF_NETIO_DATA_EXCHANGE) {
if (cstr->state == DECRYPT_STATE_ERROR)
return;
if (!cstr->res_size) {
if (cstr->body) gf_free(cstr->body);
cstr->body = NULL;
}
if (cstr->res_size > 32) {
cstr->state = DECRYPT_STATE_ERROR;
return;
}
cstr->body = gf_realloc(cstr->body, (cstr->res_size + par->size + 1));
if (!cstr->body) {
cstr->state = DECRYPT_STATE_ERROR;
return;
}
memcpy(cstr->body + cstr->res_size, par->data, par->size);
cstr->res_size+=par->size;
cstr->body[cstr->res_size] = 0;
} else if (par->msg_type==GF_NETIO_DATA_TRANSFERED) {
if (cstr->body && (cstr->res_size <= 32)) {
//first 16 bytes is the key, in some case we have IV repeated after the key (to do ?)
memcpy(cstr->keys[0], cstr->body, sizeof(bin128));
//load keys
if (!cstr->crypts) {
cstr->crypts = gf_malloc(sizeof(CENCDecKey));
memset(cstr->crypts, 0, sizeof(CENCDecKey));
}
memcpy(cstr->crypts[0].key, cstr->keys[0], sizeof(bin128));
cstr->crypts[0].key_valid = 1;
} else {
GF_LOG(GF_LOG_ERROR, GF_LOG_DASH, ("[CENC/HLS] Invalid key size, greater than 16 bytes\n"))
cstr->state = DECRYPT_STATE_ERROR;
}
if (cstr->body) gf_free(cstr->body);
cstr->body = NULL;
gf_dm_sess_del(cstr->sess);
cstr->sess = NULL;
cstr->ctx->pending_keys--;
} else if (par->msg_type==GF_NETIO_STATE_ERROR) {
if (cstr->sess) {
gf_dm_sess_del(cstr->sess);
cstr->sess = NULL;
cstr->ctx->pending_keys--;
if (cstr->body) gf_free(cstr->body);
cstr->body = NULL;
}
}
}
#endif
static GF_Err cenc_dec_set_hls_key(GF_CENCDecCtx *ctx, GF_CENCDecStream *cstr, char *key_url, u8 *key_IV)
{
cstr->is_hls = GF_TRUE;
//copy IV
memcpy(cstr->hls_IV, key_IV, sizeof(bin128));
//switch key if needed IV
if (cstr->hls_key_url && key_url && !strcmp(cstr->hls_key_url, key_url)) {
if (cstr->crypt_init)
gf_crypt_set_IV(cstr->crypts[0].crypt, cstr->hls_IV, 16);
return GF_OK;
}
if (cstr->hls_key_url) gf_free(cstr->hls_key_url);
if (!key_url) {
cstr->hls_key_url = NULL;
return GF_OK;
}
cstr->hls_key_url = gf_strdup(key_url);
if (!cstr->hls_key_url) {
return GF_OUT_OF_MEM;
}
GF_LOG(GF_LOG_DEBUG, GF_LOG_DASH, ("[CENC/HLS] Switching key to %s\n", key_url))
if (ctx->hls_cenc_patch_iv)
cstr->force_hls_iv = GF_TRUE;
cstr->KID_count = 1;
cstr->keys = (bin128 *)gf_realloc(cstr->keys, cstr->KID_count*sizeof(bin128));
if (!strncmp(key_url, "urn:gpac:keys:value:", 20)) {
u32 i;
u8 *key_data = (u8 *) cstr->keys[0];
key_url += 20;
if (!strncmp(key_url, "0x", 2)) key_url += 2;
i = (u32) strlen(key_url);
if (i != 32) {
GF_LOG(GF_LOG_ERROR, GF_LOG_DASH, ("[CENC/HLS] key %s not found\n", key_url))
return GF_BAD_PARAM;
}
for (i=0; i<16; i++) {
char szV[3];
u32 v;
szV[0] = key_url[2*i];
szV[1] = key_url[2*i + 1];
szV[2] = 0;
sscanf(szV, "%X", &v);
key_data[i] = v;
}
}
//key is local, activate right away
else if (gf_url_is_local(key_url)) {
FILE *fkey = gf_fopen(key_url, "rb");
if (!fkey) {
GF_LOG(GF_LOG_ERROR, GF_LOG_DASH, ("[CENC/HLS] key %s not found\n", key_url))
return GF_URL_ERROR;
} else {
u32 read = (u32) gf_fread(cstr->keys[0], 16, fkey);
if (read != 16) {
GF_LOG(GF_LOG_ERROR, GF_LOG_DASH, ("[CENC/HLS] key %s too short, expecting 16 bytes got %d\n", key_url, read))
return GF_BAD_PARAM;
}
gf_fclose(fkey);
}
}
//load key
else {
GF_Err e = GF_SERVICE_ERROR;
#ifdef GPAC_USE_DOWNLOADER
GF_DownloadManager *dm = gf_filter_get_download_manager(ctx->filter);
GF_DownloadSession *sess = gf_dm_sess_new(dm, key_url, GF_NETIO_SESSION_NOT_CACHED, hls_kms_io, cstr, &e);
if (e) {
GF_LOG(GF_LOG_ERROR, GF_LOG_DASH, ("[CENC/HLS] Failed to setup download session for key %s: %s\n", key_url, gf_error_to_string(e)))
return e;
}
ctx->pending_keys++;
return gf_dm_sess_process(sess);
#else
e = GF_NOT_SUPPORTED;
#endif
}
if (!cstr->crypts) {
cstr->crypts = gf_malloc(sizeof(CENCDecKey));
memset(cstr->crypts, 0, sizeof(CENCDecKey));
}
memcpy(cstr->crypts[0].key, cstr->keys[0], sizeof(bin128));
cstr->crypts[0].key_valid = 1;
return GF_OK;
}
static GF_Err cenc_dec_push_iv(GF_CENCDecStream *cstr, u32 key_idx, u8 *IV, u32 iv_size, u32 const_iv_size, const u8 *const_iv);
static GF_Err cenc_dec_load_pssh(GF_CENCDecCtx *ctx, GF_CENCDecStream *cstr, const GF_PropertyValue *pssh_prop, Bool is_pck_pssh, const GF_PropertyValue *cinfo_prop)
{
u32 i, nb_pssh;
if (pssh_prop) {
gf_bs_reassign_buffer(ctx->bs_r, pssh_prop->value.data.ptr, pssh_prop->value.data.size);
nb_pssh = gf_bs_read_u32(ctx->bs_r);
} else {
nb_pssh = 0;
}
for (i = 0; i < nb_pssh; i++) {
u32 cypherOffset;
char *enc_data;
bin128 cypherKey, cypherIV;
GF_Crypt *mc;
u32 pos, priv_len;
char szSystemID[33];
bin128 sysID;
Bool is_leaf_key=GF_FALSE;
u32 j, kid_count=0, enc_payload_len;
u8 *pssh_data = (u8 *) pssh_prop->value.data.ptr ;