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