protected void ThrowIfContextInvalid()
 {
     if (!IsValid)
     {
         throw X509Helper.GetInvalidContextException();
     }
 }
 public X509Certificate(byte[] data)
 {
     if (data != null && data.Length != 0)
     {
         impl = X509Helper.Import(data);
     }
 }
Exemple #3
0
 public override byte[] Export(X509ContentType contentType, string password)
 {
     X509Helper.ThrowIfContextInvalid(Impl);
     using (var handle = new SafePasswordHandle(password)) {
         return(Impl.Export(contentType, handle));
     }
 }
Exemple #4
0
 public override int GetHashCode()
 {
     if (!X509Helper.IsValid(impl))
     {
         return(0);
     }
     return(impl.GetHashCode());
 }
Exemple #5
0
        internal X509Certificate(X509CertificateImpl impl)
        {
            if (impl == null)
            {
                throw new ArgumentNullException("impl");
            }

            this.impl = X509Helper.InitFromCertificate(impl);
        }
Exemple #6
0
        public X509Certificate(IntPtr handle)
        {
            if (handle == IntPtr.Zero)
            {
                throw new ArgumentException("Invalid handle.");
            }

            impl = X509Helper.InitFromHandle(handle);
        }
Exemple #7
0
        public X509CertificateMono(X509CertificateMono cert)
        {
            if (cert == null)
            {
                throw new ArgumentNullException("cert");
            }

            impl = X509Helper.InitFromCertificate(cert);
        }
Exemple #8
0
 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
 {
     if (!X509Helper.IsValid(impl))
     {
         throw new NullReferenceException();
     }
     // will throw a NRE if info is null (just like MS implementation)
     info.AddValue("RawData", impl.RawData);
 }
Exemple #9
0
        public X509Certificate(X509Certificate cert)
        {
            if (cert == null)
            {
                throw new ArgumentNullException(nameof(cert));
            }

            impl = X509Helper.InitFromCertificate(cert);
        }
Exemple #10
0
 public X509Certificate(byte[] data)
 {
     if (data != null && data.Length != 0)
     {
         // For compat reasons, this constructor treats passing a null or empty data set as the same as calling the nullary constructor.
         using (var safePasswordHandle = new SafePasswordHandle((string)null))
             impl = X509Helper.Import(data, safePasswordHandle, X509KeyStorageFlags.DefaultKeySet);
     }
 }
Exemple #11
0
        public virtual string ToString(bool fVerbose)
        {
            if (!fVerbose || !X509Helper.IsValid(impl))
            {
                return(base.ToString());
            }

            return(impl.ToString(true));
        }
Exemple #12
0
        public X509Certificate(System.Security.Cryptography.X509Certificates.X509Certificate cert)
        {
            if (cert == null)
            {
                throw new ArgumentNullException("cert");
            }

            impl      = X509Helper.InitFromCertificate(cert);
            hideDates = false;
        }
Exemple #13
0
        // strangly there are no DateTime returning function
        public virtual string GetExpirationDateString()
        {
            if (hideDates)
            {
                return(null);
            }
            X509Helper.ThrowIfContextInvalid(impl);

            return(impl.GetValidUntil().ToLocalTime().ToString());
        }
Exemple #14
0
        // strangly there are no DateTime returning function
        public virtual string GetEffectiveDateString()
        {
            if (hideDates)
            {
                return(null);
            }
            X509Helper.ThrowIfContextInvalid(impl);

            return(impl.GetValidFrom().ToLocalTime().ToString());
        }
        public override string ToString(bool verbose)
        {
            if (_cert == null)
            {
                return("System.Security.Cryptography.X509Certificates.X509Certificate2");
            }

            string        nl = Environment.NewLine;
            StringBuilder sb = new StringBuilder();

            // the non-verbose X509Certificate2 == verbose X509Certificate
            if (!verbose)
            {
                sb.AppendFormat("[Subject]{0}  {1}{0}{0}", nl, GetSubjectName(false));
                sb.AppendFormat("[Issuer]{0}  {1}{0}{0}", nl, GetIssuerName(false));
                sb.AppendFormat("[Not Before]{0}  {1}{0}{0}", nl, GetValidFrom().ToLocalTime());
                sb.AppendFormat("[Not After]{0}  {1}{0}{0}", nl, GetValidUntil().ToLocalTime());
                sb.AppendFormat("[Thumbprint]{0}  {1}{0}", nl, X509Helper.ToHexString(GetCertHash()));
                sb.Append(nl);
                return(sb.ToString());
            }

            sb.AppendFormat("[Version]{0}  V{1}{0}{0}", nl, Version);
            sb.AppendFormat("[Subject]{0}  {1}{0}{0}", nl, GetSubjectName(false));
            sb.AppendFormat("[Issuer]{0}  {1}{0}{0}", nl, GetIssuerName(false));
            sb.AppendFormat("[Serial Number]{0}  {1}{0}{0}", nl, GetSerialNumber());
            sb.AppendFormat("[Not Before]{0}  {1}{0}{0}", nl, GetValidFrom().ToLocalTime());
            sb.AppendFormat("[Not After]{0}  {1}{0}{0}", nl, GetValidUntil().ToLocalTime());
            sb.AppendFormat("[Thumbprint]{0}  {1}{0}", nl, X509Helper.ToHexString(GetCertHash()));
            sb.AppendFormat("[Signature Algorithm]{0}  {1}({2}){0}{0}", nl, SignatureAlgorithm.FriendlyName,
                            SignatureAlgorithm.Value);

            AsymmetricAlgorithm key = PublicKey.Key;

            sb.AppendFormat("[Public Key]{0}  Algorithm: ", nl);
            if (key is RSA)
            {
                sb.Append("RSA");
            }
            else if (key is DSA)
            {
                sb.Append("DSA");
            }
            else
            {
                sb.Append(key.ToString());
            }
            sb.AppendFormat("{0}  Length: {1}{0}  Key Blob: ", nl, key.KeySize);
            AppendBuffer(sb, PublicKey.EncodedKeyValue.RawData);
            sb.AppendFormat("{0}  Parameters: ", nl);
            AppendBuffer(sb, PublicKey.EncodedParameters.RawData);
            sb.Append(nl);

            return(sb.ToString());
        }
Exemple #16
0
        internal static void ExportAsPEM(X509Certificate certificate, Stream stream, bool includeHumanReadableForm)
        {
#if MONO_FEATURE_BTLS
            X509Helper.ThrowIfContextInvalid(certificate.Impl);
            using (var x509 = GetNativeInstance(certificate.Impl))
                using (var bio = MonoBtlsBio.CreateMonoStream(stream))
                    x509.ExportAsPEM(bio, includeHumanReadableForm);
#else
            throw new PlatformNotSupportedException();
#endif
        }
Exemple #17
0
        internal static long GetSubjectNameHash(X509Certificate certificate)
        {
#if MONO_FEATURE_BTLS
            X509Helper.ThrowIfContextInvalid(certificate.Impl);
            using (var x509 = GetNativeInstance(certificate.Impl))
                using (var subject = x509.GetSubjectName())
                    return(subject.GetHash());
#else
            throw new PlatformNotSupportedException();
#endif
        }
Exemple #18
0
        public virtual byte[] GetKeyAlgorithmParameters()
        {
            X509Helper.ThrowIfContextInvalid(impl);

            byte[] kap = impl.GetKeyAlgorithmParameters();
            if (kap == null)
            {
                throw new CryptographicException(Locale.GetText("Parameters not part of the certificate"));
            }

            return(kap);
        }
Exemple #19
0
 public X509Certificate2(byte[] rawData)
 {
     // MONO: temporary hack until `X509CertificateImplApple` derives from
     //       `X509Certificate2Impl`.
     if (rawData != null && rawData.Length != 0)
     {
         using (var safePasswordHandle = new SafePasswordHandle((string)null)) {
             var impl = X509Helper.Import(rawData, safePasswordHandle, X509KeyStorageFlags.DefaultKeySet);
             ImportHandle(impl);
         }
     }
 }
Exemple #20
0
        public X509Certificate(byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
        {
            if (rawData == null || rawData.Length == 0)
            {
                throw new ArgumentException(SR.Arg_EmptyOrNullArray, nameof(rawData));
            }

            ValidateKeyStorageFlags(keyStorageFlags);

            using (var safePasswordHandle = new SafePasswordHandle(password))
                impl = X509Helper.Import(rawData, safePasswordHandle, keyStorageFlags);
        }
Exemple #21
0
 internal byte[] Export(X509ContentType contentType, byte[] password)
 {
     try {
         X509Helper.ThrowIfContextInvalid(impl);
         return(impl.Export(contentType, password));
     } finally {
         // protect password
         if (password != null)
         {
             Array.Clear(password, 0, password.Length);
         }
     }
 }
Exemple #22
0
        public virtual string ToString(bool fVerbose)
        {
            if (!fVerbose || !X509Helper.IsValid(impl))
            {
                return(base.ToString());
            }

            StringBuilder sb = new StringBuilder();

            // Subject
            sb.AppendLine("[Subject]");
            sb.Append("  ");
            sb.AppendLine(Subject);

            // Issuer
            sb.AppendLine();
            sb.AppendLine("[Issuer]");
            sb.Append("  ");
            sb.AppendLine(Issuer);

            // Serial Number
            sb.AppendLine();
            sb.AppendLine("[Serial Number]");
            sb.Append("  ");
            byte[] serialNumber = GetSerialNumber();
            Array.Reverse(serialNumber);
            sb.Append(serialNumber.ToHexArrayUpper());
            sb.AppendLine();

            // NotBefore
            sb.AppendLine();
            sb.AppendLine("[Not Before]");
            sb.Append("  ");
            sb.AppendLine(FormatDate(GetNotBefore()));

            // NotAfter
            sb.AppendLine();
            sb.AppendLine("[Not After]");
            sb.Append("  ");
            sb.AppendLine(FormatDate(GetNotAfter()));

            // Thumbprint
            sb.AppendLine();
            sb.AppendLine("[Thumbprint]");
            sb.Append("  ");
            sb.Append(GetRawCertHash().ToHexArrayUpper());
            sb.AppendLine();

            return(sb.ToString());
        }
Exemple #23
0
        public X509Certificate(string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags) : this()
        {
            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            ValidateKeyStorageFlags(keyStorageFlags);

            var rawData = File.ReadAllBytes(fileName);

            using (var safePasswordHandle = new SafePasswordHandle(password))
                impl = X509Helper.Import(rawData, safePasswordHandle, keyStorageFlags);
        }
Exemple #24
0
        static MonoBtlsX509 GetNativeInstance(X509CertificateImpl impl)
        {
            X509Helper.ThrowIfContextInvalid(impl);
            var btlsImpl = impl as X509CertificateImplBtls;

            if (btlsImpl != null)
            {
                return(btlsImpl.X509.Copy());
            }
            else
            {
                return(MonoBtlsX509.LoadFromData(impl.GetRawCertData(), MonoBtlsX509Format.DER));
            }
        }
Exemple #25
0
        public override string ToString(bool full)
        {
            ThrowIfContextInvalid();

            string        nl = Environment.NewLine;
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("[Subject]{0}  {1}{0}{0}", nl, GetSubjectName(false));
            sb.AppendFormat("[Issuer]{0}  {1}{0}{0}", nl, GetIssuerName(false));
            sb.AppendFormat("[Not Before]{0}  {1}{0}{0}", nl, GetEffectiveDateString());
            sb.AppendFormat("[Not After]{0}  {1}{0}{0}", nl, GetExpirationDateString());
            sb.AppendFormat("[Thumbprint]{0}  {1}{0}", nl, X509Helper.ToHexString(GetCertHash()));
            sb.Append(nl);
            return(sb.ToString());
        }
Exemple #26
0
        // public methods

        public virtual bool Equals(System.Security.Cryptography.X509Certificates.X509Certificate other)
        {
            if (other == null)
            {
                return(false);
            }
            else
            {
                if (!X509Helper.IsValid(other.impl))
                {
                    if (!X509Helper.IsValid(impl))
                    {
                        return(true);
                    }
                    throw new CryptographicException(Locale.GetText("Certificate instance is empty."));
                }

                return(X509CertificateImpl.Equals(impl, other.impl));
            }
        }
Exemple #27
0
        public static X509CertificateImpl Import(byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
        {
            MX.X509Certificate x509;
            IntPtr             handle;

            if (password == null)
            {
                handle = CFHelpers.CreateCertificateFromData(rawData);
                if (handle != IntPtr.Zero)
                {
                    return(new X509CertificateImplApple(handle, true));
                }

                try {
                    x509 = new MX.X509Certificate(rawData);
                } catch (Exception e) {
                    try {
                        x509 = X509Helper.ImportPkcs12(rawData, null);
                    } catch {
                        string msg = Locale.GetText("Unable to decode certificate.");
                        // inner exception is the original (not second) exception
                        throw new CryptographicException(msg, e);
                    }
                }
            }
            else
            {
                // try PKCS#12
                try {
                    x509 = X509Helper.ImportPkcs12(rawData, password);
                }
                catch {
                    // it's possible to supply a (unrequired/unusued) password
                    // fix bug #79028
                    x509 = new MX.X509Certificate(rawData);
                }
            }

            return(new X509CertificateImplMono(x509));
        }
Exemple #28
0
        public override string ToString(bool full)
        {
            ThrowIfContextInvalid();

            if (!full || fallback == null)
            {
                var summary = GetSubjectSummary();
                return(string.Format("[X509Certificate: {0}]", summary));
            }

            string        nl = Environment.NewLine;
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("[Subject]{0}  {1}{0}{0}", nl, GetSubjectName(false));

            sb.AppendFormat("[Issuer]{0}  {1}{0}{0}", nl, GetIssuerName(false));
            sb.AppendFormat("[Not Before]{0}  {1}{0}{0}", nl, GetValidFrom().ToLocalTime());
            sb.AppendFormat("[Not After]{0}  {1}{0}{0}", nl, GetValidUntil().ToLocalTime());
            sb.AppendFormat("[Thumbprint]{0}  {1}{0}", nl, X509Helper.ToHexString(GetCertHash()));

            sb.Append(nl);
            return(sb.ToString());
        }
Exemple #29
0
 internal void ThrowIfInvalid()
 {
     X509Helper.ThrowIfContextInvalid(impl);
 }
Exemple #30
0
 public virtual void Import(string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
 {
     byte[] rawData = File.ReadAllBytes(fileName);
     using (var safePasswordHandle = new SafePasswordHandle(password))
         impl = X509Helper.Import(rawData, safePasswordHandle, keyStorageFlags);
 }