fixes bug #467
[vuplus_dvbapp] / lib / python / Plugins / DemoPlugins / TPMDemo / plugin.py
1 from Screens.Screen import Screen
2 from Plugins.Plugin import PluginDescriptor
3 from enigma import eTPM
4 import sha
5
6 def bin2long(s):
7         return reduce( lambda x,y:(x<<8L)+y, map(ord, s))
8
9 def long2bin(l):
10         res = ""
11         for byte in range(128):
12                 res += chr((l >> (1024 - (byte + 1) * 8)) & 0xff)
13         return res
14
15 def rsa_pub1024(src, mod):
16         return long2bin(pow(bin2long(src), 65537, bin2long(mod)))
17         
18 def decrypt_block(src, mod):
19         if len(src) != 128 and len(src) != 202:
20                 return None
21         dest = rsa_pub1024(src[:128], mod)
22         hash = sha.new(dest[1:107])
23         if len(src) == 202:
24                 hash.update(src[131:192])       
25         result = hash.digest()
26         if result == dest[107:127]:
27                 return dest
28         return None
29
30 def validate_cert(cert, key):
31         buf = decrypt_block(cert[8:], key) 
32         if buf is None:
33                 return None
34         return buf[36:107] + cert[139:196]
35
36 def read_random():
37         try:
38                 fd = open("/dev/urandom", "r")
39                 buf = fd.read(8)
40                 fd.close()
41                 return buf
42         except:
43                 return None
44
45 def main(session, **kwargs):
46         rootkey = ['\x9f', '|', '\xe4', 'G', '\xc9', '\xb4', '\xf4', '#', '&', '\xce', '\xb3', '\xfe', '\xda', '\xc9', 'U', '`', '\xd8', '\x8c', 's', 'o', '\x90', '\x9b', '\\', 'b', '\xc0', '\x89', '\xd1', '\x8c', '\x9e', 'J', 'T', '\xc5', 'X', '\xa1', '\xb8', '\x13', '5', 'E', '\x02', '\xc9', '\xb2', '\xe6', 't', '\x89', '\xde', '\xcd', '\x9d', '\x11', '\xdd', '\xc7', '\xf4', '\xe4', '\xe4', '\xbc', '\xdb', '\x9c', '\xea', '}', '\xad', '\xda', 't', 'r', '\x9b', '\xdc', '\xbc', '\x18', '3', '\xe7', '\xaf', '|', '\xae', '\x0c', '\xe3', '\xb5', '\x84', '\x8d', '\r', '\x8d', '\x9d', '2', '\xd0', '\xce', '\xd5', 'q', '\t', '\x84', 'c', '\xa8', ')', '\x99', '\xdc', '<', '"', 'x', '\xe8', '\x87', '\x8f', '\x02', ';', 'S', 'm', '\xd5', '\xf0', '\xa3', '_', '\xb7', 'T', '\t', '\xde', '\xa7', '\xf1', '\xc9', '\xae', '\x8a', '\xd7', '\xd2', '\xcf', '\xb2', '.', '\x13', '\xfb', '\xac', 'j', '\xdf', '\xb1', '\x1d', ':', '?']
47         
48         etpm = eTPM()
49         l2cert = etpm.getCert(eTPM.TPMD_DT_LEVEL2_CERT)
50         if l2cert is None:
51                 print "l2cert not found"
52                 return
53
54         l2key = validate_cert(l2cert, rootkey)
55         if l2key is None:
56                 print "l2cert invalid"
57                 return
58         
59         l3cert = etpm.getCert(eTPM.TPMD_DT_LEVEL3_CERT)
60         if l3cert is None:
61                 print "l3cert not found (can be fixed by running the genuine dreambox plugin and running the offered update)"
62                 return
63         
64         l3key = validate_cert(l3cert, l2key)
65         if l3key is None:
66                 print "l3cert invalid"
67                 return
68         
69         rnd = read_random()
70         if rnd is None:
71                 print "random error"
72                 return
73         val = etpm.challenge(rnd)
74         result = decrypt_block(val, l3key)
75         if result[80:88] == rnd:
76                 print "successfully finished the tpm test"
77                 # would start your plugin here
78
79 def Plugins(**kwargs):
80         return [PluginDescriptor(name = "TPM Demo", description = _("A demo plugin for TPM usage."), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main),
81                 PluginDescriptor(name = "TPM Demo", description = _("A demo plugin for TPM usage."), icon = "plugin.png", where = PluginDescriptor.WHERE_PLUGINMENU, fnc = main)]
82