NAME
tls - binding to OpenSSL toolkit.
SYNOPSIS
package require Tcl ?8.4?
package require tls
 
tls::init ?options?
tls::socket ?options? host port
tls::socket ?-server command? ?options? port
tls::handshake channel
tls::status ?-local? channel
tls::connection channel
tls::import channel ?options?
tls::unimport channel
 
tls::ciphers protocol ?verbose? ?supported?
tls::protocols
tls::version
 
tls::hash type data
tls::hashes
tls::md4 data
tls::md5 data
tls::sha1 data
tls::sha256 data
COMMANDS
CALLBACK OPTIONS
HTTPS EXAMPLE
SPECIAL CONSIDERATIONS
SEE ALSO

NAME

tls - binding to OpenSSL toolkit.

SYNOPSIS

package require Tcl 8.5
package require tls

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

tls::hash type data
tls::hashes
tls::md4 data
tls::md5 data
tls::sha1 data
tls::sha256 data

DESCRIPTION

This extension provides a generic binding to OpenSSL, utilizing the Tcl_StackChannel API for Tcl 8.4 and higher. The sockets behave exactly the same as channels created using Tcl's built-in socket command with additional options for controlling the SSL session.

COMMANDS

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.

tls::init ?options?
Optional function to set the default options used by tls::socket. If you call tls::import directly this routine has no effect. Any of the options that tls::socket accepts can be set using this command, though you should limit your options to only TLS related ones.
 
tls::socket ?options? host port
tls::socket ?-server command? ?options? port
This is a helper function that utilizes the underlying commands (tls::import). It behaves exactly the same as the native Tcl socket command except that the options can include any of the applicable tls:import options with one additional option:
-autoservername bool
Automatically send the -servername as the host argument (default is false)
tls::import channel ?options?
SSL-enable a regular Tcl channel - it need not be a socket, but must provide bi-directional flow. Also setting session parameters for SSL handshake.
-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
Set the CA certificates path. The default directory is platform specific and can be set at compile time. This can be overridden via the SSL_CERT_DIR environment variable.
-cafile filename
Set the certificate authority (CA) certificates file. The default is the cert.pem file in the OpsnSSL directory. This can also be overridden via the SSL_CERT_FILE environment variable.
-certfile filename
Specify the filename with the certificate to use.
-cert filename
Specify the contents of a certificate to use, as a DER encoded binary value (X.509 DER).
-cipher string
List of ciphers to use. String is a colon (":") separated list of ciphers or cipher suites. Cipher suites can be combined using the + character. Prefixes can be used to permanently remove ("!"), delete ("-"), or move a cypher 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. (TLS 1.2 and earlier only)
-ciphersuites string
List of cipher suites to use. String is a colon (":") separated list of cipher suite names. (TLS 1.3 only)
-command callback
Callback to invoke at several points during the handshake. This is used to pass errors and tracing information, and it can allow Tcl scripts to perform their own certificate validation in place of the default validation provided by OpenSSL. See CALLBACK OPTIONS for further discussion.
-dhparams filename
Specify the Diffie-Hellman parameters file.
-keyfile filename
Specify the private key file. (default is value of -certfile)
-key filename
Specify the private key to use as a DER encoded value (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
Callback to invoke when OpenSSL needs to obtain a password, typically to unlock the private key of a certificate. The callback should return a string which represents the password to be used. See CALLBACK OPTIONS for further discussion.
-post_handshake bool
Allow post-handshake ticket updates.
-request bool
Request a certificate from peer during SSL handshake. (default is true)
-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. (default is false)
-securitylevel integer
Set security level. Must be 0 to 5. The security level affects cipher suite encryption algorithms, supported ECC curves, supported signature algorithms, DH parameter sizes, certificate key sizes and signature algorithms. The default is 1. Level 3 and higher disable support for session tickets and only accept cipher suites that provide forward secrecy.
-server bool
Handshake as server if true, else handshake as client. (default is false)
-servername host
Specify server hostname. Only available if the OpenSSL library the package is linked against supports the TLS hostname extension for 'Server Name Indication' (SNI). Use to name the logical host we are talking to and expecting a certificate for.
-session_id string
Session id to resume session.
-ssl2 bool
Enable use of SSL v2. (default is false)
-ssl3 bool
Enable use of SSL v3. (default is false)
-tls1 bool
Enable use of TLS v1. (default is true)
-tls1.1 bool
Enable use of TLS v1.1 (default is true)
-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
Callback to invoke to verify or validate protocol config parameters during the protocol negotiation phase. See CALLBACK OPTIONS for further discussion.
tls::unimport channel
Provided for symmetry to tls::import, this unstacks the SSL-enabling of a regular Tcl channel. An error is thrown if TLS is not the top stacked channel type.
 
tls::handshake channel
Forces handshake to take place, and returns 0 if handshake is still in progress (non-blocking), or 1 if the handshake was successful. If the handshake failed this routine will throw an error.
 
tls::status ?-local? channel
Returns the current status of the certificate for an SSL channel. The result is a list of key-value pairs describing the certificate. If the result is an empty list then the SSL handshake has not yet completed. If -local is specified, then the local certificate is used.
SSL Status
alpn protocol
The protocol selected after Application-Layer Protocol Negotiation (ALPN).
cipher cipher
The current cipher in use between the client and server channels.
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.
Certificate Status
all string
Dump of all certificate info.
version value
The certificate version.
serialNumber n
The serial number of the certificate as hex string.
signature algorithm
Cipher algorithm used for certificate signature.
issuer dn
The distinguished name (DN) of the certificate issuer.
notBefore date
The begin date for the validity of the certificate.
notAfter date
The expiration date for the certificate.
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 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 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 certificate signature.
signatureValue string
Certificate signature as hex string.
signatureDigest version
Certificate signing digest.
publicKeyAlgorithm algorithm
Certificate signature public key algorithm.
publicKey string
Certificate signature public key as hex string.
bits n
Number of bits used for certificate signature key
self_signed boolean
Is certificate signature self signed.
sha1_hash hash
The SHA1 hash of the certificate as hex string.
sha256_hash hash
The SHA256 hash of the certificate as hex string.
tls::connection channel
Returns the current connection status of an SSL channel. The result is a list of key-value pairs describing the connected peer.
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 boolean
Whether protocol renegotiation is supported or not.
securitylevel 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.
Cipher Info
cipher cipher
The current cipher in use for the connection.
standard_name name
The standard RFC name of cipher.
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.
id id
The OpenSSL cipher id.
description string
A text description of the cipher.
Session Info
alpn protocol
The protocol selected after Application-Layer Protocol Negotiation (ALPN).
resumable boolean
Can the session 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?
Returns a list of supported ciphers available for protocol, where protocol must be one of ssl2, ssl3, tls1, tls1.1, tls1.2, or tls1.3. 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.
tls::protocols
Returns a list of supported protocols. Valid values are: ssl2, ssl3, tls1, tls1.1, tls1.2, and tls1.3. Exact list depends on OpenSSL version and compile time flags.
tls::version
Returns the OpenSSL version string.

tls::hash type data
Calculate the hash for data using type digest. Returns value as a hex string. Type cam be any OpenSSL supported hash digest including: md4, md5, sha1, sha2 (sha224, sha256, sha384, sha512, sha512-224, and sha512-256), sha3 (sha3-224, sha3-256, sha3-384, and sha3-512), etc.
tls::hashes
Returns a list of the valid hash digests.
tls::md4 data
Calculate the hash for data using the MD4 message-digest algorithm.
tls::md5 data
Calculate the hash for data using the MD5 message-digest algorithm.
tls::sha1 data
Calculate the hash for data using the SHA-1 secure hash algorithm.
tls::sha256 data
Calculate the hash for data using the SHA-2 SHA-256 secure hash algorithm.

CALLBACK OPTIONS

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. If the callback generates an error, the bgerror command with be invoked with the error information.

-command callback
Invokes the specified callback script at several points during the OpenSSL handshake and use. See below for the possible arguments passed to the callback script. Values returned from the callback are ignored.

error channel message
The message argument contains an error message generated by the OpenSSL function ERR_reason_error_string().

info channel major minor message type
This form of callback is invoked by the OpenSSL function SSL_CTX_set_info_callback() during connection setup and use.
  • Possible values for major are: handshake, alert, connect, accept.
  • Possible values for minor are: start, done, read, write, loop, exit.
  • The message argument is a descriptive string which may be generated either by SSL_state_string_long() or by SSL_alert_desc_string_long(), depending on the context.
  • For alerts, the possible values for type are: warning, fatal, and unknown. For others, info is used.
message channel direction version content_type data
This form of callback is invoked by the OpenSSL function SSL_set_msg_callback() whenever a message is sent or received. It is only available when OpenSSL is complied with the enable-ssl-trace option. Where direction is Sent or Received, version is the protocol version, content_type is the message content type, and data is more info on the message from the SSL_trace API.

session channel session_id ticket lifetime
This form of callback is invoked by the OpenSSL function SSL_CTX_sess_set_new_cb(). Where session_id is the current session identifier, ticket is the session ticket info, and lifetime is the the ticket lifetime in seconds.


-password callback
Invokes the specified callback script when OpenSSL needs to obtain a password. See below for the possible arguments passed to the callback script. See below for valid return values.

password rwflag size
Invoked when loading or storing a PEM certificate with encryption. Where rwflag is 0 for reading/decryption or 1 for writing/encryption (can prompt user to confirm) and size is the max password length in bytes. The callback should return the password as a string.

-validatecommand callback
Invokes the specified callback script during handshake in order to validate the provided value(s). See below for the possible arguments passed to the callback script. To reject the value and abort 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.

alpn channel protocol match
For servers, this form of callback is invoked when the client ALPN extension is received. If match is true, protocol is the first -alpn specified protocol common to the both the client and server. If not, the first client specified protocol is used. Called after hello and ALPN callbacks.

hello channel servername
For servers, this form of callback is invoked during client hello message processing. It is used to select an appropriate certificate to present, and make other configuration adjustments relevant to that server name and its configuration. Called before SNI and ALPN callbacks.

sni channel servername
For servers, this form of callback is invoked when the SNI extension from the client is received. Where servername is the client provided server name from the -servername option. This is used when a server supports multiple names, so the right certificate can be used. Called after hello callback but before ALPN callback.

verify channel depth cert status error
This form of callback is invoked by OpenSSL when a new certificate is received from the peer. It allows the client to check the certificate verification results and choose whether to continue or not. It is called for each certificate in the certificate chain.
  • The depth argument is an integer representing the current depth on the certificate chain, with 0 as the peer certificate and higher values going up to the Certificate Authority (CA).
  • The cert argument is a list of key-value pairs similar to those returned by tls::status.
  • The status argument is an boolean representing the validity of the current certificate. A value of 0 means the certificate is deemed invalid. A value of 1 means the certificate is deemed valid.
  • The error argument supplies the message, if any, generated by 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 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.

The use of the reference callbacks tls::callback, tls::password, and tls::validate_command is not recommended. They may be removed from future releases.

The use of the variable tls::debug is not recommended. It may be removed from future releases.

DEBUG

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.

HTTPS EXAMPLE

This example uses a sample server.pem provided with the TLS release, courtesy of the OpenSSL project.


package require http
package require tls

http::register https 443 [list ::tls::socket -autoservername true -require true -cadir /etc/ssl/certs]

set tok [http::geturl https://www.tcl.tk/]

SPECIAL CONSIDERATIONS

The capabilities of this package can vary enormously based upon how your OpenSSL library was configured and built. New versions may obsolete older protocol versions, add or remove ciphers, change default values, etc. Use the tls::ciphers and tls::protocols commands to obtain the supported versions.

SEE ALSO

socket, fileevent, OpenSSL


Copyright © 1999 Matt Newman.
Copyright © 2004 Starfish Systems.