proxy-certificates.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?xml version="1.0" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>proxy-certificates</title>
  6. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  7. <link rev="made" href="mailto:root@localhost" />
  8. </head>
  9. <body>
  10. <ul id="index">
  11. <li><a href="#NAME">NAME</a></li>
  12. <li><a href="#DESCRIPTION">DESCRIPTION</a>
  13. <ul>
  14. <li><a href="#Enabling-proxy-certificate-verification">Enabling proxy certificate verification</a></li>
  15. <li><a href="#Creating-proxy-certificates">Creating proxy certificates</a></li>
  16. <li><a href="#Using-proxy-certs-in-applications">Using proxy certs in applications</a></li>
  17. </ul>
  18. </li>
  19. <li><a href="#NOTES">NOTES</a></li>
  20. <li><a href="#SEE-ALSO">SEE ALSO</a></li>
  21. <li><a href="#COPYRIGHT">COPYRIGHT</a></li>
  22. </ul>
  23. <h1 id="NAME">NAME</h1>
  24. <p>proxy-certificates - Proxy certificates in OpenSSL</p>
  25. <h1 id="DESCRIPTION">DESCRIPTION</h1>
  26. <p>Proxy certificates are defined in RFC 3820. They are used to extend rights to some other entity (a computer process, typically, or sometimes to the user itself). This allows the entity to perform operations on behalf of the owner of the EE (End Entity) certificate.</p>
  27. <p>The requirements for a valid proxy certificate are:</p>
  28. <ul>
  29. <li><p>They are issued by an End Entity, either a normal EE certificate, or another proxy certificate.</p>
  30. </li>
  31. <li><p>They must not have the <b>subjectAltName</b> or <b>issuerAltName</b> extensions.</p>
  32. </li>
  33. <li><p>They must have the <b>proxyCertInfo</b> extension.</p>
  34. </li>
  35. <li><p>They must have the subject of their issuer, with one <b>commonName</b> added.</p>
  36. </li>
  37. </ul>
  38. <h2 id="Enabling-proxy-certificate-verification">Enabling proxy certificate verification</h2>
  39. <p>OpenSSL expects applications that want to use proxy certificates to be specially aware of them, and make that explicit. This is done by setting an X509 verification flag:</p>
  40. <pre><code> X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS);</code></pre>
  41. <p>or</p>
  42. <pre><code> X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_ALLOW_PROXY_CERTS);</code></pre>
  43. <p>See <a href="#NOTES">&quot;NOTES&quot;</a> for a discussion on this requirement.</p>
  44. <h2 id="Creating-proxy-certificates">Creating proxy certificates</h2>
  45. <p>Creating proxy certificates can be done using the <a href="../man1/openssl-x509.html">openssl-x509(1)</a> command, with some extra extensions:</p>
  46. <pre><code> [ v3_proxy ]
  47. # A proxy certificate MUST NEVER be a CA certificate.
  48. basicConstraints=CA:FALSE
  49. # Usual authority key ID
  50. authorityKeyIdentifier=keyid,issuer:always
  51. # The extension which marks this certificate as a proxy
  52. proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:1,policy:text:AB</code></pre>
  53. <p>It&#39;s also possible to specify the proxy extension in a separate section:</p>
  54. <pre><code> proxyCertInfo=critical,@proxy_ext
  55. [ proxy_ext ]
  56. language=id-ppl-anyLanguage
  57. pathlen=0
  58. policy=text:BC</code></pre>
  59. <p>The policy value has a specific syntax, <i>syntag</i>:<i>string</i>, where the <i>syntag</i> determines what will be done with the string. The following <i>syntag</i>s are recognised:</p>
  60. <dl>
  61. <dt id="text"><b>text</b></dt>
  62. <dd>
  63. <p>indicates that the string is a byte sequence, without any encoding:</p>
  64. <pre><code> policy=text:r&auml;ksm&ouml;rg&aring;s</code></pre>
  65. </dd>
  66. <dt id="hex"><b>hex</b></dt>
  67. <dd>
  68. <p>indicates the string is encoded hexadecimal encoded binary data, with colons between each byte (every second hex digit):</p>
  69. <pre><code> policy=hex:72:E4:6B:73:6D:F6:72:67:E5:73</code></pre>
  70. </dd>
  71. <dt id="file"><b>file</b></dt>
  72. <dd>
  73. <p>indicates that the text of the policy should be taken from a file. The string is then a filename. This is useful for policies that are large (more than a few lines, e.g. XML documents).</p>
  74. </dd>
  75. </dl>
  76. <p><i>NOTE: The proxy policy value is what determines the rights granted to the process during the proxy certificate. It&#39;s up to the application to interpret and combine these policies.</i></p>
  77. <p>With a proxy extension, creating a proxy certificate is a matter of two commands:</p>
  78. <pre><code> openssl req -new -config proxy.cnf \
  79. -out proxy.req -keyout proxy.key \
  80. -subj &quot;/DC=org/DC=openssl/DC=users/CN=proxy 1&quot;
  81. openssl x509 -req -CAcreateserial -in proxy.req -out proxy.crt \
  82. -CA user.crt -CAkey user.key -days 7 \
  83. -extfile proxy.cnf -extensions v3_proxy1</code></pre>
  84. <p>You can also create a proxy certificate using another proxy certificate as issuer (note: using a different configuration section for the proxy extensions):</p>
  85. <pre><code> openssl req -new -config proxy.cnf \
  86. -out proxy2.req -keyout proxy2.key \
  87. -subj &quot;/DC=org/DC=openssl/DC=users/CN=proxy 1/CN=proxy 2&quot;
  88. openssl x509 -req -CAcreateserial -in proxy2.req -out proxy2.crt \
  89. -CA proxy.crt -CAkey proxy.key -days 7 \
  90. -extfile proxy.cnf -extensions v3_proxy2</code></pre>
  91. <h2 id="Using-proxy-certs-in-applications">Using proxy certs in applications</h2>
  92. <p>To interpret proxy policies, the application would normally start with some default rights (perhaps none at all), then compute the resulting rights by checking the rights against the chain of proxy certificates, user certificate and CA certificates.</p>
  93. <p>The complicated part is figuring out how to pass data between your application and the certificate validation procedure.</p>
  94. <p>The following ingredients are needed for such processing:</p>
  95. <ul>
  96. <li><p>a callback function that will be called for every certificate being validated. The callback is called several times for each certificate, so you must be careful to do the proxy policy interpretation at the right time. You also need to fill in the defaults when the EE certificate is checked.</p>
  97. </li>
  98. <li><p>a data structure that is shared between your application code and the callback.</p>
  99. </li>
  100. <li><p>a wrapper function that sets it all up.</p>
  101. </li>
  102. <li><p>an ex_data index function that creates an index into the generic ex_data store that is attached to an X509 validation context.</p>
  103. </li>
  104. </ul>
  105. <p>The following skeleton code can be used as a starting point:</p>
  106. <pre><code> #include &lt;string.h&gt;
  107. #include &lt;netdb.h&gt;
  108. #include &lt;openssl/x509.h&gt;
  109. #include &lt;openssl/x509v3.h&gt;
  110. #define total_rights 25
  111. /*
  112. * In this example, I will use a view of granted rights as a bit
  113. * array, one bit for each possible right.
  114. */
  115. typedef struct your_rights {
  116. unsigned char rights[(total_rights + 7) / 8];
  117. } YOUR_RIGHTS;
  118. /*
  119. * The following procedure will create an index for the ex_data
  120. * store in the X509 validation context the first time it&#39;s
  121. * called. Subsequent calls will return the same index.
  122. */
  123. static int get_proxy_auth_ex_data_idx(X509_STORE_CTX *ctx)
  124. {
  125. static volatile int idx = -1;
  126. if (idx &lt; 0) {
  127. X509_STORE_lock(X509_STORE_CTX_get0_store(ctx));
  128. if (idx &lt; 0) {
  129. idx = X509_STORE_CTX_get_ex_new_index(0,
  130. &quot;for verify callback&quot;,
  131. NULL,NULL,NULL);
  132. }
  133. X509_STORE_unlock(X509_STORE_CTX_get0_store(ctx));
  134. }
  135. return idx;
  136. }
  137. /* Callback to be given to the X509 validation procedure. */
  138. static int verify_callback(int ok, X509_STORE_CTX *ctx)
  139. {
  140. if (ok == 1) {
  141. /*
  142. * It&#39;s REALLY important you keep the proxy policy check
  143. * within this section. It&#39;s important to know that when
  144. * ok is 1, the certificates are checked from top to
  145. * bottom. You get the CA root first, followed by the
  146. * possible chain of intermediate CAs, followed by the EE
  147. * certificate, followed by the possible proxy
  148. * certificates.
  149. */
  150. X509 *xs = X509_STORE_CTX_get_current_cert(ctx);
  151. if (X509_get_extension_flags(xs) &amp; EXFLAG_PROXY) {
  152. YOUR_RIGHTS *rights =
  153. (YOUR_RIGHTS *)X509_STORE_CTX_get_ex_data(ctx,
  154. get_proxy_auth_ex_data_idx(ctx));
  155. PROXY_CERT_INFO_EXTENSION *pci =
  156. X509_get_ext_d2i(xs, NID_proxyCertInfo, NULL, NULL);
  157. switch (OBJ_obj2nid(pci-&gt;proxyPolicy-&gt;policyLanguage)) {
  158. case NID_Independent:
  159. /*
  160. * Do whatever you need to grant explicit rights
  161. * to this particular proxy certificate, usually
  162. * by pulling them from some database. If there
  163. * are none to be found, clear all rights (making
  164. * this and any subsequent proxy certificate void
  165. * of any rights).
  166. */
  167. memset(rights-&gt;rights, 0, sizeof(rights-&gt;rights));
  168. break;
  169. case NID_id_ppl_inheritAll:
  170. /*
  171. * This is basically a NOP, we simply let the
  172. * current rights stand as they are.
  173. */
  174. break;
  175. default:
  176. /*
  177. * This is usually the most complex section of
  178. * code. You really do whatever you want as long
  179. * as you follow RFC 3820. In the example we use
  180. * here, the simplest thing to do is to build
  181. * another, temporary bit array and fill it with
  182. * the rights granted by the current proxy
  183. * certificate, then use it as a mask on the
  184. * accumulated rights bit array, and voil&agrave;, you
  185. * now have a new accumulated rights bit array.
  186. */
  187. {
  188. int i;
  189. YOUR_RIGHTS tmp_rights;
  190. memset(tmp_rights.rights, 0,
  191. sizeof(tmp_rights.rights));
  192. /*
  193. * process_rights() is supposed to be a
  194. * procedure that takes a string and its
  195. * length, interprets it and sets the bits
  196. * in the YOUR_RIGHTS pointed at by the
  197. * third argument.
  198. */
  199. process_rights((char *) pci-&gt;proxyPolicy-&gt;policy-&gt;data,
  200. pci-&gt;proxyPolicy-&gt;policy-&gt;length,
  201. &amp;tmp_rights);
  202. for(i = 0; i &lt; total_rights / 8; i++)
  203. rights-&gt;rights[i] &amp;= tmp_rights.rights[i];
  204. }
  205. break;
  206. }
  207. PROXY_CERT_INFO_EXTENSION_free(pci);
  208. } else if (!(X509_get_extension_flags(xs) &amp; EXFLAG_CA)) {
  209. /* We have an EE certificate, let&#39;s use it to set default! */
  210. YOUR_RIGHTS *rights =
  211. (YOUR_RIGHTS *)X509_STORE_CTX_get_ex_data(ctx,
  212. get_proxy_auth_ex_data_idx(ctx));
  213. /*
  214. * The following procedure finds out what rights the
  215. * owner of the current certificate has, and sets them
  216. * in the YOUR_RIGHTS structure pointed at by the
  217. * second argument.
  218. */
  219. set_default_rights(xs, rights);
  220. }
  221. }
  222. return ok;
  223. }
  224. static int my_X509_verify_cert(X509_STORE_CTX *ctx,
  225. YOUR_RIGHTS *needed_rights)
  226. {
  227. int ok;
  228. int (*save_verify_cb)(int ok,X509_STORE_CTX *ctx) =
  229. X509_STORE_CTX_get_verify_cb(ctx);
  230. YOUR_RIGHTS rights;
  231. X509_STORE_CTX_set_verify_cb(ctx, verify_callback);
  232. X509_STORE_CTX_set_ex_data(ctx, get_proxy_auth_ex_data_idx(ctx),
  233. &amp;rights);
  234. X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS);
  235. ok = X509_verify_cert(ctx);
  236. if (ok == 1) {
  237. ok = check_needed_rights(rights, needed_rights);
  238. }
  239. X509_STORE_CTX_set_verify_cb(ctx, save_verify_cb);
  240. return ok;
  241. }</code></pre>
  242. <p>If you use SSL or TLS, you can easily set up a callback to have the certificates checked properly, using the code above:</p>
  243. <pre><code> SSL_CTX_set_cert_verify_callback(s_ctx, my_X509_verify_cert,
  244. &amp;needed_rights);</code></pre>
  245. <h1 id="NOTES">NOTES</h1>
  246. <p>To this date, it seems that proxy certificates have only been used in environments that are aware of them, and no one seems to have investigated how they can be used or misused outside of such an environment.</p>
  247. <p>For that reason, OpenSSL requires that applications aware of proxy certificates must also make that explicit.</p>
  248. <p><b>subjectAltName</b> and <b>issuerAltName</b> are forbidden in proxy certificates, and this is enforced in OpenSSL. The subject must be the same as the issuer, with one commonName added on.</p>
  249. <h1 id="SEE-ALSO">SEE ALSO</h1>
  250. <p><a href="../man3/X509_STORE_CTX_set_flags.html">X509_STORE_CTX_set_flags(3)</a>, <a href="../man3/X509_STORE_CTX_set_verify_cb.html">X509_STORE_CTX_set_verify_cb(3)</a>, <a href="../man3/X509_VERIFY_PARAM_set_flags.html">X509_VERIFY_PARAM_set_flags(3)</a>, <a href="../man3/SSL_CTX_set_cert_verify_callback.html">SSL_CTX_set_cert_verify_callback(3)</a>, <a href="../man1/openssl-req.html">openssl-req(1)</a>, <a href="../man1/openssl-x509.html">openssl-x509(1)</a>, <a href="https://tools.ietf.org/html/rfc3820">RFC 3820</a></p>
  251. <h1 id="COPYRIGHT">COPYRIGHT</h1>
  252. <p>Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.</p>
  253. <p>Licensed under the Apache License 2.0 (the &quot;License&quot;). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <a href="https://www.openssl.org/source/license.html">https://www.openssl.org/source/license.html</a>.</p>
  254. </body>
  255. </html>