2 // X509CertificateImplMono.cs: X.509 implementation using Mono.Security.X509.
5 // Sebastien Pouliot <sebastien@ximian.com>
6 // Martin Baulig <martin.baulig@xamarin.com>
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2015 Xamarin, Inc. (http://www.xamarin.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using MX = Mono.Security.X509;
35 namespace System.Security.Cryptography.X509Certificates
37 class X509CertificateImplMono : X509CertificateImpl
39 MX.X509Certificate x509;
41 public X509CertificateImplMono (MX.X509Certificate x509)
46 public override bool IsValid {
47 get { return x509 != null; }
50 public override IntPtr Handle {
51 get { return IntPtr.Zero; }
54 public override X509CertificateImpl Clone ()
56 ThrowIfContextInvalid ();
57 return new X509CertificateImplMono (x509);
60 public override string GetIssuerName (bool legacyV1Mode)
62 ThrowIfContextInvalid ();
64 return x509.IssuerName;
66 return MX.X501.ToString (x509.GetIssuerName (), true, ", ", true);
69 public override string GetSubjectSummary ()
71 ThrowIfContextInvalid ();
72 return x509.SubjectName;
75 public override string GetSubjectName (bool legacyV1Mode)
77 ThrowIfContextInvalid ();
79 return x509.SubjectName;
81 return MX.X501.ToString (x509.GetSubjectName (), true, ", ", true);
84 public override byte[] GetRawCertData ()
86 ThrowIfContextInvalid ();
90 protected override byte[] GetCertHash (bool lazy)
92 ThrowIfContextInvalid ();
93 SHA1 sha = SHA1.Create ();
94 return sha.ComputeHash (x509.RawData);
97 public override DateTime GetEffectiveDateString ()
99 ThrowIfContextInvalid ();
100 return x509.ValidFrom.ToLocalTime ();
103 public override DateTime GetExpirationDateString ()
105 ThrowIfContextInvalid ();
106 return x509.ValidUntil.ToLocalTime ();
109 public override bool Equals (X509CertificateImpl other, out bool result)
111 // Use default implementation
116 public override string GetKeyAlgorithm ()
118 ThrowIfContextInvalid ();
119 return x509.KeyAlgorithm;
122 public override byte[] GetKeyAlgorithmParameters ()
124 ThrowIfContextInvalid ();
125 return x509.KeyAlgorithmParameters;
128 public override byte[] GetPublicKey ()
130 ThrowIfContextInvalid ();
131 return x509.PublicKey;
134 public override byte[] GetSerialNumber ()
136 ThrowIfContextInvalid ();
137 return x509.SerialNumber;
140 public override byte[] Export (X509ContentType contentType, byte[] password)
142 ThrowIfContextInvalid ();
144 switch (contentType) {
145 case X509ContentType.Cert:
146 return GetRawCertData ();
147 case X509ContentType.Pfx: // this includes Pkcs12
149 throw new NotSupportedException ();
150 case X509ContentType.SerializedCert:
152 throw new NotSupportedException ();
154 string msg = Locale.GetText ("This certificate format '{0}' cannot be exported.", contentType);
155 throw new CryptographicException (msg);
159 public override string ToString (bool full)
161 ThrowIfContextInvalid ();
163 string nl = Environment.NewLine;
164 StringBuilder sb = new StringBuilder ();
165 sb.AppendFormat ("[Subject]{0} {1}{0}{0}", nl, GetSubjectName (false));
166 sb.AppendFormat ("[Issuer]{0} {1}{0}{0}", nl, GetIssuerName (false));
167 sb.AppendFormat ("[Not Before]{0} {1}{0}{0}", nl, GetEffectiveDateString ());
168 sb.AppendFormat ("[Not After]{0} {1}{0}{0}", nl, GetExpirationDateString ());
169 sb.AppendFormat ("[Thumbprint]{0} {1}{0}", nl, X509Helper.ToHexString (GetCertHash ()));
171 return sb.ToString ();
174 protected override void Dispose (bool disposing)