blob: 1eed876f8235d864726d0036fd7135b7332ec9e1 [file] [log] [blame]
Jonathan Ballet6381da32011-07-20 16:43:38 +09001.. _openssl-ssl:
2
3:py:mod:`SSL` --- An interface to the SSL-specific parts of OpenSSL
4===================================================================
5
Jonathan Balletc9e066c2011-07-17 22:56:05 +09006.. py:module:: OpenSSL.SSL
Jonathan Ballet6381da32011-07-20 16:43:38 +09007 :synopsis: An interface to the SSL-specific parts of OpenSSL
Jonathan Balletc9e066c2011-07-17 22:56:05 +09008
Jonathan Balletc9e066c2011-07-17 22:56:05 +09009
10This module handles things specific to SSL. There are two objects defined:
11Context, Connection.
12
13.. py:data:: SSLv2_METHOD
Jonathan Ballet6381da32011-07-20 16:43:38 +090014 SSLv3_METHOD
15 SSLv23_METHOD
16 TLSv1_METHOD
Jonathan Balletc9e066c2011-07-17 22:56:05 +090017
18 These constants represent the different SSL methods to use when creating a
19 context object.
20
21
22.. py:data:: VERIFY_NONE
Jonathan Ballet6381da32011-07-20 16:43:38 +090023 VERIFY_PEER
24 VERIFY_FAIL_IF_NO_PEER_CERT
Jonathan Balletc9e066c2011-07-17 22:56:05 +090025
26 These constants represent the verification mode used by the Context
27 object's :py:meth:`set_verify` method.
28
29
30.. py:data:: FILETYPE_PEM
Jonathan Ballet6381da32011-07-20 16:43:38 +090031 FILETYPE_ASN1
Jonathan Balletc9e066c2011-07-17 22:56:05 +090032
33 File type constants used with the :py:meth:`use_certificate_file` and
34 :py:meth:`use_privatekey_file` methods of Context objects.
35
36
37.. py:data:: OP_SINGLE_DH_USE
Jonathan Ballet6381da32011-07-20 16:43:38 +090038 OP_EPHEMERAL_RSA
39 OP_NO_SSLv2
40 OP_NO_SSLv3
41 OP_NO_TLSv1
Jean-Paul Calderone21641542011-09-11 09:18:14 -040042 OP_NO_TICKET
43 OP_NO_COMPRESSION
Jonathan Balletc9e066c2011-07-17 22:56:05 +090044
45 Constants used with :py:meth:`set_options` of Context objects.
46
47 :py:const:`OP_SINGLE_DH_USE` means to always create a new key when using
48 ephemeral Diffie-Hellman. :py:const:`OP_EPHEMERAL_RSA` means to always use
49 ephemeral RSA keys when doing RSA operations. :py:const:`OP_NO_SSLv2`,
50 :py:const:`OP_NO_SSLv3` and :py:const:`OP_NO_TLSv1` means to disable those
51 specific protocols. This is interesting if you're using e.g.
52 :py:const:`SSLv23_METHOD` to get an SSLv2-compatible handshake, but don't want
53 to use SSLv2.
54
55
Jean-Paul Calderone21641542011-09-11 09:18:14 -040056.. py:data:: MODE_NO_COMPRESSION
57
58 Constant used with :py:meth:`set_mode` of Context objects to disable
59 automatic compression of application traffic.
60
61
Jonathan Balletc9e066c2011-07-17 22:56:05 +090062.. py:data:: SSLEAY_VERSION
Jonathan Ballet6381da32011-07-20 16:43:38 +090063 SSLEAY_CFLAGS
64 SSLEAY_BUILT_ON
65 SSLEAY_PLATFORM
66 SSLEAY_DIR
Jonathan Balletc9e066c2011-07-17 22:56:05 +090067
68 Constants used with :py:meth:`SSLeay_version` to specify what OpenSSL version
69 information to retrieve. See the man page for the :py:func:`SSLeay_version` C
70 API for details.
71
72
73.. py:data:: OPENSSL_VERSION_NUMBER
74
75 An integer giving the version number of the OpenSSL library used to build this
76 version of pyOpenSSL. See the man page for the :py:func:`SSLeay_version` C API
77 for details.
78
79
80.. py:function:: SSLeay_version(type)
81
82 Retrieve a string describing some aspect of the underlying OpenSSL version. The
83 type passed in should be one of the :py:const:`SSLEAY_*` constants defined in
84 this module.
85
86
87.. py:data:: ContextType
88
89 See :py:class:`Context`.
90
91
92.. py:class:: Context(method)
93
94 A class representing SSL contexts. Contexts define the parameters of one or
95 more SSL connections.
96
97 *method* should be :py:const:`SSLv2_METHOD`, :py:const:`SSLv3_METHOD`,
98 :py:const:`SSLv23_METHOD` or :py:const:`TLSv1_METHOD`.
99
100
101.. py:data:: ConnectionType
102
103 See :py:class:`Connection`.
104
105
106.. py:class:: Connection(context, socket)
107
108 A class representing SSL connections.
109
110 *context* should be an instance of :py:class:`Context` and *socket*
111 should be a socket [#connection-context-socket]_ object. *socket* may be
112 *None*; in this case, the Connection is created with a memory BIO: see
113 the :py:meth:`bio_read`, :py:meth:`bio_write`, and :py:meth:`bio_shutdown`
114 methods.
115
116.. py:exception:: Error
117
118 This exception is used as a base class for the other SSL-related
119 exceptions, but may also be raised directly.
120
121 Whenever this exception is raised directly, it has a list of error messages
122 from the OpenSSL error queue, where each item is a tuple *(lib, function,
123 reason)*. Here *lib*, *function* and *reason* are all strings, describing
124 where and what the problem is. See :manpage:`err(3)` for more information.
125
126
127.. py:exception:: ZeroReturnError
128
129 This exception matches the error return code
130 :py:data:`SSL_ERROR_ZERO_RETURN`, and is raised when the SSL Connection has
131 been closed. In SSL 3.0 and TLS 1.0, this only occurs if a closure alert has
132 occurred in the protocol, i.e. the connection has been closed cleanly. Note
133 that this does not necessarily mean that the transport layer (e.g. a socket)
134 has been closed.
135
136 It may seem a little strange that this is an exception, but it does match an
137 :py:data:`SSL_ERROR` code, and is very convenient.
138
139
140.. py:exception:: WantReadError
141
142 The operation did not complete; the same I/O method should be called again
143 later, with the same arguments. Any I/O method can lead to this since new
144 handshakes can occur at any time.
145
146 The wanted read is for **dirty** data sent over the network, not the
147 **clean** data inside the tunnel. For a socket based SSL connection,
148 **read** means data coming at us over the network. Until that read
149 succeeds, the attempted :py:meth:`OpenSSL.SSL.Connection.recv`,
150 :py:meth:`OpenSSL.SSL.Connection.send`, or
151 :py:meth:`OpenSSL.SSL.Connection.do_handshake` is prevented or incomplete. You
152 probably want to :py:meth:`select()` on the socket before trying again.
153
154
155.. py:exception:: WantWriteError
156
157 See :py:exc:`WantReadError`. The socket send buffer may be too full to
158 write more data.
159
160
161.. py:exception:: WantX509LookupError
162
163 The operation did not complete because an application callback has asked to be
164 called again. The I/O method should be called again later, with the same
165 arguments.
166
167 .. note:: This won't occur in this version, as there are no such
168 callbacks in this version.
169
170
171.. py:exception:: SysCallError
172
173 The :py:exc:`SysCallError` occurs when there's an I/O error and OpenSSL's
174 error queue does not contain any information. This can mean two things: An
175 error in the transport protocol, or an end of file that violates the protocol.
176 The parameter to the exception is always a pair *(errnum,
177 errstr)*.
178
179
180
181.. _openssl-context:
182
183Context objects
184---------------
185
186Context objects have the following methods:
187
188.. :py:class:: OpenSSL.SSL.Context
189
190.. py:method:: Context.check_privatekey()
191
192 Check if the private key (loaded with :py:meth:`use_privatekey`) matches the
193 certificate (loaded with :py:meth:`use_certificate`). Returns
194 :py:data:`None` if they match, raises :py:exc:`Error` otherwise.
195
196
197.. py:method:: Context.get_app_data()
198
199 Retrieve application data as set by :py:meth:`set_app_data`.
200
201
202.. py:method:: Context.get_cert_store()
203
204 Retrieve the certificate store (a X509Store object) that the context uses.
205 This can be used to add "trusted" certificates without using the.
206 :py:meth:`load_verify_locations` method.
207
208
209.. py:method:: Context.get_timeout()
210
211 Retrieve session timeout, as set by :py:meth:`set_timeout`. The default is 300
212 seconds.
213
214
215.. py:method:: Context.get_verify_depth()
216
217 Retrieve the Context object's verify depth, as set by
218 :py:meth:`set_verify_depth`.
219
220
221.. py:method:: Context.get_verify_mode()
222
223 Retrieve the Context object's verify mode, as set by :py:meth:`set_verify`.
224
225
226.. py:method:: Context.load_client_ca(pemfile)
227
228 Read a file with PEM-formatted certificates that will be sent to the client
229 when requesting a client certificate.
230
231
232.. py:method:: Context.set_client_ca_list(certificate_authorities)
233
234 Replace the current list of preferred certificate signers that would be
235 sent to the client when requesting a client certificate with the
236 *certificate_authorities* sequence of :py:class:`OpenSSL.crypto.X509Name`'s.
237
238 .. versionadded:: 0.10
239
240
241.. py:method:: Context.add_client_ca(certificate_authority)
242
243 Extract a :py:class:`OpenSSL.crypto.X509Name` from the *certificate_authority*
244 :py:class:`OpenSSL.crypto.X509` certificate and add it to the list of preferred
245 certificate signers sent to the client when requesting a client certificate.
246
247 .. versionadded:: 0.10
248
249
250.. py:method:: Context.load_verify_locations(pemfile, capath)
251
252 Specify where CA certificates for verification purposes are located. These
253 are trusted certificates. Note that the certificates have to be in PEM
254 format. If capath is passed, it must be a directory prepared using the
Jonathan Ballet6381da32011-07-20 16:43:38 +0900255 ``c_rehash`` tool included with OpenSSL. Either, but not both, of
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900256 *pemfile* or *capath* may be :py:data:`None`.
257
258
259.. py:method:: Context.set_default_verify_paths()
260
261 Specify that the platform provided CA certificates are to be used for
262 verification purposes. This method may not work properly on OS X.
263
264
265.. py:method:: Context.load_tmp_dh(dhfile)
266
267 Load parameters for Ephemeral Diffie-Hellman from *dhfile*.
268
269
270.. py:method:: Context.set_app_data(data)
271
272 Associate *data* with this Context object. *data* can be retrieved
273 later using the :py:meth:`get_app_data` method.
274
275
276.. py:method:: Context.set_cipher_list(ciphers)
277
278 Set the list of ciphers to be used in this context. See the OpenSSL manual for
279 more information (e.g. :manpage:`ciphers(1)`)
280
281
282.. py:method:: Context.set_info_callback(callback)
283
284 Set the information callback to *callback*. This function will be called
285 from time to time during SSL handshakes.
286
Jonathan Ballet6381da32011-07-20 16:43:38 +0900287 *callback* should take three arguments: a Connection object and two integers.
288 The first integer specifies where in the SSL handshake the function was
289 called, and the other the return code from a (possibly failed) internal
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900290 function call.
291
292
293.. py:method:: Context.set_options(options)
294
295 Add SSL options. Options you have set before are not cleared!
296 This method should be used with the :py:const:`OP_*` constants.
297
298
Jean-Paul Calderone21641542011-09-11 09:18:14 -0400299.. py:method:: Context.set_mode(mode)
300
301 Add SSL mode. Modes you have set before are not cleared! This method should
302 be used with the :py:const:`MODE_*` constants.
303
304
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900305.. py:method:: Context.set_passwd_cb(callback[, userdata])
306
307 Set the passphrase callback to *callback*. This function will be called
308 when a private key with a passphrase is loaded. *callback* must accept
309 three positional arguments. First, an integer giving the maximum length of
310 the passphrase it may return. If the returned passphrase is longer than
311 this, it will be truncated. Second, a boolean value which will be true if
312 the user should be prompted for the passphrase twice and the callback should
313 verify that the two values supplied are equal. Third, the value given as the
314 *userdata* parameter to :py:meth:`set_passwd_cb`. If an error occurs,
315 *callback* should return a false value (e.g. an empty string).
316
317
318.. py:method:: Context.set_session_id(name)
319
320 Set the context *name* within which a session can be reused for this
321 Context object. This is needed when doing session resumption, because there is
322 no way for a stored session to know which Context object it is associated with.
323 *name* may be any binary data.
324
325
326.. py:method:: Context.set_timeout(timeout)
327
328 Set the timeout for newly created sessions for this Context object to
329 *timeout*. *timeout* must be given in (whole) seconds. The default
330 value is 300 seconds. See the OpenSSL manual for more information (e.g.
331 :manpage:`SSL_CTX_set_timeout(3)`).
332
333
334.. py:method:: Context.set_verify(mode, callback)
335
336 Set the verification flags for this Context object to *mode* and specify
337 that *callback* should be used for verification callbacks. *mode* should be
338 one of :py:const:`VERIFY_NONE` and :py:const:`VERIFY_PEER`. If
339 :py:const:`VERIFY_PEER` is used, *mode* can be OR:ed with
340 :py:const:`VERIFY_FAIL_IF_NO_PEER_CERT` and :py:const:`VERIFY_CLIENT_ONCE`
341 to further control the behaviour.
342
343 *callback* should take five arguments: A Connection object, an X509 object,
344 and three integer variables, which are in turn potential error number, error
345 depth and return code. *callback* should return true if verification passes
346 and false otherwise.
347
348
349.. py:method:: Context.set_verify_depth(depth)
350
351 Set the maximum depth for the certificate chain verification that shall be
352 allowed for this Context object.
353
354
355.. py:method:: Context.use_certificate(cert)
356
357 Use the certificate *cert* which has to be a X509 object.
358
359
360.. py:method:: Context.add_extra_chain_cert(cert)
361
362 Adds the certificate *cert*, which has to be a X509 object, to the
363 certificate chain presented together with the certificate.
364
365
366.. py:method:: Context.use_certificate_chain_file(file)
367
368 Load a certificate chain from *file* which must be PEM encoded.
369
370
371.. py:method:: Context.use_privatekey(pkey)
372
373 Use the private key *pkey* which has to be a PKey object.
374
375
376.. py:method:: Context.use_certificate_file(file[, format])
377
378 Load the first certificate found in *file*. The certificate must be in the
379 format specified by *format*, which is either :py:const:`FILETYPE_PEM` or
380 :py:const:`FILETYPE_ASN1`. The default is :py:const:`FILETYPE_PEM`.
381
382
383.. py:method:: Context.use_privatekey_file(file[, format])
384
385 Load the first private key found in *file*. The private key must be in the
386 format specified by *format*, which is either :py:const:`FILETYPE_PEM` or
387 :py:const:`FILETYPE_ASN1`. The default is :py:const:`FILETYPE_PEM`.
388
389
390.. py:method:: Context.set_tlsext_servername_callback(callback)
391
392 Specify a one-argument callable to use as the TLS extension server name
Jonathan Ballet6381da32011-07-20 16:43:38 +0900393 callback. When a connection using the server name extension is made using
394 this context, the callback will be invoked with the :py:class:`Connection`
395 instance.
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900396
397 .. versionadded:: 0.13
398
399
400.. _openssl-connection:
401
402Connection objects
403------------------
404
405Connection objects have the following methods:
406
407.. py:method:: Connection.accept()
408
409 Call the :py:meth:`accept` method of the underlying socket and set up SSL on the
410 returned socket, using the Context object supplied to this Connection object at
411 creation. Returns a pair *(conn, address)*. where *conn* is the new
412 Connection object created, and *address* is as returned by the socket's
413 :py:meth:`accept`.
414
415
416.. py:method:: Connection.bind(address)
417
418 Call the :py:meth:`bind` method of the underlying socket.
419
420
421.. py:method:: Connection.close()
422
423 Call the :py:meth:`close` method of the underlying socket. Note: If you want
424 correct SSL closure, you need to call the :py:meth:`shutdown` method first.
425
426
427.. py:method:: Connection.connect(address)
428
429 Call the :py:meth:`connect` method of the underlying socket and set up SSL on the
430 socket, using the Context object supplied to this Connection object at
431 creation.
432
433
434.. py:method:: Connection.connect_ex(address)
435
436 Call the :py:meth:`connect_ex` method of the underlying socket and set up SSL on
437 the socket, using the Context object supplied to this Connection object at
438 creation. Note that if the :py:meth:`connect_ex` method of the socket doesn't
439 return 0, SSL won't be initialized.
440
441
442.. py:method:: Connection.do_handshake()
443
444 Perform an SSL handshake (usually called after :py:meth:`renegotiate` or one of
445 :py:meth:`set_accept_state` or :py:meth:`set_accept_state`). This can raise the
446 same exceptions as :py:meth:`send` and :py:meth:`recv`.
447
448
449.. py:method:: Connection.fileno()
450
451 Retrieve the file descriptor number for the underlying socket.
452
453
454.. py:method:: Connection.listen(backlog)
455
456 Call the :py:meth:`listen` method of the underlying socket.
457
458
459.. py:method:: Connection.get_app_data()
460
461 Retrieve application data as set by :py:meth:`set_app_data`.
462
463
464.. py:method:: Connection.get_cipher_list()
465
466 Retrieve the list of ciphers used by the Connection object. WARNING: This API
467 has changed. It used to take an optional parameter and just return a string,
468 but not it returns the entire list in one go.
469
470
471.. py:method:: Connection.get_client_ca_list()
472
473 Retrieve the list of preferred client certificate issuers sent by the server
474 as :py:class:`OpenSSL.crypto.X509Name` objects.
475
476 If this is a client :py:class:`Connection`, the list will be empty until the
477 connection with the server is established.
478
479 If this is a server :py:class:`Connection`, return the list of certificate
480 authorities that will be sent or has been sent to the client, as controlled
481 by this :py:class:`Connection`'s :py:class:`Context`.
482
483 .. versionadded:: 0.10
484
485
486.. py:method:: Connection.get_context()
487
488 Retrieve the Context object associated with this Connection.
489
490
491.. py:method:: Connection.set_context(context)
492
493 Specify a replacement Context object for this Connection.
494
495
496.. py:method:: Connection.get_peer_certificate()
497
498 Retrieve the other side's certificate (if any)
499
500
501.. py:method:: Connection.get_peer_cert_chain()
502
503 Retrieve the tuple of the other side's certificate chain (if any)
504
505
506.. py:method:: Connection.getpeername()
507
508 Call the :py:meth:`getpeername` method of the underlying socket.
509
510
511.. py:method:: Connection.getsockname()
512
513 Call the :py:meth:`getsockname` method of the underlying socket.
514
515
516.. py:method:: Connection.getsockopt(level, optname[, buflen])
517
518 Call the :py:meth:`getsockopt` method of the underlying socket.
519
520
521.. py:method:: Connection.pending()
522
523 Retrieve the number of bytes that can be safely read from the SSL buffer
524 (**not** the underlying transport buffer).
525
526
527.. py:method:: Connection.recv(bufsize)
528
529 Receive data from the Connection. The return value is a string representing the
530 data received. The maximum amount of data to be received at once, is specified
531 by *bufsize*.
532
533
534.. py:method:: Connection.bio_write(bytes)
535
536 If the Connection was created with a memory BIO, this method can be used to add
537 bytes to the read end of that memory BIO. The Connection can then read the
538 bytes (for example, in response to a call to :py:meth:`recv`).
539
540
541.. py:method:: Connection.renegotiate()
542
543 Renegotiate the SSL session. Call this if you wish to change cipher suites or
544 anything like that.
545
546
547.. py:method:: Connection.send(string)
548
549 Send the *string* data to the Connection.
550
551
552.. py:method:: Connection.bio_read(bufsize)
553
554 If the Connection was created with a memory BIO, this method can be used to
555 read bytes from the write end of that memory BIO. Many Connection methods will
556 add bytes which must be read in this manner or the buffer will eventually fill
557 up and the Connection will be able to take no further actions.
558
559
560.. py:method:: Connection.sendall(string)
561
562 Send all of the *string* data to the Connection. This calls :py:meth:`send`
563 repeatedly until all data is sent. If an error occurs, it's impossible to tell
564 how much data has been sent.
565
566
567.. py:method:: Connection.set_accept_state()
568
569 Set the connection to work in server mode. The handshake will be handled
570 automatically by read/write.
571
572
573.. py:method:: Connection.set_app_data(data)
574
575 Associate *data* with this Connection object. *data* can be retrieved
576 later using the :py:meth:`get_app_data` method.
577
578
579.. py:method:: Connection.set_connect_state()
580
581 Set the connection to work in client mode. The handshake will be handled
582 automatically by read/write.
583
584
585.. py:method:: Connection.setblocking(flag)
586
587 Call the :py:meth:`setblocking` method of the underlying socket.
588
589
590.. py:method:: Connection.setsockopt(level, optname, value)
591
592 Call the :py:meth:`setsockopt` method of the underlying socket.
593
594
595.. py:method:: Connection.shutdown()
596
597 Send the shutdown message to the Connection. Returns true if the shutdown
598 message exchange is completed and false otherwise (in which case you call
599 :py:meth:`recv` or :py:meth:`send` when the connection becomes
600 readable/writeable.
601
602
603.. py:method:: Connection.get_shutdown()
604
605 Get the shutdown state of the Connection. Returns a bitvector of either or
606 both of *SENT_SHUTDOWN* and *RECEIVED_SHUTDOWN*.
607
608
609.. py:method:: Connection.set_shutdown(state)
610
611 Set the shutdown state of the Connection. *state* is a bitvector of
612 either or both of *SENT_SHUTDOWN* and *RECEIVED_SHUTDOWN*.
613
614
615.. py:method:: Connection.sock_shutdown(how)
616
617 Call the :py:meth:`shutdown` method of the underlying socket.
618
619
620.. py:method:: Connection.bio_shutdown()
621
622 If the Connection was created with a memory BIO, this method can be used to
623 indicate that *end of file* has been reached on the read end of that memory
624 BIO.
625
626
627.. py:method:: Connection.state_string()
628
629 Retrieve a verbose string detailing the state of the Connection.
630
631
632.. py:method:: Connection.client_random()
633
634 Retrieve the random value used with the client hello message.
635
636
637.. py:method:: Connection.server_random()
638
639 Retrieve the random value used with the server hello message.
640
641
642.. py:method:: Connection.master_key()
643
644 Retrieve the value of the master key for this session.
645
646
647.. py:method:: Connection.want_read()
648
649 Checks if more data has to be read from the transport layer to complete an
650 operation.
651
652
653.. py:method:: Connection.want_write()
654
655 Checks if there is data to write to the transport layer to complete an
656 operation.
657
658
659.. py:method:: Connection.set_tlsext_host_name(name)
660
661 Specify the byte string to send as the server name in the client hello message.
662
663 .. versionadded:: 0.13
664
665
666.. py:method:: Connection.get_servername()
667
668 Get the value of the server name received in the client hello message.
669
670 .. versionadded:: 0.13
671
672
Jonathan Ballet6381da32011-07-20 16:43:38 +0900673.. Rubric:: Footnotes
674
Jonathan Balletc9e066c2011-07-17 22:56:05 +0900675.. [#connection-context-socket] Actually, all that is required is an object that
676 **behaves** like a socket, you could even use files, even though it'd be
677 tricky to get the handshakes right!