From 4007630dac238cccbfddd2ededa484f8fb14eb7c Mon Sep 17 00:00:00 2001 From: Stefan Pluecken Date: Fri, 9 Apr 2010 15:21:13 +0200 Subject: fixes bug #467 add tpm interface to enigma 2 introduce a demo plugin for tpm usage in plugins to validate running on dream multimedia hardware to be used in plugins (see doc/TPM for further information) --- lib/python/Plugins/DemoPlugins/Makefile.am | 6 +- lib/python/Plugins/DemoPlugins/TPMDemo/Makefile.am | 5 ++ lib/python/Plugins/DemoPlugins/TPMDemo/README | 1 + lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py | 82 ++++++++++++++++++++++ lib/python/Plugins/Makefile.am | 1 + lib/python/enigma_python.i | 2 + 6 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 lib/python/Plugins/DemoPlugins/TPMDemo/Makefile.am create mode 100644 lib/python/Plugins/DemoPlugins/TPMDemo/README create mode 100644 lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py (limited to 'lib/python') diff --git a/lib/python/Plugins/DemoPlugins/Makefile.am b/lib/python/Plugins/DemoPlugins/Makefile.am index aace17cc..9e16bfc1 100755 --- a/lib/python/Plugins/DemoPlugins/Makefile.am +++ b/lib/python/Plugins/DemoPlugins/Makefile.am @@ -1,6 +1,8 @@ installdir = $(pkglibdir)/python/Plugins/DemoPlugins -SUBDIRS = TestPlugin +SUBDIRS = TestPlugin TPMDemo install_PYTHON = \ - __init__.py \ No newline at end of file + __init__.py + + \ No newline at end of file diff --git a/lib/python/Plugins/DemoPlugins/TPMDemo/Makefile.am b/lib/python/Plugins/DemoPlugins/TPMDemo/Makefile.am new file mode 100644 index 00000000..3ccca98e --- /dev/null +++ b/lib/python/Plugins/DemoPlugins/TPMDemo/Makefile.am @@ -0,0 +1,5 @@ +installdir = $(LIBDIR)/enigma2/python/Plugins/DemoPlugins/TPMDemo + +install_PYTHON = \ + __init__.py \ + plugin.py diff --git a/lib/python/Plugins/DemoPlugins/TPMDemo/README b/lib/python/Plugins/DemoPlugins/TPMDemo/README new file mode 100644 index 00000000..89a972aa --- /dev/null +++ b/lib/python/Plugins/DemoPlugins/TPMDemo/README @@ -0,0 +1 @@ +Please read enigma2/doc/TPM for further instructions on how to integrate this into your own plugins. \ No newline at end of file diff --git a/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py b/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py new file mode 100644 index 00000000..dafb2837 --- /dev/null +++ b/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py @@ -0,0 +1,82 @@ +from Screens.Screen import Screen +from Plugins.Plugin import PluginDescriptor +from enigma import eTPM +import sha + +def bin2long(s): + return reduce( lambda x,y:(x<<8L)+y, map(ord, s)) + +def long2bin(l): + res = "" + for byte in range(128): + res += chr((l >> (1024 - (byte + 1) * 8)) & 0xff) + return res + +def rsa_pub1024(src, mod): + return long2bin(pow(bin2long(src), 65537, bin2long(mod))) + +def decrypt_block(src, mod): + if len(src) != 128 and len(src) != 202: + return None + dest = rsa_pub1024(src[:128], mod) + hash = sha.new(dest[1:107]) + if len(src) == 202: + hash.update(src[131:192]) + result = hash.digest() + if result == dest[107:127]: + return dest + return None + +def validate_cert(cert, key): + buf = decrypt_block(cert[8:], key) + if buf is None: + return None + return buf[36:107] + cert[139:196] + +def read_random(): + try: + fd = open("/dev/urandom", "r") + buf = fd.read(8) + fd.close() + return buf + except: + return None + +def main(session, **kwargs): + 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', ':', '?'] + + etpm = eTPM() + l2cert = etpm.getCert(eTPM.TPMD_DT_LEVEL2_CERT) + if l2cert is None: + print "l2cert not found" + return + + l2key = validate_cert(l2cert, rootkey) + if l2key is None: + print "l2cert invalid" + return + + l3cert = etpm.getCert(eTPM.TPMD_DT_LEVEL3_CERT) + if l3cert is None: + print "l3cert not found (can be fixed by running the genuine dreambox plugin and running the offered update)" + return + + l3key = validate_cert(l3cert, l2key) + if l3key is None: + print "l3cert invalid" + return + + rnd = read_random() + if rnd is None: + print "random error" + return + val = etpm.challenge(rnd) + result = decrypt_block(val, l3key) + if result[80:88] == rnd: + print "successfully finished the tpm test" + # would start your plugin here + +def Plugins(**kwargs): + return [PluginDescriptor(name = "TPM Demo", description = _("A demo plugin for TPM usage."), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main), + PluginDescriptor(name = "TPM Demo", description = _("A demo plugin for TPM usage."), icon = "plugin.png", where = PluginDescriptor.WHERE_PLUGINMENU, fnc = main)] + \ No newline at end of file diff --git a/lib/python/Plugins/Makefile.am b/lib/python/Plugins/Makefile.am index f0435036..79d2b0b6 100644 --- a/lib/python/Plugins/Makefile.am +++ b/lib/python/Plugins/Makefile.am @@ -4,3 +4,4 @@ SUBDIRS = Extensions SystemPlugins DemoPlugins install_PYTHON = \ __init__.py Plugin.py + diff --git a/lib/python/enigma_python.i b/lib/python/enigma_python.i index 2fec2ff1..19fb9254 100644 --- a/lib/python/enigma_python.i +++ b/lib/python/enigma_python.i @@ -39,6 +39,7 @@ is usually caused by not marking PSignals as immutable. #include #include #include +#include #include #include #include @@ -157,6 +158,7 @@ typedef long time_t; %immutable ePythonMessagePump::recv_msg; %immutable eDVBLocalTimeHandler::m_timeUpdated; %include +%include %include %include %include -- cgit v1.2.3 From 31addde4d83dfbbaf273bc48a1f0783dc476a30f Mon Sep 17 00:00:00 2001 From: Stefan Pluecken Date: Fri, 9 Apr 2010 19:10:52 +0200 Subject: refs bug #467 add __init__.py in TPMDemo plugin --- lib/python/Plugins/DemoPlugins/TPMDemo/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/python/Plugins/DemoPlugins/TPMDemo/__init__.py (limited to 'lib/python') diff --git a/lib/python/Plugins/DemoPlugins/TPMDemo/__init__.py b/lib/python/Plugins/DemoPlugins/TPMDemo/__init__.py new file mode 100644 index 00000000..e69de29b -- cgit v1.2.3 From b8d09c6e14ce43b375fcb5006444551713b42a7c Mon Sep 17 00:00:00 2001 From: Stefan Pluecken Date: Fri, 9 Apr 2010 20:40:28 +0200 Subject: refs bug #467 whitelist dm7025 from tpm test and add beta warning to the documentation --- doc/TPM | 5 +- lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py | 63 +++++++++++++----------- 2 files changed, 37 insertions(+), 31 deletions(-) (limited to 'lib/python') diff --git a/doc/TPM b/doc/TPM index 2b2b2062..20c02300 100644 --- a/doc/TPM +++ b/doc/TPM @@ -1,3 +1,6 @@ +The TPM check is currently to be considered a beta version. So please expect +code changes in the future. + If you'd like to write your own plugins and honor the efforts, Dream Multimedia puts into developing Enigma 2, you can protect your plugin against execution on Non-Dream Multimedia Hardware by implementing a TPM (Trusted Platform Module) @@ -21,4 +24,4 @@ TPM check each time the plugin is called) or directly use it in the Plugins(**kwargs) function and not return the Plugins-list if the TPM check failes (which will prevent the plugin from showing up at all). You can also implement a warning message for all possible TPM failure scenarios. - \ No newline at end of file + diff --git a/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py b/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py index dafb2837..0be09ccf 100644 --- a/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py +++ b/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py @@ -1,5 +1,6 @@ from Screens.Screen import Screen from Plugins.Plugin import PluginDescriptor +from Tools.HardwareInfo import HardwareInfo from enigma import eTPM import sha @@ -43,38 +44,40 @@ def read_random(): return None def main(session, **kwargs): - 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', ':', '?'] + device = HardwareInfo().get_device_name() + if device != "dm7025": + 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', ':', '?'] + + etpm = eTPM() + l2cert = etpm.getCert(eTPM.TPMD_DT_LEVEL2_CERT) + if l2cert is None: + print "l2cert not found" + return - etpm = eTPM() - l2cert = etpm.getCert(eTPM.TPMD_DT_LEVEL2_CERT) - if l2cert is None: - print "l2cert not found" - return - - l2key = validate_cert(l2cert, rootkey) - if l2key is None: - print "l2cert invalid" - return - - l3cert = etpm.getCert(eTPM.TPMD_DT_LEVEL3_CERT) - if l3cert is None: - print "l3cert not found (can be fixed by running the genuine dreambox plugin and running the offered update)" - return - - l3key = validate_cert(l3cert, l2key) - if l3key is None: - print "l3cert invalid" - return - - rnd = read_random() - if rnd is None: - print "random error" - return - val = etpm.challenge(rnd) - result = decrypt_block(val, l3key) - if result[80:88] == rnd: + l2key = validate_cert(l2cert, rootkey) + if l2key is None: + print "l2cert invalid" + return + + l3cert = etpm.getCert(eTPM.TPMD_DT_LEVEL3_CERT) + if l3cert is None: + print "l3cert not found (can be fixed by running the genuine dreambox plugin and running the offered update)" + return + + l3key = validate_cert(l3cert, l2key) + if l3key is None: + print "l3cert invalid" + return + + rnd = read_random() + if rnd is None: + print "random error" + return + val = etpm.challenge(rnd) + result = decrypt_block(val, l3key) + if device == "dm7025" or result[80:88] == rnd: print "successfully finished the tpm test" - # would start your plugin here + # would start your plugin here def Plugins(**kwargs): return [PluginDescriptor(name = "TPM Demo", description = _("A demo plugin for TPM usage."), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main), -- cgit v1.2.3 From cdf6f671e3089e8269ce3dd0dd20dbc4878b898a Mon Sep 17 00:00:00 2001 From: Stefan Pluecken Date: Fri, 9 Apr 2010 20:49:15 +0200 Subject: refs bug #467 read proc device instead of using internal methods --- lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib/python') diff --git a/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py b/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py index 0be09ccf..2c078d35 100644 --- a/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py +++ b/lib/python/Plugins/DemoPlugins/TPMDemo/plugin.py @@ -1,6 +1,5 @@ from Screens.Screen import Screen from Plugins.Plugin import PluginDescriptor -from Tools.HardwareInfo import HardwareInfo from enigma import eTPM import sha @@ -44,7 +43,10 @@ def read_random(): return None def main(session, **kwargs): - device = HardwareInfo().get_device_name() + try: + device = open("/proc/stb/info/model", "r").readline().strip() + except: + device = "" if device != "dm7025": 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', ':', '?'] -- cgit v1.2.3