tls - binding to OpenSSL library for encrypted socket and I/O channel communications.
package require Tcl ?8.5-?
package require tls ?1.8-?
tls::init ?options?
tls::socket ?options? host port
tls::socket ?-server command? ?options? port
tls::status ?-local? channel
tls::connection channel
tls::handshake channel
tls::import channel ?options?
tls::unimport channel
tls::ciphers ?protocol? ?verbose? ?supported?
tls::protocols
tls::version
This extension provides TCL script access to secure socket communications using the Transport Layer Security (TLS) protocol. It provides a generic binding to OpenSSL, utilizing the Tcl_StackChannel API in TCL 8.4 and higher. These sockets behave exactly the same as channels created using the built-in socket command, along with additional options for controlling the SSL/TLS session.
Typically one would use the tls::socket command which provides compatibility with the native TCL socket command. In such cases tls::import should not be used directly.
- -autoservername bool
- Automatically set the -servername argument to the host argument (default is false).
- -alpn list
- List of protocols to offer during Application-Layer Protocol Negotiation (ALPN). For example: h2 and http/1.1, but not h3 or quic.
- -cadir dir
- Specifies the directory where the Certificate Authority (CA) certificates are stored. The default is platform specific and can be set at compile time. The default location can be overridden via the SSL_CERT_DIR environment variable. See CERTIFICATE VALIDATION.
- -cafile filename
- Specifies the file with the Certificate Authority (CA) certificates to use. The default is cert.pem, in the OpenSSL directory. The default file can be overridden via the SSL_CERT_FILE environment variable. See CERTIFICATE VALIDATION.
- -castore URI
- URI for a Certificate Authority (CA) store, which may be a single container or a catalog of containers. Starting with OpenSSL 3.2 on Windows, set to "org.openssl.winstore://" to use the built-in Windows Cert Store. The Windows cert store only supports root certificate stores. See CERTIFICATE VALIDATION.
- -certfile filename
- Specifies the file with the certificate to use in PEM format. This also contains the public key.
- -cert binary_string
- Specifies the certificate to use as a DER encoded string (X.509 DER).
- -cipher string
- Specifies the list of ciphers to use for TLS 1.2 and earlier. String is a colon (":") separated list of ciphers. Ciphers can be combined using the + character. Prefixes can be used to permanently remove ("!"), delete ("-"), or move a cipher to the end of the list ("+"). Keywords @STRENGTH (sort by algorithm key length), @SECLEVEL=n (set security level to n), and DEFAULT (use default cipher list, at start only) can also be specified. See OpenSSL documentation for the full list of valid values.
- -ciphersuites string
- Specifies the list of cipher suites to use for TLS 1.3. String is a colon (":") separated list of cipher suite names.
- -command callback
- Specifies the callback command to be invoked at several points during the handshake to pass errors, tracing information, and protocol messages. See CALLBACK OPTIONS for more info.
- -dhparams filename
- Specifies the Diffie-Hellman (DH) parameters file.
- -keyfile filename
- Specifies the private key file. (default is value of -certfile).
- -key filename
- Specifies the private key to use as a DER encoded string (PKCS#1 DER).
- -model channel
- Force this channel to share the same SSL_CTX structure as the specified channel, and therefore share callbacks etc.
- -password callback
- Specifies the callback command to invoke when OpenSSL needs to obtain a password. This is typically used to unlock the private key of a certificate. The callback should return a password string. See CALLBACK OPTIONS for more info.
- -post_handshake bool
- Allow post-handshake session ticket updates.
- -request bool
- Request a certificate from peer during the SSL handshake. This is needed to do certificate validation. (default is true). See CERTIFICATE VALIDATION.
- -require bool
- Require a valid certificate from peer during SSL handshake. If this is set to true, then -request must also be set to true and a either a -cadir, -cafile, -castore, or platform default must be provided in order to validate against. (default is false). See CERTIFICATE VALIDATION.
- -security_level integer
- Specifies the security level (value from 0 to 5). The security level affects the cipher suite encryption algorithms, supported ECC curves, supported signature algorithms, DH parameter sizes, certificate key sizes and signature algorithms. The default is 1 prior to OpenSSL 3.2 and 2 thereafter. Level 3 and higher disable support for session tickets and only accept cipher suites that provide forward secrecy.
- -server bool
- Specifies whether to act as a server and respond with a server handshake when a client connects and provides a client handshake. (default is false)
- -servername host
- Specify server's hostname. This is used to set the TLS Server Name Indication (SNI) extension. Set this to the expected servername in the server's certificate or one of the subjectAltName alternates.
- -session_id string
- Specifies the session id to resume session.
- -ssl2 bool
- Enable use of SSL v2. (default is false). Note: Recent versions of OpenSSL don't support SSLv2.
- -ssl3 bool
- Enable use of SSL v3. (default is false). Note: SSL v3 must also be enabled with a compile time option.
- -tls1 bool
- Enable use of TLS v1. (default is true). Note: TLS 1.0 needs SHA1 to operate, which is only available in security level 0 for Open SSL 3.0+.
- -tls1.1 bool
- Enable use of TLS v1.1 (default is true). Note: TLS 1.1 needs SHA1 to operate, which is only available in security level 0 for Open SSL 3.0+.
- -tls1.2 bool
- Enable use of TLS v1.2 (default is true)
- -tls1.3 bool
- Enable use of TLS v1.3 (default is true)
- -validatecommand callback
- Specifies the callback command to invoke to validate protocol config parameters during the protocol negotiation phase. This can be used by TCL scripts to perform their own certificate validation to supplement the default validation provided by OpenSSL. The script must return a boolean true to continue the negotiation. See CALLBACK OPTIONS for more info.
SSL StatusCertificate Status
- alpn protocol
- The protocol selected after Application-Layer Protocol Negotiation (ALPN).
- cipher cipher
- The current cipher in use for the session.
- peername name
- The peername from the certificate.
- protocol version
- The protocol version used for the connection: SSL2, SSL3, TLS1, TLS1.1, TLS1.2, TLS1.3, or unknown.
- sbits n
- The number of bits used for the session key.
- signatureHashAlgorithm algorithm
- The signature hash algorithm.
- signatureType type
- The signature type value.
- verifyDepth n
- Maximum depth for the certificate chain verification. Default is -1, to check all.
- verifyMode list
- List of certificate verification modes.
- verifyResult result
- Certificate verification result.
- ca_names list
- List of the Certificate Authorities used to create the certificate.
- all string
- Dump of all certificate info.
- version value
- The certificate version.
- serialNumber n
- The serial number of the certificate as a hex string.
- signature algorithm
- Cipher algorithm used for certificate signature.
- issuer dn
- 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 dn
- 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.
- subjectUniqueID string
- The subject unique id.
- num_extensions n
- Number of certificate extensions.
- extensions list
- List of certificate extension names.
- authorityKeyIdentifier string
- (AKI) Key identifier 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.
- subjectKeyIdentifier string
- (SKI) Hash of the public key inside the certificate as a hex string. Used to identify certificates that contain a particular public key.
- subjectAltName list
- List of all of the alternative domain names, sub domains, and IP addresses that are secured by the certificate.
- ocsp list
- List of all Online Certificate Status Protocol (OCSP) URLs.
- certificate cert
- The PEM encoded certificate.
- signatureAlgorithm algorithm
- Cipher algorithm used for the certificate signature.
- signatureValue string
- Certificate signature as a hex string.
- signatureDigest version
- Certificate signing digest as a hex string.
- publicKeyAlgorithm algorithm
- Certificate signature public key algorithm.
- publicKey string
- Certificate signature public key as a hex string.
- bits n
- Number of bits used for certificate signature key.
- self_signed boolean
- Whether the certificate signature is self signed.
- sha1_hash hash
- The SHA1 hash of the certificate as a hex string.
- sha256_hash hash
- The SHA256 hash of the certificate as a hex string.
SSL StatusCipher Info
- 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.
Session 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.
- 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).
The following options are used for certificate validation:
By default, a client TLS connection does NOT validate the server certificate chain. 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 Windows do not. In order to use the -require option, one of the following must be true:
As indicated above, individual channels 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 TCL TLS, only if the callback generates an error, will the bgerror command will be invoked with the error information.
ERR_reason_error_string()
, or a custom message.
SSL_set_info_callback()
during the initial connection
and handshake operations. The type argument is new for
TLS 1.8. The arguments are:
handshake, alert, connect, accept
.start, done, read, write, loop, exit
.SSL_state_string_long()
or by
SSL_alert_desc_string_long()
, depending on the context.warning, fatal, and unknown
. For others,
info
is used.SSL_set_msg_callback()
whenever a message is sent or
received during the initial connection, handshake, or I/O operations.
It is only available when OpenSSL is complied with the
enable-ssl-trace option. Arguments are: direction
is Sent or Received, version is the protocol
version, content_type is the message content type, and
message is more info from the SSL_trace
API.
This callback is new for TLS 1.8.
SSL_CTX_sess_set_new_cb()
whenever a new session id is
sent by the server during the initial connection and handshake, but
can also be received later if the -post_handshake option is
used. Arguments are: session_id is the current
session identifier, ticket is the session ticket info, and
lifetime is the the ticket lifetime in seconds.
This callback is new for TLS 1.8.
X509_STORE_CTX_get_error()
.Reference implementations of these callbacks are provided in the distribution as tls::callback, tls::password, and tls::validate_command respectively. Note that these are sample implementations only. 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 TLS to process the associated library callbacks internally. The default behavior when the -password option is not specified is for TLS 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.
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 these reference callbacks. Its value is zero by default. Higher values produce more diagnostic output, and will also force the verify method in tls::callback to accept the certificate, even when it is invalid if the tls::validate_command callback is used for the -validatecommand option.
The use of the variable tls::debug is not recommended. It may be removed from future releases.
These examples use the default Unix platform SSL certificates. For standard installations, -cadir and -cafile should not be needed. If your certificates are in non-standard locations, update -cadir or use -cafile as needed.
Example #1: Use HTTP package
package require http
package require tls
set url "https://www.tcl.tk/"
http::register https 443 [list ::tls::socket -autoservername true -require true -cadir /etc/ssl/certs \
-command ::tls::callback -password ::tls::password -validatecommand ::tls::validate_command]
# Check for error
set token [http::geturl $url]
if {[http::status $token] ne "ok"} {
puts [format "Error %s" [http::status $token]]
}
# Get web page
set data [http::data $token]
puts [string length $data]
# Cleanup
::http::cleanup $token
Example #2: Use raw socket
package require tls
set url "www.tcl-lang.org"
set port 443
set ch [tls::socket -autoservername 1 -servername $url -request 1 -require 1 \
-alpn {http/1.1} -cadir /etc/ssl/certs -command ::tls::callback \
-password ::tls::password -validatecommand ::tls::validate_command $url $port]
chan configure $ch -buffersize 65536
tls::handshake $ch
puts $ch "GET / HTTP/1.1"
flush $ch
after 500
set data [read $ch]
array set status [tls::status $ch]
array set conn [tls::connection $ch]
array set chan [chan configure $ch]
close $ch
parray status
parray conn
parray chan
These examples use the default Unix platform SSL certificates. For standard installations, -cadir and -cafile should not be needed. If your certificates are in non-standard locations, update -cadir or use -cafile as needed.
Example #1: Get web page
package require http
package require tls
set url "https://www.tcl.tk/"
http::register https 443 [list ::tls::socket -autoservername true -require true -cadir /etc/ssl/certs]
# Check for error
set token [http::geturl $url]
if {[http::status $token] ne "ok"} {
puts [format "Error %s" [http::status $token]]
}
# Get web page
set data [http::data $token]
puts $data
# Cleanup
::http::cleanup $token
Example #2: Download file
package require http
package require tls
set url "https://wiki.tcl-lang.org/sitemap.xml"
set filename [file tail $url]
http::register https 443 [list ::tls::socket -autoservername true -require true -cadir /etc/ssl/certs]
# Get file
set ch [open $filename wb]
set token [::http::geturl $url -blocksize 65536 -channel $ch]
# Cleanup
close $ch
::http::cleanup $token
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 commands to obtain the supported protocol versions.
socket, fileevent, http, OpenSSL
Copyright © 1999 Matt Newman. Copyright © 2004 Starfish Systems. Copyright © 2023 Brian O'Hagan.