Exemplo n.º 1
0
def getDecrypter(key,iv):
    global USEDec
    if USEDec==1:
        enc =AES.new(key, AES.MODE_CBC, iv)
    elif USEDec==3:
        ivb=array.array('B',iv)
        keyb= array.array('B',key)
        enc=python_aes.new(keyb, 2, ivb)
    else:
        enc = binascii.hexlify(key)+':'+binascii.hexlify(iv) # dummy return, we must be using android here
    return  enc       
Exemplo n.º 2
0
def getDecrypter(key,iv):
    global USEDec
    if USEDec==1:
        enc =AES.new(key, AES.MODE_CBC, iv)
    elif USEDec==3:
        ivb=array.array('B',iv)
        keyb= array.array('B',key)
        enc=python_aes.new(keyb, 2, ivb)
    else:
        enc =androidsslPy._load_crypto_libcrypto()
        enc = enc(key, iv)
    return  enc       
Exemplo n.º 3
0
def getDecrypter(key, iv):
    global USEDec
    if USEDec == 1:
        enc = AES.new(key, AES.MODE_CBC, iv)
    elif USEDec == 3:
        ivb = array.array('B', iv)
        keyb = array.array('B', key)
        enc = python_aes.new(keyb, 2, ivb)
    else:
        enc = androidsslPy._load_crypto_libcrypto()
        enc = enc(key, iv)
    return enc
Exemplo n.º 4
0
def handle_basic_m3u(url):
    global iv
    global key
    global USEDec
    seq = 1
    enc = None
    nextlen = 5
    duration = 5
    targetduration = 5
    for line in gen_m3u(url):
        if line.startswith('#EXT'):
            tag, attribs = parse_m3u_tag(line)
            if tag == '#EXTINF':
                duration = float(attribs[0])
            elif tag == '#EXT-X-TARGETDURATION':
                assert len(
                    attribs) == 1, "too many attribs in EXT-X-TARGETDURATION"
                targetduration = int(attribs[0])
                pass
            elif tag == '#EXT-X-MEDIA-SEQUENCE':
                assert len(
                    attribs) == 1, "too many attribs in EXT-X-MEDIA-SEQUENCE"
                seq = int(attribs[0])
            elif tag == '#EXT-X-KEY':
                attribs = parse_kv(attribs, ('METHOD', 'URI', 'IV'))
                assert 'METHOD' in attribs, 'expected METHOD in EXT-X-KEY'
                if attribs['METHOD'] == 'NONE':
                    assert 'URI' not in attribs, 'EXT-X-KEY: METHOD=NONE, but URI found'
                    assert 'IV' not in attribs, 'EXT-X-KEY: METHOD=NONE, but IV found'
                    enc = None
                elif attribs['METHOD'] == 'AES-128':
                    assert 'URI' in attribs, 'EXT-X-KEY: METHOD=AES-128, but no URI found'
                    # from Crypto.Cipher import AES
                    key = download_file(attribs['URI'].strip('"'))
                    assert len(
                        key
                    ) == 16, 'EXT-X-KEY: downloaded key file has bad length'
                    if 'IV' in attribs:
                        assert attribs['IV'].lower().startswith(
                            '0x'), 'EXT-X-KEY: IV attribute has bad format'
                        iv = attribs['IV'][2:].zfill(32).decode('hex')
                        assert len(
                            iv) == 16, 'EXT-X-KEY: IV attribute has bad length'
                    else:
                        iv = '\0' * 8 + struct.pack('>Q', seq)

                    if not USEDec == 3:
                        enc = AES.new(key, AES.MODE_CBC, iv)
                    else:
                        ivb = array.array('B', iv)
                        keyb = array.array('B', key)
                        enc = python_aes.new(keyb, 2, ivb)
                    # enc = AES_CBC(key)
                    # print key
                    # print iv
                    # enc=AESDecrypter.new(key, 2, iv)
                else:
                    assert False, 'EXT-X-KEY: METHOD=%s unknown' % attribs[
                        'METHOD']
            elif tag == '#EXT-X-PROGRAM-DATE-TIME':
                assert len(
                    attribs
                ) == 1, "too many attribs in EXT-X-PROGRAM-DATE-TIME"
                # TODO parse attribs[0] as ISO8601 date/time
                pass
            elif tag == '#EXT-X-ALLOW-CACHE':
                # XXX deliberately ignore
                pass
            elif tag == '#EXT-X-ENDLIST':
                assert not attribs
                yield None
                return
            elif tag == '#EXT-X-STREAM-INF':
                raise ValueError(
                    "don't know how to handle EXT-X-STREAM-INF in basic playlist"
                )
            elif tag == '#EXT-X-DISCONTINUITY':
                assert not attribs
                print "[warn] discontinuity in stream"
            elif tag == '#EXT-X-VERSION':
                assert len(attribs) == 1
                if int(attribs[0]) > SUPPORTED_VERSION:
                    print "[warn] file version %s exceeds supported version %d; some things might be broken" % (
                        attribs[0], SUPPORTED_VERSION)
            # else:
            #    raise ValueError("tag %s not known"%tag)
        else:
            yield (seq, enc, duration, targetduration, line)
            seq += 1
Exemplo n.º 5
0
def handle_basic_m3u(url):
    global iv
    global key
    global USEDec
    seq = 1
    enc = None
    nextlen = 5
    duration = 5
    targetduration=5
    for line in gen_m3u(url):
        if line.startswith('#EXT'):
            tag, attribs = parse_m3u_tag(line)
            if tag == '#EXTINF':
                duration = float(attribs[0])
            elif tag == '#EXT-X-TARGETDURATION':
                assert len(attribs) == 1, "too many attribs in EXT-X-TARGETDURATION"
                targetduration = int(attribs[0])
                pass
            elif tag == '#EXT-X-MEDIA-SEQUENCE':
                assert len(attribs) == 1, "too many attribs in EXT-X-MEDIA-SEQUENCE"
                seq = int(attribs[0])
            elif tag == '#EXT-X-KEY':
                attribs = parse_kv(attribs, ('METHOD', 'URI', 'IV'))
                assert 'METHOD' in attribs, 'expected METHOD in EXT-X-KEY'
                if attribs['METHOD'] == 'NONE':
                    assert 'URI' not in attribs, 'EXT-X-KEY: METHOD=NONE, but URI found'
                    assert 'IV' not in attribs, 'EXT-X-KEY: METHOD=NONE, but IV found'
                    enc = None
                elif attribs['METHOD'] == 'AES-128':
                    assert 'URI' in attribs, 'EXT-X-KEY: METHOD=AES-128, but no URI found'
                    #from Crypto.Cipher import AES
                    key = download_file(attribs['URI'].strip('"'))
                    assert len(key) == 16, 'EXT-X-KEY: downloaded key file has bad length'
                    if 'IV' in attribs:
                        assert attribs['IV'].lower().startswith('0x'), 'EXT-X-KEY: IV attribute has bad format'
                        iv = attribs['IV'][2:].zfill(32).decode('hex')
                        assert len(iv) == 16, 'EXT-X-KEY: IV attribute has bad length'
                    else:
                        iv = '\0'*8 + struct.pack('>Q', seq)
                    
                    if not USEDec==3:
                        enc = AES.new(key, AES.MODE_CBC, iv)
                    else:
                        ivb=array.array('B',iv)
                        keyb= array.array('B',key)
                        enc=python_aes.new(keyb, 2, ivb)
                    #enc = AES_CBC(key)
                    #print key
                    #print iv
                    #enc=AESDecrypter.new(key, 2, iv)
                else:
                    assert False, 'EXT-X-KEY: METHOD=%s unknown'%attribs['METHOD']
            elif tag == '#EXT-X-PROGRAM-DATE-TIME':
                assert len(attribs) == 1, "too many attribs in EXT-X-PROGRAM-DATE-TIME"
                # TODO parse attribs[0] as ISO8601 date/time
                pass
            elif tag == '#EXT-X-ALLOW-CACHE':
                # XXX deliberately ignore
                pass
            elif tag == '#EXT-X-ENDLIST':
                assert not attribs
                yield None
                return
            elif tag == '#EXT-X-STREAM-INF':
                raise ValueError("don't know how to handle EXT-X-STREAM-INF in basic playlist")
            elif tag == '#EXT-X-DISCONTINUITY':
                assert not attribs
                print "[warn] discontinuity in stream"
            elif tag == '#EXT-X-VERSION':
                assert len(attribs) == 1
                if int(attribs[0]) > SUPPORTED_VERSION:
                    print "[warn] file version %s exceeds supported version %d; some things might be broken"%(attribs[0], SUPPORTED_VERSION)
            #else:
            #    raise ValueError("tag %s not known"%tag)
        else:
            yield (seq, enc, duration, targetduration, line)
            seq += 1