proxy-certificates.pod 12 KB

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