CyaSSL example using x509 certs and DTLS over ethernet.

Dependencies:   EthernetInterface NTPClient cyassl-lib mbed-rtos mbed-src CyaSSL_DTLS_Ethernet

Dependents:   CyaSSL_DTLS_Ethernet

Testing DTLS out

To test this client you need to setup a server and use the correct CA certificate, server certificate, and server private key. This will allow the client to authenticate to the server, and enable the server to authenticate the client. The server can authenticate the client because the test program includes a device certificate signed by the server CA.

The server certificate and and CA certificate are identical as this is a self-signed certificate:

-----BEGIN CERTIFICATE-----
MIIClTCCAf6gAwIBAgICEREwDQYJKoZIhvcNAQEFBQAwZzELMAkGA1UEBhMCR0Ix
EjAQBgNVBAgMCUJlcmtzaGlyZTEQMA4GA1UEBwwHTmV3YnVyeTERMA8GA1UECgwI
Vm9kYWZvbmUxDDAKBgNVBAsMA1ImRDERMA8GA1UEAwwIRE1TZXJ2ZXIwHhcNMTMw
OTAzMTQwNjA4WhcNMjMwNzEzMTQwNjA4WjBnMQswCQYDVQQGEwJHQjESMBAGA1UE
CAwJQmVya3NoaXJlMRAwDgYDVQQHDAdOZXdidXJ5MREwDwYDVQQKDAhWb2RhZm9u
ZTEMMAoGA1UECwwDUiZEMREwDwYDVQQDDAhETVNlcnZlcjCBnzANBgkqhkiG9w0B
AQEFAAOBjQAwgYkCgYEAupWZHm51RbMkEkvKAvglM96BcWVScxW7KaXFhm1Artt1
1Vm5KTC0rI+0kiG54kxhvY7euWeUcQqJKHxUTFjUWv8TcJrzmjIe5EthipLpdN+V
/PJCO/FiLXSiykQsC+VhyU8BKNYrpspyiQ109KPoybH8kK7W2IXf2d9AaLrzcgUC
AwEAAaNQME4wHQYDVR0OBBYEFKonGm+IcowtLcJaxXSCpUTRPaMVMB8GA1UdIwQY
MBaAFKonGm+IcowtLcJaxXSCpUTRPaMVMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcN
AQEFBQADgYEAfThlu2u73hm3quZJX57joMRn/N+l2KY4q16YI+gZIoJlLF/uIZw6
4OuxfKNfIvKvCL54LQ+/plh+8CzsmZdjdV9S/1+Jefe+RhEogjSvFjs2oyVaMCjZ
OxWujvZJ3XdhpXZJsdnEx4rgmHij3es3SzarTSjPVW8MpBU4H8NKlWI=
-----END CERTIFICATE-----

The server private key is as follows:

-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALqVmR5udUWzJBJL
ygL4JTPegXFlUnMVuymlxYZtQK7bddVZuSkwtKyPtJIhueJMYb2O3rlnlHEKiSh8
VExY1Fr/E3Ca85oyHuRLYYqS6XTflfzyQjvxYi10ospELAvlYclPASjWK6bKcokN
dPSj6Mmx/JCu1tiF39nfQGi683IFAgMBAAECgYEAuAFGQtud7YHQRfbWHv2G+tMp
BqJsoDBDJrxjwsFFs+ucFi5oyzVMSI1j/2UhQwoerekSvvdmTeCdCP8rxysnJ3MG
vepBkUXZ9kek7qZ561JC6qLZNcTeVc/R1rxMsFZSV/Zuzr7KFG47J/Di15GxGoui
x4fRE9ww0coC8PDWbaECQQDgKP48PD2gNaD2jjqd/xNki2kXP9PE1AAnevVaEidS
ceOKXgqx7qGYrN1+NFnRGwJhUSoicmUQeDKkC9CU/5UpAkEA1RZCT9M8lWpqx60d
oZlF9I3oOoQxXrx4RgoF+2y1tT+C7nUwmeYk2mEi/Gat9/NtKz/J9Tlwu3/Ef9eB
G7lVfQJAdvzLl2XSwIw3GSl+bAfOX3yxGCpFWyG9wzc9rcYdIWJbCkPJIZIuLUD1
gdyAIMNZjBbtasOWahujpfivi6fvoQJAHRIdV+CculG7YaR3j21mwtd9YEZaqe8S
hZOmf0e0fpa8mSW5zLU+P+DSshacE2DNewIH3oHIpHvo5fwTrCGbzQJAWaFq9L9C
lAgoPfuKjKQ0RJ8M+lDVOZo/Okl0VtrU/Z5fQm+yR4Lb+o1iogYjgjvdOmoMkPtb
Q/xu4c1qn2ksnA==
-----END PRIVATE KEY-----

Both the certificate and the private key are in PEM format. It is worth noting that all the certificates and keys on the device are in DER format to save space. You might be wondering how to easily get PEM keys and certs into a format suitable for the mbed, well it's easy, you can just use the openssl tools to convert, and then xxd to get a hex sequence which you can put into an array. To convert a cert, use the following command:

openssl x509 -in cert.pem -outform DER | xxd -i

This will dump the x509 cert in DER format and pipe it to xxd, where the -i switch is used to convert the byte sequence into c-include style. All it really does is dump the bytes in hexadecimal, you still need to wrap it in an array like this:

const static unsigned char deviceCertificate[] = {
   OUTPUT OF ABOVE
};
const static int deviceCertificateLength = sizeof(deviceCertificate);

For converting keys, the command is the same buy you use the rsa subcommand of openssl (or dsa if you used DSA keys):

openssl rsa -in key.pem -outform DER | xxd -i

In anycase, to use the PEM files with the CyaSSL server, download the CyaSSL example server from here: http://yassl.com/yaSSL/download/downloadForm.php. Configure it with debugging and dtls before compiling (I've also enabled PSK because I'm testing that):

./configure --enable-psk --enable-debug --enable-dtls

Then do

make

Now, the cyaSSL server needs the PEM certificates above to work with those setup on the mbed device, but it is particular about where these PEM certs are found. You need to put them in the certs directory in the root of the unzipped code (it seems to ignore flags which point it elsewhere).

And for some reason cyaSSL calls the CA certificate the "client-cert.pem", so copy the server certificate above into "certs/client-cert.pem" and also copy it into "certs/server-cert.pem". Copy the server private key into "certs/server-key.pem".

Now change directory into example/server/ and run the server like this:

./server -u -b -f

These flags are -u for UDP i.e DTLS, -b to bind to all interfaces, and -f to group messages (fewer packets). For some reason this last flag is needed to work properly with the mbed.

You should now be able to fire up the mbed, change the code to point to your server, and see the handshake and test messages go back and forth. You can see it works because you should see the message "I hear you fa shizzle!" on the mbed (this is the message they define), and you should see the message "onion" received on the server.

Generating your own keys and certificates

OK so you want to generate your own self-signed certificate for your server and device. Great, but I'm too busy/lazy to write a detailed tutorial right now. Instead I've created you a shell script that randomly generates a new self-signed CA/server-cert and uses this to sign a device certificate.

The device certificate and private key are placed into a directory called mbed/certs where as the server certs are placed in server/certs. You'll have to read up on these commands to work out how to generate and sign more device certs, and how to change options etc. HTH.

#!/bin/sh
CA_KEY_FILE=ca_key.pem
CA_KEY_FORMAT=PEM
CA_KEY_ALGORITHM=rsa
CA_KEY_BITS=1024
# 1. Make a key for the CA (in this case the server)
echo Generating $CA_KEY_ALGORITHM key: $CA_KEY_FILE
openssl genpkey \
	-out $CA_KEY_FILE \
	-outform $CA_KEY_FORMAT \
	-algorithm $CA_KEY_ALGORITHM \
	-pkeyopt rsa_keygen_bits:$CA_KEY_BITS
echo DONE 
# extract public key (for vanity)
openssl pkey \
	-in $CA_KEY_FILE \
	-pubout \
	-out $CA_KEY_FILE.pub

# 2. Make a self signed certificate for the CA (trusted certificate)
# req - PKCS#10 certificate request and certificate generating utility.
CA_CERT_EXPIRY=3600
CA_CERT_SERIAL=0x1111
CA_CERT_FILE=ca_cert.pem
CA_CERT_REQ=ca_cert_req.pem
CA_ID=DMServer
echo Generating self signed root CA certificate: $CA_CERT_FILE
openssl req \
	-new \
	-key $CA_KEY_FILE \
	-days $CA_CERT_EXPIRY \
	-set_serial $CA_CERT_SERIAL \
	-subj "/C=GB/ST=Berkshire/L=Newbury/O=Vodafone/OU=R&D/CN=$CA_ID" \
	-out $CA_CERT_FILE \
	-x509 \
	-verbose 
echo DONE.

# 3. Make a key for the device
DEV_KEY_FILE=dev_key.pem
DEV_KEY_FORMAT=PEM
DEV_KEY_ALGORITHM=rsa
DEV_KEY_BITS=1024
echo Generating $DEV_KEY_ALGORITHM key: $DEV_KEY_FILE
openssl genpkey \
	-out $DEV_KEY_FILE \
	-outform $DEV_KEY_FORMAT \
	-algorithm $DEV_KEY_ALGORITHM \
	-pkeyopt rsa_keygen_bits:$DEV_KEY_BITS
echo DONE 
# extract public key (for vanity)
openssl pkey \
	-in $DEV_KEY_FILE \
	-pubout \
	-out $DEV_KEY_FILE.pub

# 4. Make a certificate request for the device
DEV_CERT_EXPIRY=3600
DEV_CERT_SERIAL=2222
DEV_CERT_REQ_FILE=dev_cert_req.pem
DEV_CERT_FILE=dev_cert.pem
DEV_ID=Device1
echo Generating certificate request for device: $DEV_CERT_REQ_FILE
openssl req \
	-new \
	-key $DEV_KEY_FILE \
	-days $DEV_CERT_EXPIRY \
	-set_serial $DEV_CERT_SERIAL \
	-subj "/C=GB/ST=Berkshire/L=Newbury/O=Vodafone/OU=R&D/CN=$DEV_ID" \
	-out $DEV_CERT_REQ_FILE \
	-verbose 

echo DONE.

# 5. Sign the device certificate using the root CA certificate
echo Creating directories and other stuff I really wish was not needed
mkdir -p demoCA/newcerts
touch demoCA/index.txt
echo $DEV_CERT_SERIAL > demoCA/serial
echo DONE.
# do the actual signing
echo Signing certificate request $DEV_CERT_REQ_FILE with certificate $CA_CERT_FILE
openssl ca \
	-in $DEV_CERT_REQ_FILE \
	-out $DEV_CERT_FILE \
	-days $DEV_CERT_EXPIRY \
	-keyfile $CA_KEY_FILE \
	-cert $CA_CERT_FILE \
	-batch \
	-verbose

echo DONE.

# 6 convert this stuff into a format suitable for mbed
echo Creating mbed include files
MBED_DIR=mbed/certs
mkdir -p $MBED_DIR
# private key
OUT=$MBED_DIR/device_private_key.h
echo "#pragma once" > $OUT
echo "const static unsigned char devicePrivateKey[] = {" >> $OUT
openssl $DEV_KEY_ALGORITHM -in $DEV_KEY_FILE -outform DER | xxd -i >> $OUT
echo "};" >> $OUT
echo "const static int devicePrivateKeyLength = sizeof(devicePrivateKey);" >> $OUT

# device cert
OUT=$MBED_DIR/device_certificate.h
echo "#pragma once" > $OUT
echo "const static unsigned char deviceCertificate[] = {" >> $OUT
openssl x509 -in $DEV_CERT_FILE -outform DER | xxd -i >> $OUT
echo "};" >> $OUT
echo "const static int deviceCertificateLength = sizeof(deviceCertificate);" >> $OUT

# CA cert
OUT=$MBED_DIR/root_certificate.h
echo "#pragma once" > $OUT
echo "const static unsigned char rootCertificate[] = {" >> $OUT
openssl x509 -in $CA_CERT_FILE -outform DER | xxd -i >> $OUT
echo "};" >> $OUT
echo "const static int rootCertificateLength = sizeof(rootCertificate);" >> $OUT
echo DONE.

# create (copy) the server files for CyaSSL
echo Creating PEM file structure of CyaSSL server
OUT=server/certs
mkdir -p $OUT
cp $CA_CERT_FILE $OUT/client_cert.pem
cp $CA_CERT_FILE $OUT/server_cert.pem
cp $CA_KEY_FILE $OUT/server_key.pem
echo DONE.
Committer:
ashleymills
Date:
Thu Sep 19 13:26:15 2013 +0000
Revision:
4:df1e7ada3ef2
Parent:
0:00174d07d068
Added DN verification.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:00174d07d068 1 #ifndef BSD_SOCKET_H_
ashleymills 0:00174d07d068 2 #define BSD_SOCKET_H_
ashleymills 0:00174d07d068 3
ashleymills 0:00174d07d068 4 #ifdef __cplusplus
ashleymills 0:00174d07d068 5 extern "C" {
ashleymills 0:00174d07d068 6 #endif
ashleymills 0:00174d07d068 7
ashleymills 0:00174d07d068 8 #include "lwip/sockets.h"
ashleymills 0:00174d07d068 9
ashleymills 0:00174d07d068 10 #include "lwip/inet.h"
ashleymills 0:00174d07d068 11
ashleymills 0:00174d07d068 12 #include "lwip/netdb.h"
ashleymills 0:00174d07d068 13
ashleymills 0:00174d07d068 14 //Sockets
ashleymills 0:00174d07d068 15
ashleymills 0:00174d07d068 16 inline int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
ashleymills 0:00174d07d068 17 {
ashleymills 0:00174d07d068 18 return lwip_accept(s, addr, addrlen);
ashleymills 0:00174d07d068 19 }
ashleymills 0:00174d07d068 20
ashleymills 0:00174d07d068 21 inline int bind(int s, const struct sockaddr *name, socklen_t namelen)
ashleymills 0:00174d07d068 22 {
ashleymills 0:00174d07d068 23 return lwip_bind(s, name, namelen);
ashleymills 0:00174d07d068 24 }
ashleymills 0:00174d07d068 25
ashleymills 0:00174d07d068 26 inline int shutdown(int s, int how)
ashleymills 0:00174d07d068 27 {
ashleymills 0:00174d07d068 28 return lwip_shutdown(s, how);
ashleymills 0:00174d07d068 29 }
ashleymills 0:00174d07d068 30
ashleymills 0:00174d07d068 31 inline int getsockname (int s, struct sockaddr *name, socklen_t *namelen)
ashleymills 0:00174d07d068 32 {
ashleymills 0:00174d07d068 33 return lwip_getsockname(s, name, namelen);
ashleymills 0:00174d07d068 34 }
ashleymills 0:00174d07d068 35
ashleymills 0:00174d07d068 36 inline int getpeername (int s, struct sockaddr *name, socklen_t *namelen)
ashleymills 0:00174d07d068 37 {
ashleymills 0:00174d07d068 38 return lwip_getpeername(s, name, namelen);
ashleymills 0:00174d07d068 39 }
ashleymills 0:00174d07d068 40
ashleymills 0:00174d07d068 41 inline int getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen)
ashleymills 0:00174d07d068 42 {
ashleymills 0:00174d07d068 43 return lwip_getsockopt(s, level, optname, optval, optlen);
ashleymills 0:00174d07d068 44 }
ashleymills 0:00174d07d068 45
ashleymills 0:00174d07d068 46 inline int setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen)
ashleymills 0:00174d07d068 47 {
ashleymills 0:00174d07d068 48 return lwip_setsockopt(s, level, optname, optval, optlen);
ashleymills 0:00174d07d068 49 }
ashleymills 0:00174d07d068 50
ashleymills 0:00174d07d068 51 inline int connect(int s, const struct sockaddr *name, socklen_t namelen)
ashleymills 0:00174d07d068 52 {
ashleymills 0:00174d07d068 53 return lwip_connect(s, name, namelen);
ashleymills 0:00174d07d068 54 }
ashleymills 0:00174d07d068 55
ashleymills 0:00174d07d068 56 inline int listen(int s, int backlog)
ashleymills 0:00174d07d068 57 {
ashleymills 0:00174d07d068 58 return lwip_listen(s, backlog);
ashleymills 0:00174d07d068 59 }
ashleymills 0:00174d07d068 60
ashleymills 0:00174d07d068 61 inline int recv(int s, void *mem, size_t len, int flags)
ashleymills 0:00174d07d068 62 {
ashleymills 0:00174d07d068 63 return lwip_recv(s, mem, len, flags);
ashleymills 0:00174d07d068 64 }
ashleymills 0:00174d07d068 65
ashleymills 0:00174d07d068 66 inline int recvfrom(int s, void *mem, size_t len, int flags,
ashleymills 0:00174d07d068 67 struct sockaddr *from, socklen_t *fromlen)
ashleymills 0:00174d07d068 68 {
ashleymills 0:00174d07d068 69 return lwip_recvfrom(s, mem, len, flags, from, fromlen);
ashleymills 0:00174d07d068 70 }
ashleymills 0:00174d07d068 71
ashleymills 0:00174d07d068 72 inline int send(int s, const void *dataptr, size_t size, int flags)
ashleymills 0:00174d07d068 73 {
ashleymills 0:00174d07d068 74 return lwip_send(s, dataptr, size, flags);
ashleymills 0:00174d07d068 75 }
ashleymills 0:00174d07d068 76
ashleymills 0:00174d07d068 77 inline int sendto(int s, const void *dataptr, size_t size, int flags,
ashleymills 0:00174d07d068 78 const struct sockaddr *to, socklen_t tolen)
ashleymills 0:00174d07d068 79 {
ashleymills 0:00174d07d068 80 return lwip_sendto(s, dataptr, size, flags, to, tolen);
ashleymills 0:00174d07d068 81 }
ashleymills 0:00174d07d068 82
ashleymills 0:00174d07d068 83 inline int socket(int domain, int type, int protocol)
ashleymills 0:00174d07d068 84 {
ashleymills 0:00174d07d068 85 return lwip_socket(domain, type, protocol);
ashleymills 0:00174d07d068 86 }
ashleymills 0:00174d07d068 87
ashleymills 0:00174d07d068 88 inline int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
ashleymills 0:00174d07d068 89 struct timeval *timeout)
ashleymills 0:00174d07d068 90 {
ashleymills 0:00174d07d068 91 return lwip_select(maxfdp1, readset, writeset, exceptset, timeout);
ashleymills 0:00174d07d068 92 }
ashleymills 0:00174d07d068 93
ashleymills 0:00174d07d068 94 inline int ioctlsocket(int s, long cmd, void *argp)
ashleymills 0:00174d07d068 95 {
ashleymills 0:00174d07d068 96 return lwip_ioctl(s, cmd, argp);
ashleymills 0:00174d07d068 97 }
ashleymills 0:00174d07d068 98
ashleymills 0:00174d07d068 99 inline int read(int s, void *mem, size_t len)
ashleymills 0:00174d07d068 100 {
ashleymills 0:00174d07d068 101 return lwip_read(s, mem, len);
ashleymills 0:00174d07d068 102 }
ashleymills 0:00174d07d068 103
ashleymills 0:00174d07d068 104 inline int write(int s, const void *dataptr, size_t size)
ashleymills 0:00174d07d068 105 {
ashleymills 0:00174d07d068 106 return lwip_write(s, dataptr, size);
ashleymills 0:00174d07d068 107 }
ashleymills 0:00174d07d068 108
ashleymills 0:00174d07d068 109 inline int close(int s)
ashleymills 0:00174d07d068 110 {
ashleymills 0:00174d07d068 111 return lwip_close(s);
ashleymills 0:00174d07d068 112 }
ashleymills 0:00174d07d068 113
ashleymills 0:00174d07d068 114 //DNS
ashleymills 0:00174d07d068 115 /*
ashleymills 0:00174d07d068 116 inline struct hostent *gethostbyname(const char *name)
ashleymills 0:00174d07d068 117 {
ashleymills 0:00174d07d068 118 return lwip_gethostbyname(name);
ashleymills 0:00174d07d068 119 }
ashleymills 0:00174d07d068 120
ashleymills 0:00174d07d068 121 inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop)
ashleymills 0:00174d07d068 122 {
ashleymills 0:00174d07d068 123 return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
ashleymills 0:00174d07d068 124 }*/
ashleymills 0:00174d07d068 125
ashleymills 0:00174d07d068 126 inline void freeaddrinfo(struct addrinfo *ai)
ashleymills 0:00174d07d068 127 {
ashleymills 0:00174d07d068 128 return lwip_freeaddrinfo(ai);
ashleymills 0:00174d07d068 129 }
ashleymills 0:00174d07d068 130
ashleymills 0:00174d07d068 131 inline int getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res)
ashleymills 0:00174d07d068 132 {
ashleymills 0:00174d07d068 133 return lwip_getaddrinfo(nodename, servname, hints, res);
ashleymills 0:00174d07d068 134 }
ashleymills 0:00174d07d068 135
ashleymills 0:00174d07d068 136 #ifdef __cplusplus
ashleymills 0:00174d07d068 137 }
ashleymills 0:00174d07d068 138 #endif
ashleymills 0:00174d07d068 139
ashleymills 0:00174d07d068 140 #endif