# 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:** 3\
**Page:** 1

<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 11, 2021, 4:28am UTC](https://discourse.julialang.org/t/using-mbedtls-to-create-an-encrypted-tcp-server/60927/1 "2021-05-11T04:28:57Z")

</div>

Hello, any ideas on how to use MbedTLS to encrypt new incoming connections?  
I have been having a lot of trouble finding anything on doing this

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 12, 2021, 2:07pm UTC](https://discourse.julialang.org/t/using-mbedtls-to-create-an-encrypted-tcp-server/60927/2 "2021-05-12T14:07:44Z")

</div>

What are you having trouble with beyond [Usage of MbedTLS with TCP sockets](https://discourse.julialang.org/t/usage-of-mbedtls-with-tcp-sockets/60811)? I think you may have more success finding help if you can describe your goal and difficulties in a bit more detail.

---

<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

```
