proxy_certificates.txt 12 KB

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