refs bug #467
[enigma2.git] / lib / python / Plugins / DemoPlugins / TPMDemo / plugin.py
1 from Screens.Screen import Screen
2 from Plugins.Plugin import PluginDescriptor
3 from Tools.HardwareInfo import HardwareInfo
4 from enigma import eTPM
5 import sha
6
7 def bin2long(s):
8         return reduce( lambda x,y:(x<<8L)+y, map(ord, s))
9
10 def long2bin(l):
11         res = ""
12         for byte in range(128):
13                 res += chr((l >> (1024 - (byte + 1) * 8)) & 0xff)
14         return res
15
16 def rsa_pub1024(src, mod):
17         return long2bin(pow(bin2long(src), 65537, bin2long(mod)))
18         
19 def decrypt_block(src, mod):
20         if len(src) != 128 and len(src) != 202:
21                 return None
22         dest = rsa_pub1024(src[:128], mod)
23         hash = sha.new(dest[1:107])
24         if len(src) == 202:
25                 hash.update(src[131:192])       
26         result = hash.digest()
27         if result == dest[107:127]:
28                 return dest
29         return None
30
31 def validate_cert(cert, key):
32         buf = decrypt_block(cert[8:], key) 
33         if buf is None:
34                 return None
35         return buf[36:107] + cert[139:196]
36
37 def read_random():
38         try:
39                 fd = open("/dev/urandom", "r")
40                 buf = fd.read(8)
41                 fd.close()
42                 return buf
43         except:
44                 return None
45
46 def main(session, **kwargs):
47         device = HardwareInfo().get_device_name()
48         if device != "dm7025":
49                 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', ':', '?']
50                 
51                 etpm = eTPM()
52                 l2cert = etpm.getCert(eTPM.TPMD_DT_LEVEL2_CERT)
53                 if l2cert is None:
54                         print "l2cert not found"
55                         return
56         
57                 l2key = validate_cert(l2cert, rootkey)
58                 if l2key is None:
59                         print "l2cert invalid"
60                         return
61                 
62                 l3cert = etpm.getCert(eTPM.TPMD_DT_LEVEL3_CERT)
63                 if l3cert is None:
64                         print "l3cert not found (can be fixed by running the genuine dreambox plugin and running the offered update)"
65                         return
66                 
67                 l3key = validate_cert(l3cert, l2key)
68                 if l3key is None:
69                         print "l3cert invalid"
70                         return
71                 
72                 rnd = read_random()
73                 if rnd is None:
74                         print "random error"
75                         return
76                 val = etpm.challenge(rnd)
77                 result = decrypt_block(val, l3key)
78         if device == "dm7025" or result[80:88] == rnd:
79                 print "successfully finished the tpm test"
80                         # would start your plugin here
81
82 def Plugins(**kwargs):
83         return [PluginDescriptor(name = "TPM Demo", description = _("A demo plugin for TPM usage."), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main),
84                 PluginDescriptor(name = "TPM Demo", description = _("A demo plugin for TPM usage."), icon = "plugin.png", where = PluginDescriptor.WHERE_PLUGINMENU, fnc = main)]
85