ssl
A familiar OpenSSL style interface for secure connections, on top of Salam's own TLS.
import ssl
This package gives the same OpenSSL style context and connection interface, but underneath it is Salam's own tls and it needs no outside library. For new code tls is simpler; this package is for code already written in this style. Set the hostname before Connect when certificates are verified.
The flow: make a context, hand the TCP connection to it with Wrap, for a client set the hostname and call Connect, then read and write.
Types
Context
struct Context| Name | Type | Description |
|---|---|---|
handle | void* | The pointer to the context state. |
import ssl
import net.tcp
func main:
ctx := ssl.NewClientContext()
if not ssl.CtxOk(ctx):
println ssl.LastErrorString()
ret
end
_ := ssl.SetMinVersion(ctx, ssl.TLS1_3)
_ := ssl.SetDefaultVerifyPaths(ctx)
sock := tcp.Dial("example.com", 443)
conn := ssl.Wrap(ctx, sock.fd)
_ := ssl.SetHostname(conn, "example.com")
if not ssl.ConnOk(conn) or not ssl.Connect(conn):
println "the handshake failed:", ssl.LastErrorString()
ret
end
println ssl.PeerVerified(conn), ssl.VerifyResult(conn) == ssl.CERT_OK
n := ssl.Write(conn, "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")
if n <= 0:
println "error code:", ssl.ErrorCode(conn, n)
end
println ssl.Read(conn, 4096).len() > 0
ssl.Shutdown(conn)
ssl.Close(conn)
ssl.CloseContext(ctx)
tcp.Close(sock)
endConn
struct Conn| Name | Type | Description |
|---|---|---|
handle | void* | The pointer to the connection state. |
The example for this item is shown with the ones above: ssl-Client.salam
Constants and variables
VERIFY_NONE
const VERIFY_NONE: i32 = 0import ssl
import net.tcp
func main:
ctx := ssl.NewServerContext("cert.pem", "key.pem")
if not ssl.CtxOk(ctx):
println ssl.LastErrorString()
ret
end
ssl.SetVerify(ctx, ssl.VERIFY_NONE)
_ := ssl.LoadVerifyLocationsFile(ctx, "clients-ca.pem")
ssl.SetVerify(ctx, ssl.VERIFY_PEER)
println ssl.FILETYPE_PEM, ssl.TLS1_2
server := tcp.Bind(8443)
until true:
sock := tcp.Accept(server)
conn := ssl.Wrap(ctx, sock.fd)
if ssl.Accept(conn):
request := ssl.Read(conn, 4096)
_ := ssl.Write(conn, "HTTP/1.1 200 OK\r\nContent-Length: " + request.len() + "\r\n\r\n" + request)
end
ssl.Close(conn)
end
endVERIFY_PEER
const VERIFY_PEER: i32 = 1The example for this item is shown with the ones above: ssl-Server.salam
FILETYPE_PEM
const FILETYPE_PEM: i32 = 1The example for this item is shown with the ones above: ssl-Server.salam
TLS1_2
const TLS1_2: i32 = 771The example for this item is shown with the ones above: ssl-Server.salam
TLS1_3
const TLS1_3: i32 = 772The example for this item is shown with the ones above: ssl-Client.salam
CERT_OK
const CERT_OK: i64 = 0The example for this item is shown with the ones above: ssl-Client.salam
Functions
ErrorCode
func ErrorCode(c: Conn, rc: int): intThe example for this item is shown with the ones above: ssl-Client.salam
ConnErrorString
func ConnErrorString(c: Conn): strLastErrorString it is not shared between threads.The example for this item is shown with the ones above: ssl-Client.salam
LastErrorString
func LastErrorString(): strThe example for this item is shown with the ones above: ssl-Client.salam
Wrap
func Wrap(ctx: Context, fd: i64): ConnThe example for this item is shown with the ones above: ssl-Client.salam
ConnOk
func ConnOk(c: Conn): boolThe example for this item is shown with the ones above: ssl-Client.salam
Connect
func Connect(c: Conn): boolThe example for this item is shown with the ones above: ssl-Client.salam
Accept
func Accept(c: Conn): boolThe example for this item is shown with the ones above: ssl-Server.salam
Read
func Read(c: Conn, max: int): strThe example for this item is shown with the ones above: ssl-Client.salam
Write
func Write(c: Conn, data: str): intThe example for this item is shown with the ones above: ssl-Client.salam
Shutdown
func Shutdown(c: Conn)The example for this item is shown with the ones above: ssl-Client.salam
Close
func Close(c: Conn)The example for this item is shown with the ones above: ssl-Client.salam
SetHostname
func SetHostname(c: Conn, host: str): boolThe example for this item is shown with the ones above: ssl-Client.salam
VerifyResult
func VerifyResult(c: Conn): i64The example for this item is shown with the ones above: ssl-Client.salam
PeerVerified
func PeerVerified(c: Conn): boolThe example for this item is shown with the ones above: ssl-Client.salam
NewClientContext
func NewClientContext(): ContextThe example for this item is shown with the ones above: ssl-Client.salam
NewServerContext
func NewServerContext(cert_file: str, key_file: str): ContextThe example for this item is shown with the ones above: ssl-Server.salam
CtxOk
func CtxOk(ctx: Context): boolThe example for this item is shown with the ones above: ssl-Client.salam
SetVerify
func SetVerify(ctx: Context, mode: int)The example for this item is shown with the ones above: ssl-Server.salam
LoadVerifyLocationsFile
func LoadVerifyLocationsFile(ctx: Context, path: str): boolThe example for this item is shown with the ones above: ssl-Server.salam
SetDefaultVerifyPaths
func SetDefaultVerifyPaths(ctx: Context): boolThe example for this item is shown with the ones above: ssl-Client.salam
SetMinVersion
func SetMinVersion(ctx: Context, version: i64): boolThe example for this item is shown with the ones above: ssl-Client.salam
CloseContext
func CloseContext(ctx: Context)The example for this item is shown with the ones above: ssl-Client.salam