tls - Tcl TLS extension
tls(n) 2.0b2 tls "Tcl TLS extension"
Name
tls - binding to the OpenSSL library for encrypted socket and I/O channel communications
Table Of Contents
Synopsis
- package require Tcl 8.5-
- package require tls 2.0b2
- tls::init ?-option? ?value? ?-option value ...?
- tls::socket ?-option? ?value? ?-option value ...? host port
- tls::socket -server command ?-option? ?value? ?-option value ...? port
- tls::import channel ?-option? ?value? ?-option value ...?
- tls::starttls channel ?-option? ?value? ?-option value ...?
- tls::handshake channel
- tls::shutdown channel
- tls::unimport channel
- tls::unstack channel
- tls::status ?-local? channel
- tls::connection channel
- tls::ciphers ?protocol? ?verbose? ?supported?
- tls::protocols
- tls::version
Description
This extension provides TCL script access to Secure Socket Layer (SSL) communications using the Transport Layer Security (TLS) protocol. It provides a generic binding to OpenSSL, utilizing the Tcl_StackChannel API in TCL 8.4 or later. These sockets behave exactly the same as channels created using the built-in socket command, but provide additional options for controlling the SSL/TLS session.
Compatibility
This extension is compatible with OpenSSL 1.1.1 or later. It requires Tcl version 8.5 or later and will work with Tcl 9.0.
Commands
The following are the commands provided by the TcLTLS package. See Examples for example usage and the "demos" directory for more example usage.
- tls::init ?-option? ?value? ?-option value ...?
Optional function to set the default options used by tls::socket. If you call tls::import directly, the values set by this command have no effect. This command supports all of the same options as the tls::socket command, though you should limit your options to only the TLS related ones.
- tls::socket ?-option? ?value? ?-option value ...? host port
This is a helper function that utilizes the underlying commands socket and tls::import to create the connection. It behaves the same as the native TCL socket command, but also supports the tls::import command options with one additional option. It returns the channel handle id for the new socket. Additional options are:
- tls::socket -server command ?-option? ?value? ?-option value ...? port
Same as previous command, but instead creates a server socket for clients to connect to just like the Tcl socket -server command. It returns the channel handle id for the new socket.
- tls::import channel ?-option? ?value? ?-option value ...?
- tls::starttls channel ?-option? ?value? ?-option value ...?
Start TLS encryption on TCL channel channel via a stacked channel. It need not be a socket, but must provide bi-directional flow. Also sets session parameters for SSL handshake. Valid options are:
- tls::handshake channel
Forces the TLS negotiation handshake to take place immediately, and returns 0 if handshake is still in progress (non-blocking), or 1 if the handshake was successful. If the handshake failed, an error will be returned.
- tls::shutdown channel
- tls::unimport channel
- tls::unstack channel
This terminates the SSL/TLS session by sending the "close_notify" message and removes the top level stacked channel from channel, but it does not close the socket. It is the compliment to tls::import by ending encryption of a TCL channel. An error is thrown if TLS is not the top stacked channel type.
- tls::status ?-local? channel
Returns the current status of an SSL channel. The result is a list of key-value pairs describing the SSL, certificate, and certificate verification status. If the SSL handshake has not yet completed, an empty list is returned. If the -local option is specified, then the local certificate is used. Returned values include:
SSL Status
- alpn protocol
The protocol selected after Application-Layer Protocol Negotiation (ALPN). This value is new for TclTLS 1.8.
- cipher cipher
The current cipher in use for the session.
- peername name
The peername from the certificate. This value is new for TclTLS 1.8.
- protocol version
The protocol version used for the connection: SSL2, SSL3, TLS1, TLS1.1, TLS1.2, TLS1.3, or unknown. This value is new for TclTLS 1.8.
- sbits n
The number of bits used for the session key.
- signatureHashAlgorithm algorithm
The signature hash algorithm. This value is new for TclTLS 1.8.
- signatureType type
The signature type value. This value is new for TclTLS 1.8.
- verifyDepth n
Maximum depth for the certificate chain verification. Default is -1, to check all. This value is new for TclTLS 1.8.
- verifyMode list
List of certificate verification modes. This value is new for TclTLS 1.8.
- verifyResult result
Certificate verification result. This value is new for TclTLS 1.8.
- ca_names list
List of the Certificate Authorities used to create the certificate. This value is new for TclTLS 1.8.
Certificate Status
- all string
Dump of all certificate info. This value is new for TclTLS 1.8.
- version value
The certificate version.
- serialNumber string
The serial number of the certificate as a hex string. This value was changed from serial in TclTLS 1.8.
- signature algorithm
Cipher algorithm used for certificate signature. This value is new for TclTLS 1.8.
- issuer string
The distinguished name (DN) of the certificate issuer.
- notBefore date
The beginning date of the certificate validity.
- notAfter date
The expiration date of the certificate validity.
- subject string
The distinguished name (DN) of the certificate subject. Fields include: Common Name (CN), Organization (O), Locality or City (L), State or Province (S), and Country Name (C).
- issuerUniqueID string
The issuer unique id. This value is new for TclTLS 1.8.
- subjectUniqueID string
The subject unique id. This value is new for TclTLS 1.8.
- num_extensions n
Number of certificate extensions. This value is new for TclTLS 1.8.
- extensions list
List of certificate extension names. This value is new for TclTLS 1.8.
- authorityKeyIdentifier string
Authority Key Identifier (AKI) of the Issuing CA certificate that signed the SSL certificate as a hex string. This value matches the SKI value of the Intermediate CA certificate. This value is new for TclTLS 1.8.
- subjectKeyIdentifier string
Subject Key Identifier (SKI) hash of the public key inside the certificate as a hex string. Used to identify certificates that contain a particular public key. This value is new for TclTLS 1.8.
- subjectAltName list
List of all of the Subject Alternative Names (SAN) including domain names, sub domains, and IP addresses that are secured by the certificate. This value is new for TclTLS 1.8.
- ocsp list
List of all Online Certificate Status Protocol (OCSP) URLs that can be used to check the validity of this certificate. This value is new for TclTLS 1.8.
- certificate cert
The PEM encoded certificate.
- signatureAlgorithm algorithm
Cipher algorithm used for the certificate signature. This value is new for TclTLS 1.8.
- signatureValue string
Certificate signature as a hex string. This value is new for TclTLS 1.8.
- signatureDigest version
Certificate signing digest as a hex string. This value is new for TclTLS 1.8.
- publicKeyAlgorithm algorithm
Certificate signature public key algorithm. This value is new for TclTLS 1.8.
- publicKey string
Certificate signature public key as a hex string. This value is new for TclTLS 1.8.
- bits n
Number of bits used for certificate signature key. This value is new for TclTLS 1.8.
- self_signed boolean
Whether the certificate signature is self signed. This value is new for TclTLS 1.8.
- sha1_hash hash
The SHA1 hash of the certificate as a hex string. This value is new for TclTLS 1.8.
- sha256_hash hash
The SHA256 hash of the certificate as a hex string. This value is new for TclTLS 1.8.
- tls::connection channel
Returns the current connection status of an SSL channel. The result is a list of key-value pairs describing the connection. This command is new for TclTLS 1.8. Returned values include:
SSL Status
- state state
State of the connection.
- servername name
The name of the connected to server.
- protocol version
The protocol version used for the connection: SSL2, SSL3, TLS1, TLS1.1, TLS1.2, TLS1.3, or unknown.
- renegotiation_allowed boolean
Whether protocol renegotiation is supported or not.
- security_level level
The security level used for selection of ciphers, key size, etc.
- session_reused boolean
Whether the session has been reused or not.
- is_server boolean
Whether the connection is configured as a server (1) or client (0).
- compression mode
Compression method.
- expansion mode
Expansion method.
- caList list
List of Certificate Authorities (CA) for X.509 certificate.
Cipher Info
- cipher cipher
The current cipher in use for the connection.
- standard_name name
The standard RFC name of cipher.
- algorithm_bits n
The number of processed bits used for cipher.
- secret_bits n
The number of secret bits used for cipher.
- min_version version
The minimum protocol version for cipher.
- cipher_is_aead boolean
Whether the cipher is Authenticated Encryption with Associated Data (AEAD).
- cipher_id id
The OpenSSL cipher id.
- description string
A text description of the cipher.
- handshake_digest boolean
Digest used during handshake.
Session Info
- alpn protocol
The protocol selected after Application-Layer Protocol Negotiation (ALPN).
- resumable boolean
Whether the session can be resumed or not.
- start_time seconds
Time since session started in seconds since epoch.
- timeout seconds
Max duration of session in seconds before time-out.
- lifetime seconds
Session ticket lifetime hint in seconds.
- session_id binary_string
Unique session id for use in resuming the session.
- session_ticket binary_string
Unique session ticket for use in resuming the session.
- ticket_app_data binary_string
Unique session ticket application data.
- master_key binary_string
Unique session master key.
- session_cache_mode mode
Server cache mode (client, server, or both).
- tls::ciphers ?protocol? ?verbose? ?supported?
Without any options, it returns a list of all symmetric ciphers for use with the -cipher option. With protocol, only the ciphers supported for that protocol are returned. See the tls::protocols command for the supported protocols. If verbose is specified as true then a verbose, human readable list is returned with additional information on the cipher. If supported is specified as true, then only the ciphers supported for protocol will be listed. The supported arg is new for TclTLS 1.8.
- tls::protocols
Returns a list of the supported SSL/TLS protocols. Valid values are: ssl2, ssl3, tls1, tls1.1, tls1.2, and tls1.3. Exact list depends on OpenSSL version and compile time flags. This command is new for TclTLS 1.8.
- tls::version
Returns the OpenSSL version string.
Certificate Validation
PKI and Certificates
Using the Public Key Infrastructure (PKI), each user creates a private key that only they know about and a public key they can exchange with others for use in encrypting and decrypting data. The process is the sender encrypts their data using their private key and the receiver's public key. The data is then sent to the receiver. In a similar manner, the receiver uses their private key and the sender's public key to decrypt the data. This provides data integrity, to ensure the data can't be viewed or altered during transport. See the -key and -keyfile options for how to specify the private key. Also see the -password option for how to provide the password.
In order to provide authentication, i.e. ensuring someone is who they say they are, the public key and user identification info is stored in a X.509 certificate and that certificate is authenticated (i.e. signed) by a Certificate Authority (CA). Users can then exchange these certificates during the TLS initialization process and check them against the root CA certificates to ensure they are valid. This is handled by OpenSSL via the -request and -require options. See the -cadir, -cadir, and -castore options for how to specify where to find the CA certificates. Optionally, in a future release, they can also be checked against the Certificate Revocation List (CRL) of revoked certificates. Certificates can also be self-signed, but they are by default not trusted unless you add them to your certificate store.
Typically when visiting web sites, only the client needs to check the server's certificate to ensure it is valid. The server doesn't need to check the client certificate unless you need to authenticate with them to login, etc. See the -cert and -certfile options if you need to provide a certificate.
Summary of command line options
The following options are used for peer certificate validation:
When are command line options needed?
In TclTLS 1.8 and earlier versions, certificate validation is NOT enabled by default. This limitation is due to the lack of a common cross platform database of Certificate Authority (CA) provided certificates to validate against. Many Linux systems natively support OpenSSL and thus have these certificates installed as part of the OS, but MacOS and MS Windows do not. Staring in TclTLS 2.0, the default for client connections has been changed to require certificate validation by default. In order to use the -require option, one of the following must be true:
On Linux and Unix systems with OpenSSL already installed or if the CA certificates are available in PEM format, and if they are stored in the standard locations, or if the SSL_CERT_DIR or SSL_CERT_FILE environment variables are set, then -cadir, -cadir, and -castore aren't needed.
If OpenSSL is not installed in the default location, or when using Mac OS or MS Windows and OpenSSL is installed, the SSL_CERT_DIR and/or SSL_CERT_FILE environment variables or the one of the -cadir, -cadir, or -castore options must be defined.
On MS Windows, starting in OpenSSL 3.2, it is now possible to access the built-in Windows Certificate Store from OpenSSL. This can be utilized by setting the -castore option to "org.openssl.winstore://". In TclTLS 2.0, this is the default value if -cadir, -cadir, and -castore are not specified.
If OpenSSL is not installed or the CA certificates are not available in PEM format, the CA certificates must be downloaded and installed with the user software. The CURL team makes them available at CA certificates extracted from Mozilla in the "cacert.pem" file. You must then either set the SSL_CERT_DIR and/or SSL_CERT_FILE environment variables or the -cadir or -cafile options to the CA cert file's install location. It is your responsibility to keep this file up to date.
Callback Options
As previously described, each channel can be given their own callbacks to handle intermediate processing by the OpenSSL library, using the -command, -password, and -validate_command options passed to either of tls::socket or tls::import. Unlike previous versions of TclTLS, only if the callback generates an error, will the bgerror command be invoked with the error information.
Values for Command Callback
The callback for the -command option is invoked at several points during the OpenSSL handshake and during routine operations. See below for the possible arguments passed to the callback script. Values returned from the callback are ignored.
Values for Password Callback
The callback for the -password option is invoked by TclTLS whenever OpenSSL needs to obtain a password. See below for the possible arguments passed to the callback script. The user provided password is expected to be returned by the callback.
Values for Validate Command Callback
The callback for the -validatecommand option is invoked during the handshake process in order for the application to validate the provided value(s). See below for the possible arguments passed to the callback script. If not specified, OpenSSL will accept all valid certificates and extensions. To reject the value and abort the connection, the callback should return 0. To accept the value and continue the connection, it should return 1. To reject the value, but continue the connection, it should return 2. This callback is new for TclTLS 1.8.
Reference implementations of these callbacks are provided in "tls.tcl" as tls::callback, tls::password, and tls::validate_command respectively. Note that these are only sample implementations. In a more realistic deployment you would specify your own callback scripts on each TLS channel using the -command, -password, and -validate_command options.
The default behavior when the -command and -validate_command options are not specified, is for TclTLS to process the associated library callbacks internally. The default behavior when the -password option is not specified is for TclTLS to process the associated library callbacks by attempting to call tls::password. The difference between these two behaviors is a consequence of maintaining compatibility with earlier implementations.
The use of the reference callbacks tls::callback, tls::password, and tls::validate_command is not recommended. They may be removed from future releases.
Debug
For most debugging needs, the -callback option can be used to provide sufficient insight and information on the TLS handshake and progress. If further troubleshooting insight is needed, the compile time option --enable-debug can be used to get detailed execution flow status.
TLS key logging can be enabled by setting the environment variable SSLKEYLOGFILE to the name of the file to log to. Then whenever TLS key material is generated or received it will be logged to the file. This is useful for logging key data for network logging tools to use to decrypt the data.
The tls::debug variable provides some additional control over the debug logging in the tls::callback, tls::password, and tls::validate_command default handlers in "tls.tcl". The default value is 0 with higher values producing more diagnostic output, and will also force the verify method in tls::callback to accept the certificate, even if it is invalid when the -validatecommand option is set to tls::validate_command.
The use of the variable tls::debug is not recommended. It may be removed from future releases.
Examples
The following are example scripts to download a webpage and file using the http package. See Certificate Validation for when the -cadir, -cafile, and -castore options are also needed. See the "demos" directory for more example scripts.
Example #1: Download a web page
package require http package require tls set url "https://www.tcl.tk/" http::register https 443 list ::tls::socket -autoservername 1 -require 1 # Get URL set token :geturl $url # Check for error if {:status $token ne "ok"} { puts format "Error %s" [http::status $token] } # Save web page to file set ch open example.html wb puts $ch :data $token close $ch # Cleanup ::http::cleanup $token
Example #2: Download a file
package require http package require tls set url "https://wiki.tcl-lang.org/sitemap.xml" http::register https 443 list ::tls::socket -autoservername 1 -require 1 # Open output file set filename file tail $url set ch open $filename wb # Get file set token :http::geturl $url -blocksize 65536 -channel $ch # Check for error if {:status $token ne "ok"} { puts format "Error %s" [http::status $token] } # Cleanup close $ch ::http::cleanup $token
Special Considerations
The capabilities of this package can vary enormously based upon how the linked to OpenSSL library was configured and built. New versions may obsolete older protocol versions, add or remove ciphers, change default values, etc. Use the tls::protocols command to obtain the supported protocol versions.
Error Messages
Some OpsnSSl error messages have cryptic meanings. This is a list of messages along with their true meaning.
- packet length too long
Client has tried to connect to a HTTP server on the plain-text port instead of the SSL/TLS port.
- unexpected eof while reading
Peer has closed the connection without sending the "close notify" shutdown alert.
- wrong version number
Client has tried to connect to a non-HTTP server on a non-TLS (i.e. plain text) port.
Keywords
I/O, IP Address, OpenSSL, SSL, TCP, TLS, TclTLS, asynchronous I/O, bind, certificate, channel, connection, domain name, host, https, network, network address, socket, tls
Category
tls
Copyright
Copyright © 1999 Matt Newman
Copyright © 2004 Starfish Systems
Copyright © 2024 Brian O'Hagan