This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python accepts SSL certificate that should be rejected on OSX
Type: security Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: ned.deily Nosy List: christian.heimes, dstufft, jpakkane, ned.deily, pitrou, ronaldoussoren
Priority: normal Keywords:

Created on 2015-07-16 19:04 by jpakkane, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
sslbug.py jpakkane, 2015-07-16 19:04
ca-dump.m ronaldoussoren, 2015-07-22 12:20
Messages (16)
msg246813 - (view) Author: Jussi Pakkanen (jpakkane) Date: 2015-07-16 19:04
Create a dummy certificate and build an ssl context like this:

ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
ctx.verify_mode = ssl.CERT_REQUIRED
ctx.load_verify_locations(cadata=dummy_certificate)

Then try to connect to a public service like this:

u = urllib.request.urlopen('https://www.google.com', context=ctx)
data = u.read()

Python will validate the server certificate even though it should reject it. Attached is a script to demonstrate this.

This happens with Python 3.4.3 on OSX 10.10.4. Running the same script in Ubuntu raises a certificate rejection exception as expected.
msg246814 - (view) Author: Donald Stufft (dstufft) * (Python committer) Date: 2015-07-16 19:11
I think the only way to actually fix this, is to stop using the OpenSSL provided by OSX.
msg246859 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-07-17 15:55
Do we know exactly why OS X's OpenSSL accepts it?
msg246860 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2015-07-17 16:01
The fork of OpenSSL that Apple ships also looks at the CA list in the Keychain. IIRC that cannot be disabled. 

BTW. Annoyingly this fork uses a private API to access the keychain, which means we couldn't optionally use this behavior when not using Apple's binaries. 

--
On the road, hence brief. 

Op 17 jul. 2015 om 17:55 heeft Antoine Pitrou <report@bugs.python.org> het volgende geschreven:

> 
> Antoine Pitrou added the comment:
> 
> Do we know exactly why OS X's OpenSSL accepts it?
> 
> ----------
> nosy: +ned.deily, pitrou, ronaldoussoren
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue24646>
> _______________________________________
msg246881 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2015-07-18 04:00
And the tradeoff for supplying private copies of newer OpenSSL libs with the Pythons installed by python.org OS X installers is that we would then need to solve the CA management problem for all users of those Pythons.  So far there hasn't been a good solution to that problem so we have elected to continue to use the least unattractive solution of continuing to use the Apple-supplied libs with the 10.6+ installer variants (Issue17128).  Eventually, we will have to bite the bullet and come up with s better solution as Apple will likely eventually stop shipping OpenSSL libs altogether.
msg246882 - (view) Author: Donald Stufft (dstufft) * (Python committer) Date: 2015-07-18 04:02
For what it's worth, the El Capitan Beta's apparently don't ship with OpenSSL headers anymore though they do still ship with the dylibs.
msg246883 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2015-07-18 04:22
> For what it's worth, the El Capitan Beta's apparently don't ship with
> OpenSSL headers anymore though they do still ship with the dylibs.

Hmm, I had tested installing existing python.org binary releases with the first DPs of 10.11 and I *thought* I had tested building from source, as well.  But, yes, it appears that the headers are no longer there, at least on the most recent DP I have installed.  I'm traveling and essentially "off-the-net" for another week but I will take a closer look at the situation then.
msg246980 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2015-07-20 12:19
Using our own OpenSSL build should be saver in the long run anyway.  Apple provides enough API’s to reproduce the behaviour of Apple’s build in a cleaner way (by making the loading of system CA certs an explicit action). Problem is: that likely requires using API’s higher up in the API stack, which could cause problems when using os.fork without os.exec (the old “CoreFoundation crashes in child processes” problem).

Ronald

> On 18 Jul 2015, at 06:22, Ned Deily <report@bugs.python.org> wrote:
> 
> 
> Ned Deily added the comment:
> 
>> For what it's worth, the El Capitan Beta's apparently don't ship with
>> OpenSSL headers anymore though they do still ship with the dylibs.
> 
> Hmm, I had tested installing existing python.org binary releases with the first DPs of 10.11 and I *thought* I had tested building from source, as well.  But, yes, it appears that the headers are no longer there, at least on the most recent DP I have installed.  I'm traveling and essentially "off-the-net" for another week but I will take a closer look at the situation then.
> 
> ----------
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue24646>
> _______________________________________
msg246981 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2015-07-20 12:20
It's a platform bug but Apple doesn't consider it a bug. Hynek has analyzed and reported it over a year ago: https://hynek.me/articles/apple-openssl-verification-surprises/
msg246982 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2015-07-20 12:31
Ronald: Can you check if SecTrustSettingsCopyCertificates() or SecTrustCopyAnchorCertificates() are affected by the fork() issue?
msg246984 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2015-07-20 12:53
I'll check, but they probably are because the use data structures from CoreFoundation.
msg246986 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2015-07-20 13:00
BTW. I think someone (me?) should write down the problems with using higher levels in the API stack w.r.t. os.fork in a PEP-style document. This can then be used to decide whether or not we want to use such APIs in the stdlib (and if so, what should be changed to avoid crashes).

I'm slighlty in favour of using such APIs if that makes Python better on OSX, even if that introduces slight differences w.r.t. Linux (for example, multiprocessing could no longer use only os.fork).  The disadvantage is that it would no longer be possible to develop and test pre-forking code on OSX before deploying to Linux.
msg247113 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2015-07-22 12:20
The attached program (which is pure C except for a call to NSLog) calls SecTrustCopyAnchorCertificates in a child process (and with a minor change the other function as well).

This doesn't crash for me. 

However, that doesn't really mean anything: We know from earlier bugreports that calling _scproxy._get_proxy_settings in a child process can crash, and that is something that only sporadicly happens for me (I cannot reproduce it with a trivial script).

Maybe this is something that was fixed in 10.10, but I'd be surprised at that (and that doesn't really help us as we support older OSX releases as well). I cannot test on other OSX releases at the moment, I'm at europython and don't have access to my test systems.
msg247311 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2015-07-25 03:42
Ronald, FWIW, your test program seems to work without crashing on both 10.6 and 10.8; not surprisingly, it failed to compile on 10.5 (no 'errSecSuccess').
msg275205 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2016-09-08 23:03
I think this bug is no longer relevant with Python 3.5 and newer. We no longer use system OpenSSL in our Python builds.

Ned, is that right?
msg275343 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2016-09-09 16:51
We will no longer use the OS X system OpenSSL for 3.6.
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68834
2016-09-09 16:51:06ned.deilysetstatus: pending -> closed
resolution: not a bug -> third party
messages: + msg275343

stage: resolved
2016-09-08 23:03:45christian.heimessetstatus: open -> pending
assignee: ned.deily
resolution: not a bug
messages: + msg275205
2015-07-25 03:42:40ned.deilysetmessages: + msg247311
2015-07-22 12:20:43ronaldoussorensetfiles: + ca-dump.m

messages: + msg247113
2015-07-20 13:00:28ronaldoussorensetmessages: + msg246986
2015-07-20 12:53:06ronaldoussorensetmessages: + msg246984
2015-07-20 12:31:17christian.heimessetmessages: + msg246982
2015-07-20 12:20:45christian.heimessetnosy: + christian.heimes
messages: + msg246981
2015-07-20 12:19:07ronaldoussorensetmessages: + msg246980
2015-07-18 04:22:10ned.deilysetmessages: + msg246883
2015-07-18 04:02:06dstufftsetmessages: + msg246882
2015-07-18 04:00:54ned.deilysetmessages: + msg246881
2015-07-17 16:01:20ronaldoussorensetmessages: + msg246860
2015-07-17 15:55:41pitrousetnosy: + ned.deily, ronaldoussoren, pitrou
messages: + msg246859
2015-07-16 19:11:02dstufftsetnosy: + dstufft
messages: + msg246814
2015-07-16 19:04:33jpakkanecreate