[btls] Create a native BTLS key handle for X509CertificateImplBtls.PrivateKey
authorAlexander Köplinger <alex.koeplinger@outlook.com>
Tue, 10 Jan 2017 20:40:58 +0000 (21:40 +0100)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Tue, 10 Jan 2017 21:33:49 +0000 (22:33 +0100)
commita9d75b4b6649eb2a182635c37cdd5e20d55fa9d0
tree631589dced488dff64bfb748ab62834c9a33d4a9
parentaf85a0932137be489d19ac70851e4a0e68629c37
[btls] Create a native BTLS key handle for X509CertificateImplBtls.PrivateKey

When setting X509CertificateImplBtls.PrivateKey we'd previously just
update the fallback instance. This causes problems if other code
internal to System.dll like MonoBtlsContext.SetPrivateCertificate()
tries to fetch the NativePrivateKey since it'd be null at that point.

This scenario happens e.g. with HttpListener, it supports a special
directory in ~/.config/.mono/httplistener which contains <port>.cer
and <port>.pvk files (public and private key) that will be used
as the server certificate. In that code we're reading the private
key from the file and assigning it to X509CertificateImplBtls.PrivateKey
without ever having a native BTLS handle.

The fix is to create such a native BTLS handle from the managed
private key when needed.

Note that we can't simply always do this e.g. in the PrivateKey
setter because the setter will be used with instances that
don't actually contain a private key. We can't throw in those
cases so instead we do it only when NativePrivateKey is accessed.

To make the code clearer and easier to read, `privateKey` was
renamed to `nativePrivateKey` in X509CertificateImplBtls.

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=46602
mcs/class/System/Mono.Btls/MonoBtlsKey.cs
mcs/class/System/Mono.Btls/X509CertificateImplBtls.cs
mono/btls/btls-key.c
mono/btls/btls-key.h