Skip to contentSkip to conversation

unit: Add a certificate verification test.

PATCH Applied Andrew Zaborowski 1 participant 8 messages Jul 24, 2015 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 10/17 unit: Add a certificate verification test.

Uses the included example certificate files and corresponding private key files are included for use in future tests (for TLS). This tests most of the cert.c functions and the RSA verify operation.

---
 .gitignore               |  1 +
 Makefile.am              |  7 ++++--
 unit/cert-ca-key.pem     | 27 +++++++++++++++++++++++
 unit/cert-ca.pem         | 26 ++++++++++++++++++++++
 unit/cert-client-key.pem | 27 +++++++++++++++++++++++
 unit/cert-client.pem     | 26 ++++++++++++++++++++++
 unit/cert-server-key.pem | 27 +++++++++++++++++++++++
 unit/cert-server.pem     | 26 ++++++++++++++++++++++
 unit/test-cert.c         | 57 ++++++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 222 insertions(+), 2 deletions(-)
 create mode 100644 unit/cert-ca-key.pem
 create mode 100644 unit/cert-ca.pem
 create mode 100644 unit/cert-client-key.pem
 create mode 100644 unit/cert-client.pem
 create mode 100644 unit/cert-server-key.pem
 create mode 100644 unit/cert-server.pem
 create mode 100644 unit/test-cert.c
.gitignore +1 -0
index 16f0103..b122511 100644
@@ -46,6 +46,7 @@ unit/test-cipher
 unit/test-random
 unit/test-util
 unit/test-uintset
+unit/test-cert
 unit/*.log
 unit/*.trs
 examples/dbus-service
Makefile.am +5 -2
index 576641f..4792544 100644
@@ -125,7 +125,8 @@ unit_tests = unit/test-unit \
 			unit/test-cipher \
 			unit/test-random \
 			unit/test-util \
-			unit/test-uintset
+			unit/test-uintset \
+			unit/test-cert
 
 if MAINTAINER_MODE
 noinst_PROGRAMS += $(unit_tests)
@@ -187,6 +188,8 @@ unit_test_util_LDADD = ell/libell-private.la
 
 unit_test_uintset_LDADD = ell/libell-private.la
 
+unit_test_cert_LDADD = ell/libell-private.la
+
 if MAINTAINER_MODE
 noinst_LTLIBRARIES += unit/example-plugin.la
 endif
@@ -194,7 +197,7 @@ endif
 unit_example_plugin_la_LDFLAGS = -no-undefined -module -avoid-version \
 							-rpath /dummy
 
-unit_test_data_files = unit/settings.test
+unit_test_data_files = unit/settings.test unit/cert-ca.pem unit/cert-client.pem unit/cert-server.pem unit/cert-client-key.pem unit/cert-server-key.pem
 
 examples = examples/dbus-service
 
unit/cert-ca-key.pem new file +27 -0
new file mode 100644
index 0000000..f121d73
@@ -0,0 +1,27 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEpQIBAAKCAQEAyz0GzVnm43TnX8Gl2aK1XknC4hhnw9MdJsEgOy3XJWYo8ijo
+9YxJTuxPIFc0bz8wJYj7JO/Z7GuRsybNt4pq80ILU0LB5VSnO/yZdpEv01gfgMUk
+2aCxIwZrnnQ3owHUq6x0+amlZD5NuWmPGE8TYUTXZILHRlLac6NP7fq1bHtT+CT4
+/HIivb9lgGg/tPuMac5nFxdXyal4coVAmoQa+s8kAlbiFDEVwSMlWkh7IXf/8acx
+31UOgD8XbNdo2HGrpN0TFkPX3qwLSq1CBApu4oX2PJ8/BemRbmsWZvAIxlHUyySa
+J5zg3bxQaX/nS9nre65SXXN1DEVmPMymMxb7yQIDAQABAoIBADAuopsychOAlo3U
+Ce6jrV3SHNqfMJJzTVjvtiVxg60BIDnwyvHTtVl5mnSvV+X70/1CEVA7cK2k/uC7
+FVArB5nuEWNG01L8E72b8AH0/6KaJLXRhx/1AamtDvx8zufdOj8M3pqFxokSYDjR
+trPro0uPF4Vjn7yjTdjWYc+ofTRfSFyOW25HviTztEFzWeZt4KVTbZ/a7Nc1mEDj
+m04noAPh2+24akeahCQUeNLzy197DDpoCLWnrtAwUQc9o4v2RwOakTdoPUT+n8H/
+oxhimB1sfZs3PS74iwf3Z2alFVarF6Surmt6cj7OqsT8GR1bcW5r4+zpaVq0MWs4
+dch7kgECgYEA7AOx/+pHbVzrPN3G+flYnLuCivwck6jUueMjBzJ6Q8mntnI9r2vp
+kHCmCV3sDgvust78jDj4oS8kQ/Q86wy28slZJbQv71XcUxbXCzDeRp/ZvGrAemHQ
+RzfLMsuGdirL9g5XaHpzOYa67Gd2JR6aK3MBOCE24jtm+Iwd6cYs34kCgYEA3HLQ
+dmZC9JJggTIM7Q72mqx3V8aLG54psO0B1O9QaG02yZe87YU4WY8GR729MGEK5VgQ
+AqqfP8slczRYeOzd+5tgyQTtSsPmIc75zXBdwrrpEv0kC+ZS06vAPlPYfqh/4kRq
+rXbGZulSIkmqOXHdnijqfUOc19GeJoUi6fOn6kECgYEArEPqOUvsBKQMw2lw4WYh
+So0GtKFdzl0tn9xs5cTvPulCc8Wgxpiyaw398tpOANauzHo7xbGTsDpzcvIjQMEQ
+C9fxLLb/QebeoPsLzQxHQS9Nwtv8T5dzaXgM5a9uHnQQWTGQlR7DWZrAdurxbhiP
+GOWhXzHHGIuZFhNXhhlzCVkCgYEArdBVL1V361BSiOtGp88Jf3EGdA3WdsMhCiAF
+NrguRPf/n7llPD2y7WJIyF4BAlkxdx0VGpUfFPR+pbQbIdVZPV4IVNbupemJ1RRV
+f7beJoCq7s7rEfrbSYv8+uevJkaC9ouNNRIWYuQKX4CHl92AqIiIQpYB32Smdqa4
+nZAA0EECgYEAoKA19JDFAJ28fAIPgxYmEvuO07P0O4QJrINzVNvXcQn+yUQ4jBcN
+eiAuqObZV1pGquOdPcnvDmOZZ+/gu1xJwXaPaZInuVJN1UCCRRcUcy40tXkZgW40
+kqb3e0GJPLMR31p5X1WdTpd7y4uh+IV0R9chuGVTc5dBwwoFNRMdWfk=
+-----END RSA PRIVATE KEY-----
unit/cert-ca.pem new file +26 -0
new file mode 100644
index 0000000..724f852
@@ -0,0 +1,26 @@
+-----BEGIN CERTIFICATE-----
+MIIEVDCCAzygAwIBAgIJAIOyXTOJrYsaMA0GCSqGSIb3DQEBCwUAMHgxNTAzBgNV
+BAoTLEludGVybmF0aW9uYWwgVW5pb24gb2YgRXhhbXBsZSBPcmdhbml6YXRpb25z
+MR8wHQYDVQQDExZDZXJ0aWZpY2F0ZSBpc3N1ZXIgZ3V5MR4wHAYJKoZIhvcNAQkB
+Fg9jYUBtYWlsLmV4YW1wbGUwHhcNMTUwNzIxMjIzMDU4WhcNMTcwNzIwMjIzMDU4
+WjB4MTUwMwYDVQQKEyxJbnRlcm5hdGlvbmFsIFVuaW9uIG9mIEV4YW1wbGUgT3Jn
+YW5pemF0aW9uczEfMB0GA1UEAxMWQ2VydGlmaWNhdGUgaXNzdWVyIGd1eTEeMBwG
+CSqGSIb3DQEJARYPY2FAbWFpbC5leGFtcGxlMIIBIjANBgkqhkiG9w0BAQEFAAOC
+AQ8AMIIBCgKCAQEAyz0GzVnm43TnX8Gl2aK1XknC4hhnw9MdJsEgOy3XJWYo8ijo
+9YxJTuxPIFc0bz8wJYj7JO/Z7GuRsybNt4pq80ILU0LB5VSnO/yZdpEv01gfgMUk
+2aCxIwZrnnQ3owHUq6x0+amlZD5NuWmPGE8TYUTXZILHRlLac6NP7fq1bHtT+CT4
+/HIivb9lgGg/tPuMac5nFxdXyal4coVAmoQa+s8kAlbiFDEVwSMlWkh7IXf/8acx
+31UOgD8XbNdo2HGrpN0TFkPX3qwLSq1CBApu4oX2PJ8/BemRbmsWZvAIxlHUyySa
+J5zg3bxQaX/nS9nre65SXXN1DEVmPMymMxb7yQIDAQABo4HgMIHdMB0GA1UdDgQW
+BBQwJusAXVPeEDyctL5+1/gVROm4vTCBqgYDVR0jBIGiMIGfgBQwJusAXVPeEDyc
+tL5+1/gVROm4vaF8pHoweDE1MDMGA1UEChMsSW50ZXJuYXRpb25hbCBVbmlvbiBv
+ZiBFeGFtcGxlIE9yZ2FuaXphdGlvbnMxHzAdBgNVBAMTFkNlcnRpZmljYXRlIGlz
+c3VlciBndXkxHjAcBgkqhkiG9w0BCQEWD2NhQG1haWwuZXhhbXBsZYIJAIOyXTOJ
+rYsaMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBADKZwqldI8Il
+9ZgA4bhbEarcopiUUC3RMiH9cvy1rXDgYoSbC5RX2x6ptCIXDLfuAsTk+Qcl90KZ
+xno2zrKjjB7QUeb5kdZAwuZ0IoqgLWPxvdayVUDHT/7B/rNFH2ExmKR4Qz2k+gxb
+d5fUYQecRajfDQjOGhhoiDzJBq/GERXnJ2/WEOr3gFdOQ3QLFzTLjFRFlq+cxbm7
+OmBt+f2wTaX1l/EMGbxdvLfD96VbDihAl6yIsK2hNYNghW032nJjFDs7pOqneRue
+sCSbyRp2O8rBT8IPkn2e3QQl/zrdEv5FLnqrhSaB80bLJi/HGixkFkvvp+ZsWIBd
+NQZHpZyYJ+Y=
+-----END CERTIFICATE-----
unit/cert-client-key.pem new file +27 -0
new file mode 100644
index 0000000..b8f88cf
@@ -0,0 +1,27 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEowIBAAKCAQEApmgm9gy2iv2QqjzXom8XlT34Vp5XV0QWHwaWmIYuCAYpw5A2
+Y5cYNULg4M3BjsrjjmMVG9xncQ4WJHETkAs+l2d2cIqyvzmsRJ5EakeeQuxPbX+O
+E4XTVUWK7U8MtFbioynO7fy4FXYOvG0rG9BUAocuaMgBVJZC0TIQsD+LPQizMMwl
+gmkcK2dy0MAD7q4ouvIrWxdnn/JfcQernbl1s/ThLiFFxA+7s/QzP1zxm9nKaGSi
+ITteqrwKPkPiRjlQPM3Ia8i3nvskMlyZTVgEU4z0ZmoQ9T+odZ94vhR4VGib0Xr2
+7sn1Ejhlcl3+0Lt/v086hkhPSZ7nV5kjzOWy5wIDAQABAoIBACC03duEeihtWA6t
+sLBDZ3401OepbX9AaZylfdnynX+EPpcqdBY071QglfR5XG7FX95viQmV01r8Ksv0
+0aSBIUc+1Z+fEyzUmBBQjx/YfbmnmqDp7jHWHshmx7yGyfd0EE2DfSF3h3LoZlMQ
+neV0l957VVrQ3LoUYoOddHw/sQ1IpEDzjHKxcCXrBRVDp/Tcc9yrg+rWRMC8smmj
+bgf5bhUMIJLKaB1lhENLmwRYAALvo2hjUPMHhH9j1F+Q0js9MJGgBP772SEzDp4P
+34pUuxrg0zJeygULc2kJHBNJZ1DOO9hXIxiVtiGlvHC79+7N9OYlwd3kd9xWxvmc
+D7Fz9XECgYEA17VSI79KZaB2pSqP2nzHESHDMDZcfjLKa0GYMB6qbtUWbwexDxqv
+bLP7L0UsYdJbY1bNJGsof97wypz2uiC9s5X1AdXFe2zylmSjXZ6MVdqik1EpWU4I
+QDiU4lVPgIxGsu8v9owCGxo/9FV/jKME8u+C45KuVAxfmj1pxSeJRe8CgYEAxX1Z
+RB4RFR38m8yH3MyzL6nj54VGhD8eT9auJwXWKmBYa+5fntOOgMngruql0AWZs/Yr
+uexcX+ajNOj2yZVn+uERDBYLqNjQc92IoPAd6ZYIXjKHH8H6SO1GleylMmgW2MEp
+uS6YQh9S95y7S4wtcY/jtiLgKYe5JNRTjf77GokCgYAyvyuV0Vj1U6zV6o9xPU2v
+0BuVahSUc+p/FS1ylKRP8r2WXV7gSMzga+86DbIFSXHKiw8z9CnQ+2f2orrAyxcV
++r9rMRmGkfj4PLDaQ4SMZiU9NyjdI6h5Q1ZzcNKFUXsIeZD1FbchHSnSl5KsSWXO
+qV5Hn6h3iXmsVapegZCf7QKBgEzWv5OkSys3NqD7zGTZdGJ4LIdZMJ2l3UpdEfd2
+6uQkBmNAJQ4r7AcqBPSpsYglla8SeeLmFu7tUtT0S4kVS5auWS6lqTWx3jEmodFU
+e5711B2qdS6jD5muUEdnAsOBAlqllXFKss0PWP2kiq8TRF8qCARGv4cJGr08q6HU
+8qhZAoGBAJNvpqpB6pPy5Y0j0wGkc9gRFXFUy8Ulxk6bBA15GW0tiGiyEt1P1SbK
+/kwiWjwFryDn0eSATp4YRgqi1da0KG1oxdgzxbGNqNpVzZ9pDW4UcLyzzkBYDrjX
+YY+reFnomF8Wd3XaXmoI4wESHIWJMN3W2deXt75fkb48hxCf9kzX
+-----END RSA PRIVATE KEY-----
unit/cert-client.pem new file +26 -0
new file mode 100644
index 0000000..6b0fccf
@@ -0,0 +1,26 @@
+-----BEGIN CERTIFICATE-----
+MIIEbzCCA1egAwIBAgIJAIOyXTOJrYsbMA0GCSqGSIb3DQEBBAUAMHgxNTAzBgNV
+BAoTLEludGVybmF0aW9uYWwgVW5pb24gb2YgRXhhbXBsZSBPcmdhbml6YXRpb25z
+MR8wHQYDVQQDExZDZXJ0aWZpY2F0ZSBpc3N1ZXIgZ3V5MR4wHAYJKoZIhvcNAQkB
+Fg9jYUBtYWlsLmV4YW1wbGUwIBcNMTUwNzIxMjIzMTE4WhgPMjExNTA2MjcyMjMx
+MThaMGcxITAfBgNVBAoTGEJhciBFeGFtcGxlIE9yZ2FuaXphdGlvbjEhMB8GA1UE
+AxMYQmFyIEV4YW1wbGUgT3JnYW5pemF0aW9uMR8wHQYJKoZIhvcNAQkBFhBiYXJA
+bWFpbC5leGFtcGxlMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApmgm
+9gy2iv2QqjzXom8XlT34Vp5XV0QWHwaWmIYuCAYpw5A2Y5cYNULg4M3BjsrjjmMV
+G9xncQ4WJHETkAs+l2d2cIqyvzmsRJ5EakeeQuxPbX+OE4XTVUWK7U8MtFbioynO
+7fy4FXYOvG0rG9BUAocuaMgBVJZC0TIQsD+LPQizMMwlgmkcK2dy0MAD7q4ouvIr
+Wxdnn/JfcQernbl1s/ThLiFFxA+7s/QzP1zxm9nKaGSiITteqrwKPkPiRjlQPM3I
+a8i3nvskMlyZTVgEU4z0ZmoQ9T+odZ94vhR4VGib0Xr27sn1Ejhlcl3+0Lt/v086
+hkhPSZ7nV5kjzOWy5wIDAQABo4IBCTCCAQUwCQYDVR0TBAIwADAsBglghkgBhvhC
+AQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYEFICx
+nSSUXhyrxVVOqUv/f3vZfTBoMIGqBgNVHSMEgaIwgZ+AFDAm6wBdU94QPJy0vn7X
++BVE6bi9oXykejB4MTUwMwYDVQQKEyxJbnRlcm5hdGlvbmFsIFVuaW9uIG9mIEV4
+YW1wbGUgT3JnYW5pemF0aW9uczEfMB0GA1UEAxMWQ2VydGlmaWNhdGUgaXNzdWVy
+IGd1eTEeMBwGCSqGSIb3DQEJARYPY2FAbWFpbC5leGFtcGxlggkAg7JdM4mtixow
+DQYJKoZIhvcNAQEEBQADggEBADqzKUMa7JZr9e7d8zmWj/kBP47XzXDl/3LZXHGT
+mbdhB/j73BICEK4+2WAF8d8uKLz3zYXOXgq72l6arcPPJjyA7Q8MKfXPCIkCe1Oy
+0oEUtTeiwrsT9cPGHXcbYInNZZ0GSw/w1nYoYTsmJeLV8dakY78bdvGjHjjYlUTU
+OWC7V4HuIBq2ablHJ/i54vS+k4iN4XIppLBwXytNEnu2l5QEz+29ZIEhTlvhhIR+
+D+DiPgNBxNGlPJH0m20qpErW6as/+UXKNQgadQbZ16gjmzCoZQJxUhT/WKqPhLhK
+ApzmXLQwarIjHW2qmy4I4VXUeqe5PFT2638qshylw+Qxeug=
+-----END CERTIFICATE-----
unit/cert-server-key.pem new file +27 -0
new file mode 100644
index 0000000..efb3922
@@ -0,0 +1,27 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEowIBAAKCAQEAw52fx0CLmdfz2JzBse6CfzTJqYljq3j6W4z3XsGz3qFtmi1e
+ZsPY9ZNj9jpA9+vvRNRG+VXfkF27OVIkyiwkdN6zeVtY7PdmAQjoSv1Akwr18Mrh
+2mnGeKcN2+27PBglARl0uS7IWIGcGdHEV4idin54ExtMm7d/OW2H8WvUeMJ/vE4t
+0BagPAMJosOtlIO0QzlfYsOuPfTxh2G4viRnw6vqD0My1jut68BqrwWJfO/h35Ok
+hYj7sdWmWni38v2j8sbs/6YH3mR7YkmWijfGLlVke/tkOjx2PlItT7PIjwDOkHxs
+Uu0yZCsEuh3w+wzuVv/ByzyW3K/0eAkF3ov6dQIDAQABAoIBAFWW6ykgabSHk68M
+b4et5zVHYJEohw6o4UobdMTmFhBsK2uRPEPUn2Jb+hnrRS/08Hz6MI+57hbZnCip
+c2VuSQlFw9tuh4sveFctkY32H0H3QsL30jhhnbB+z4aSDyFtIwvHGpZIDOsE0stN
+SA5QvTWyvlEF2dAsSqNS9G9CkfW/yE46UU1sWb9YHCpK6bQDKolRPb6HxHFY7KBn
+58MQd3mdripSjVUShEcXxuwq7aM/5hIPRVv/9SQyd450+STEKnA5iqjh3Wr1xvUN
+BuqEcTwhUhx9pp4vwft+0TIcwadGymTf4xsdb0Id3TrTSP9TtqdTIOvuVwm5bm9f
+DfHKFSUCgYEA7BXQEgqVWPU4vnlkrfZ21UMIh3tUYpwEKz+kqGapucdhdldb6EXp
+LgWLyuJqCy7wvwh/1uP0qKFNUOzSrSBnEAUfuteJWZoGM/VxUOkUimKIgYN5daPY
+JahRZl8Sehm24b6a2oh5v1oQhyLxYXuOCXrl8/fEsn5JU92LQE0HYJcCgYEA1B3i
+oQKHS0vzn79T8jVv+M5k+KcUa9esNeP+hQ/ZyjR9KbeX8gyxzIvm6v0BZSmxKw3P
+RV3A2GgJK67wzATdqrMB9I+mbC1lTkh7kVS5CFmViHkTmm/f6ppxHcebqEmqFCPh
+BCLP2NqwDV0s7+SoRhTWfxDeytOCauxhUQmRUtMCgYA7+K6KZrXZizGG8T9qEZnO
+QuD/yYx+IDPO6LPjM3EaCFPxw+9M6bV6Q15ng2x8usfdP0N6Sx3zWEZTsFU0NW74
+2tRcmjQB1uaMfGT0sMDPBoi2nbdU8vINXtyOrMf8zFtwrQKZUOHcgfyA15eJs+E4
+90dC2kXVMc9exoCBLRAasQKBgQCpiuWdKmc3oRKuigAwJdRE+7urH+jrtex4+q5y
+btP2knPEh3GUz97BjsVY6DdYQ7XPou8cE67TinxiBSeE+xKuoPwcHUG5T6OcqcTS
+yfRHn0stVRF9LJS+cQme8vAQ2ChwhqkRZD3bqBh15hbH/G0f7wHveG2Lkvm4Ow4h
+9tLIbwKBgDIN1MBmY5Fwf7KLaXwqnX2pstt+tHXUlA74emNZd5L405kiGkqK7fUG
+ezofIAm1mizAv4hcVOoxOllMPDjJadfhELoi9JMbkJxmpQ1ezW0d+cc6DgRsWEZ+
+T0mhkJKaKfjWvNxsXZhsLgakjAU8J0/kqUdC6VLOO2e/HprlpEv9
+-----END RSA PRIVATE KEY-----
unit/cert-server.pem new file +26 -0
new file mode 100644
index 0000000..7f54b7a
@@ -0,0 +1,26 @@
+-----BEGIN CERTIFICATE-----
+MIIEbzCCA1egAwIBAgIJAIOyXTOJrYscMA0GCSqGSIb3DQEBBAUAMHgxNTAzBgNV
+BAoTLEludGVybmF0aW9uYWwgVW5pb24gb2YgRXhhbXBsZSBPcmdhbml6YXRpb25z
+MR8wHQYDVQQDExZDZXJ0aWZpY2F0ZSBpc3N1ZXIgZ3V5MR4wHAYJKoZIhvcNAQkB
+Fg9jYUBtYWlsLmV4YW1wbGUwIBcNMTUwNzIxMjIzMTUxWhgPMjExNTA2MjcyMjMx
+NTFaMGcxITAfBgNVBAoTGEZvbyBFeGFtcGxlIE9yZ2FuaXphdGlvbjEhMB8GA1UE
+AxMYRm9vIEV4YW1wbGUgT3JnYW5pemF0aW9uMR8wHQYJKoZIhvcNAQkBFhBmb29A
+bWFpbC5leGFtcGxlMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw52f
+x0CLmdfz2JzBse6CfzTJqYljq3j6W4z3XsGz3qFtmi1eZsPY9ZNj9jpA9+vvRNRG
++VXfkF27OVIkyiwkdN6zeVtY7PdmAQjoSv1Akwr18Mrh2mnGeKcN2+27PBglARl0
+uS7IWIGcGdHEV4idin54ExtMm7d/OW2H8WvUeMJ/vE4t0BagPAMJosOtlIO0Qzlf
+YsOuPfTxh2G4viRnw6vqD0My1jut68BqrwWJfO/h35OkhYj7sdWmWni38v2j8sbs
+/6YH3mR7YkmWijfGLlVke/tkOjx2PlItT7PIjwDOkHxsUu0yZCsEuh3w+wzuVv/B
+yzyW3K/0eAkF3ov6dQIDAQABo4IBCTCCAQUwCQYDVR0TBAIwADAsBglghkgBhvhC
+AQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYEFL2v
+/nxaoNJQM65oOfksgwHMOgy2MIGqBgNVHSMEgaIwgZ+AFDAm6wBdU94QPJy0vn7X
++BVE6bi9oXykejB4MTUwMwYDVQQKEyxJbnRlcm5hdGlvbmFsIFVuaW9uIG9mIEV4
+YW1wbGUgT3JnYW5pemF0aW9uczEfMB0GA1UEAxMWQ2VydGlmaWNhdGUgaXNzdWVy
+IGd1eTEeMBwGCSqGSIb3DQEJARYPY2FAbWFpbC5leGFtcGxlggkAg7JdM4mtixow
+DQYJKoZIhvcNAQEEBQADggEBAECllH+Ph+sxeYlYR4Xn/QcvBNhMW5p8jtWt0KJ+
+drpnqGz428LojEm8E2i+ab6jIDcEEzDTnDZqcszpkIAKNhNhjJyREAoYpg0EXtW8
+1SQ6rLuZwE+mkG7yGUISRN/fn9RGKkDBdDCOeeKjEkklBLijejSwTJQiai1xdIsg
+KuQpqUX5iy++MS9gL/8BeFVGgqY5MJXWXNXryjnqy7gdhUc1YHwzJbG95rAFuo/v
+yP1NTqnduchiBXbFxLPTMenpe0cGfBdfDVR39hsFZSEMnCohc+WpYSF2PPxq58iw
+zdfAwPzGdSLU/F0knKRVxtAJ7/Paz6Mvv5bTYe8i8EfbLtU=
+-----END CERTIFICATE-----
unit/test-cert.c new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.1.4
#1 lore raw

PATCH 11/17 Add TLS utilities and definitions as ell/tls{.c, .h, -private.h}

This is the groundwork for TLS support, only general structures are added here and a part of the public API (l_tls_* functions). Also the cryptographic utilities for the TLS1.2 Pseudo Random Function (RFC5246 section 5) and the pre-master-secret generation (section 8.1) and the Change Cipher Spec operation used at the end of a succesful handshake are added.

---
 Makefile.am       |   7 +-
 ell/ell.h         |   1 +
 ell/tls-private.h | 185 ++++++++++++++++++++++++
 ell/tls.c         | 417 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/tls.h         | 102 +++++++++++++
 5 files changed, 710 insertions(+), 2 deletions(-)
 create mode 100644 ell/tls-private.h
 create mode 100644 ell/tls.c
 create mode 100644 ell/tls.h
Makefile.am +5 -2
index 4792544..da1f4f1 100644
@@ -37,7 +37,8 @@ pkginclude_HEADERS = ell/ell.h \
 			ell/cipher.h \
 			ell/random.h \
 			ell/uintset.h \
-			ell/cert.h
+			ell/cert.h \
+			ell/tls.h
 
 lib_LTLIBRARIES = ell/libell.la
 
@@ -81,7 +82,9 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/cert-private.h \
 			ell/cert.c \
 			ell/rsa-private.h \
-			ell/rsa.c
+			ell/rsa.c \
+			ell/tls-private.h \
+			ell/tls.c
 
 ell_libell_la_LDFLAGS = -no-undefined \
 			-version-info $(ELL_CURRENT):$(ELL_REVISION):$(ELL_AGE)
ell/ell.h +1 -0
index 2c88216..c3c8ce5 100644
@@ -39,6 +39,7 @@
 #include <ell/random.h>
 #include <ell/uintset.h>
 #include <ell/cert.h>
+#include <ell/tls.h>
 
 #include <ell/netlink.h>
 #include <ell/genl.h>
ell/tls-private.h new file +185 -0

185 lines not on this page · raw message

ell/tls.c new file +417 -0

417 lines not on this page · raw message

ell/tls.h new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
2.1.4
#2 lore raw

PATCH 12/17 unit: Add TLS 1.2 PRF tests.

In reply to Andrew Zaborowski
---
 .gitignore      |   1 +
 Makefile.am     |   5 +-
 unit/test-tls.c | 182 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+), 1 deletion(-)
 create mode 100644 unit/test-tls.c
.gitignore +1 -0
index b122511..c50d7e7 100644
@@ -46,6 +46,7 @@ unit/test-cipher
 unit/test-random
 unit/test-util
 unit/test-uintset
+unit/test-tls
 unit/test-cert
 unit/*.log
 unit/*.trs
Makefile.am +4 -1
index da1f4f1..29bf54e 100644
@@ -129,7 +129,8 @@ unit_tests = unit/test-unit \
 			unit/test-random \
 			unit/test-util \
 			unit/test-uintset \
-			unit/test-cert
+			unit/test-cert \
+			unit/test-tls
 
 if MAINTAINER_MODE
 noinst_PROGRAMS += $(unit_tests)
@@ -193,6 +194,8 @@ unit_test_uintset_LDADD = ell/libell-private.la
 
 unit_test_cert_LDADD = ell/libell-private.la
 
+unit_test_tls_LDADD = ell/libell-private.la
+
 if MAINTAINER_MODE
 noinst_LTLIBRARIES += unit/example-plugin.la
 endif
unit/test-tls.c new file +182 -0

182 lines not on this page · raw message

Signature · 1 line
2.1.4
#3 lore raw

PATCH 13/17 tls: TLS Record Protocol layer.

In reply to Andrew Zaborowski

The Record Protocol is the lower of the two layers in TLS, described in section 6 of RFC5246. tls_tx_record in tls-record.c is called by the higher layer to send Handshake messages during set up, to report errors and to transmit application data to the peer. The Record layer in turn calls tls_handle_message (not in this patch) in tls.c when an incoming message of any of these types is decoded.

---
 Makefile.am       |   3 +-
 ell/tls-private.h |   9 +
 ell/tls-record.c  | 508 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/tls.c         |  47 +++++
 ell/tls.h         |   9 +
 5 files changed, 575 insertions(+), 1 deletion(-)
 create mode 100644 ell/tls-record.c
Makefile.am +2 -1
index 29bf54e..0896100 100644
@@ -84,7 +84,8 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/rsa-private.h \
 			ell/rsa.c \
 			ell/tls-private.h \
-			ell/tls.c
+			ell/tls.c \
+			ell/tls-record.c
 
 ell_libell_la_LDFLAGS = -no-undefined \
 			-version-info $(ELL_CURRENT):$(ELL_REVISION):$(ELL_AGE)
ell/tls-private.h +9 -0
index 033403a..ff7e51f 100644
@@ -183,3 +183,12 @@ void tls12_prf(enum l_checksum_type type, size_t hash_len,
 		const char *label,
 		const uint8_t *seed, size_t seed_len,
 		uint8_t *out, size_t out_len);
+
+void tls_disconnect(struct l_tls *tls, enum l_tls_alert_desc desc,
+			enum l_tls_alert_desc local_desc);
+
+void tls_tx_record(struct l_tls *tls, enum tls_content_type type,
+			const uint8_t *data, size_t len);
+
+bool tls_handle_message(struct l_tls *tls, const uint8_t *message,
+			int len, uint8_t type, uint16_t version);
ell/tls-record.c new file +508 -0

508 lines not on this page · raw message

ell/tls.c +47 -0

59 lines not on this page · raw message

ell/tls.h +9 -0
index 86c8521..af0ebe3 100644
@@ -75,6 +75,15 @@ struct l_tls *l_tls_new(bool server, l_tls_write_cb_t app_data_handler,
 
 void l_tls_free(struct l_tls *tls);
 
+/* Properly disconnect a connected session */
+void l_tls_close(struct l_tls *tls);
+
+/* Submit plaintext data to be encrypted and transmitted */
+void l_tls_write(struct l_tls *tls, const uint8_t *data, size_t len);
+
+/* Submit TLS payload from underlying transport to be decrypted */
+void l_tls_handle_rx(struct l_tls *tls, const uint8_t *data, size_t len);
+
 /* If peer is to be authenticated, supply the CA certificate */
 void l_tls_set_cacert(struct l_tls *tls, const char *ca_cert_path);
 
Signature · 1 line
2.1.4
#4 lore raw

PATCH 14/17 tls: TLS 1.2 Handshake implementation.

In reply to Andrew Zaborowski

The Handshake Protocol (RFC5246 section 7) runs on top of the Record protocol and carries out the cryptographic negotiation. Only version 1.2 is implemented here. It distinguishes server and client roles. I have tested both the client and server roles against OpenSSL as server and client and ell against ell. I've also now tested the server against Firefox as https client and the client against a few popular https websites.

---
 ell/tls.c | 1078 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 1078 insertions(+)
ell/tls.c +1078 -0
index 312a42b..8d88ea4 100644
@@ -76,6 +76,13 @@ void tls12_prf(enum l_checksum_type type, size_t hash_len,
 	l_checksum_free(hmac);
 }
 
+static void tls_write_random(uint8_t *buf)
+{
+	l_put_be32(time(NULL), buf);
+
+	l_getrandom(buf + 4, 28);
+}
+
 static void tls_reset_handshake(struct l_tls *tls)
 {
 	memset(tls->pending.master_secret, 0, 48);
@@ -249,6 +256,283 @@ static struct tls_compression_method *tls_find_compression_method(
 	return NULL;
 }
 
+static uint8_t tls_cert_type_pref[] = {
+};
+
+struct tls_signature_hash_algorithms {
+	uint8_t hash_id;
+	uint8_t signature_id;
+};
+
+static struct tls_signature_hash_algorithms tls_signature_hash_pref[] = {
+};
+
+enum tls_handshake_type {
+	TLS_HELLO_REQUEST	= 0,
+	TLS_CLIENT_HELLO	= 1,
+	TLS_SERVER_HELLO	= 2,
+	TLS_CERTIFICATE		= 11,
+	TLS_SERVER_KEY_EXCHANGE	= 12,
+	TLS_CERTIFICATE_REQUEST	= 13,
+	TLS_SERVER_HELLO_DONE	= 14,
+	TLS_CERTIFICATE_VERIFY	= 15,
+	TLS_CLIENT_KEY_EXCHANGE	= 16,
+	TLS_FINISHED		= 20,
+};
+
+#define TLS_HANDSHAKE_HEADER_SIZE	4
+
+static void tls_tx_handshake(struct l_tls *tls, int type, uint8_t *buf,
+				size_t length)
+{
+	/* Fill in the handshake header */
+
+	buf[0] = type;
+	buf[1] = (length - TLS_HANDSHAKE_HEADER_SIZE) >> 16;
+	buf[2] = (length - TLS_HANDSHAKE_HEADER_SIZE) >>  8;
+	buf[3] = (length - TLS_HANDSHAKE_HEADER_SIZE) >>  0;
+
+	l_checksum_update(tls->handshake_hash, buf, length);
+
+	tls_tx_record(tls, TLS_CT_HANDSHAKE, buf, length);
+}
+
+static void tls_send_client_hello(struct l_tls *tls)
+{
+	uint8_t buf[128 + L_ARRAY_SIZE(tls_compression_pref) +
+			2 * L_ARRAY_SIZE(tls_cipher_suite_pref)];
+	uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+	int i;
+
+	/* Fill in the Client Hello body */
+
+	*ptr++ = (uint8_t) (TLS_VERSION >> 8);
+	*ptr++ = (uint8_t) (TLS_VERSION >> 0);
+
+	tls_write_random(tls->pending.client_random);
+	memcpy(ptr, tls->pending.client_random, 32);
+	ptr += 32;
+
+	*ptr++ = 0; /* No SessionID */
+
+	/*
+	 * We can list all supported key exchange mechanisms regardless of the
+	 * certificate type we are actually presenting (if any).
+	 *
+	 * TODO: perhaps scan /proc/crypto for supported ciphers so we don't
+	 * include ones that will cause an internal error later in the
+	 * handshake.  We can add camellia whan this is done.
+	 */
+	l_put_be16(L_ARRAY_SIZE(tls_cipher_suite_pref) * 2, ptr);
+	ptr += 2;
+	for (i = 0; i < (int) L_ARRAY_SIZE(tls_cipher_suite_pref); i++) {
+		*ptr++ = tls_cipher_suite_pref[i].id[0];
+		*ptr++ = tls_cipher_suite_pref[i].id[1];
+	}
+
+	*ptr++ = L_ARRAY_SIZE(tls_compression_pref);
+	for (i = 0; i < (int) L_ARRAY_SIZE(tls_compression_pref); i++)
+		*ptr++ = tls_compression_pref[i].id;
+
+	tls_tx_handshake(tls, TLS_CLIENT_HELLO, buf, ptr - buf);
+}
+
+static void tls_send_server_hello(struct l_tls *tls)
+{
+	uint8_t buf[128];
+	uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+
+	/* Fill in the Server Hello body */
+
+	*ptr++ = tls->negotiated_version >> 8;
+	*ptr++ = tls->negotiated_version >> 0;
+
+	tls_write_random(tls->pending.server_random);
+	memcpy(ptr, tls->pending.server_random, 32);
+	ptr += 32;
+
+	*ptr++ = 0; /* Sessions are not cached */
+
+	*ptr++ = tls->pending.cipher_suite->id[0];
+	*ptr++ = tls->pending.cipher_suite->id[1];
+
+	*ptr++ = tls->pending.compression_method->id;
+
+	tls_tx_handshake(tls, TLS_SERVER_HELLO, buf, ptr - buf);
+}
+
+static bool tls_send_certificate(struct l_tls *tls)
+{
+	uint8_t *buf, *ptr;
+	struct l_cert *cert, *i;
+	size_t total;
+
+	if (tls->cert_path) {
+		cert = l_cert_load_file(tls->cert_path);
+
+		if (tls->server && !cert) {
+			tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+					TLS_ALERT_BAD_CERT);
+
+			return false;
+		}
+	} else
+		cert = NULL;
+
+	if (cert && !l_cert_find_certchain(cert, tls->ca_cert_path)) {
+		if (tls->server) {
+			tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+					TLS_ALERT_UNKNOWN_CA);
+
+			return false;
+		} else
+			cert = NULL;
+	}
+
+	/* TODO: might want check this earlier and exclude the cipher suite */
+	if (cert && !tls->pending.cipher_suite->key_xchg->
+			validate_cert_key_type(cert)) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+				TLS_ALERT_CERT_UNKNOWN);
+
+		return false;
+	}
+
+	/*
+	 * TODO: check that the certificate is compatible with hash and
+	 * signature algorithms lists supplied to us in the Client Hello
+	 * extensions (if we're a server) or in the Certificate Request
+	 * (if we act as a 1.2+ client).
+	 *
+	 *  - for the hash and signature_algorithms list, check all
+	 *    certs in the cert chain.
+	 *
+	 *  - also if !cipher_suite->key_xchg->key_exchange_msg, check that the
+	 *    end entity certificate's key type matches and is usable with some
+	 *    hash/signature pair.
+	 *
+	 *  - on client check if any of the supplied DNs (if any) match
+	 *    anything in our cert chain.
+	 */
+
+	total = 0;
+	for (i = cert; i; i = i->issuer)
+		total += 3 + i->size;
+
+	buf = l_malloc(128 + total);
+	ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+
+	/* Fill in the Certificate body */
+
+	*ptr++ = total >> 16;
+	*ptr++ = total >>  8;
+	*ptr++ = total >>  0;
+
+	for (i = cert; i; i = i->issuer) {
+		*ptr++ = i->size >> 16;
+		*ptr++ = i->size >>  8;
+		*ptr++ = i->size >>  0;
+
+		memcpy(ptr, i->asn1, i->size);
+		ptr += i->size;
+	}
+
+	tls_tx_handshake(tls, TLS_CERTIFICATE, buf, ptr - buf);
+
+	l_free(buf);
+
+	if (cert)
+		tls->cert_sent = true;
+
+	return true;
+}
+
+static bool tls_send_certificate_request(struct l_tls *tls)
+{
+	uint8_t *buf, *ptr, *dn_ptr;
+	int i;
+	struct l_cert *ca_cert;
+	uint8_t *ca_name;
+	int ca_name_len;
+
+	/* TODO: support multiple CAs if directory path given */
+	ca_cert = l_cert_load_file(tls->ca_cert_path);
+	if (!ca_cert) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+				TLS_ALERT_BAD_CERT);
+
+		return false;
+	}
+
+	ca_name = l_cert_find_dn(ca_cert, false, &ca_name_len);
+	if (!ca_name) {
+		l_free(ca_cert);
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+				TLS_ALERT_BAD_CERT);
+
+		return false;
+	}
+
+	buf = l_malloc(128 + L_ARRAY_SIZE(tls_cert_type_pref) +
+			2 * L_ARRAY_SIZE(tls_signature_hash_pref) +
+			strlen((char *) ca_name));
+	ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+
+	/* Fill in the Certificate Request body */
+
+	*ptr++ = L_ARRAY_SIZE(tls_cert_type_pref);
+	for (i = 0; i < (int) L_ARRAY_SIZE(tls_cert_type_pref); i++)
+		*ptr++ = tls_cert_type_pref[i];
+
+	/*
+	 * This only makes sense as a variable-length field, assume there's
+	 * a typo in RFC5246 7.4.4 here.
+	 *
+	 * TODO: we support the full list of hash algorithms when used
+	 * in the client certificate chain but we can only verify the
+	 * Certificate Verify signature when the hash algorithm matches
+	 * HANDSHAKE_HASH_TYPE_TLS.  The values we include here will
+	 * affect both of these steps so revisit which set we're passing
+	 * here.
+	 *
+	 * TODO: not present in TLS 1.0
+	 */
+	l_put_be16(L_ARRAY_SIZE(tls_signature_hash_pref) * 2, ptr);
+	ptr += 2;
+	for (i = 0; i < (int) L_ARRAY_SIZE(tls_signature_hash_pref); i++) {
+		*ptr++ = tls_signature_hash_pref[i].hash_id;
+		*ptr++ = tls_signature_hash_pref[i].signature_id;
+	}
+
+	dn_ptr = ptr;
+	ptr += 4;				/* Leave space for sizes */
+	*ptr++ = ASN1_ID_SEQUENCE;		/* DER outer SEQUENCE tag */
+	write_asn1_definite_length(&ptr, ca_name_len); /* length */
+	memcpy(ptr, ca_name, ca_name_len);	/* value */
+	ptr += ca_name_len;
+
+	ca_name_len = ptr - dn_ptr - 4;
+	l_put_be16(ca_name_len + 2, dn_ptr);	/* DistinguishedNames size */
+	l_put_be16(ca_name_len, dn_ptr + 2);	/* DistinguishedName[0] size */
+
+	tls_tx_handshake(tls, TLS_CERTIFICATE_REQUEST, buf, ptr - buf);
+
+	l_free(ca_cert);
+	l_free(buf);
+
+	return true;
+}
+
+static void tls_send_server_hello_done(struct l_tls *tls)
+{
+	uint8_t buf[32];
+
+	/* No body */
+
+	tls_tx_handshake(tls, TLS_SERVER_HELLO_DONE, buf,
+				TLS_HANDSHAKE_HEADER_SIZE);
+}
+
 static void tls_generate_master_secret(struct l_tls *tls,
 					const uint8_t *pre_master_secret,
 					int pre_master_secret_len)
@@ -295,6 +579,710 @@ static void tls_get_handshake_hash(struct l_tls *tls, uint8_t *out)
 	l_checksum_free(hash);
 }
 
+static bool tls_send_certificate_verify(struct l_tls *tls)
+{
+	uint8_t buf[2048];
+	uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+	uint8_t hash[HANDSHAKE_HASH_SIZE];
+
+	/* Fill in the Certificate Verify body */
+
+	tls_get_handshake_hash(tls, hash);
+
+	if (!tls->pending.cipher_suite->key_xchg->sign(tls, &ptr, hash))
+		return false;
+
+	tls_tx_handshake(tls, TLS_CERTIFICATE_VERIFY, buf, ptr - buf);
+
+	return true;
+}
+
+static void tls_send_change_cipher_spec(struct l_tls *tls)
+{
+	uint8_t buf = 1;
+
+	tls_tx_record(tls, TLS_CT_CHANGE_CIPHER_SPEC, &buf, 1);
+}
+
+static void tls_send_finished(struct l_tls *tls)
+{
+	uint8_t buf[512];
+	uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+	uint8_t seed[HANDSHAKE_HASH_SIZE];
+
+	/*
+	 * Same hash type as that used for the PRF, i.e. SHA256 unless an
+	 * exotic cipher suite was negotiated that dictates a different
+	 * hash for the PRF and for the Finished hash.  We don't support
+	 * any such ciphers so it's always SHA256.
+	 */
+	tls_get_handshake_hash(tls, seed);
+
+	tls12_prf(L_CHECKSUM_SHA256, 32, tls->pending.master_secret, 48,
+			tls->server ? "server finished" : "client finished",
+			seed, sizeof(seed),
+			ptr, tls->cipher_suite[1]->verify_data_length);
+	ptr += tls->cipher_suite[1]->verify_data_length;
+
+	tls_tx_handshake(tls, TLS_FINISHED, buf, ptr - buf);
+}
+
+static bool tls_verify_finished(struct l_tls *tls, const uint8_t *received,
+				size_t len)
+{
+	uint8_t expected[tls->cipher_suite[0]->verify_data_length];
+
+	if (len != (size_t) tls->cipher_suite[0]->verify_data_length) {
+		tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+
+		return false;
+	}
+
+	tls12_prf(L_CHECKSUM_SHA256, 32, tls->pending.master_secret, 48,
+			tls->server ? "client finished" : "server finished",
+			tls->prev_digest, HANDSHAKE_HASH_SIZE,
+			expected, tls->cipher_suite[0]->verify_data_length);
+
+	if (memcmp(received, expected, len)) {
+		tls_disconnect(tls, TLS_ALERT_DECRYPT_ERROR, 0);
+
+		return false;
+	}
+
+	return true;
+}
+
+static void tls_handle_client_hello(struct l_tls *tls,
+					const uint8_t *buf, size_t len)
+{
+	uint16_t cipher_suites_size;
+	uint8_t session_id_size, compression_methods_size;
+	const uint8_t *cipher_suites;
+	const uint8_t *compression_methods;
+
+	/* Length checks */
+
+	if (len < 2 + 32 + 1)
+		goto decode_error;
+	memcpy(tls->pending.client_random, buf + 2, 32);
+	session_id_size = buf[34];
+	len -= 35;
+
+	if (len < (size_t) session_id_size + 4)
+		goto decode_error;
+	len -= session_id_size + 2;
+
+	cipher_suites_size = l_get_be16(buf + 35 + session_id_size);
+	cipher_suites = buf + 37 + session_id_size;
+	if (len < (size_t) cipher_suites_size + 2 ||
+			(cipher_suites_size & 1) || cipher_suites_size == 0)
+		goto decode_error;
+	len -= cipher_suites_size + 1;
+
+	compression_methods_size = cipher_suites[cipher_suites_size];
+	compression_methods = cipher_suites + cipher_suites_size + 1;
+	if (len < (size_t) compression_methods_size ||
+			compression_methods == 0)
+		goto decode_error;
+	len -= compression_methods_size;
+
+	if (len) {
+		uint16_t extensions_size;
+
+		if (len < 2 || len > 2 + 65535)
+			goto decode_error;
+
+		extensions_size = l_get_be16(compression_methods +
+						compression_methods_size);
+		len -= 2;
+
+		if (len != extensions_size)
+			goto decode_error;
+
+		/* TODO: validate each extension in the vector, 7.4.1.4 */
+		/* TODO: check for duplicates? */
+	}
+
+	/*
+	 * Note: if the client is supplying a SessionID we know it is false
+	 * because our server implementation never generates any SessionIDs
+	 * yet so either the client is attempting something strange or was
+	 * trying to connect somewhere else.  We might want to throw an error.
+	 */
+
+	/*
+	 * TODO: Obligatory in 1.2: check for signature_algorithms extension,
+	 * store the list of algorithms for later checking in
+	 * tls_send_certificate on both server and client sides.  If not
+	 * present assume only SHA1+RSA (7.4.1.4.1).
+	 */
+
+	/* Save client_version for Premaster Secret verification */
+	tls->client_version = l_get_be16(buf);
+
+	if (tls->client_version < TLS_MIN_VERSION) {
+		tls_disconnect(tls, TLS_ALERT_PROTOCOL_VERSION, 0);
+		return;
+	}
+	tls->negotiated_version = TLS_VERSION < tls->client_version ?
+		TLS_VERSION : tls->client_version;
+
+	/* Select a cipher suite according to client's preference list */
+
+	while (cipher_suites_size) {
+		/*
+		 * TODO: filter supported cipher suites by the certificate/key
+		 * type that was submitted by tls_set_auth_data() if any.
+		 * Perhaps just call cipher_suite->verify_cert_type() on each
+		 * cipher suite passing a pre-parsed certificate ASN.1 struct.
+		 */
+		tls->pending.cipher_suite =
+			tls_find_cipher_suite(cipher_suites);
+		if (tls->pending.cipher_suite)
+			break;
+
+		cipher_suites += 2;
+		cipher_suites_size -= 2;
+	}
+	if (!cipher_suites_size) {
+		tls_disconnect(tls, TLS_ALERT_HANDSHAKE_FAIL, 0);
+		return;
+	}
+
+	/* Select a compression method */
+
+	/* CompressionMethod.null must be present in the vector */
+	if (!memchr(compression_methods, 0, compression_methods_size))
+		goto decode_error;
+
+	while (compression_methods_size) {
+		tls->pending.compression_method =
+			tls_find_compression_method(*compression_methods);
+		if (tls->pending.compression_method)
+			break;
+
+		compression_methods++;
+		compression_methods_size--;
+	}
+
+	tls_send_server_hello(tls);
+
+	if (tls->pending.cipher_suite->key_xchg->certificate_check &&
+			tls->cert_path)
+		if (!tls_send_certificate(tls))
+			return;
+
+	if (tls->pending.cipher_suite->key_xchg->key_exchange_msg)
+		if (!tls->pending.cipher_suite->key_xchg->
+				send_server_key_exchange(tls))
+			return;
+
+	/* TODO: don't bother if configured to not authenticate client */
+	if (tls->pending.cipher_suite->key_xchg->certificate_check &&
+			tls->ca_cert_path)
+		if (!tls_send_certificate_request(tls))
+			return;
+
+	tls_send_server_hello_done(tls);
+
+	if (tls->pending.cipher_suite->key_xchg->certificate_check &&
+			tls->ca_cert_path)
+		tls->state = TLS_HANDSHAKE_WAIT_CERTIFICATE;
+	else
+		tls->state = TLS_HANDSHAKE_WAIT_KEY_EXCHANGE;
+
+	return;
+
+decode_error:
+	tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+}
+
+static void tls_handle_server_hello(struct l_tls *tls,
+					const uint8_t *buf, size_t len)
+{
+	uint8_t session_id_size, cipher_suite_id[2], compression_method_id;
+
+	/* Length checks */
+
+	if (len < 2 + 32 + 1)
+		goto decode_error;
+	memcpy(tls->pending.server_random, buf + 2, 32);
+	session_id_size = buf[34];
+	len -= 35;
+
+	if (len < (size_t) session_id_size + 2 + 1)
+		goto decode_error;
+	cipher_suite_id[0] = buf[35 + session_id_size + 0];
+	cipher_suite_id[1] = buf[35 + session_id_size + 1];
+	compression_method_id = buf[35 + session_id_size + 2];
+	len -= session_id_size + 2 + 1;
+
+	if (len != 0) { /* We know we haven't solicited any extensions */
+		tls_disconnect(tls, TLS_ALERT_UNSUPPORTED_EXTENSION, 0);
+		return;
+	}
+
+	tls->negotiated_version = l_get_be16(buf);
+
+	if (tls->negotiated_version < TLS_MIN_VERSION ||
+			tls->negotiated_version > TLS_VERSION) {
+		tls_disconnect(tls, tls->negotiated_version < TLS_MIN_VERSION ?
+				TLS_ALERT_PROTOCOL_VERSION :
+				TLS_ALERT_ILLEGAL_PARAM, 0);
+		return;
+	}
+
+	/* Set the new cipher suite and compression method structs */
+
+	tls->pending.cipher_suite = tls_find_cipher_suite(cipher_suite_id);
+	if (!tls->pending.cipher_suite) {
+		tls_disconnect(tls, TLS_ALERT_HANDSHAKE_FAIL, 0);
+		return;
+	}
+
+	tls->pending.compression_method =
+		tls_find_compression_method(compression_method_id);
+	if (!tls->pending.compression_method) {
+		tls_disconnect(tls, TLS_ALERT_HANDSHAKE_FAIL, 0);
+		return;
+	}
+
+	if (tls->pending.cipher_suite->key_xchg->certificate_check)
+		tls->state = TLS_HANDSHAKE_WAIT_CERTIFICATE;
+	else
+		tls->state = TLS_HANDSHAKE_WAIT_KEY_EXCHANGE;
+
+	return;
+
+decode_error:
+	tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+}
+
+static void tls_handle_certificate(struct l_tls *tls,
+					const uint8_t *buf, size_t len)
+{
+	int total, cert_len, pubkey_len;
+	struct l_cert *certchain = NULL, **tail = &certchain;
+	struct l_cert *ca_cert = NULL;
+	uint8_t *pubkey;
+
+	/* Length checks */
+
+	total = *buf++ << 16;
+	total |= *buf++ << 8;
+	total |= *buf++ << 0;
+	if ((size_t) total + 3 != len)
+		goto decode_error;
+
+	while (total) {
+		cert_len = *buf++ << 16;
+		cert_len |= *buf++ << 8;
+		cert_len |= *buf++ << 0;
+
+		if (cert_len + 3 > total)
+			goto decode_error;
+
+		*tail = l_cert_decode(buf, cert_len);
+		if (!*tail)
+			goto decode_error;
+
+		tail = &(*tail)->issuer;
+
+		buf += cert_len;
+		total -= cert_len + 3;
+	}
+
+	/*
+	 * "Note that a client MAY send no certificates if it does not have any
+	 * appropriate certificate to send in response to the server's
+	 * authentication request." -- for now we unconditionally accept
+	 * an empty certificate chain from the client.  Later on we need to
+	 * make this configurable, if we don't want to authenticate the
+	 * client then also don't bother sending a Certificate Request.
+	 */
+	if (!certchain) {
+		if (!tls->server) {
+			tls_disconnect(tls, TLS_ALERT_HANDSHAKE_FAIL, 0);
+
+			goto done;
+		}
+
+		tls->state = TLS_HANDSHAKE_WAIT_KEY_EXCHANGE;
+
+		goto done;
+	}
+
+	/*
+	 * Validate the certificate chain's consistency and validate it
+	 * against our CA if we have any.
+	 */
+
+	if (ca_cert) {
+		ca_cert = l_cert_load_file(tls->ca_cert_path);
+		if (!ca_cert) {
+			tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+					TLS_ALERT_BAD_CERT);
+
+			goto done;
+		}
+	}
+
+	if (!l_cert_verify_certchain(certchain, ca_cert)) {
+		tls_disconnect(tls, TLS_ALERT_BAD_CERT, 0);
+
+		goto done;
+	}
+
+	/*
+	 * RFC5246 7.4.2:
+	 * "The end entity certificate's public key (and associated
+	 * restrictions) MUST be compatible with the selected key exchange
+	 * algorithm."
+	 */
+	if (!tls->pending.cipher_suite->key_xchg->
+			validate_cert_key_type(certchain)) {
+		tls_disconnect(tls, TLS_ALERT_UNSUPPORTED_CERT, 0);
+
+		goto done;
+	}
+
+	/*
+	 * Save the public key from the certificate for use in premaster
+	 * secret encryption.
+	 */
+	pubkey = l_cert_find_pubkey(certchain, &pubkey_len);
+	if (!pubkey && !tls->pending.cipher_suite->key_xchg->key_exchange_msg) {
+		tls_disconnect(tls, TLS_ALERT_UNSUPPORTED_CERT, 0);
+
+		goto done;
+	}
+
+	if (pubkey) {
+		/* Save the end-entity cert and free the rest of the chain */
+		tls->peer_cert = certchain;
+		l_cert_free_certchain(certchain->issuer);
+		certchain->issuer = NULL;
+		certchain = NULL;
+
+		tls->peer_pubkey = pubkey;
+		tls->peer_pubkey_length = pubkey_len;
+	}
+
+	if (tls->server)
+		tls->state = TLS_HANDSHAKE_WAIT_KEY_EXCHANGE;
+	else {
+		if (tls->pending.cipher_suite->key_xchg->key_exchange_msg)
+			tls->state = TLS_HANDSHAKE_WAIT_KEY_EXCHANGE;
+		else
+			tls->state = TLS_HANDSHAKE_WAIT_HELLO_DONE;
+	}
+
+	goto done;
+
+decode_error:
+	tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+
+done:
+	if (ca_cert)
+		l_free(ca_cert);
+
+	l_cert_free_certchain(certchain);
+}
+
+static void tls_handle_certificate_request(struct l_tls *tls,
+						const uint8_t *buf, size_t len)
+{
+	tls->cert_requested = 1;
+
+	/*
+	 * TODO: parse and save key type, signature algorithms and DNs list
+	 * for use in tls_send_certificate.
+	 */
+}
+
+static void tls_handle_server_hello_done(struct l_tls *tls,
+						const uint8_t *buf, size_t len)
+{
+	if (len) {
+		tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+
+		return;
+	}
+
+	if (tls->cert_requested)
+		if (!tls_send_certificate(tls))
+			return;
+	if (!tls->pending.cipher_suite->key_xchg->send_client_key_exchange(tls))
+		return;
+	if (tls->cert_sent)
+		if (!tls_send_certificate_verify(tls))
+			return;
+	tls_send_change_cipher_spec(tls);
+	if (!tls_change_cipher_spec(tls, 1)) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR, 0);
+		return;
+	}
+	tls_send_finished(tls);
+
+	tls->state = TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC;
+}
+
+static void tls_handle_certificate_verify(struct l_tls *tls,
+						const uint8_t *buf, size_t len)
+{
+	if (!tls->pending.cipher_suite->key_xchg->verify(tls, buf, len,
+							tls->prev_digest))
+		return;
+
+	/*
+	 * The client's certificate is now verified based on the following
+	 * logic:
+	 *   - If we received an (expected) Certificate Verify, we must have
+	 *     sent a Certificate Request.
+	 *   - If we sent a Certificate Request that's because
+	 *     tls->ca_cert_path is non-NULL.
+	 *   - If tls->ca_cert_path is non-NULL then tls_handle_certificate
+	 *     will have checked the whole certificate chain to be valid and
+	 *     additionally trusted by our CA if known.
+	 *   - Additionally cipher_suite->key_xchg->verify has just confirmed
+	 *     that the peer owns the end-entity certificate because it was
+	 *     able to sign the contents of the handshake messages and that
+	 *     signature could be verified with the public key from that
+	 *     certificate.
+	 */
+	tls->peer_authenticated = true;
+
+	tls->state = TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC;
+}
+
+static void tls_finished(struct l_tls *tls)
+{
+	uint8_t *subject_name = NULL;
+	int subject_name_len;
+	uint8_t *printable_name = NULL;
+	int printable_name_len;
+	char *peer_identity = NULL;
+
+	if (tls->peer_authenticated && tls->peer_cert)
+		subject_name = l_cert_find_dn(tls->peer_cert, false,
+						&subject_name_len);
+	if (subject_name)
+		printable_name = l_cert_dn_find_printable_name(subject_name,
+							subject_name_len,
+							&printable_name_len);
+	if (printable_name)
+		peer_identity = l_strndup((char *) printable_name,
+						printable_name_len);
+
+	/* Free up the resources used in the handshake */
+	tls_reset_handshake(tls);
+
+	tls->state = TLS_HANDSHAKE_DONE;
+	tls->ready = true;
+
+	tls->ready_handle(tls->user_data, peer_identity);
+
+	if (peer_identity)
+		l_free(peer_identity);
+}
+
+static void tls_handle_handshake(struct l_tls *tls, int type,
+					const uint8_t *buf, size_t len)
+{
+	switch (type) {
+	case TLS_HELLO_REQUEST:
+		if (tls->server) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		if (len != 0) {
+			tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+			break;
+		}
+
+		/*
+		 * May be sent by the server at any time but "SHOULD be ignored
+		 * by the client if it arrives in the middle of a handshake"
+		 * and "MAY be ignored by the client if it does not wish to
+		 * renegotiate a session".
+		 */
+
+		break;
+
+	case TLS_CLIENT_HELLO:
+		if (!tls->server) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		if (tls->state != TLS_HANDSHAKE_WAIT_HELLO &&
+				tls->state != TLS_HANDSHAKE_DONE) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		tls_handle_client_hello(tls, buf, len);
+
+		break;
+
+	case TLS_SERVER_HELLO:
+		if (tls->server) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		if (tls->state != TLS_HANDSHAKE_WAIT_HELLO) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		tls_handle_server_hello(tls, buf, len);
+
+		break;
+
+	case TLS_CERTIFICATE:
+		if (tls->state != TLS_HANDSHAKE_WAIT_CERTIFICATE) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		tls_handle_certificate(tls, buf, len);
+
+		break;
+
+	case TLS_SERVER_KEY_EXCHANGE:
+		if (tls->server) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		if (tls->state != TLS_HANDSHAKE_WAIT_KEY_EXCHANGE) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		if (!tls->pending.cipher_suite->key_xchg->
+				handle_server_key_exchange(tls, buf, len))
+			break;
+
+		tls->state = TLS_HANDSHAKE_WAIT_HELLO_DONE;
+
+		break;
+
+	case TLS_CERTIFICATE_REQUEST:
+		/*
+		 * Server sends this optionally so in the WAIT_HELLO_DONE
+		 * state we accept either this or a Server Hello Done (below).
+		 */
+		if (tls->state != TLS_HANDSHAKE_WAIT_HELLO_DONE ||
+				tls->cert_requested ||
+				!tls->pending.cipher_suite->key_xchg->
+				certificate_check) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		tls_handle_certificate_request(tls, buf, len);
+
+		break;
+
+	case TLS_SERVER_HELLO_DONE:
+		if (tls->state != TLS_HANDSHAKE_WAIT_HELLO_DONE) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		tls_handle_server_hello_done(tls, buf, len);
+
+		break;
+
+	case TLS_CERTIFICATE_VERIFY:
+		if (tls->state != TLS_HANDSHAKE_WAIT_CERTIFICATE_VERIFY) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		tls_handle_certificate_verify(tls, buf, len);
+
+		break;
+
+	case TLS_CLIENT_KEY_EXCHANGE:
+		if (!tls->server) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		if (tls->state != TLS_HANDSHAKE_WAIT_KEY_EXCHANGE) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		tls->pending.cipher_suite->key_xchg->handle_client_key_exchange(
+								tls, buf, len);
+
+		/*
+		 * If we accepted a client Certificate message with a
+		 * certificate that has signing capability (TODO: check
+		 * usage bitmask), Certiifcate Verify is received next.  It
+		 * sounds as if this is mandatory for the client although
+		 * this isn't 100% clear.
+		 */
+		if (tls->peer_pubkey)
+			tls->state = TLS_HANDSHAKE_WAIT_CERTIFICATE_VERIFY;
+		else
+			tls->state = TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC;
+
+		break;
+
+	case TLS_FINISHED:
+		if (tls->state != TLS_HANDSHAKE_WAIT_FINISHED) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+			break;
+		}
+
+		if (!tls_verify_finished(tls, buf, len))
+			break;
+
+		if (tls->server) {
+			tls_send_change_cipher_spec(tls);
+			if (!tls_change_cipher_spec(tls, 1)) {
+				tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+						0);
+				break;
+			}
+			tls_send_finished(tls);
+		}
+
+		/*
+		 * On the client, the server's certificate is only now
+		 * verified, based on the following logic:
+		 *  - tls->ca_cert_path is non-NULL so tls_handle_certificate
+		 *    (always called on the client) must have veritifed the
+		 *    server's certificate chain to be valid and additionally
+		 *    trusted by our CA.
+		 *  - the correct receival of this Finished message confirms
+		 *    that the peer owns the end-entity certificate because
+		 *    it was able to decrypt the master secret which we had
+		 *    encrypted with the public key from that certificate, and
+		 *    the posession of the master secret in turn is verified
+		 *    by both the successful decryption and the MAC of this
+		 *    message (either should be enough).
+		 */
+		if (!tls->server && tls->cipher_suite[0]->key_xchg->
+				certificate_check &&
+				tls->ca_cert_path)
+			tls->peer_authenticated = true;
+
+		tls_finished(tls);
+
+		break;
+
+	default:
+		tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+	}
+}
+
 LIB_EXPORT struct l_tls *l_tls_new(bool server,
 				l_tls_write_cb_t app_data_handler,
 				l_tls_write_cb_t tx_handler,
@@ -362,6 +1350,96 @@ LIB_EXPORT void l_tls_write(struct l_tls *tls, const uint8_t *data, size_t len)
 	tls_tx_record(tls, TLS_CT_APPLICATION_DATA, data, len);
 }
 
+bool tls_handle_message(struct l_tls *tls, const uint8_t *message,
+			int len, uint8_t type, uint16_t version)
+{
+	switch (type) {
+	case TLS_CT_CHANGE_CIPHER_SPEC:
+		if (len != 1 || message[0] != 0x01) {
+			tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+
+			return false;
+		}
+
+		if (tls->state != TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+
+			return false;
+		}
+
+		if (!tls_change_cipher_spec(tls, 0)) {
+			tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR, 0);
+
+			return false;
+		}
+
+		tls->state = TLS_HANDSHAKE_WAIT_FINISHED;
+
+		break;
+
+	case TLS_CT_ALERT:
+		/* Verify AlertLevel */
+		if (message[0] != 0x01 && message[0] != 0x02) {
+			tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+
+			return false;
+		}
+
+		/*
+		 * On a fatal alert we are obligated to respond with a
+		 * fatal alert and disconnect but also not complain if
+		 * the connection has been torn down by the peer before
+		 * we were able to send our alert.  However on a non-fatal
+		 * alert (warning) we're also allowed to panic and send
+		 * a fatal alert, then disconnect, so we do that
+		 * regardless of the alert level.
+		 */
+		tls_disconnect(tls, TLS_ALERT_CLOSE_NOTIFY, message[1]);
+
+		return false;
+
+	case TLS_CT_HANDSHAKE:
+		/*
+		 * Corner case: When handling a Certificate Verify or a
+		 * Finished message we need access to the messages hash from
+		 * before this message was transmitted on the Tx side so we
+		 * can verify it matches the hash the sender included in the
+		 * message.  We save it here for that purpose.  Everywhere
+		 * else we need to update the hash before handling the new
+		 * message because 1. we may need the new hash to build our
+		 * own Certificate Verify or Finished messages, and 2. we
+		 * update the message hash with newly transmitted messages
+		 * inside tls_tx_handshake which may be called as part of
+		 * handling incoming message, and if we didn't call
+		 * l_checksum_update before, the calls would end up being
+		 * out of order.
+		 */
+		if (message[0] == TLS_CERTIFICATE_VERIFY ||
+				message[0] == TLS_FINISHED)
+			tls_get_handshake_hash(tls, tls->prev_digest);
+		l_checksum_update(tls->handshake_hash, message, len);
+
+		tls_handle_handshake(tls, message[0],
+					message + TLS_HANDSHAKE_HEADER_SIZE,
+					len - TLS_HANDSHAKE_HEADER_SIZE);
+
+		break;
+
+	case TLS_CT_APPLICATION_DATA:
+		if (!tls->ready) {
+			tls_disconnect(tls, TLS_ALERT_UNEXPECTED_MESSAGE, 0);
+
+			return false;
+		}
+
+		tls->rx(tls->user_data, message, len);
+
+		break;
+	}
+
+	return true;
+}
+
 LIB_EXPORT void l_tls_close(struct l_tls *tls)
 {
 	tls_disconnect(tls, TLS_ALERT_CLOSE_NOTIFY, 0);
Signature · 1 line
2.1.4
#5 lore raw

PATCH 15/17 tls: Add the cipher suite implementations.

In reply to Andrew Zaborowski

7 cipher suites are supported, tested against OpenSSL. All 7 are based on RSA. TLS defines two more signature algorithms / key-exchange methods beside RSA but they depend on algorithms not supported by the kernel.

---
 ell/tls.c | 402 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 402 insertions(+)
ell/tls.c +402 -0
index 8d88ea4..25c938a 100644
@@ -222,7 +222,125 @@ void tls_disconnect(struct l_tls *tls, enum l_tls_alert_desc desc,
 				local_desc && !desc);
 }
 
+static bool tls_send_rsa_client_key_xchg(struct l_tls *tls);
+static void tls_handle_rsa_client_key_xchg(struct l_tls *tls,
+						const uint8_t *buf, size_t len);
+
+static bool tls_rsa_sign(struct l_tls *tls, uint8_t **out, const uint8_t *hash);
+static bool tls_rsa_verify(struct l_tls *tls, const uint8_t *in, size_t len,
+				const uint8_t *hash);
+
+static bool tls_rsa_validate_cert_key(struct l_cert *cert)
+{
+	return l_cert_get_pubkey_type(cert) == L_CERT_KEY_RSA;
+}
+
+static struct tls_key_exchange_algorithm tls_rsa = {
+	.id = 1, /* RSA_sign */
+	.certificate_check = true,
+	.key_exchange_msg = false,
+	.validate_cert_key_type = tls_rsa_validate_cert_key,
+	.send_client_key_exchange = tls_send_rsa_client_key_xchg,
+	.handle_client_key_exchange = tls_handle_rsa_client_key_xchg,
+	.sign = tls_rsa_sign,
+	.verify = tls_rsa_verify,
+};
+
+static struct tls_bulk_encryption_algorithm tls_rc4 = {
+	.cipher_type = TLS_CIPHER_STREAM,
+	.l_id = L_CIPHER_ARC4,
+	.key_length = 16,
+}, tls_aes128 = {
+	.cipher_type = TLS_CIPHER_BLOCK,
+	.l_id = L_CIPHER_AES_CBC,
+	.key_length = 16,
+	.iv_length = 16,
+	.block_length = 16,
+}, tls_aes256 = {
+	.cipher_type = TLS_CIPHER_BLOCK,
+	.l_id = L_CIPHER_AES_CBC,
+	.key_length = 32,
+	.iv_length = 16,
+	.block_length = 16,
+}, tls_3des_ede = {
+	.cipher_type = TLS_CIPHER_BLOCK,
+	.l_id = L_CIPHER_DES3_EDE_CBC,
+	.key_length = 24,
+	.iv_length = 8,
+	.block_length = 8,
+};
+
+static struct tls_mac_algorithm tls_md5 = {
+	.id = 1,
+	.hmac_type = L_CHECKSUM_MD5,
+	.mac_length = 16,
+}, tls_sha = {
+	.id = 2,
+	.hmac_type = L_CHECKSUM_SHA1,
+	.mac_length = 20,
+}, tls_sha256 = {
+	.id = 4,
+	.hmac_type = L_CHECKSUM_SHA256,
+	.mac_length = 32,
+};
+
 static struct tls_cipher_suite tls_cipher_suite_pref[] = {
+	{
+		.id = { 0x00, 0x35 },
+		.name = "TLS_RSA_WITH_AES_256_CBC_SHA",
+		.verify_data_length = 12,
+		.encryption = &tls_aes256,
+		.mac = &tls_sha,
+		.key_xchg = &tls_rsa,
+	},
+	{
+		.id = { 0x00, 0x2f },
+		.name = "TLS_RSA_WITH_AES_128_CBC_SHA",
+		.verify_data_length = 12,
+		.encryption = &tls_aes128,
+		.mac = &tls_sha,
+		.key_xchg = &tls_rsa,
+	},
+	{
+		.id = { 0x00, 0x3d },
+		.name = "TLS_RSA_WITH_AES_256_CBC_SHA256",
+		.verify_data_length = 12,
+		.encryption = &tls_aes256,
+		.mac = &tls_sha256,
+		.key_xchg = &tls_rsa,
+	},
+	{
+		.id = { 0x00, 0x3c },
+		.name = "TLS_RSA_WITH_AES_128_CBC_SHA256",
+		.verify_data_length = 12,
+		.encryption = &tls_aes128,
+		.mac = &tls_sha256,
+		.key_xchg = &tls_rsa,
+	},
+	{
+		.id = { 0x00, 0x0a },
+		.name = "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
+		.verify_data_length = 12,
+		.encryption = &tls_3des_ede,
+		.mac = &tls_sha,
+		.key_xchg = &tls_rsa,
+	},
+	{
+		.id = { 0x00, 0x05 },
+		.name = "TLS_RSA_WITH_RC4_128_SHA",
+		.verify_data_length = 12,
+		.encryption = &tls_rc4,
+		.mac = &tls_sha,
+		.key_xchg = &tls_rsa,
+	},
+	{
+		.id = { 0x00, 0x04 },
+		.name = "TLS_RSA_WITH_RC4_128_MD5",
+		.verify_data_length = 12,
+		.encryption = &tls_rc4,
+		.mac = &tls_md5,
+		.key_xchg = &tls_rsa,
+	},
 };
 
 static struct tls_cipher_suite *tls_find_cipher_suite(const uint8_t *id)
@@ -257,6 +375,7 @@ static struct tls_compression_method *tls_find_compression_method(
 }
 
 static uint8_t tls_cert_type_pref[] = {
+	1, /* RSA_sign */
 };
 
 struct tls_signature_hash_algorithms {
@@ -265,6 +384,11 @@ struct tls_signature_hash_algorithms {
 };
 
 static struct tls_signature_hash_algorithms tls_signature_hash_pref[] = {
+	{ 6, 1 }, /* SHA512 + RSA */
+	{ 5, 1 }, /* SHA384 + RSA */
+	{ 4, 1 }, /* SHA256 + RSA */
+	{ 2, 1 }, /* SHA1 + RSA */
+	{ 1, 1 }, /* MD5 + RSA */
 };
 
 enum tls_handshake_type {
@@ -567,6 +691,192 @@ static void tls_generate_master_secret(struct l_tls *tls,
 	memset(tls->pending.server_random, 0, 32);
 }
 
+static bool tls_send_rsa_client_key_xchg(struct l_tls *tls)
+{
+	uint8_t buf[1024 + 32];
+	uint8_t *ptr = buf + TLS_HANDSHAKE_HEADER_SIZE;
+	uint8_t pre_master_secret[48];
+	struct l_rsa *rsa_server_pubkey;
+	int key_size;
+	bool result;
+
+	if (!tls->peer_pubkey) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR, 0);
+
+		return false;
+	}
+
+	pre_master_secret[0] = (uint8_t) (TLS_VERSION >> 8);
+	pre_master_secret[1] = (uint8_t) (TLS_VERSION >> 0);
+	l_getrandom(pre_master_secret + 2, 46);
+
+	/* Fill in the RSA Client Key Exchange body */
+
+	rsa_server_pubkey = l_rsa_pkcs1_v1_5_new(tls->peer_pubkey,
+						tls->peer_pubkey_length);
+	if (!rsa_server_pubkey) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR, 0);
+
+		return false;
+	}
+
+	key_size = l_rsa_get_key_size(rsa_server_pubkey);
+
+	if (key_size + 32 > (int) sizeof(buf)) {
+		l_rsa_free(rsa_server_pubkey);
+
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR, 0);
+
+		return false;
+	}
+
+	l_put_be16(key_size, ptr);
+	result = l_rsa_encrypt(rsa_server_pubkey, pre_master_secret, ptr + 2,
+				48, key_size);
+	ptr += key_size + 2;
+
+	l_rsa_free(rsa_server_pubkey);
+
+	if (!result) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR, 0);
+
+		return false;
+	}
+
+	tls_tx_handshake(tls, TLS_CLIENT_KEY_EXCHANGE, buf, ptr - buf);
+
+	tls_generate_master_secret(tls, pre_master_secret, 48);
+	memset(pre_master_secret, 0, 48);
+
+	return true;
+}
+
+static bool tls_rsa_sign(struct l_tls *tls, uint8_t **out, const uint8_t *hash)
+{
+	struct l_rsa *rsa_privkey;
+	uint8_t *privkey, *privkey_short;
+	size_t key_size, short_size;
+	bool result, encrypted;
+	uint8_t digest_info[HANDSHAKE_HASH_SIZE + 32];
+	size_t digest_info_len;
+
+	if (!tls->priv_key_path) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+				TLS_ALERT_BAD_CERT);
+
+		return false;
+	}
+
+	privkey = l_cert_load_key_file(tls->priv_key_path,
+					tls->priv_key_passphrase,
+					&key_size, &encrypted);
+	if (!privkey) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+				TLS_ALERT_BAD_CERT);
+
+		return false;
+	}
+
+	privkey_short = l_cert_extract_rsakey(privkey, key_size, &short_size);
+	l_cert_free_key(privkey, key_size);
+
+	if (!privkey_short) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+				TLS_ALERT_BAD_CERT);
+
+		return false;
+	}
+
+	rsa_privkey = l_rsa_pkcs1_v1_5_new(privkey_short, short_size);
+	l_cert_free_key(privkey_short, short_size);
+
+	if (!rsa_privkey) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR, 0);
+
+		return false;
+	}
+
+	key_size = l_rsa_get_key_size(rsa_privkey);
+
+	/*
+	 * TODO: According to 4.7 we need to support at least two forms
+	 * of the signed content here and at least three in the verification:
+	 *  - pre 1.2 version which didn't use the DigestInfo form at all,
+	 *  - DigestInfo with NULL AlgorithmIdentifier.parameters,
+	 *  - DigestInfo with empty AlgorithmIdentifier.parameters.
+	 *
+	 * Additionally PKCS#1 now says BER is used in place of DER for
+	 * DigestInfo encoding which adds more ambiguity in the encoding.
+	 */
+	pkcs1_write_digest_info(HANDSHAKE_HASH_TYPE,
+				digest_info, &digest_info_len,
+				hash, HANDSHAKE_HASH_SIZE);
+
+	(*out)[0] = HANDSHAKE_HASH_TYPE_TLS;
+	(*out)[1] = 1;	/* RSA_sign */
+	l_put_be16(key_size, *out + 2);
+	result = l_rsa_sign(rsa_privkey, digest_info, *out + 4,
+				digest_info_len, key_size);
+	*out += key_size + 4;
+
+	l_rsa_free(rsa_privkey);
+
+	return result;
+}
+
+static bool tls_rsa_verify(struct l_tls *tls, const uint8_t *in, size_t len,
+				const uint8_t *hash)
+{
+	struct l_rsa *rsa_client_pubkey;
+	size_t key_size;
+	bool result;
+	uint8_t expected[HANDSHAKE_HASH_SIZE + 32];
+	size_t expected_len;
+	uint8_t *digest_info;
+
+	if (len < 4 || (size_t) l_get_be16(in + 2) + 4 != len) {
+		tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+
+		return false;
+	}
+
+	rsa_client_pubkey = l_rsa_pkcs1_v1_5_new(tls->peer_pubkey,
+						tls->peer_pubkey_length);
+	if (!rsa_client_pubkey) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR, 0);
+
+		return false;
+	}
+
+	key_size = l_rsa_get_key_size(rsa_client_pubkey);
+
+	/* Only the default hash type supported */
+	if (len != 4 + key_size ||
+			in[0] != HANDSHAKE_HASH_TYPE_TLS ||
+			in[1] != 1 /* RSA_sign */) {
+		tls_disconnect(tls, TLS_ALERT_DECRYPT_ERROR, 0);
+
+		return false;
+	}
+
+	pkcs1_write_digest_info(HANDSHAKE_HASH_TYPE, expected, &expected_len,
+				hash, HANDSHAKE_HASH_SIZE);
+	digest_info = alloca(key_size);
+
+	result = l_rsa_verify(rsa_client_pubkey, in + 4, digest_info,
+				key_size, expected_len);
+
+	l_rsa_free(rsa_client_pubkey);
+
+	if (!result || memcmp(digest_info, expected, expected_len)) {
+		tls_disconnect(tls, TLS_ALERT_DECRYPT_ERROR, 0);
+
+		return false;
+	}
+
+	return true;
+}
+
 static void tls_get_handshake_hash(struct l_tls *tls, uint8_t *out)
 {
 	struct l_checksum *hash = l_checksum_clone(tls->handshake_hash);
@@ -1027,6 +1337,98 @@ static void tls_handle_server_hello_done(struct l_tls *tls,
 	tls->state = TLS_HANDSHAKE_WAIT_CHANGE_CIPHER_SPEC;
 }
 
+static void tls_handle_rsa_client_key_xchg(struct l_tls *tls,
+						const uint8_t *buf, size_t len)
+{
+	uint8_t pre_master_secret[48], random_secret[46];
+	struct l_rsa *rsa_server_privkey;
+	uint8_t *privkey, *privkey_short;
+	size_t key_size, short_size;
+	bool result, encrypted;
+
+	if (!tls->priv_key_path) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+				TLS_ALERT_BAD_CERT);
+
+		return;
+	}
+
+	privkey = l_cert_load_key_file(tls->priv_key_path,
+					tls->priv_key_passphrase,
+					&key_size, &encrypted);
+	if (!privkey) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+				TLS_ALERT_BAD_CERT);
+
+		return;
+	}
+
+	privkey_short = l_cert_extract_rsakey(privkey, key_size, &short_size);
+	l_cert_free_key(privkey, key_size);
+
+	if (!privkey_short) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR,
+				TLS_ALERT_BAD_CERT);
+
+		return;
+	}
+
+	rsa_server_privkey = l_rsa_pkcs1_v1_5_new(privkey_short, short_size);
+	l_cert_free_key(privkey_short, short_size);
+
+	if (!rsa_server_privkey) {
+		tls_disconnect(tls, TLS_ALERT_INTERNAL_ERROR, 0);
+
+		return;
+	}
+
+	key_size = l_rsa_get_key_size(rsa_server_privkey);
+
+	if (len != key_size + 2) {
+		l_rsa_free(rsa_server_privkey);
+
+		tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+
+		return;
+	}
+
+	len = l_get_be16(buf);
+
+	if (len != key_size) {
+		l_rsa_free(rsa_server_privkey);
+
+		tls_disconnect(tls, TLS_ALERT_DECODE_ERROR, 0);
+
+		return;
+	}
+
+	result = l_rsa_decrypt(rsa_server_privkey, buf + 2, pre_master_secret,
+				key_size, 48);
+	l_rsa_free(rsa_server_privkey);
+
+	/*
+	 * Assume correct premaster secret client version which according
+	 * to the TLS1.2 spec is unlikely in client implementations SSLv3
+	 * and prior.  Spec suggests either not supporting them or adding
+	 * a configurable override for <= SSLv3 clients.  For now we have
+	 * no need to support them.
+	 *
+	 * On any decode error randomise the Pre Master Secret as per the
+	 * countermeasures in 7.4.7.1 and don't generate any alerts.
+	 */
+	l_getrandom(random_secret, 46);
+
+	pre_master_secret[0] = tls->client_version >> 8;
+	pre_master_secret[1] = tls->client_version >> 0;
+
+	if (!result)
+		memcpy(pre_master_secret + 2, random_secret, 46);
+
+	tls_generate_master_secret(tls, pre_master_secret, 48);
+	memset(pre_master_secret, 0, 48);
+	memset(random_secret, 0, 46);
+}
+
 static void tls_handle_certificate_verify(struct l_tls *tls,
 						const uint8_t *buf, size_t len)
 {
Signature · 1 line
2.1.4
#6 lore raw

PATCH 16/17 examples: Add TLS server and client example programs.

In reply to Andrew Zaborowski
---
 .gitignore                   |   2 +
 Makefile.am                  |   4 +-
 examples/https-client-test.c | 181 +++++++++++++++++++++++++++++++++++++++++++
 examples/https-server-test.c | 169 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 355 insertions(+), 1 deletion(-)
 create mode 100644 examples/https-client-test.c
 create mode 100644 examples/https-server-test.c
.gitignore +2 -0
index c50d7e7..4f964de 100644
@@ -51,4 +51,6 @@ unit/test-cert
 unit/*.log
 unit/*.trs
 examples/dbus-service
+examples/https-server-test
+examples/https-client-test
 test-suite.log
Makefile.am +3 -1
index 0896100..1708341 100644
@@ -206,13 +206,15 @@ unit_example_plugin_la_LDFLAGS = -no-undefined -module -avoid-version \
 
 unit_test_data_files = unit/settings.test unit/cert-ca.pem unit/cert-client.pem unit/cert-server.pem unit/cert-client-key.pem unit/cert-server-key.pem
 
-examples = examples/dbus-service
+examples = examples/dbus-service examples/https-client-test examples/https-server-test
 
 if MAINTAINER_MODE
 noinst_PROGRAMS += $(examples)
 endif
 
 examples_dbus_service_LDADD = ell/libell-private.la
+examples_https_client_test_LDADD = ell/libell-private.la
+examples_https_server_test_LDADD = ell/libell-private.la
 
 EXTRA_DIST = $(unit_test_data_files)
 
examples/https-client-test.c new file +181 -0

181 lines not on this page · raw message

examples/https-server-test.c new file +169 -0

169 lines not on this page · raw message

Signature · 1 line
2.1.4
#7 lore raw

PATCH 17/17 unit: TLS connection end-to-end tests.

In reply to Andrew Zaborowski

Add a test of 6 scenarios where two struct l_tls instances negotiate a connection and exchange some data. They use the example certificates and private keys added earlier.

---
 unit/test-tls.c | 203 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 203 insertions(+)
unit/test-tls.c +203 -0
index 49d754f..b07603e 100644
@@ -165,6 +165,196 @@ static void test_tls12_prf(const void *data)
 	assert(!memcmp(out_buf, test->expected, test->out_len));
 }
 
+struct tls_conn_test {
+	const char *server_cert_path;
+	const char *server_key_path;
+	const char *server_key_passphrase;
+	const char *server_ca_cert_path;
+	const char *server_expect_identity;
+	const char *client_cert_path;
+	const char *client_key_path;
+	const char *client_key_passphrase;
+	const char *client_ca_cert_path;
+	const char *client_expect_identity;
+};
+
+static const struct tls_conn_test tls_conn_test_no_auth = {
+	.server_cert_path = TESTDATADIR "/cert-server.pem",
+	.server_key_path = TESTDATADIR "/cert-server-key.pem",
+	.server_expect_identity = NULL,
+	.client_expect_identity = NULL,
+};
+
+static const struct tls_conn_test tls_conn_test_server_auth = {
+	.server_cert_path = TESTDATADIR "/cert-server.pem",
+	.server_key_path = TESTDATADIR "/cert-server-key.pem",
+	.server_expect_identity = NULL,
+	.client_ca_cert_path = TESTDATADIR "/cert-ca.pem",
+	.client_expect_identity = "Foo Example Organization",
+};
+
+static const struct tls_conn_test tls_conn_test_client_auth_attempt = {
+	.server_cert_path = TESTDATADIR "/cert-server.pem",
+	.server_key_path = TESTDATADIR "/cert-server-key.pem",
+	.server_ca_cert_path = TESTDATADIR "/cert-ca.pem",
+	.server_expect_identity = NULL,
+	.client_expect_identity = NULL,
+};
+
+static const struct tls_conn_test tls_conn_test_client_auth = {
+	.server_cert_path = TESTDATADIR "/cert-server.pem",
+	.server_key_path = TESTDATADIR "/cert-server-key.pem",
+	.server_ca_cert_path = TESTDATADIR "/cert-ca.pem",
+	.server_expect_identity = "Bar Example Organization",
+	.client_cert_path = TESTDATADIR "/cert-client.pem",
+	.client_key_path = TESTDATADIR "/cert-client-key.pem",
+	.client_expect_identity = NULL,
+};
+
+static const struct tls_conn_test tls_conn_test_full_auth_attempt = {
+	.server_cert_path = TESTDATADIR "/cert-server.pem",
+	.server_key_path = TESTDATADIR "/cert-server-key.pem",
+	.server_ca_cert_path = TESTDATADIR "/cert-ca.pem",
+	.server_expect_identity = NULL,
+	.client_ca_cert_path = TESTDATADIR "/cert-ca.pem",
+	.client_expect_identity = "Foo Example Organization",
+};
+
+static const struct tls_conn_test tls_conn_test_full_auth = {
+	.server_cert_path = TESTDATADIR "/cert-server.pem",
+	.server_key_path = TESTDATADIR "/cert-server-key.pem",
+	.server_ca_cert_path = TESTDATADIR "/cert-ca.pem",
+	.server_expect_identity = "Bar Example Organization",
+	.client_cert_path = TESTDATADIR "/cert-client.pem",
+	.client_key_path = TESTDATADIR "/cert-client-key.pem",
+	.client_ca_cert_path = TESTDATADIR "/cert-ca.pem",
+	.client_expect_identity = "Foo Example Organization",
+};
+
+#define identity_compare(a, b) ((!(a) && !(b)) || ((a) && (b) && !strcmp(a, b)))
+
+struct tls_test_state {
+	struct l_tls *tls;
+
+	bool ready, success;
+
+	uint8_t raw_buf[16384];
+	int raw_buf_len;
+
+	uint8_t plaintext_buf[128];
+	int plaintext_buf_len;
+
+	const char *send_data;
+	const char *expect_data;
+	const char *expect_peer;
+};
+
+static void tls_test_new_data(void *user_data, const uint8_t *data, size_t len)
+{
+	struct tls_test_state *s = user_data;
+
+	assert(s->ready);
+	assert(s->plaintext_buf_len + len <= strlen(s->expect_data));
+
+	memcpy(s->plaintext_buf + s->plaintext_buf_len, data, len);
+	s->plaintext_buf_len += len;
+
+	if (s->plaintext_buf_len == (int) strlen(s->expect_data) &&
+			!memcmp(s->plaintext_buf, s->expect_data,
+				s->plaintext_buf_len))
+		s->success = true;
+}
+
+static void tls_test_write(void *user_data, const uint8_t *data, size_t len)
+{
+	struct tls_test_state *s = user_data;
+
+	assert(s->raw_buf_len + len <= sizeof(s->raw_buf));
+
+	memcpy(s->raw_buf + s->raw_buf_len, data, len);
+	s->raw_buf_len += len;
+}
+
+static void tls_test_ready(void *user_data, const char *peer_identity)
+{
+	struct tls_test_state *s = user_data;
+
+	assert(!s->ready);
+	assert(identity_compare(s->expect_peer, peer_identity));
+	s->ready = true;
+
+	l_tls_write(s->tls, (const uint8_t *) s->send_data,
+			strlen(s->send_data));
+}
+
+static void tls_test_disconnected(void *user_data, enum l_tls_alert_desc reason,
+					bool remote)
+{
+	assert(false);
+}
+
+static void test_tls_test(const void *data)
+{
+	const struct tls_conn_test *test = data;
+	struct tls_test_state s[2] = {
+		{
+			.ready = false,
+			.success = false,
+			.raw_buf_len = 0,
+			.plaintext_buf_len = 0,
+			.send_data = "server to client",
+			.expect_data = "client to server",
+			.expect_peer = test->server_expect_identity,
+		},
+		{
+			.ready = false,
+			.success = false,
+			.raw_buf_len = 0,
+			.plaintext_buf_len = 0,
+			.send_data = "client to server",
+			.expect_data = "server to client",
+			.expect_peer = test->client_expect_identity,
+		},
+	};
+
+	/* Server */
+	s[0].tls = l_tls_new(true, tls_test_new_data, tls_test_write,
+				tls_test_ready, tls_test_disconnected, &s[0]);
+	/* Client */
+	s[1].tls = l_tls_new(false, tls_test_new_data, tls_test_write,
+				tls_test_ready, tls_test_disconnected, &s[1]);
+
+	assert(s[0].tls);
+	assert(s[1].tls);
+
+	l_tls_set_auth_data(s[0].tls, test->server_cert_path,
+				test->server_key_path,
+				test->server_key_passphrase);
+	l_tls_set_auth_data(s[1].tls, test->client_cert_path,
+				test->client_key_path,
+				test->client_key_passphrase);
+	l_tls_set_cacert(s[0].tls, test->server_ca_cert_path);
+	l_tls_set_cacert(s[1].tls, test->client_ca_cert_path);
+
+	while (1) {
+		if (s[0].raw_buf_len) {
+			l_tls_handle_rx(s[1].tls, s[0].raw_buf,
+					s[0].raw_buf_len);
+			s[0].raw_buf_len = 0;
+		} else if (s[1].raw_buf_len) {
+			l_tls_handle_rx(s[0].tls, s[1].raw_buf,
+					s[1].raw_buf_len);
+			s[1].raw_buf_len = 0;
+		} else
+			break;
+	}
+
+	assert(s[0].success && s[1].success);
+
+	l_tls_free(s[0].tls);
+	l_tls_free(s[1].tls);
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -178,5 +368,18 @@ int main(int argc, char *argv[])
 	l_test_add("TLS 1.2 PRF with SHA512", test_tls12_prf,
 			&tls12_prf_sha512_0);
 
+	l_test_add("TLS connection no auth", test_tls_test,
+			&tls_conn_test_no_auth);
+	l_test_add("TLS connection server auth", test_tls_test,
+			&tls_conn_test_server_auth);
+	l_test_add("TLS connection client auth attempt", test_tls_test,
+			&tls_conn_test_client_auth_attempt);
+	l_test_add("TLS connection client auth", test_tls_test,
+			&tls_conn_test_client_auth);
+	l_test_add("TLS connection full auth attempt", test_tls_test,
+			&tls_conn_test_full_auth_attempt);
+	l_test_add("TLS connection full auth", test_tls_test,
+			&tls_conn_test_full_auth);
+
 	return l_test_run();
 }
Signature · 1 line
2.1.4