<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Ok i am trying to use this code:</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
TokenEngine::TokenEngine( const StringList & modulePaths )
<div>{</div>
<div>    ENGINE * tok = ENGINE_by_id( "pkcs11" );</div>
<div>    if ( ! tok )</div>
<div>        throw Exception( "token: unable to get engine" );</div>
<div><br>
</div>
<div>    m_pEngine = tok;</div>
<div><br>
</div>
<div>    const string modulePath( findFirstExisting( modulePaths ) );</div>
<div>    if ( modulePath.empty() )</div>
<div>        throw Exception( "token: unable to find module path" );</div>
<div><br>
</div>
<div>    DEBUG( "token: ctor: module_path=" << QS( modulePath ) );</div>
<div>    if ( 1 != ENGINE_ctrl_cmd_string( tok, "MODULE_PATH", modulePath.c_str(), CMD_MANDATORY ) )</div>
<div>        throw Exception( "token: setting module_path <= " + QS( modulePath ) );</div>
<div><br>
</div>
<div>    DEBUG( "token: ctor: initializing " << m_pEngine );</div>
<div>    if ( 1 != ENGINE_init( tok ) )</div>
<div>        throw Exception( "token: unable to initialize" );</div>
<div><br>
</div>
<div>    DEBUG( "token: ctor: done" );</div>
}<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
which is from this site <a href="https://github.com/tkil/openssl-pkcs11-samples/blob/master/OpenSSLWrappers.cpp" id="LPlnk">https://github.com/tkil/openssl-pkcs11-samples/blob/master/OpenSSLWrappers.cpp</a> and the problem is that if i change modulePath value
 to a not existing file the method is still returning 1 which i think it should not. Correct me if i'm wrong.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
BR</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Piotr</div>
<div class="_Entity _EType_OWALinkPreview _EId_OWALinkPreview _EReadonly_1"></div>
<br>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>Od:</b> Michael McKenney <mike.mckenney@scsiraidguru.com><br>
<b>Wysłane:</b> poniedziałek, 24 maja 2021 14:17<br>
<b>Do:</b> Piotr Lobacz <piotr.lobacz@softgent.com>; openssl-users@openssl.org <openssl-users@openssl.org><br>
<b>Temat:</b> RE: CSR generation using pkcs11 token engine from C# code</font>
<div> </div>
</div>
<style>
<!--
@font-face
        {font-family:"Cambria Math"}
@font-face
        {font-family:Calibri}
p.x_MsoNormal, li.x_MsoNormal, div.x_MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif}
a:link, span.x_MsoHyperlink
        {color:blue;
        text-decoration:underline}
span.x_EmailStyle20
        {font-family:"Calibri",sans-serif;
        color:windowtext}
.x_MsoChpDefault
        {font-size:10.0pt}
@page WordSection1
        {margin:1.0in 1.0in 1.0in 1.0in}
div.x_WordSection1
        {}
-->
</style>
<div lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="x_WordSection1">
<p class="x_MsoNormal">Usually I reserve C# for Windows servers.   I use PHP on Ubuntu other Linux Distros.    Most web servers that need OpenSSL are Linux.   Glad you got it working.    <br>
<br>
<br>
</p>
<p class="x_MsoNormal"> </p>
<div>
<div style="border:none; border-top:solid #E1E1E1 1.0pt; padding:3.0pt 0in 0in 0in">
<p class="x_MsoNormal"><b>From:</b> Piotr Lobacz <piotr.lobacz@softgent.com> <br>
<b>Sent:</b> Monday, May 24, 2021 8:10 AM<br>
<b>To:</b> Michael McKenney <mike.mckenney@scsiraidguru.com>; openssl-users@openssl.org<br>
<b>Subject:</b> ODP: CSR generation using pkcs11 token engine from C# code</p>
</div>
</div>
<p class="x_MsoNormal"> </p>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black">Hi Michael,</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black">thx for your quick reply. Unfortunately i can't use your script because i need to use native code not bash implementation.</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black">For the first question about loading token module i have found a solution on github <a href="https://github.com/tkil/openssl-pkcs11-samples">https://github.com/tkil/openssl-pkcs11-samples</a> .
 This code is a C/C++ code but i can fairly port it to C#, and i was right about different load of engine for pkcs11 engine. So this can be closed.</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black">Another thing is to generate CSR with the usage of token and openssl. This will be more complicated and i don't know yet how to do that.</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black">BR</span></p>
</div>
<div>
<p class="x_MsoNormal"><span style="font-size:12.0pt; color:black">Piotr</span></p>
</div>
<p class="x_MsoNormal"> </p>
<div class="x_MsoNormal" align="center" style="text-align:center">
<hr size="2" width="98%" align="center">
</div>
<div id="x_divRplyFwdMsg">
<p class="x_MsoNormal"><b><span style="color:black">Od:</span></b><span style="color:black"> Michael McKenney <<a href="mailto:mike.mckenney@scsiraidguru.com">mike.mckenney@scsiraidguru.com</a>><br>
<b>Wysłane:</b> poniedziałek, 24 maja 2021 13:28<br>
<b>Do:</b> Piotr Lobacz <<a href="mailto:piotr.lobacz@softgent.com">piotr.lobacz@softgent.com</a>>;
<a href="mailto:openssl-users@openssl.org">openssl-users@openssl.org</a> <<a href="mailto:openssl-users@openssl.org">openssl-users@openssl.org</a>><br>
<b>Temat:</b> RE: CSR generation using pkcs11 token engine from C# code</span> </p>
<div>
<p class="x_MsoNormal"> </p>
</div>
</div>
<div>
<div>
<p class="x_MsoNormal">I wrote this script years ago when I switched to Godaddy 10 site  certificates.   I don't use it from C#   You could easily put it into C# or PHP.   <  >  would be variables at the top.   I have it filled in so I just modify the alt_names.  
 I just cut and paste the all of it into Ubuntu and run it in the directory  /etc/apache2/ssl.   If you don't need all 10, you can delete the extra ones in alt_names.  
<br>
<br>
<br>
openssl req -new -sha256 -nodes -out \<crs_name.csr> -newkey rsa:2048 -keyout \<your key name.key> -config <(<br>
cat <<-EOF<br>
[req]<br>
default_bits = 2048<br>
prompt = no<br>
default_md = sha256<br>
req_extensions = req_ext<br>
distinguished_name = dn<br>
<br>
[ dn ]<br>
C= < country ><br>
ST= < Your States ><br>
L= < City or location ><br>
O= < Organization ><br>
OU= <Organizational Unit ><br>
emailAddress= <your email><br>
CN = <The common name of the cert><br>
<br>
[ req_ext ]<br>
subjectAltName = @alt_names<br>
<br>
[ alt_names ]<br>
DNS.1 = < domain #1 ><br>
DNS.2 = < domain #2 ><br>
DNS.3 = < domain #3 ><br>
DNS.4 = < domain #4 ><br>
DNS.5 = < domain #5 ><br>
DNS.6 = < domain #6 ><br>
DNS.7 = < domain #7 ><br>
DNS.8 = < domain #8 ><br>
DNS.9 = < domain #9 ><br>
EOF<br>
)<br>
<br>
<br>
<br>
<br>
<br>
<br>
-----Original Message-----<br>
From: openssl-users <<a href="mailto:openssl-users-bounces@openssl.org">openssl-users-bounces@openssl.org</a>> On Behalf Of Piotr Lobacz<br>
Sent: Monday, May 24, 2021 5:54 AM<br>
To: <a href="mailto:openssl-users@openssl.org">openssl-users@openssl.org</a><br>
Subject: CSR generation using pkcs11 token engine from C# code<br>
<br>
Hi all,<br>
i am currently trying to generate CSR with the usage of tpm2-pkcs11 module together with pkcs11 engine from opensc and the whole thing running with openssl api from C# code.<br>
<br>
I have checked that my solution works from command line. I have added these lines:<br>
<br>
openssl_conf = openssl_init<br>
<br>
[openssl_init]<br>
engines = engine_section<br>
<br>
[engine_section]<br>
pkcs11 = pkcs11_section<br>
<br>
[pkcs11_section]<br>
engine_id = pkcs11<br>
dynamic_path = /usr/lib/engines-1.1/libpkcs11.so MODULE_PATH = /usr/lib/libtpm2_pkcs11.so init = 0<br>
<br>
to the /etc/ssl/openssl.cnf configuration file and than this command:<br>
<br>
openssl req -new -subj '/C=PL/ST=Gdansk/L=Gdansk/CN=softgent.com/' -sha256 -engine pkcs11 -keyform engine -key "pkcs11:token=foo;object=tls;type=private;pin-value=1234567890"<br>
<br>
produces CSR for me.<br>
<br>
Now i want to do all this, from C# code. I have found a C# library <a href="https://github.com/andyhopp/OpenSsl.DynamicEngine">
https://github.com/andyhopp/OpenSsl.DynamicEngine</a> which will load the engine, but i think that this won't be sufficient in a matter of pkcs11 engine, because i also need to load pkcs11 module. The question is what should i add to this library for propper
 work in means of pkcs11 api? What i mean is to use all this data from cnf file to configure openssl. Another question is how to execute this command above for csr from C#? I suspect that because on linux C# sdk uses openssl api for all cryptographic operations
 than it should be somehow similar to the C solution. I would be gratefull if someone could point me at least for a C solution of this issue.<br>
<br>
Best regards<br>
Piotr Lobacz<br>
[<a href="https://softgent.com/wp-content/uploads/2020/01/Zasob-14.png%5d%3chttps:/www.softgent.com">https://softgent.com/wp-content/uploads/2020/01/Zasob-14.png]<https://www.softgent.com</a>><br>
<br>
Softgent Sp. z o.o., Budowlanych 31d, 80-298 Gdansk, POLAND<br>
<br>
KRS: 0000674406, NIP: 9581679801, REGON: 367090912<br>
<br>
<a href="http://www.softgent.com">www.softgent.com</a><br>
<br>
Sąd Rejonowy Gdańsk-Północ w Gdańsku, VII Wydział Gospodarczy Krajowego Rejestru Sądowego<br>
<br>
KRS 0000674406, Kapitał zakładowy: 25 000,00 zł wpłacony w całości.</p>
</div>
</div>
</div>
</div>
</body>
</html>