##// END OF EJS Templates
tests: test case where default ca certs not available...
Gregory Szorc -
r29448:afbe1fe4 default
parent child Browse files
Show More
@@ -1,404 +1,409
1 1 #require serve ssl
2 2
3 3 Proper https client requires the built-in ssl from Python 2.6.
4 4
5 5 Make server certificates:
6 6
7 7 $ CERTSDIR="$TESTDIR/sslcerts"
8 8 $ cat "$CERTSDIR/priv.pem" "$CERTSDIR/pub.pem" >> server.pem
9 9 $ PRIV=`pwd`/server.pem
10 10 $ cat "$CERTSDIR/priv.pem" "$CERTSDIR/pub-not-yet.pem" > server-not-yet.pem
11 11 $ cat "$CERTSDIR/priv.pem" "$CERTSDIR/pub-expired.pem" > server-expired.pem
12 12
13 13 $ hg init test
14 14 $ cd test
15 15 $ echo foo>foo
16 16 $ mkdir foo.d foo.d/bAr.hg.d foo.d/baR.d.hg
17 17 $ echo foo>foo.d/foo
18 18 $ echo bar>foo.d/bAr.hg.d/BaR
19 19 $ echo bar>foo.d/baR.d.hg/bAR
20 20 $ hg commit -A -m 1
21 21 adding foo
22 22 adding foo.d/bAr.hg.d/BaR
23 23 adding foo.d/baR.d.hg/bAR
24 24 adding foo.d/foo
25 25 $ hg serve -p $HGPORT -d --pid-file=../hg0.pid --certificate=$PRIV
26 26 $ cat ../hg0.pid >> $DAEMON_PIDS
27 27
28 28 cacert not found
29 29
30 30 $ hg in --config web.cacerts=no-such.pem https://localhost:$HGPORT/
31 31 abort: could not find web.cacerts: no-such.pem
32 32 [255]
33 33
34 34 Test server address cannot be reused
35 35
36 36 #if windows
37 37 $ hg serve -p $HGPORT --certificate=$PRIV 2>&1
38 38 abort: cannot start server at ':$HGPORT':
39 39 [255]
40 40 #else
41 41 $ hg serve -p $HGPORT --certificate=$PRIV 2>&1
42 42 abort: cannot start server at ':$HGPORT': Address already in use
43 43 [255]
44 44 #endif
45 45 $ cd ..
46 46
47 47 Our test cert is not signed by a trusted CA. It should fail to verify if
48 48 we are able to load CA certs.
49 49
50 50 #if defaultcacerts
51 51 $ hg clone https://localhost:$HGPORT/ copy-pull
52 52 abort: error: *certificate verify failed* (glob)
53 53 [255]
54 #else
55 $ hg clone https://localhost:$HGPORT/ copy-pull
56 abort: localhost certificate error: no certificate received
57 (set hostsecurity.localhost:certfingerprints=sha256:62:09:97:2f:97:60:e3:65:8f:12:5d:78:9e:35:a1:36:7a:65:4b:0e:9f:ac:db:c3:bc:6e:b6:a3:c0:16:e0:30 config setting or use --insecure to connect insecurely)
58 [255]
54 59 #endif
55 60
56 61 Specifying a per-host certificate file that doesn't exist will abort
57 62
58 63 $ hg --config hostsecurity.localhost:verifycertsfile=/does/not/exist clone https://localhost:$HGPORT/
59 64 abort: path specified by hostsecurity.localhost:verifycertsfile does not exist: /does/not/exist
60 65 [255]
61 66
62 67 A malformed per-host certificate file will raise an error
63 68
64 69 $ echo baddata > badca.pem
65 70 #if sslcontext
66 71 $ hg --config hostsecurity.localhost:verifycertsfile=badca.pem clone https://localhost:$HGPORT/
67 72 abort: error loading CA file badca.pem: * (glob)
68 73 (file is empty or malformed?)
69 74 [255]
70 75 #else
71 76 $ hg --config hostsecurity.localhost:verifycertsfile=badca.pem clone https://localhost:$HGPORT/
72 77 abort: error: * (glob)
73 78 [255]
74 79 #endif
75 80
76 81 A per-host certificate mismatching the server will fail verification
77 82
78 83 $ hg --config hostsecurity.localhost:verifycertsfile="$CERTSDIR/client-cert.pem" clone https://localhost:$HGPORT/
79 84 abort: error: *certificate verify failed* (glob)
80 85 [255]
81 86
82 87 A per-host certificate matching the server's cert will be accepted
83 88
84 89 $ hg --config hostsecurity.localhost:verifycertsfile="$CERTSDIR/pub.pem" clone -U https://localhost:$HGPORT/ perhostgood1
85 90 requesting all changes
86 91 adding changesets
87 92 adding manifests
88 93 adding file changes
89 94 added 1 changesets with 4 changes to 4 files
90 95
91 96 A per-host certificate with multiple certs and one matching will be accepted
92 97
93 98 $ cat "$CERTSDIR/client-cert.pem" "$CERTSDIR/pub.pem" > perhost.pem
94 99 $ hg --config hostsecurity.localhost:verifycertsfile=perhost.pem clone -U https://localhost:$HGPORT/ perhostgood2
95 100 requesting all changes
96 101 adding changesets
97 102 adding manifests
98 103 adding file changes
99 104 added 1 changesets with 4 changes to 4 files
100 105
101 106 Defining both per-host certificate and a fingerprint will print a warning
102 107
103 108 $ hg --config hostsecurity.localhost:verifycertsfile="$CERTSDIR/pub.pem" --config hostsecurity.localhost:fingerprints=sha1:914f1aff87249c09b6859b88b1906d30756491ca clone -U https://localhost:$HGPORT/ caandfingerwarning
104 109 (hostsecurity.localhost:verifycertsfile ignored when host fingerprints defined; using host fingerprints for verification)
105 110 requesting all changes
106 111 adding changesets
107 112 adding manifests
108 113 adding file changes
109 114 added 1 changesets with 4 changes to 4 files
110 115
111 116 $ DISABLECACERTS="--config devel.disableloaddefaultcerts=true"
112 117
113 118 Inability to verify peer certificate will result in abort
114 119
115 120 $ hg clone https://localhost:$HGPORT/ copy-pull $DISABLECACERTS
116 121 abort: unable to verify security of localhost (no loaded CA certificates); refusing to connect
117 122 (see https://mercurial-scm.org/wiki/SecureConnections for how to configure Mercurial to avoid this error or set hostsecurity.localhost:fingerprints=sha256:62:09:97:2f:97:60:e3:65:8f:12:5d:78:9e:35:a1:36:7a:65:4b:0e:9f:ac:db:c3:bc:6e:b6:a3:c0:16:e0:30 to trust this server)
118 123 [255]
119 124
120 125 $ hg clone --insecure https://localhost:$HGPORT/ copy-pull
121 126 warning: connection security to localhost is disabled per current settings; communication is susceptible to eavesdropping and tampering
122 127 requesting all changes
123 128 adding changesets
124 129 adding manifests
125 130 adding file changes
126 131 added 1 changesets with 4 changes to 4 files
127 132 updating to branch default
128 133 4 files updated, 0 files merged, 0 files removed, 0 files unresolved
129 134 $ hg verify -R copy-pull
130 135 checking changesets
131 136 checking manifests
132 137 crosschecking files in changesets and manifests
133 138 checking files
134 139 4 files, 1 changesets, 4 total revisions
135 140 $ cd test
136 141 $ echo bar > bar
137 142 $ hg commit -A -d '1 0' -m 2
138 143 adding bar
139 144 $ cd ..
140 145
141 146 pull without cacert
142 147
143 148 $ cd copy-pull
144 149 $ echo '[hooks]' >> .hg/hgrc
145 150 $ echo "changegroup = printenv.py changegroup" >> .hg/hgrc
146 151 $ hg pull $DISABLECACERTS
147 152 pulling from https://localhost:$HGPORT/
148 153 abort: unable to verify security of localhost (no loaded CA certificates); refusing to connect
149 154 (see https://mercurial-scm.org/wiki/SecureConnections for how to configure Mercurial to avoid this error or set hostsecurity.localhost:fingerprints=sha256:62:09:97:2f:97:60:e3:65:8f:12:5d:78:9e:35:a1:36:7a:65:4b:0e:9f:ac:db:c3:bc:6e:b6:a3:c0:16:e0:30 to trust this server)
150 155 [255]
151 156
152 157 $ hg pull --insecure
153 158 pulling from https://localhost:$HGPORT/
154 159 warning: connection security to localhost is disabled per current settings; communication is susceptible to eavesdropping and tampering
155 160 searching for changes
156 161 adding changesets
157 162 adding manifests
158 163 adding file changes
159 164 added 1 changesets with 1 changes to 1 files
160 165 changegroup hook: HG_NODE=5fed3813f7f5e1824344fdc9cf8f63bb662c292d HG_NODE_LAST=5fed3813f7f5e1824344fdc9cf8f63bb662c292d HG_SOURCE=pull HG_TXNID=TXN:* HG_URL=https://localhost:$HGPORT/ (glob)
161 166 (run 'hg update' to get a working copy)
162 167 $ cd ..
163 168
164 169 cacert configured in local repo
165 170
166 171 $ cp copy-pull/.hg/hgrc copy-pull/.hg/hgrc.bu
167 172 $ echo "[web]" >> copy-pull/.hg/hgrc
168 173 $ echo "cacerts=$CERTSDIR/pub.pem" >> copy-pull/.hg/hgrc
169 174 $ hg -R copy-pull pull --traceback
170 175 pulling from https://localhost:$HGPORT/
171 176 searching for changes
172 177 no changes found
173 178 $ mv copy-pull/.hg/hgrc.bu copy-pull/.hg/hgrc
174 179
175 180 cacert configured globally, also testing expansion of environment
176 181 variables in the filename
177 182
178 183 $ echo "[web]" >> $HGRCPATH
179 184 $ echo 'cacerts=$P/pub.pem' >> $HGRCPATH
180 185 $ P="$CERTSDIR" hg -R copy-pull pull
181 186 pulling from https://localhost:$HGPORT/
182 187 searching for changes
183 188 no changes found
184 189 $ P="$CERTSDIR" hg -R copy-pull pull --insecure
185 190 pulling from https://localhost:$HGPORT/
186 191 warning: connection security to localhost is disabled per current settings; communication is susceptible to eavesdropping and tampering
187 192 searching for changes
188 193 no changes found
189 194
190 195 empty cacert file
191 196
192 197 $ touch emptycafile
193 198
194 199 #if sslcontext
195 200 $ hg --config web.cacerts=emptycafile -R copy-pull pull
196 201 pulling from https://localhost:$HGPORT/
197 202 abort: error loading CA file emptycafile: * (glob)
198 203 (file is empty or malformed?)
199 204 [255]
200 205 #else
201 206 $ hg --config web.cacerts=emptycafile -R copy-pull pull
202 207 pulling from https://localhost:$HGPORT/
203 208 abort: error: * (glob)
204 209 [255]
205 210 #endif
206 211
207 212 cacert mismatch
208 213
209 214 $ hg -R copy-pull pull --config web.cacerts="$CERTSDIR/pub.pem" \
210 215 > https://127.0.0.1:$HGPORT/
211 216 pulling from https://127.0.0.1:$HGPORT/
212 217 abort: 127.0.0.1 certificate error: certificate is for localhost
213 218 (set hostsecurity.127.0.0.1:certfingerprints=sha256:62:09:97:2f:97:60:e3:65:8f:12:5d:78:9e:35:a1:36:7a:65:4b:0e:9f:ac:db:c3:bc:6e:b6:a3:c0:16:e0:30 config setting or use --insecure to connect insecurely)
214 219 [255]
215 220 $ hg -R copy-pull pull --config web.cacerts="$CERTSDIR/pub.pem" \
216 221 > https://127.0.0.1:$HGPORT/ --insecure
217 222 pulling from https://127.0.0.1:$HGPORT/
218 223 warning: connection security to 127.0.0.1 is disabled per current settings; communication is susceptible to eavesdropping and tampering
219 224 searching for changes
220 225 no changes found
221 226 $ hg -R copy-pull pull --config web.cacerts="$CERTSDIR/pub-other.pem"
222 227 pulling from https://localhost:$HGPORT/
223 228 abort: error: *certificate verify failed* (glob)
224 229 [255]
225 230 $ hg -R copy-pull pull --config web.cacerts="$CERTSDIR/pub-other.pem" \
226 231 > --insecure
227 232 pulling from https://localhost:$HGPORT/
228 233 warning: connection security to localhost is disabled per current settings; communication is susceptible to eavesdropping and tampering
229 234 searching for changes
230 235 no changes found
231 236
232 237 Test server cert which isn't valid yet
233 238
234 239 $ hg serve -R test -p $HGPORT1 -d --pid-file=hg1.pid --certificate=server-not-yet.pem
235 240 $ cat hg1.pid >> $DAEMON_PIDS
236 241 $ hg -R copy-pull pull --config web.cacerts="$CERTSDIR/pub-not-yet.pem" \
237 242 > https://localhost:$HGPORT1/
238 243 pulling from https://localhost:$HGPORT1/
239 244 abort: error: *certificate verify failed* (glob)
240 245 [255]
241 246
242 247 Test server cert which no longer is valid
243 248
244 249 $ hg serve -R test -p $HGPORT2 -d --pid-file=hg2.pid --certificate=server-expired.pem
245 250 $ cat hg2.pid >> $DAEMON_PIDS
246 251 $ hg -R copy-pull pull --config web.cacerts="$CERTSDIR/pub-expired.pem" \
247 252 > https://localhost:$HGPORT2/
248 253 pulling from https://localhost:$HGPORT2/
249 254 abort: error: *certificate verify failed* (glob)
250 255 [255]
251 256
252 257 Fingerprints
253 258
254 259 - works without cacerts (hostkeyfingerprints)
255 260 $ hg -R copy-pull id https://localhost:$HGPORT/ --insecure --config hostfingerprints.localhost=91:4f:1a:ff:87:24:9c:09:b6:85:9b:88:b1:90:6d:30:75:64:91:ca
256 261 5fed3813f7f5
257 262
258 263 - works without cacerts (hostsecurity)
259 264 $ hg -R copy-pull id https://localhost:$HGPORT/ --config hostsecurity.localhost:fingerprints=sha1:914f1aff87249c09b6859b88b1906d30756491ca
260 265 5fed3813f7f5
261 266
262 267 $ hg -R copy-pull id https://localhost:$HGPORT/ --config hostsecurity.localhost:fingerprints=sha256:62:09:97:2f:97:60:e3:65:8f:12:5d:78:9e:35:a1:36:7a:65:4b:0e:9f:ac:db:c3:bc:6e:b6:a3:c0:16:e0:30
263 268 5fed3813f7f5
264 269
265 270 - multiple fingerprints specified and first matches
266 271 $ hg --config 'hostfingerprints.localhost=914f1aff87249c09b6859b88b1906d30756491ca, deadbeefdeadbeefdeadbeefdeadbeefdeadbeef' -R copy-pull id https://localhost:$HGPORT/ --insecure
267 272 5fed3813f7f5
268 273
269 274 $ hg --config 'hostsecurity.localhost:fingerprints=sha1:914f1aff87249c09b6859b88b1906d30756491ca, sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbeef' -R copy-pull id https://localhost:$HGPORT/
270 275 5fed3813f7f5
271 276
272 277 - multiple fingerprints specified and last matches
273 278 $ hg --config 'hostfingerprints.localhost=deadbeefdeadbeefdeadbeefdeadbeefdeadbeef, 914f1aff87249c09b6859b88b1906d30756491ca' -R copy-pull id https://localhost:$HGPORT/ --insecure
274 279 5fed3813f7f5
275 280
276 281 $ hg --config 'hostsecurity.localhost:fingerprints=sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbeef, sha1:914f1aff87249c09b6859b88b1906d30756491ca' -R copy-pull id https://localhost:$HGPORT/
277 282 5fed3813f7f5
278 283
279 284 - multiple fingerprints specified and none match
280 285
281 286 $ hg --config 'hostfingerprints.localhost=deadbeefdeadbeefdeadbeefdeadbeefdeadbeef, aeadbeefdeadbeefdeadbeefdeadbeefdeadbeef' -R copy-pull id https://localhost:$HGPORT/ --insecure
282 287 abort: certificate for localhost has unexpected fingerprint 91:4f:1a:ff:87:24:9c:09:b6:85:9b:88:b1:90:6d:30:75:64:91:ca
283 288 (check hostfingerprint configuration)
284 289 [255]
285 290
286 291 $ hg --config 'hostsecurity.localhost:fingerprints=sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbeef, sha1:aeadbeefdeadbeefdeadbeefdeadbeefdeadbeef' -R copy-pull id https://localhost:$HGPORT/
287 292 abort: certificate for localhost has unexpected fingerprint sha1:91:4f:1a:ff:87:24:9c:09:b6:85:9b:88:b1:90:6d:30:75:64:91:ca
288 293 (check hostsecurity configuration)
289 294 [255]
290 295
291 296 - fails when cert doesn't match hostname (port is ignored)
292 297 $ hg -R copy-pull id https://localhost:$HGPORT1/ --config hostfingerprints.localhost=914f1aff87249c09b6859b88b1906d30756491ca
293 298 abort: certificate for localhost has unexpected fingerprint 28:ff:71:bf:65:31:14:23:ad:62:92:b4:0e:31:99:18:fc:83:e3:9b
294 299 (check hostfingerprint configuration)
295 300 [255]
296 301
297 302
298 303 - ignores that certificate doesn't match hostname
299 304 $ hg -R copy-pull id https://127.0.0.1:$HGPORT/ --config hostfingerprints.127.0.0.1=914f1aff87249c09b6859b88b1906d30756491ca
300 305 5fed3813f7f5
301 306
302 307 HGPORT1 is reused below for tinyproxy tests. Kill that server.
303 308 $ killdaemons.py hg1.pid
304 309
305 310 Prepare for connecting through proxy
306 311
307 312 $ tinyproxy.py $HGPORT1 localhost >proxy.log </dev/null 2>&1 &
308 313 $ while [ ! -f proxy.pid ]; do sleep 0; done
309 314 $ cat proxy.pid >> $DAEMON_PIDS
310 315
311 316 $ echo "[http_proxy]" >> copy-pull/.hg/hgrc
312 317 $ echo "always=True" >> copy-pull/.hg/hgrc
313 318 $ echo "[hostfingerprints]" >> copy-pull/.hg/hgrc
314 319 $ echo "localhost =" >> copy-pull/.hg/hgrc
315 320
316 321 Test unvalidated https through proxy
317 322
318 323 $ http_proxy=http://localhost:$HGPORT1/ hg -R copy-pull pull --insecure --traceback
319 324 pulling from https://localhost:$HGPORT/
320 325 warning: connection security to localhost is disabled per current settings; communication is susceptible to eavesdropping and tampering
321 326 searching for changes
322 327 no changes found
323 328
324 329 Test https with cacert and fingerprint through proxy
325 330
326 331 $ http_proxy=http://localhost:$HGPORT1/ hg -R copy-pull pull \
327 332 > --config web.cacerts="$CERTSDIR/pub.pem"
328 333 pulling from https://localhost:$HGPORT/
329 334 searching for changes
330 335 no changes found
331 336 $ http_proxy=http://localhost:$HGPORT1/ hg -R copy-pull pull https://127.0.0.1:$HGPORT/ --config hostfingerprints.127.0.0.1=914f1aff87249c09b6859b88b1906d30756491ca
332 337 pulling from https://127.0.0.1:$HGPORT/
333 338 searching for changes
334 339 no changes found
335 340
336 341 Test https with cert problems through proxy
337 342
338 343 $ http_proxy=http://localhost:$HGPORT1/ hg -R copy-pull pull \
339 344 > --config web.cacerts="$CERTSDIR/pub-other.pem"
340 345 pulling from https://localhost:$HGPORT/
341 346 abort: error: *certificate verify failed* (glob)
342 347 [255]
343 348 $ http_proxy=http://localhost:$HGPORT1/ hg -R copy-pull pull \
344 349 > --config web.cacerts="$CERTSDIR/pub-expired.pem" https://localhost:$HGPORT2/
345 350 pulling from https://localhost:$HGPORT2/
346 351 abort: error: *certificate verify failed* (glob)
347 352 [255]
348 353
349 354
350 355 $ killdaemons.py hg0.pid
351 356
352 357 #if sslcontext
353 358
354 359 Start patched hgweb that requires client certificates:
355 360
356 361 $ cat << EOT > reqclientcert.py
357 362 > import ssl
358 363 > from mercurial.hgweb import server
359 364 > class _httprequesthandlersslclientcert(server._httprequesthandlerssl):
360 365 > @staticmethod
361 366 > def preparehttpserver(httpserver, ssl_cert):
362 367 > sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
363 368 > sslcontext.verify_mode = ssl.CERT_REQUIRED
364 369 > sslcontext.load_cert_chain(ssl_cert)
365 370 > # verify clients by server certificate
366 371 > sslcontext.load_verify_locations(ssl_cert)
367 372 > httpserver.socket = sslcontext.wrap_socket(httpserver.socket,
368 373 > server_side=True)
369 374 > server._httprequesthandlerssl = _httprequesthandlersslclientcert
370 375 > EOT
371 376 $ cd test
372 377 $ hg serve -p $HGPORT -d --pid-file=../hg0.pid --certificate=$PRIV \
373 378 > --config extensions.reqclientcert=../reqclientcert.py
374 379 $ cat ../hg0.pid >> $DAEMON_PIDS
375 380 $ cd ..
376 381
377 382 without client certificate:
378 383
379 384 $ P="$CERTSDIR" hg id https://localhost:$HGPORT/
380 385 abort: error: *handshake failure* (glob)
381 386 [255]
382 387
383 388 with client certificate:
384 389
385 390 $ cat << EOT >> $HGRCPATH
386 391 > [auth]
387 392 > l.prefix = localhost
388 393 > l.cert = $CERTSDIR/client-cert.pem
389 394 > l.key = $CERTSDIR/client-key.pem
390 395 > EOT
391 396
392 397 $ P="$CERTSDIR" hg id https://localhost:$HGPORT/ \
393 398 > --config auth.l.key="$CERTSDIR/client-key-decrypted.pem"
394 399 5fed3813f7f5
395 400
396 401 $ printf '1234\n' | env P="$CERTSDIR" hg id https://localhost:$HGPORT/ \
397 402 > --config ui.interactive=True --config ui.nontty=True
398 403 passphrase for */client-key.pem: 5fed3813f7f5 (glob)
399 404
400 405 $ env P="$CERTSDIR" hg id https://localhost:$HGPORT/
401 406 abort: error: * (glob)
402 407 [255]
403 408
404 409 #endif
General Comments 0
You need to be logged in to leave comments. Login now