[tpm2] tpm2-pytss connect to dockerized swtpm

henry.gadacz at stud.h-da.de Wed, 27 Jul 2022 10:29:16 +0200
Newsgroups dev.linux.lists.tpm2
Message-ID <[email protected]>
Hello everyone,

 

I am trying to accomplish the following, but did not succeed and I hope
someone can help me.

I want to have a docker compose with two containers. In the first container
I want to run the swtpm (https://github.com/stefanberger/swtpm) and in the
other a python script that uses tpm2-pytss to connect to the swtpm in the
first container. 

 

When I run swtpm and the python script in the same container it works.

In order to run them in separate containers I just duplicated the Dockerfile
(I know this has some overhead, but to make sure don't miss any
dependencies), changed the docker CMD command to either run the python
script or the swtpm and renamed them to Dockerfile_app_test and
Dockerfile_tpm_test.

 

My docker compose file is looking like this:

version: '3.7'
services:

  app:
    container_name: app
    build:
      context: .
      dockerfile: Dockerfile_app_test
    restart: unless-stopped

  tpm:
    container_name: tpm
    build:
      context: .
      dockerfile: Dockerfile_tpm_test
    ports:
      - "2321:2321"
      - "2322:2322"
    restart: unless-stopped

 

 

My python script is:

from tpm2_pytss import *
if __name__ == '__main__':
    print("TPM test application")
    tpm = ESAPI(tcti="swtpm:host=tpm,port=2321")
    tpm.startup(TPM2_SU.CLEAR)
    
    r = tpm.get_random(8)
    print("type is ", type(r))
    print("r    is ", str(r))
    print("as int  ", int(str(r), 16))

 

When I run it in one Dockerfile I used 

tpm = ESAPI(tcti="swtpm:host=localhost,port=2321")

so I thought changing the host name to the docker container name should do
it but I always get the following errors:

app  | WARNING:tcti:src/util/io.c:262:socket_connect() Failed to connect to
host 172.21.0.2, port 2321: errno 111: Connection refused 
app  | ERROR:tcti:src/tss2-tcti/tcti-swtpm.c:614:Tss2_Tcti_Swtpm_Init()
Cannot connect to swtpm TPM socket 
app  | ERROR:tcti:src/tss2-tcti/tctildr-dl.c:170:tcti_from_file() Could not
initialize TCTI file: swtpm 
app  | ERROR:tcti:src/tss2-tcti/tctildr.c:428:Tss2_TctiLdr_Initialize_Ex()
Failed to instantiate TCTI 
app  | Traceback (most recent call last):
app  |   File "/app/main.py", line 70, in <module>
app  |     tpm = ESAPI(tcti="swtpm:host=tpm,port=2321")
app  |   File "/usr/local/lib/python3.10/dist-packages/tpm2_pytss/ESAPI.py",
line 123, in __init__
app  |     tcti = TCTILdr.parse(tcti)
app  |   File
"/usr/local/lib/python3.10/dist-packages/tpm2_pytss/TCTILdr.py", line 54, in
parse
app  |     return cls(name, conf)
app  |   File
"/usr/local/lib/python3.10/dist-packages/tpm2_pytss/TCTILdr.py", line 29, in
__init__
app  |     _chkrc(lib.Tss2_TctiLdr_Initialize_Ex(name, conf, self._ctx_pp))
app  |   File
"/usr/local/lib/python3.10/dist-packages/tpm2_pytss/internal/utils.py", line
32, in _chkr
app  |     raise TSS2_Exception(rc)
app  | tpm2_pytss.TSS2_Exception.TSS2_Exception: tcti:IO failure
app exited with code 1

 

 

I know it's not a plain tpm2-tss question, but does anyone has experience
with that and can help me? 

 

Kind regards, 
Henry
attachment.htm (text/html, 11.9 KB)
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:#0563C1;
	text-decoration:underline;}
pre
	{mso-style-priority:99;
	mso-style-link:"HTML Vorformatiert Zchn";
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:10.0pt;
	font-family:"Courier New";}
span.HTMLVorformatiertZchn
	{mso-style-name:"HTML Vorformatiert Zchn";
	mso-style-priority:99;
	mso-style-link:"HTML Vorformatiert";
	font-family:"Courier New";}
.MsoChpDefault
	{mso-style-type:export-only;
	font-family:"Calibri",sans-serif;}
@page WordSection1
	{size:612.0pt 792.0pt;
	margin:70.85pt 70.85pt 2.0cm 70.85pt;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link="#0563C1" vlink="#954F72" style='word-wrap:break-word'><div class=WordSection1><p class=MsoNormal>Hello everyone,<o:p></o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal>I am trying to accomplish the following, but did not succeed and I hope someone can help me.<o:p></o:p></p><p class=MsoNormal>I want to have a docker compose with two containers. In the first container I want to run the swtpm (<a href="https://github.com/stefanberger/swtpm">https://github.com/stefanberger/swtpm</a><span style='color:black'>)</span> and in the other a python script that uses tpm2-pytss to connect to the swtpm in the first container. <o:p></o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal>When I run swtpm and the python script in the same container it works.<o:p></o:p></p><p class=MsoNormal>In order to run them in separate containers I just duplicated the Dockerfile (I know this has some overhead, but to make sure don&#8217;t miss any dependencies), changed the docker CMD command to either run the python script or the swtpm and renamed them to Dockerfile_app_test and Dockerfile_tpm_test.<o:p></o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal>My docker compose file is looking like this:<o:p></o:p></p><p class=MsoNormal style='background:white'><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>version</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>: </span><span style='font-size:10.0pt;font-family:"Courier New";color:#067D17'>'3.7'<br></span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>services</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>:<br><br>&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>app</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>:<br>&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>container_name</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>: app<br>&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>build</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>context</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>: .<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>dockerfile</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>: Dockerfile_app_test<br>&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>restart</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>: unless-stopped<br><br>&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>tpm</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>:<br>&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>container_name</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>: tpm<br>&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>build</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>context</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>: .<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>dockerfile</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>: Dockerfile_tpm_test<br>&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>ports</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - </span><span style='font-size:10.0pt;font-family:"Courier New";color:#067D17'>&quot;2321:2321&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>- </span><span style='font-size:10.0pt;font-family:"Courier New";color:#067D17'>&quot;2322:2322&quot;<br>&nbsp;&nbsp;&nbsp; </span><span style='font-size:10.0pt;font-family:"Courier New";color:#0033B3'>restart</span><span style='font-size:10.0pt;font-family:"Courier New";color:#080808'>: unless-stopped<o:p></o:p></span></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal>My python script is:<o:p></o:p></p><pre style='background:white'><span style='color:#0033B3'>from </span><span style='color:#080808'>tpm2_pytss </span><span style='color:#0033B3'>import </span><span style='color:#080808'>*<o:p></o:p></span></pre><pre style='background:white'><span style='color:#0033B3'>if </span><span style='color:#080808'>__name__ == </span><span style='color:#067D17'>'__main__'</span><span style='color:#080808'>:<br>&nbsp;&nbsp;&nbsp; </span><span style='color:navy'>print</span><span style='color:#080808'>(</span><span style='color:#067D17'>&quot;TPM test application&quot;</span><span style='color:#080808'>)<br>&nbsp;&nbsp;&nbsp; tpm = ESAPI(</span><span style='color:#660099'>tcti</span><span style='color:#080808'>=</span><span style='color:#067D17'>&quot;swtpm:host=tpm,port=2321&quot;</span><span style='color:#080808'>)<br>&nbsp;&nbsp;&nbsp; tpm.startup(TPM2_SU.CLEAR)<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;r = tpm.get_random(</span><span style='color:#1750EB'>8</span><span style='color:#080808'>)<br>&nbsp;&nbsp;&nbsp; </span><span style='color:navy'>print</span><span style='color:#080808'>(</span><span style='color:#067D17'>&quot;type is &quot;</span><span style='color:#080808'>, </span><span style='color:navy'>type</span><span style='color:#080808'>(r))<br>&nbsp;&nbsp;&nbsp; </span><span style='color:navy'>print</span><span style='color:#080808'>(</span><span style='color:#067D17'>&quot;r&nbsp;&nbsp;&nbsp; is &quot;</span><span style='color:#080808'>, </span><span style='color:navy'>str</span><span style='color:#080808'>(r))<br>&nbsp;&nbsp;&nbsp; </span><span style='color:navy'>print</span><span style='color:#080808'>(</span><span style='color:#067D17'>&quot;as int&nbsp; &quot;</span><span style='color:#080808'>, </span><span style='color:navy'>int</span><span style='color:#080808'>(</span><span style='color:navy'>str</span><span style='color:#080808'>(r), </span><span style='color:#1750EB'>16</span><span style='color:#080808'>))<o:p></o:p></span></pre><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal>When I run it in one Dockerfile I used <o:p></o:p></p><pre style='background:white'><span style='color:#080808'>tpm = ESAPI(</span><span style='color:#660099'>tcti</span><span style='color:#080808'>=</span><span style='color:#067D17'>&quot;swtpm:host=localhost,port=2321&quot;</span><span style='color:#080808'>)<o:p></o:p></span></pre><p class=MsoNormal>so I thought changing the host name to the docker container name should do it but I always get the following errors:<o:p></o:p></p><pre style='background:white'><span style='color:#080808'>app&nbsp; | WARNING:tcti:src/util/io.c:262:socket_connect() Failed to connect to host 172.21.0.2, port 2321: errno 111: Connection refused <o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; | ERROR:tcti:src/tss2-tcti/tcti-swtpm.c:614:Tss2_Tcti_Swtpm_Init() Cannot connect to swtpm TPM socket <o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; | ERROR:tcti:src/tss2-tcti/tctildr-dl.c:170:tcti_from_file() Could not initialize TCTI file: swtpm <o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; | ERROR:tcti:src/tss2-tcti/tctildr.c:428:Tss2_TctiLdr_Initialize_Ex() Failed to instantiate TCTI <o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; | Traceback (most recent call last):<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; |&nbsp;&nbsp; File &quot;/app/main.py&quot;, line 70, in &lt;module&gt;<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; tpm = ESAPI(tcti=&quot;swtpm:host=tpm,port=2321&quot;)<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; |&nbsp;&nbsp; File &quot;/usr/local/lib/python3.10/dist-packages/tpm2_pytss/ESAPI.py&quot;, line 123, in __init__<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; tcti = TCTILdr.parse(tcti)<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; |&nbsp;&nbsp; File &quot;/usr/local/lib/python3.10/dist-packages/tpm2_pytss/TCTILdr.py&quot;, line 54, in parse<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; return cls(name, conf)<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; |&nbsp;&nbsp; File &quot;/usr/local/lib/python3.10/dist-packages/tpm2_pytss/TCTILdr.py&quot;, line 29, in __init__<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; _chkrc(lib.Tss2_TctiLdr_Initialize_Ex(name, conf, self._ctx_pp))<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; |&nbsp;&nbsp; File &quot;/usr/local/lib/python3.10/dist-packages/tpm2_pytss/internal/utils.py&quot;, line 32, in _chkr<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; raise TSS2_Exception(rc)<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app&nbsp; | tpm2_pytss.TSS2_Exception.TSS2_Exception: tcti:IO failure<o:p></o:p></span></pre><pre style='background:white'><span style='color:#080808'>app exited with code 1<o:p></o:p></span></pre><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal>I know it&#8217;s not a plain tpm2-tss question, but does anyone has experience with that and can help me? <o:p></o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal>Kind regards, <br>Henry<o:p></o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p></div></body></html>