|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script generates a self-signed certificate for use by the ESP8266 |
| 4 | +# Replace your-name-here with somethine appropriate before running and use |
| 5 | +# the generated .H files in your code as follows: |
| 6 | +# |
| 7 | +# static const uint8_t rsakey[] ICACHE_RODATA_ATTR = { |
| 8 | +# #include "key.h" |
| 9 | +# }; |
| 10 | +# |
| 11 | +# static const uint8_t x509[] ICACHE_RODATA_ATTR = { |
| 12 | +# #include "x509.h" |
| 13 | +# }; |
| 14 | +# |
| 15 | +# .... |
| 16 | +# WiFiServerSecure server(443); |
| 17 | +# server.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509)); |
| 18 | +# .... |
| 19 | + |
| 20 | +# 1024 or 512. 512 saves memory... |
| 21 | +BITS=512 |
| 22 | +C=$PWD |
| 23 | +pushd /tmp |
| 24 | + |
| 25 | +openssl genrsa -out tls.ca_key.pem $BITS |
| 26 | +openssl genrsa -out tls.key_$BITS.pem $BITS |
| 27 | +openssl rsa -in tls.key_$BITS.pem -out tls.key_$BITS -outform DER |
| 28 | +cat > certs.conf <<EOF |
| 29 | +[ req ] |
| 30 | +distinguished_name = req_distinguished_name |
| 31 | +prompt = no |
| 32 | +
|
| 33 | +[ req_distinguished_name ] |
| 34 | +O = your-name-here |
| 35 | +CN = 127.0.0.1 |
| 36 | +EOF |
| 37 | +openssl req -out tls.ca_x509.req -key tls.ca_key.pem -new -config certs.conf |
| 38 | +openssl req -out tls.x509_$BITS.req -key tls.key_$BITS.pem -new -config certs.conf |
| 39 | +openssl x509 -req -in tls.ca_x509.req -out tls.ca_x509.pem -sha256 -days 5000 -signkey tls.ca_key.pem |
| 40 | +openssl x509 -req -in tls.x509_$BITS.req -out tls.x509_$BITS.pem -sha256 -CAcreateserial -days 5000 -CA tls.ca_x509.pem -CAkey tls.ca_key.pem |
| 41 | +openssl x509 -in tls.ca_x509.pem -outform DER -out tls.ca_x509.cer |
| 42 | +openssl x509 -in tls.x509_$BITS.pem -outform DER -out tls.x509_$BITS.cer |
| 43 | + |
| 44 | +xxd -i tls.key_$BITS | sed 's/.*{//' | sed 's/\};//' | sed 's/unsigned.*//' > "$C/key.h" |
| 45 | +xxd -i tls.x509_$BITS.cer | sed 's/.*{//' | sed 's/\};//' | sed 's/unsigned.*//' > "$C/x509.h" |
| 46 | + |
| 47 | +rm -f tls.ca_key.pem tls.key_$BITS.pem tls.key_$BITS certs.conf tls.ca_x509.req tls.x509_$BITS.req tls.ca_x509.pem tls.x509_$BITS.pem tls.srl tls.x509_$BITS.cer tls.ca_x509.cer |
| 48 | + |
| 49 | +popd |
0 commit comments