├── LICENSE ├── README.md ├── nimasyncsslclient.nim ├── nimasyncsslclient.nim.cfg ├── nimasyncsslserver.nim └── nimasyncsslserver.nim.cfg /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 David Krause 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This contains example tcp client and server Nim code for speaking 2 | to an ssl server which has a self signed certificate. 3 | 4 | The server creates a keypair on first start. 5 | 6 | The "publicKey.pem" that the server creates 7 | must be transmitted securely to the client. 8 | -------------------------------------------------------------------------------- /nimasyncsslclient.nim: -------------------------------------------------------------------------------- 1 | import asyncnet, asyncdispatch, net, os, openssl 2 | 3 | var client: AsyncSocket 4 | let port = 9090 5 | let host = "127.0.0.1" 6 | let serverPublicKey = "publicKey.pem" 7 | 8 | proc handle(client: AsyncSocket) {.async.} = 9 | ## We speek to the ssl server. 10 | var cnt = 0 11 | while true: 12 | await client.send("TEST " & $cnt & "\n") 13 | echo await client.recvLine() 14 | cnt.inc 15 | await sleepAsync(1000) 16 | 17 | proc main() {.async.} = 18 | if not fileExists(serverPublicKey): 19 | echo "[-] could not find server's public key at: ", serverPublicKey 20 | quit() 21 | client = newAsyncSocket() 22 | var ctx = newContext(verifyMode = CVerifyPeer) 23 | discard SSL_CTX_load_verify_locations(ctx.context, "publicKey.pem", "") # we gonna trust our self signed certificat 24 | wrapSocket(ctx, client) # enables SSL for this socket. 25 | try: 26 | await client.connect(host, Port port) 27 | except: 28 | echo "[-] could not connect to server: ", host , ":", port 29 | quit() 30 | await client.handle() 31 | 32 | waitFor main() -------------------------------------------------------------------------------- /nimasyncsslclient.nim.cfg: -------------------------------------------------------------------------------- 1 | -d:ssl -------------------------------------------------------------------------------- /nimasyncsslserver.nim: -------------------------------------------------------------------------------- 1 | import asyncnet, asyncdispatch, net, osproc, os 2 | 3 | var server: AsyncSocket 4 | let port = 9090 5 | let publicKey = "publicKey.pem" 6 | let secretKey = "secretKey.pem" 7 | 8 | proc handle(client: AsyncSocket) {.async.} = 9 | while true: 10 | var line: string = "" 11 | try: 12 | line = await client.recvLine() 13 | except: 14 | echo "socket breaks in read:", getCurrentExceptionMsg() 15 | break 16 | if line == "": 17 | echo "client disconnected" 18 | if not client.isClosed: 19 | client.close() 20 | break 21 | try: 22 | await client.send("GOT: " & line & "\n") 23 | except: 24 | echo "socket breaks in send:", getCurrentExceptionMsg() 25 | break 26 | 27 | proc createKeyFiles() = 28 | ## creates neccessary certificates for ssl socket. 29 | echo "[+] going to create ssl certificates" 30 | let res = execCmd "openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout " & secretKey & " -out " & publicKey 31 | if res != 0: 32 | echo "[-] could not create keyfiles" 33 | quit() 34 | echo "[+] keys created" 35 | 36 | proc main() {.async.} = 37 | if not (fileExists(publicKey) or fileExists(secretKey)): createKeyFiles() 38 | server = newAsyncSocket() 39 | server.setSockOpt(OptReuseAddr, true) 40 | server.setSockOpt(OptReusePort, true) 41 | server.bindAddr(Port port) 42 | server.listen() 43 | var ctx = newContext(certFile = publicKey, keyFile = secretKey) 44 | wrapSocket(ctx, server) 45 | echo "listening on port ", port 46 | while true: 47 | var (address, client) = await server.acceptAddr() 48 | echo "connection from: ", address 49 | wrapConnectedSocket(ctx, client, handshakeAsServer) 50 | asyncCheck client.handle 51 | 52 | waitFor main() -------------------------------------------------------------------------------- /nimasyncsslserver.nim.cfg: -------------------------------------------------------------------------------- 1 | -d:ssl --------------------------------------------------------------------------------