From d268e59fdfb58e8d36f0d6ad21b64cb33930d11d Mon Sep 17 00:00:00 2001 From: Atsushi Eno Date: Mon, 11 Sep 2006 15:03:53 +0000 Subject: 2006-09-11 Atsushi Enomoto * X509Certificate2.cs : implemented HasPrivateKey. Return null when the corresponding RSA or DSA has no private key. * X509Certificate2Test.cs : added test for PrivateKey and HasPrivateKey for non-private-inclusive certificate. svn path=/trunk/mcs/; revision=65233 --- .../ChangeLog | 5 ++++ .../X509Certificate2.cs | 35 +++++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) (limited to 'mcs/class/System/System.Security.Cryptography.X509Certificates') diff --git a/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog b/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog index 02eb7e69499..6e9c3f3aa61 100644 --- a/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog +++ b/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog @@ -1,3 +1,8 @@ +2006-09-11 Atsushi Enomoto + + * X509Certificate2.cs : implemented HasPrivateKey. Return null + when the corresponding RSA or DSA has no private key. + 2006-09-05 Sebastien Pouliot * X509Certificate2.cs: Call import in ctor to be sure the private key diff --git a/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs b/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs index 30b349e482d..0b55688e11a 100644 --- a/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs +++ b/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs @@ -31,7 +31,7 @@ using System.IO; using System.Text; - +using Mono.Security.Cryptography; using MX = Mono.Security.X509; namespace System.Security.Cryptography.X509Certificates { @@ -136,9 +136,9 @@ namespace System.Security.Cryptography.X509Certificates { set { _name = value; } } - [MonoTODO] + [MonoTODO ("Probably it could be more efficient")] public bool HasPrivateKey { - get { return false; } + get { return PrivateKey != null; } } [MonoTODO] @@ -160,10 +160,31 @@ namespace System.Security.Cryptography.X509Certificates { public AsymmetricAlgorithm PrivateKey { get { - if (_cert.RSA != null) - return _cert.RSA; - else if (_cert.DSA != null) - return _cert.DSA; + if (_cert.RSA != null) { + RSACryptoServiceProvider rcsp = _cert.RSA as RSACryptoServiceProvider; + if (rcsp != null) + return rcsp.PublicOnly ? null : rcsp; + RSAManaged rsam = _cert.RSA as RSAManaged; + if (rsam != null) + return rsam.PublicOnly ? null : rsam; + try { + _cert.RSA.ExportParameters (true); + return _cert.RSA; + } catch (CryptographicException) { + return null; + } + } + else if (_cert.DSA != null) { + DSACryptoServiceProvider dcsp = _cert.DSA as DSACryptoServiceProvider; + if (dcsp != null) + return dcsp.PublicOnly ? null : dcsp; + try { + _cert.DSA.ExportParameters (true); + return _cert.DSA; + } catch (CryptographicException) { + return null; + } + } return null; } set { -- cgit v1.2.3