# Using MbedTLS to create an encrypted TCP server

**URL:** https://discourse.julialang.org/t/using-mbedtls-to-create-an-encrypted-tcp-server/60927
**Category:** New to Julia
**Created:** [May 11, 2021, 4:28am UTC](https://discourse.julialang.org/t/using-mbedtls-to-create-an-encrypted-tcp-server/60927 "2021-05-11T04:28:56Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![ligert123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ligert123/32/24682_2.png) [@ligert123](https://discourse.julialang.org/u/ligert123)
#### Post date: [May 13, 2021, 12:42am UTC](https://discourse.julialang.org/t/using-mbedtls-to-create-an-encrypted-tcp-server/60927/3 "2021-05-13T00:42:51Z")

</div>

I was mainly just looking for a tutorial on how to actually start a server and have it encrypted with MbedTLS, i managed to figure it out eventually though so i guess this thread is not required.

Here is a client and server example i made for other users who may be looking for the same thing.  
The certificate can be made on linux using: ` openssl req -x509 -nodes -newkey rsa:2048 -keyout selfsigned.key -out selfsigned.cert` this will however not be able to run unless certificate verification is disabled.  
client:

```julia
using Sockets
using MbedTLS
client=Sockets.connect(8080)
entropy = MbedTLS.Entropy()
rng = MbedTLS.CtrDrbg()
MbedTLS.seed!(rng, entropy)
ctx = MbedTLS.SSLContext()
conf = MbedTLS.SSLConfig()
MbedTLS.config_defaults!(conf)
MbedTLS.authmode!(conf, MbedTLS.MBEDTLS_SSL_VERIFY_NONE)
MbedTLS.rng!(conf, rng)

function show_debug(level, filename, number, msg)
    println((level, filename, number, msg))
    # println(msg)
end

MbedTLS.dbg!(conf, show_debug)
MbedTLS.set_dbg_level(MbedTLS.ERROR)
MbedTLS.ca_chain!(conf)
MbedTLS.setup!(ctx, conf)
MbedTLS.set_bio!(ctx, client)
MbedTLS.handshake(ctx)

while(true)
    write(ctx,UInt8[1,1,1,1,1,1,1,1,1,1,1,1])
    flush(ctx)
    data = []
    while(true)
        data = readavailable(ctx)
        if(length(data) > 0)
            break
        end
        sleep(0.1)
    end
    println(data)

end

```

server:

```julia
using Sockets
using MbedTLS
server = Sockets.listen(8080)

conn = accept(server)
entropy = MbedTLS.Entropy()
rng = MbedTLS.CtrDrbg()
MbedTLS.seed!(rng, entropy)
ctx = MbedTLS.SSLContext()
conf = MbedTLS.SSLConfig("selfsigned.cert", "selfsigned.key")

# MbedTLS.config_defaults!(conf)
MbedTLS.authmode!(conf, MbedTLS.MBEDTLS_SSL_VERIFY_NONE)
MbedTLS.rng!(conf, rng)
MbedTLS.ca_chain!(conf)
MbedTLS.setup!(ctx, conf)
MbedTLS.associate!(ctx, conn)
MbedTLS.handshake(ctx)   

while(true)
    data = readavailable(ctx)
    sleep(0.01)

    if(length(data) == 0)
        continue
    end
    println(data)
    write(ctx,data)
    flush(ctx)
end

```

---

_[View the full topic](https://discourse.julialang.org/t/using-mbedtls-to-create-an-encrypted-tcp-server/60927)._
