/*
 * CertificateOps.cs --
 *
 * Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: $
 */

using System;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using Eagle._Attributes;
using Eagle._Components.Public;
using Eagle._Containers.Public;

namespace Eagle._Components.Private
{
    [ObjectId("acaf57c4-509f-4953-b43b-2222a40d6d33")]
    internal static class CertificateOps
    {
        #region Private Data
        internal static X509RevocationMode DefaultRevocationMode =
            X509RevocationMode.Online;

        internal static X509RevocationFlag DefaultRevocationFlag =
            X509RevocationFlag.ExcludeRoot;

        internal static X509VerificationFlags DefaultVerificationFlags =
            X509VerificationFlags.NoFlag;
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Public Certificate Methods
        public static ReturnCode GetCertificate(
            string fileName,
            ref X509Certificate certificate,
            ref Result error
            )
        {
            if (!String.IsNullOrEmpty(fileName))
            {
                try
                {
                    certificate = X509Certificate.CreateFromSignedFile(fileName);

                    return ReturnCode.Ok;
                }
                catch (Exception e)
                {
                    error = e;
                }
            }
            else
            {
                error = "invalid file name";
            }

            TraceOps.DebugTrace(String.Format(
                "{0}: GetCertificate: file {1} query failure, error = {2}{3}",
                GlobalState.GetCurrentThreadId(), FormatOps.WrapOrNull(fileName),
                FormatOps.WrapOrNull(true, true, error), Environment.NewLine),
                typeof(CertificateOps).Name);

            return ReturnCode.Error;
        }

        ///////////////////////////////////////////////////////////////////////

        public static ReturnCode GetCertificate2(
            string fileName,
            ref X509Certificate2 certificate
            )
        {
            Result error = null;

            return GetCertificate2(fileName, ref certificate, ref error);
        }

        ///////////////////////////////////////////////////////////////////////

        public static ReturnCode GetCertificate2(
            string fileName,
            ref X509Certificate2 certificate,
            ref Result error
            )
        {
            X509Certificate localCertificate = null;

            if (GetCertificate(fileName, ref localCertificate,
                    ref error) == ReturnCode.Ok)
            {
                if (localCertificate != null)
                {
                    try
                    {
                        certificate = new X509Certificate2(localCertificate);

                        return ReturnCode.Ok;
                    }
                    catch (Exception e)
                    {
                        error = e;
                    }
                }
                else
                {
                    error = "invalid certificate";
                }
            }

            TraceOps.DebugTrace(String.Format(
                "{0}: GetCertificate2: file {1} query failure, error = {2}{3}",
                GlobalState.GetCurrentThreadId(), FormatOps.WrapOrNull(fileName),
                FormatOps.WrapOrNull(true, true, error), Environment.NewLine),
                typeof(CertificateOps).Name);

            return ReturnCode.Error;
        }

        ///////////////////////////////////////////////////////////////////////

        public static ReturnCode VerifyChain(
            Assembly assembly,
            X509Certificate2 certificate,
            X509VerificationFlags verificationFlags,
            X509RevocationMode revocationMode,
            X509RevocationFlag revocationFlag,
            ref Result error
            )
        {
            if (certificate != null)
            {
                try
                {
                    X509Chain chain = X509Chain.Create();

                    if (chain != null)
                    {
                        X509ChainPolicy chainPolicy = chain.ChainPolicy;

                        if (chainPolicy != null)
                        {
                            //
                            // NOTE: Setup the chain policy settings as specified
                            //       by the caller.
                            //
                            chainPolicy.VerificationFlags = verificationFlags;
                            chainPolicy.RevocationMode = revocationMode;
                            chainPolicy.RevocationFlag = revocationFlag;

                            if (chain.Build(certificate))
                            {
                                return ReturnCode.Ok;
                            }
                            else
                            {
                                StringList list = new StringList();

                                if (chain.ChainStatus != null)
                                {
                                    foreach (X509ChainStatus status in chain.ChainStatus)
                                    {
                                        list.Add(
                                            status.Status.ToString(),
                                            status.StatusInformation);
                                    }

                                    if (assembly != null)
                                        error = String.Format(
                                            "assembly {0}: {1}",
                                            FormatOps.WrapOrNull(assembly),
                                            list.ToString());
                                    else
                                        error = list;
                                }
                                else
                                {
                                    if (assembly != null)
                                        error = String.Format(
                                            "assembly {0}: invalid chain status",
                                            FormatOps.WrapOrNull(assembly));
                                    else
                                        error = "invalid chain status";
                                }
                            }
                        }
                        else
                        {
                            if (assembly != null)
                                error = String.Format(
                                    "assembly {0}: invalid chain policy",
                                    FormatOps.WrapOrNull(assembly));
                            else
                                error = "invalid chain policy";
                        }
                    }
                    else
                    {
                        if (assembly != null)
                            error = String.Format(
                                "assembly {0}: invalid chain",
                                FormatOps.WrapOrNull(assembly));
                        else
                            error = "invalid chain";
                    }
                }
                catch (Exception e)
                {
                    if (assembly != null)
                        error = String.Format(
                            "assembly {0}: {1}",
                            FormatOps.WrapOrNull(assembly), e);
                    else
                        error = e;
                }
            }
            else
            {
                if (assembly != null)
                    error = String.Format(
                        "assembly {0}: invalid certificate",
                        FormatOps.WrapOrNull(assembly));
                else
                    error = "invalid certificate";
            }

            return ReturnCode.Error;
        }
        #endregion
    }
}
