proxy_certificates.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. HOWTO proxy certificates
  2. 0. WARNING
  3. NONE OF THE CODE PRESENTED HERE HAS BEEN CHECKED! The code is just examples to
  4. show you how things could be done. There might be typos or type conflicts, and
  5. you will have to resolve them.
  6. 1. Introduction
  7. Proxy certificates are defined in RFC 3820. They are really usual certificates
  8. with the mandatory extension proxyCertInfo.
  9. Proxy certificates are issued by an End Entity (typically a user), either
  10. directly with the EE certificate as issuing certificate, or by extension through
  11. an already issued proxy certificate. Proxy certificates are used to extend
  12. rights to some other entity (a computer process, typically, or sometimes to the
  13. user itself). This allows the entity to perform operations on behalf of the
  14. owner of the EE certificate.
  15. See http://www.ietf.org/rfc/rfc3820.txt for more information.
  16. 2. A warning about proxy certificates
  17. No one seems to have tested proxy certificates with security in mind. To this
  18. date, it seems that proxy certificates have only been used in a context highly
  19. aware of them.
  20. Existing applications might misbehave when trying to validate a chain of
  21. certificates which use a proxy certificate. They might incorrectly consider the
  22. leaf to be the certificate to check for authorisation data, which is controlled
  23. by the EE certificate owner.
  24. subjectAltName and issuerAltName are forbidden in proxy certificates, and this
  25. is enforced in OpenSSL. The subject must be the same as the issuer, with one
  26. commonName added on.
  27. Possible threats we can think of at this time include:
  28. - impersonation through commonName (think server certificates).
  29. - use of additional extensions, possibly non-standard ones used in certain
  30. environments, that would grant extra or different authorisation rights.
  31. For these reasons, OpenSSL requires that the use of proxy certificates be
  32. explicitly allowed. Currently, this can be done using the following methods:
  33. - if the application directly calls X509_verify_cert(), it can first call:
  34. X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS);
  35. Where ctx is the pointer which then gets passed to X509_verify_cert().
  36. - proxy certificate validation can be enabled before starting the application
  37. by setting the environment variable OPENSSL_ALLOW_PROXY_CERTS.
  38. In the future, it might be possible to enable proxy certificates by editing
  39. openssl.cnf.
  40. 3. How to create proxy certificates
  41. Creating proxy certificates is quite easy, by taking advantage of a lack of
  42. checks in the 'openssl x509' application (*ahem*). You must first create a
  43. configuration section that contains a definition of the proxyCertInfo extension,
  44. for example:
  45. [ v3_proxy ]
  46. # A proxy certificate MUST NEVER be a CA certificate.
  47. basicConstraints=CA:FALSE
  48. # Usual authority key ID
  49. authorityKeyIdentifier=keyid,issuer:always
  50. # The extension which marks this certificate as a proxy
  51. proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:1,policy:text:AB
  52. It's also possible to specify the proxy extension in a separate section:
  53. proxyCertInfo=critical,@proxy_ext
  54. [ proxy_ext ]
  55. language=id-ppl-anyLanguage
  56. pathlen=0
  57. policy=text:BC
  58. The policy value has a specific syntax, {syntag}:{string}, where the syntag
  59. determines what will be done with the string. The following syntags are
  60. recognised:
  61. text indicates that the string is simply bytes, without any encoding:
  62. policy=text:räksmörgås
  63. Previous versions of this design had a specific tag for UTF-8 text.
  64. However, since the bytes are copied as-is anyway, there is no need for
  65. such a specific tag.
  66. hex indicates the string is encoded in hex, with colons between each byte
  67. (every second hex digit):
  68. policy=hex:72:E4:6B:73:6D:F6:72:67:E5:73
  69. Previous versions of this design had a tag to insert a complete DER
  70. blob. However, the only legal use for this would be to surround the
  71. bytes that would go with the hex: tag with whatever is needed to
  72. construct a correct OCTET STRING. The DER tag therefore felt
  73. superfluous, and was removed.
  74. file indicates that the text of the policy should really be taken from a
  75. file. The string is then really a file name. This is useful for
  76. policies that are large (more than a few lines, e.g. XML documents).
  77. The 'policy' setting can be split up in multiple lines like this:
  78. 0.policy=This is
  79. 1.policy= a multi-
  80. 2.policy=line policy.
  81. NOTE: the proxy policy value is the part which determines the rights granted to
  82. the process using the proxy certificate. The value is completely dependent on
  83. the application reading and interpreting it!
  84. Now that you have created an extension section for your proxy certificate, you
  85. can easily create a proxy certificate by doing:
  86. openssl req -new -config openssl.cnf -out proxy.req -keyout proxy.key
  87. openssl x509 -req -CAcreateserial -in proxy.req -days 7 -out proxy.crt \
  88. -CA user.crt -CAkey user.key -extfile openssl.cnf -extensions v3_proxy
  89. You can also create a proxy certificate using another proxy certificate as
  90. issuer (note: I'm using a different configuration section for it):
  91. openssl req -new -config openssl.cnf -out proxy2.req -keyout proxy2.key
  92. openssl x509 -req -CAcreateserial -in proxy2.req -days 7 -out proxy2.crt \
  93. -CA proxy.crt -CAkey proxy.key -extfile openssl.cnf -extensions v3_proxy2
  94. 4. How to have your application interpret the policy?
  95. The basic way to interpret proxy policies is to start with some default rights,
  96. then compute the resulting rights by checking the proxy certificate against
  97. the chain of proxy certificates, user certificate and CA certificates. You then
  98. use the final computed rights. Sounds easy, huh? It almost is.
  99. The slightly complicated part is figuring out how to pass data between your
  100. application and the certificate validation procedure.
  101. You need the following ingredients:
  102. - a callback function that will be called for every certificate being
  103. validated. The callback be called several times for each certificate,
  104. so you must be careful to do the proxy policy interpretation at the right
  105. time. You also need to fill in the defaults when the EE certificate is
  106. checked.
  107. - a data structure that is shared between your application code and the
  108. callback.
  109. - a wrapper function that sets it all up.
  110. - an ex_data index function that creates an index into the generic ex_data
  111. store that is attached to an X509 validation context.
  112. Here is some skeleton code you can fill in:
  113. #include <string.h>
  114. #include <netdb.h>
  115. #include <openssl/x509.h>
  116. #include <openssl/x509v3.h>
  117. #define total_rights 25
  118. /*
  119. * In this example, I will use a view of granted rights as a bit
  120. * array, one bit for each possible right.
  121. */
  122. typedef struct your_rights {
  123. unsigned char rights[(total_rights + 7) / 8];
  124. } YOUR_RIGHTS;
  125. /*
  126. * The following procedure will create an index for the ex_data
  127. * store in the X509 validation context the first time it's called.
  128. * Subsequent calls will return the same index. */
  129. static int get_proxy_auth_ex_data_idx(X509_STORE_CTX *ctx)
  130. {
  131. static volatile int idx = -1;
  132. if (idx < 0) {
  133. X509_STORE_lock(X509_STORE_CTX_get0_store(ctx));
  134. if (idx < 0) {
  135. idx = X509_STORE_CTX_get_ex_new_index(0,
  136. "for verify callback",
  137. NULL,NULL,NULL);
  138. }
  139. X509_STORE_unlock(X509_STORE_CTX_get0_store(ctx));
  140. }
  141. return idx;
  142. }
  143. /* Callback to be given to the X509 validation procedure. */
  144. static int verify_callback(int ok, X509_STORE_CTX *ctx)
  145. {
  146. if (ok == 1) {
  147. /*
  148. * It's REALLY important you keep the proxy policy
  149. * check within this section. It's important to know
  150. * that when ok is 1, the certificates are checked
  151. * from top to bottom. You get the CA root first,
  152. * followed by the possible chain of intermediate
  153. * CAs, followed by the EE certificate, followed by
  154. * the possible proxy certificates.
  155. */
  156. X509 *xs = X509_STORE_CTX_get_current_cert(ctx);
  157. if (X509_get_extension_flags(xs) & EXFLAG_PROXY) {
  158. YOUR_RIGHTS *rights =
  159. (YOUR_RIGHTS *)X509_STORE_CTX_get_ex_data(ctx,
  160. get_proxy_auth_ex_data_idx(ctx));
  161. PROXY_CERT_INFO_EXTENSION *pci =
  162. X509_get_ext_d2i(xs, NID_proxyCertInfo, NULL, NULL);
  163. switch (OBJ_obj2nid(pci->proxyPolicy->policyLanguage)) {
  164. case NID_Independent:
  165. /*
  166. * Do whatever you need to grant explicit rights to
  167. * this particular proxy certificate, usually by
  168. * pulling them from some database. If there are none
  169. * to be found, clear all rights (making this and any
  170. * subsequent proxy certificate void of any rights).
  171. */
  172. memset(rights->rights, 0, sizeof(rights->rights));
  173. break;
  174. case NID_id_ppl_inheritAll:
  175. /*
  176. * This is basically a NOP, we simply let the current
  177. * rights stand as they are.
  178. */
  179. break;
  180. default:
  181. /* This is usually the most complex section of code.
  182. * You really do whatever you want as long as you
  183. * follow RFC 3820. In the example we use here, the
  184. * simplest thing to do is to build another, temporary
  185. * bit array and fill it with the rights granted by
  186. * the current proxy certificate, then use it as a
  187. * mask on the accumulated rights bit array, and
  188. * voilà, you now have a new accumulated rights bit
  189. * array.
  190. */
  191. {
  192. int i;
  193. YOUR_RIGHTS tmp_rights;
  194. memset(tmp_rights.rights, 0, sizeof(tmp_rights.rights));
  195. /*
  196. * process_rights() is supposed to be a procedure
  197. * that takes a string and it's length, interprets
  198. * it and sets the bits in the YOUR_RIGHTS pointed
  199. * at by the third argument.
  200. */
  201. process_rights((char *) pci->proxyPolicy->policy->data,
  202. pci->proxyPolicy->policy->length,
  203. &tmp_rights);
  204. for(i = 0; i < total_rights / 8; i++)
  205. rights->rights[i] &= tmp_rights.rights[i];
  206. }
  207. break;
  208. }
  209. PROXY_CERT_INFO_EXTENSION_free(pci);
  210. } else if (!(X509_get_extension_flags(xs) & EXFLAG_CA)) {
  211. /* We have an EE certificate, let's use it to set default! */
  212. YOUR_RIGHTS *rights =
  213. (YOUR_RIGHTS *)X509_STORE_CTX_get_ex_data(ctx,
  214. get_proxy_auth_ex_data_idx(ctx));
  215. /* The following procedure finds out what rights the owner
  216. * of the current certificate has, and sets them in the
  217. * YOUR_RIGHTS structure pointed at by the second
  218. * argument.
  219. */
  220. set_default_rights(xs, rights);
  221. }
  222. }
  223. return ok;
  224. }
  225. static int my_X509_verify_cert(X509_STORE_CTX *ctx,
  226. YOUR_RIGHTS *needed_rights)
  227. {
  228. int ok;
  229. int (*save_verify_cb)(int ok,X509_STORE_CTX *ctx) =
  230. X509_STORE_CTX_get_verify_cb(ctx);
  231. YOUR_RIGHTS rights;
  232. X509_STORE_CTX_set_verify_cb(ctx, verify_callback);
  233. X509_STORE_CTX_set_ex_data(ctx, get_proxy_auth_ex_data_idx(ctx), &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. }
  242. If you use SSL or TLS, you can easily set up a callback to have the
  243. certificates checked properly, using the code above:
  244. SSL_CTX_set_cert_verify_callback(s_ctx, my_X509_verify_cert, &needed_rights);
  245. --
  246. Richard Levitte