2
0

70-test_sslsigalgs.t 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #! /usr/bin/env perl
  2. # Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. use strict;
  9. use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
  10. use OpenSSL::Test::Utils;
  11. use TLSProxy::Proxy;
  12. my $test_name = "test_sslsigalgs";
  13. setup($test_name);
  14. plan skip_all => "TLSProxy isn't usable on $^O"
  15. if $^O =~ /^(VMS)$/;
  16. plan skip_all => "$test_name needs the dynamic engine feature enabled"
  17. if disabled("engine") || disabled("dynamic-engine");
  18. plan skip_all => "$test_name needs the sock feature enabled"
  19. if disabled("sock");
  20. plan skip_all => "$test_name needs TLS1.2 or TLS1.3 enabled"
  21. if disabled("tls1_2") && disabled("tls1_3");
  22. $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
  23. my $proxy = TLSProxy::Proxy->new(
  24. undef,
  25. cmdstr(app(["openssl"]), display => 1),
  26. srctop_file("apps", "server.pem"),
  27. (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
  28. );
  29. use constant {
  30. NO_SIG_ALGS_EXT => 0,
  31. EMPTY_SIG_ALGS_EXT => 1,
  32. NO_KNOWN_SIG_ALGS => 2,
  33. NO_PSS_SIG_ALGS => 3,
  34. PSS_ONLY_SIG_ALGS => 4,
  35. PURE_SIGALGS => 5,
  36. COMPAT_SIGALGS => 6,
  37. SIGALGS_CERT_ALL => 7,
  38. SIGALGS_CERT_PKCS => 8,
  39. SIGALGS_CERT_INVALID => 9
  40. };
  41. #Note: Throughout this test we override the default ciphersuites where TLSv1.2
  42. # is expected to ensure that a ServerKeyExchange message is sent that uses
  43. # the sigalgs
  44. #Test 1: Default sig algs should succeed
  45. $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
  46. plan tests => 21;
  47. ok(TLSProxy::Message->success, "Default sigalgs");
  48. my $testtype;
  49. SKIP: {
  50. skip "TLSv1.3 disabled", 5 if disabled("tls1_3");
  51. $proxy->filter(\&sigalgs_filter);
  52. #Test 2: Sending no sig algs extension in TLSv1.3 should fail
  53. $proxy->clear();
  54. $testtype = NO_SIG_ALGS_EXT;
  55. $proxy->start();
  56. ok(TLSProxy::Message->fail, "No TLSv1.3 sigalgs");
  57. #Test 3: Sending an empty sig algs extension in TLSv1.3 should fail
  58. $proxy->clear();
  59. $testtype = EMPTY_SIG_ALGS_EXT;
  60. $proxy->start();
  61. ok(TLSProxy::Message->fail, "Empty TLSv1.3 sigalgs");
  62. #Test 4: Sending a list with no recognised sig algs in TLSv1.3 should fail
  63. $proxy->clear();
  64. $testtype = NO_KNOWN_SIG_ALGS;
  65. $proxy->start();
  66. ok(TLSProxy::Message->fail, "No known TLSv1.3 sigalgs");
  67. #Test 5: Sending a sig algs list without pss for an RSA cert in TLSv1.3
  68. # should fail
  69. $proxy->clear();
  70. $testtype = NO_PSS_SIG_ALGS;
  71. $proxy->start();
  72. ok(TLSProxy::Message->fail, "No PSS TLSv1.3 sigalgs");
  73. #Test 6: Sending only TLSv1.3 PSS sig algs in TLSv1.3 should succeed
  74. #TODO(TLS1.3): Do we need to verify the cert to make sure its a PSS only
  75. #cert in this case?
  76. $proxy->clear();
  77. $testtype = PSS_ONLY_SIG_ALGS;
  78. $proxy->start();
  79. ok(TLSProxy::Message->success, "PSS only sigalgs in TLSv1.3");
  80. }
  81. SKIP: {
  82. skip "EC or TLSv1.3 disabled", 1
  83. if disabled("tls1_3") || disabled("ec");
  84. #Test 7: Sending a valid sig algs list but not including a sig type that
  85. # matches the certificate should fail in TLSv1.3.
  86. $proxy->clear();
  87. $proxy->clientflags("-sigalgs ECDSA+SHA256");
  88. $proxy->filter(undef);
  89. $proxy->start();
  90. ok(TLSProxy::Message->fail, "No matching TLSv1.3 sigalgs");
  91. }
  92. SKIP: {
  93. skip "EC, TLSv1.3 or TLSv1.2 disabled", 1
  94. if disabled("tls1_2") || disabled("tls1_3") || disabled("ec");
  95. #Test 8: Sending a full list of TLSv1.3 sig algs but negotiating TLSv1.2
  96. # should succeed
  97. $proxy->clear();
  98. $proxy->serverflags("-no_tls1_3");
  99. $proxy->ciphers("ECDHE-RSA-AES128-SHA");
  100. $proxy->filter(undef);
  101. $proxy->start();
  102. ok(TLSProxy::Message->success, "TLSv1.3 client TLSv1.2 server");
  103. }
  104. SKIP: {
  105. skip "EC or TLSv1.2 disabled", 8 if disabled("tls1_2") || disabled("ec");
  106. $proxy->filter(\&sigalgs_filter);
  107. #Test 9: Sending no sig algs extension in TLSv1.2 should succeed
  108. $proxy->clear();
  109. $testtype = NO_SIG_ALGS_EXT;
  110. $proxy->clientflags("-no_tls1_3");
  111. $proxy->ciphers("ECDHE-RSA-AES128-SHA");
  112. $proxy->start();
  113. ok(TLSProxy::Message->success, "No TLSv1.2 sigalgs");
  114. #Test 10: Sending an empty sig algs extension in TLSv1.2 should fail
  115. $proxy->clear();
  116. $testtype = EMPTY_SIG_ALGS_EXT;
  117. $proxy->clientflags("-no_tls1_3");
  118. $proxy->ciphers("ECDHE-RSA-AES128-SHA");
  119. $proxy->start();
  120. ok(TLSProxy::Message->fail, "Empty TLSv1.2 sigalgs");
  121. #Test 11: Sending a list with no recognised sig algs in TLSv1.2 should fail
  122. $proxy->clear();
  123. $testtype = NO_KNOWN_SIG_ALGS;
  124. $proxy->clientflags("-no_tls1_3");
  125. $proxy->ciphers("ECDHE-RSA-AES128-SHA");
  126. $proxy->start();
  127. ok(TLSProxy::Message->fail, "No known TLSv1.3 sigalgs");
  128. #Test 12: Sending a sig algs list without pss for an RSA cert in TLSv1.2
  129. # should succeed
  130. $proxy->clear();
  131. $testtype = NO_PSS_SIG_ALGS;
  132. $proxy->clientflags("-no_tls1_3");
  133. $proxy->ciphers("ECDHE-RSA-AES128-SHA");
  134. $proxy->start();
  135. ok(TLSProxy::Message->success, "No PSS TLSv1.2 sigalgs");
  136. #Test 13: Sending only TLSv1.3 PSS sig algs in TLSv1.2 should succeed
  137. $proxy->clear();
  138. $testtype = PSS_ONLY_SIG_ALGS;
  139. $proxy->serverflags("-no_tls1_3");
  140. $proxy->ciphers("ECDHE-RSA-AES128-SHA");
  141. $proxy->start();
  142. ok(TLSProxy::Message->success, "PSS only sigalgs in TLSv1.2");
  143. #Test 14: Responding with a sig alg we did not send in TLSv1.2 should fail
  144. # We send rsa_pkcs1_sha256 and respond with rsa_pss_rsae_sha256
  145. # TODO(TLS1.3): Add a similar test to the TLSv1.3 section above
  146. # when we have an API capable of configuring the TLSv1.3 sig algs
  147. $proxy->clear();
  148. $testtype = PSS_ONLY_SIG_ALGS;
  149. $proxy->clientflags("-no_tls1_3 -sigalgs RSA+SHA256");
  150. $proxy->ciphers("ECDHE-RSA-AES128-SHA");
  151. $proxy->start();
  152. ok(TLSProxy::Message->fail, "Sigalg we did not send in TLSv1.2");
  153. #Test 15: Sending a valid sig algs list but not including a sig type that
  154. # matches the certificate should fail in TLSv1.2
  155. $proxy->clear();
  156. $proxy->clientflags("-no_tls1_3 -sigalgs ECDSA+SHA256");
  157. $proxy->ciphers("ECDHE-RSA-AES128-SHA");
  158. $proxy->filter(undef);
  159. $proxy->start();
  160. ok(TLSProxy::Message->fail, "No matching TLSv1.2 sigalgs");
  161. $proxy->filter(\&sigalgs_filter);
  162. #Test 16: No sig algs extension, ECDSA cert, TLSv1.2 should succeed
  163. $proxy->clear();
  164. $testtype = NO_SIG_ALGS_EXT;
  165. $proxy->clientflags("-no_tls1_3");
  166. $proxy->serverflags("-cert " . srctop_file("test", "certs",
  167. "server-ecdsa-cert.pem") .
  168. " -key " . srctop_file("test", "certs",
  169. "server-ecdsa-key.pem")),
  170. $proxy->ciphers("ECDHE-ECDSA-AES128-SHA");
  171. $proxy->start();
  172. ok(TLSProxy::Message->success, "No TLSv1.2 sigalgs, ECDSA");
  173. }
  174. my ($dsa_status, $sha1_status, $sha224_status);
  175. SKIP: {
  176. skip "TLSv1.3 disabled", 2 if disabled("tls1_3") || disabled("dsa");
  177. #Test 17: signature_algorithms with 1.3-only ClientHello
  178. $testtype = PURE_SIGALGS;
  179. $dsa_status = $sha1_status = $sha224_status = 0;
  180. $proxy->clear();
  181. $proxy->clientflags("-tls1_3");
  182. $proxy->filter(\&modify_sigalgs_filter);
  183. $proxy->start();
  184. ok($dsa_status && $sha1_status && $sha224_status,
  185. "DSA/SHA2 sigalg sent for 1.3-only ClientHello");
  186. #Test 18: signature_algorithms with backwards compatible ClientHello
  187. SKIP: {
  188. skip "TLSv1.2 disabled", 1 if disabled("tls1_2");
  189. $testtype = COMPAT_SIGALGS;
  190. $dsa_status = $sha1_status = $sha224_status = 0;
  191. $proxy->clear();
  192. $proxy->filter(\&modify_sigalgs_filter);
  193. $proxy->start();
  194. ok($dsa_status && $sha1_status && $sha224_status,
  195. "DSA sigalg not sent for compat ClientHello");
  196. }
  197. }
  198. SKIP: {
  199. skip "TLSv1.3 disabled", 3 if disabled("tls1_3");
  200. #Test 19: Insert signature_algorithms_cert that match normal sigalgs
  201. $testtype = SIGALGS_CERT_ALL;
  202. $proxy->clear();
  203. $proxy->filter(\&modify_sigalgs_cert_filter);
  204. $proxy->start();
  205. ok(TLSProxy::Message->success, "sigalgs_cert in TLSv1.3");
  206. #Test 19: Insert signature_algorithms_cert that forces PKCS#1 cert
  207. $testtype = SIGALGS_CERT_PKCS;
  208. $proxy->clear();
  209. $proxy->filter(\&modify_sigalgs_cert_filter);
  210. $proxy->start();
  211. ok(TLSProxy::Message->success, "sigalgs_cert in TLSv1.3 with PKCS#1 cert");
  212. #Test 19: Insert signature_algorithms_cert that fails
  213. $testtype = SIGALGS_CERT_INVALID;
  214. $proxy->clear();
  215. $proxy->filter(\&modify_sigalgs_cert_filter);
  216. $proxy->start();
  217. ok(TLSProxy::Message->fail, "No matching certificate for sigalgs_cert");
  218. }
  219. sub sigalgs_filter
  220. {
  221. my $proxy = shift;
  222. # We're only interested in the initial ClientHello
  223. if ($proxy->flight != 0) {
  224. return;
  225. }
  226. foreach my $message (@{$proxy->message_list}) {
  227. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  228. if ($testtype == NO_SIG_ALGS_EXT) {
  229. $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);
  230. } else {
  231. my $sigalg;
  232. if ($testtype == EMPTY_SIG_ALGS_EXT) {
  233. $sigalg = pack "C2", 0x00, 0x00;
  234. } elsif ($testtype == NO_KNOWN_SIG_ALGS) {
  235. $sigalg = pack "C4", 0x00, 0x02, 0xff, 0xff;
  236. } elsif ($testtype == NO_PSS_SIG_ALGS) {
  237. #No PSS sig algs - just send rsa_pkcs1_sha256
  238. $sigalg = pack "C4", 0x00, 0x02, 0x04, 0x01;
  239. } else {
  240. #PSS sig algs only - just send rsa_pss_rsae_sha256
  241. $sigalg = pack "C4", 0x00, 0x02, 0x08, 0x04;
  242. }
  243. $message->set_extension(TLSProxy::Message::EXT_SIG_ALGS, $sigalg);
  244. }
  245. $message->repack();
  246. }
  247. }
  248. }
  249. sub modify_sigalgs_filter
  250. {
  251. my $proxy = shift;
  252. # We're only interested in the initial ClientHello
  253. return if ($proxy->flight != 0);
  254. foreach my $message (@{$proxy->message_list}) {
  255. my $ext;
  256. my @algs;
  257. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  258. if ($testtype == PURE_SIGALGS) {
  259. my $ok = 1;
  260. $ext = $message->extension_data->{TLSProxy::Message::EXT_SIG_ALGS};
  261. @algs = unpack('S>*', $ext);
  262. # unpack will unpack the length as well
  263. shift @algs;
  264. foreach (@algs) {
  265. if ($_ == TLSProxy::Message::SIG_ALG_DSA_SHA256
  266. || $_ == TLSProxy::Message::SIG_ALG_DSA_SHA384
  267. || $_ == TLSProxy::Message::SIG_ALG_DSA_SHA512
  268. || $_ == TLSProxy::Message::OSSL_SIG_ALG_DSA_SHA224
  269. || $_ == TLSProxy::Message::SIG_ALG_RSA_PKCS1_SHA1
  270. || $_ == TLSProxy::Message::SIG_ALG_DSA_SHA1
  271. || $_ == TLSProxy::Message::SIG_ALG_ECDSA_SHA1) {
  272. $ok = 0;
  273. }
  274. }
  275. $sha1_status = $dsa_status = $sha224_status = 1 if ($ok);
  276. } elsif ($testtype == COMPAT_SIGALGS) {
  277. $ext = $message->extension_data->{TLSProxy::Message::EXT_SIG_ALGS};
  278. @algs = unpack('S>*', $ext);
  279. # unpack will unpack the length as well
  280. shift @algs;
  281. foreach (@algs) {
  282. if ($_ == TLSProxy::Message::SIG_ALG_DSA_SHA256
  283. || $_ == TLSProxy::Message::SIG_ALG_DSA_SHA384
  284. || $_ == TLSProxy::Message::SIG_ALG_DSA_SHA512) {
  285. $dsa_status = 1;
  286. }
  287. if ($_ == TLSProxy::Message::SIG_ALG_RSA_PKCS1_SHA1
  288. || $_ == TLSProxy::Message::SIG_ALG_DSA_SHA1
  289. || $_ == TLSProxy::Message::SIG_ALG_ECDSA_SHA1) {
  290. $sha1_status = 1;
  291. }
  292. if ($_ == TLSProxy::Message::OSSL_SIG_ALG_RSA_PKCS1_SHA224
  293. || $_ == TLSProxy::Message::OSSL_SIG_ALG_DSA_SHA224
  294. || $_ == TLSProxy::Message::OSSL_SIG_ALG_ECDSA_SHA224) {
  295. $sha224_status = 1;
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. sub modify_sigalgs_cert_filter
  303. {
  304. my $proxy = shift;
  305. # We're only interested in the initial ClientHello
  306. if ($proxy->flight != 0) {
  307. return;
  308. }
  309. foreach my $message (@{$proxy->message_list}) {
  310. if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
  311. my $sigs;
  312. # two byte length at front of sigs, then two-byte sigschemes
  313. if ($testtype == SIGALGS_CERT_ALL) {
  314. $sigs = pack "C26", 0x00, 0x18,
  315. # rsa_pkcs_sha{256,512} rsa_pss_rsae_sha{256,512}
  316. 0x04, 0x01, 0x06, 0x01, 0x08, 0x04, 0x08, 0x06,
  317. # ed25518 ed448 rsa_pss_pss_sha{256,512}
  318. 0x08, 0x07, 0x08, 0x08, 0x08, 0x09, 0x08, 0x0b,
  319. # ecdsa_secp{256,512} rsa+sha1 ecdsa+sha1
  320. 0x04, 0x03, 0x06, 0x03, 0x02, 0x01, 0x02, 0x03;
  321. } elsif ($testtype == SIGALGS_CERT_PKCS) {
  322. $sigs = pack "C10", 0x00, 0x08,
  323. # rsa_pkcs_sha{256,384,512,1}
  324. 0x04, 0x01, 0x05, 0x01, 0x06, 0x01, 0x02, 0x01;
  325. } elsif ($testtype == SIGALGS_CERT_INVALID) {
  326. $sigs = pack "C4", 0x00, 0x02,
  327. # unregistered codepoint
  328. 0xb2, 0x6f;
  329. }
  330. $message->set_extension(TLSProxy::Message::EXT_SIG_ALGS_CERT, $sigs);
  331. $message->repack();
  332. }
  333. }
  334. }