<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Dear OpenSSL,</p>
    <p>I have a question for the community. Specifically, I am changing
      the implementation that we are working on for Composite Crypto
      from directly patching the OpenSSL library with a new method, we
      want to add it dynamically - this makes it easier to use Composite
      Crypto with existing OpenSSL deployments.</p>
    <p>One very discouraging thing when you do that is that with all the
      masking of data structures, it becomes quite difficult to work on
      low-level implementations and it might require including some
      definitions of data structures.</p>
    <p>Besides this side-notes, the issue we are facing is related to
      how to link the OID for the public key algorithm with the NID for
      the dynamically added one. Let me explain with some code.</p>
    <p>When our LibPKI starts up, it initializes the crypto layer and
      adds the Composite method by using the EVP_PKEY_ASN1_METHOD and
      EVP_PKEY_METHOD:</p>
    <div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Menlo, Monaco, 'Courier New', monospace;font-weight: normal;font-size: 9px;line-height: 14px;white-space: pre;"><div><span style="color: #6a9955;">    // We Need to initialize the ASN1 conversion method</span></div><div><span style="color: #d4d4d4;">    </span><span style="color: #6a9955;">// <a class="moz-txt-link-freetext" href="https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_ASN1_METHOD.html" moz-do-not-send="true">https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_ASN1_METHOD.html</a></span></div><div><span style="color: #d4d4d4;">    </span><span style="color: #569cd6;">if</span><span style="color: #d4d4d4;"> (!EVP_PKEY_asn1_add0(&combined_asn1_meth)) </span><span style="color: #569cd6;">return</span><span style="color: #d4d4d4;"> </span><span style="color: #b5cea8;">0</span><span style="color: #d4d4d4;">;</span></div>
<div><span style="color: #d4d4d4;">    </span><span style="color: #6a9955;">// We also Need to initialize the PKEY method for the algorithm</span></div><div><span style="color: #d4d4d4;">    </span><span style="color: #6a9955;">// <a class="moz-txt-link-freetext" href="https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_METHOD.html" moz-do-not-send="true">https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_METHOD.html</a></span></div><div><span style="color: #d4d4d4;">    </span><span style="color: #569cd6;">if</span><span style="color: #d4d4d4;"> (!EVP_PKEY_meth_add0(&combined_pkey_meth)) </span><span style="color: #569cd6;">return</span><span style="color: #d4d4d4;"> </span><span style="color: #b5cea8;">0</span><span style="color: #d4d4d4;">;</span></div></div>
    <div class="moz-signature"><br>
    </div>
    <div class="moz-signature">However, as part of the two structures,
      the pkey NID is already defined in the structure (pkey_id). This
      was all working fine because the pkey_id was the NID_composite
      that was originally generated via the objects tools (i.e., you can
      use the two functions OBJ_nid2obj() and OBJ_obj2nid() with the
      NID_composite value), however... I cannot find how to do this with
      the static version.</div>
    <div class="moz-signature"><br>
    </div>
    <p>I tried to create the object with the OBJ_create() first and then
      assign it to the relevant fields of the methods:</p>
    <div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Menlo, Monaco, 'Courier New', monospace;font-weight: normal;font-size: 9px;line-height: 14px;white-space: pre;"><div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Menlo, Monaco, 'Courier New', monospace;font-weight: normal;font-size: 9px;line-height: 14px;white-space: pre;"><div style="color: #d4d4d4;background-color: #1e1e1e;font-family: Menlo, Monaco, 'Courier New', monospace;font-weight: normal;font-size: 9px;line-height: 14px;white-space: pre;"><div><span style="color: #d4d4d4;">    </span><span style="color: #6a9955;">// Let's create the Initial OID for Composite Crypto</span></div><div><span style="color: #d4d4d4;">    NID_composite = OBJ_create(</span><span style="color: #ce9178;">"1.3.6.1.4.1.18277.2.1"</span><span style="color: #d4d4d4;">, </span><span style="color: #ce9178;">"composite"</span><span style="color: #d4d4d4;">, </span><span style="color: #ce9178;">"pk-Composite"</span><span style="color: #d4d4d4;">);</span></div><div><span style="color: #d4d4d4;">    </span><span style="color: #569cd6;">if</span><span style="color: #d4d4d4;"> (NID_composite == NID_undef) </span><span style="color: #569cd6;">return</span><span style="color: #d4d4d4;"> </span><span style="color: #b5cea8;">0</span><span style="color: #d4d4d4;">;</span></div>
<div><span style="color: #d4d4d4;">    </span><span style="color: #6a9955;">// // Assigns the generated IDs</span></div><div><span style="color: #d4d4d4;">    composite_asn1_meth.pkey_id = NID_composite;</span></div><div><span style="color: #d4d4d4;">    composite_asn1_meth.pkey_base_id = NID_composite;</span></div><div><span style="color: #d4d4d4;">    composite_pkey_meth.pkey_id = NID_composite;</span></div></div></div></div>
    <div class="moz-signature"><br>
    </div>
    <div class="moz-signature">right before adding the method(s) to the
      library with the EVP_PKEY_meth_add0() and EVP_PKEY_asn1_add0().
      This seems a bit clunky to me and I am facing some weird memory
      issue when I assign the pkey_id on the pkey meth (but that can
      simply be an issue with a pointer somehow... ).<br>
    </div>
    <div class="moz-signature"><br>
    </div>
    <div class="moz-signature">I was wondering if there is a better
      approach.</div>
    <div class="moz-signature"><br>
    </div>
    <div class="moz-signature">Specifically, I was thinking about
      generating the methods data structures with the dynamic allocation
      (EVP_PKEY_meth_new) and then assigning the different callbacks
      instead of using the _add0() functions ... however also that
      approach requires us, if I am not mistaken, to generate the OIDs
      first, retrieve the ID from it, and then use the
      EVP_PKEY_meth_new()/EVP_PKEY_asn1_new() to generate the new
      methods. This second approach requires a bit more code to do all
      the assigning (instead of just assigning the structure once) but
      it helps with not requiring exporting the internals of the
      method(s) structure itself.</div>
    <div class="moz-signature"><br>
    </div>
    <div class="moz-signature">Is there a better way to provide the
      algorithm?</div>
    <div class="moz-signature"><br>
    </div>
    <div class="moz-signature">The last path that I was thinking was to
      provide an ENGINE implementation, but that seemed a bit more
      complicated (probably mostly because I have never had to implement
      the interface...).</div>
    <div class="moz-signature"><br>
    </div>
    <p>Thank you for your help and have a wonderful day!</p>
    <div class="moz-signature">Cheers,<br>
      Max</div>
    <div class="moz-signature"><br>
    </div>
    <div class="moz-signature">-- <br>
      <div style="color: black; margin-top: 10px;"> Best Regards,
        <div style="margin-top: 5px; margin-left: 0px; "> Massimiliano
          Pala, Ph.D.<br>
          OpenCA Labs Director<br>
        </div>
        <img src="cid:part1.et9QmD7U.U4ZTHiTM@openca.org"
          style="vertical-align: 0px; margin-top: 10px; margin-left:
          0px;" alt="OpenCA Logo" class=""><br>
      </div>
    </div>
  </body>
</html>