Example #1
0
        //
        // Summary:
        // Return handle to the CardSpace implementation dll.
        // We will first check to see if a v2 (or above) redirection dll has been installed.
        // If not we will check to see if the v1 infocardapi.dll is installed.
        // If that's not found as well, an exception is thrown
        //
        private string GetCardSpaceImplementationDll()
        {
            string implDllFullPath = GetV2ImplementationDllPath();

            if (!File.Exists(implDllFullPath))
            {
                //
                // Choose infocardapi.dll, if v2+ dll does not exist
                //
                implDllFullPath = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.System),
                    REDIRECT_DLL_CARDSPACE_V1 + ".dll");

                if (!File.Exists(implDllFullPath))
                {
                    //
                    // If this does not exist either, then even CardSpace v1 is NOT installed
                    // on this machine. Note: Throwing an exception using IDT.ThrowHelperError
                    // does not log to event log unless it derives from InfoCardBaseException.
                    // This seems fine given that we don't want to be logging as "CardSpace X.0.0.0",
                    // rather we'll let the client application log to event log if desired.
                    //
                    throw IDT.ThrowHelperError(
                              new CardSpaceException(SR.GetString(SR.ClientAPIServiceNotInstalledError)));
                }
            }

            return(implDllFullPath);
        }
Example #2
0
        //
        // Summary
        //  Convert the XML data to a string
        //
        // Parameter
        //  xml - The xml data to be converted into a string
        //
        // Returns
        //   A string format of the XML
        //
        internal static string XmlToString(IEnumerable <XmlElement> xml)
        {
            StringBuilder builder = new StringBuilder();

            foreach (XmlElement element in xml)
            {
                if (null == element)
                {
                    throw IDT.ThrowHelperError(new ArgumentException(SR.GetString(SR.ClientAPIInvalidPolicy)));
                }
                builder.Append(element.OuterXml);
            }

            return(builder.ToString());
        }
Example #3
0
        //
        // Summary
        //  Start the management user interface
        //
        public static void Manage()
        {
            Int32 result = CardSpaceSelector.GetShim().m_csShimManageCardSpace();

            //
            // Convert HRESULTS to errors
            //
            if (0 != result)
            {
                //
                // Convert the HRESULTS to exceptions
                //
                ExceptionHelper.ThrowIfCardSpaceException((int)result);
                throw IDT.ThrowHelperError(new CardSpaceException(SR.GetString(SR.ClientAPIInfocardError)));
            }
        }
        public static void ThrowIfCardSpaceException(int status)
        {
            switch (status)
            {
            case (int)EventCode.E_ICARD_COMMUNICATION:
                throw IDT.ThrowHelperError(new CardSpaceException(SR.GetString(SR.ClientAPIInfocardError)));

            case (int)EventCode.E_ICARD_USERCANCELLED:
                throw IDT.ThrowHelperError(new UserCancellationException(SR.GetString(SR.ClientAPIUserCancellationError)));

            case (int)EventCode.E_ICARD_SERVICE:
                throw IDT.ThrowHelperError(new ServiceNotStartedException(SR.GetString(SR.ClientAPIServiceNotStartedError)));

            case (int)EventCode.E_ICARD_UNTRUSTED:
                throw IDT.ThrowHelperError(new UntrustedRecipientException(SR.GetString(SR.ClientAPIUntrustedRecipientError)));

            case (int)EventCode.E_ICARD_TRUSTEXCHANGE:
                throw IDT.ThrowHelperError(new StsCommunicationException(SR.GetString(SR.ClientStsCommunicationException)));

            case (int)EventCode.E_ICARD_IDENTITY:
                throw IDT.ThrowHelperError(new IdentityValidationException(SR.GetString(SR.ClientAPIInvalidIdentity)));

            case (int)EventCode.E_ICARD_SERVICEBUSY:
                throw IDT.ThrowHelperError(new ServiceBusyException(SR.GetString(SR.ClientAPIServiceBusy)));

            case (int)EventCode.E_ICARD_POLICY:
                throw IDT.ThrowHelperError(new PolicyValidationException(SR.GetString(SR.ClientAPIInvalidPolicy)));

            case (int)EventCode.E_ICARD_UNSUPPORTED:
                throw IDT.ThrowHelperError(new UnsupportedPolicyOptionsException(SR.GetString(SR.ClientAPIUnsupportedPolicyOptions)));

            case (int)EventCode.E_ICARD_UI_INITIALIZATION:
                throw IDT.ThrowHelperError(new UIInitializationException(SR.GetString(SR.ClientAPIUIInitializationFailed)));

            case (int)EventCode.E_ICARD_IMPORT:
                throw IDT.ThrowHelperError(new CardSpaceException(SR.GetString(SR.ClientAPICannotImport)));

            default:
                //
                // In current implementation, caller will determine what to do in the default case.
                //
                break;
            }
        }
Example #5
0
        //
        // Summary
        //  Start the import card user interface
        //
        public static void Import(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                throw IDT.ThrowHelperArgumentNull("fileName");
            }


            IDT.TraceDebug("Import Infocard has been called");
            Int32 result = CardSpaceSelector.GetShim().m_csShimImportInformationCard(fileName);

            //
            // Convert HRESULTS to errors
            //
            if (0 != result)
            {
                //
                // Convert the HRESULTS to exceptions
                //
                ExceptionHelper.ThrowIfCardSpaceException((int)result);
                throw IDT.ThrowHelperError(new CardSpaceException(SR.GetString(SR.ClientAPIInfocardError)));
            }
        }
Example #6
0
        // Summary
        //  Request a security token from the infocard system
        //
        // Parameters
        //  policyChain  - an array of PolicyElements that describe the federated security chain that the client
        //                 needs a final token to unwind.
        //
        public static GenericXmlSecurityToken GetToken(CardSpacePolicyElement[] policyChain, SecurityTokenSerializer tokenSerializer)
        {
            IDT.TraceDebug("ICARDCLIENT: GetToken called with a policy chain of length {0}", policyChain.Length);

            InfoCardProofToken       proofToken         = null;
            InternalRefCountedHandle nativeCryptoHandle = null;
            GenericXmlSecurityToken  token         = null;
            RpcGenericXmlToken       infocardToken = new RpcGenericXmlToken();
            SafeTokenHandle          nativeToken   = null;
            Int32 result = 0;

            if (null == policyChain || 0 == policyChain.Length)
            {
                throw IDT.ThrowHelperArgumentNull("policyChain");
            }
            if (null == tokenSerializer)
            {
                throw IDT.ThrowHelperArgumentNull("tokenSerializer");
            }

            if (null == tokenSerializer)
            {
                throw IDT.ThrowHelperArgumentNull("tokenSerializer");
            }

            try
            {
                RuntimeHelpers.PrepareConstrainedRegions();
                bool mustRelease = false;
                try
                {
                }
                finally
                {
                    //
                    // The PolicyChain class will do the marshalling and native buffer management for us.
                    //
                    try
                    {
                        using (PolicyChain tmpChain = new PolicyChain(policyChain))
                        {
                            IDT.TraceDebug("ICARDCLIENT: PInvoking the native GetToken call");

                            result = GetShim().m_csShimGetToken(
                                tmpChain.Length,
                                tmpChain.DoMarshal(),
                                out nativeToken,
                                out nativeCryptoHandle);
                        }

                        if (0 == result)
                        {
                            IDT.TraceDebug("ICARDCLIENT: The PInvoke of GetToken succeeded");
                            nativeToken.DangerousAddRef(ref mustRelease);

                            infocardToken = (RpcGenericXmlToken)Marshal.PtrToStructure(
                                nativeToken.DangerousGetHandle(),
                                typeof(RpcGenericXmlToken));
                        }
                    }
                    finally
                    {
                        if (mustRelease)
                        {
                            nativeToken.DangerousRelease();
                        }
                    }
                }
                if (0 == result)
                {
                    using (ProofTokenCryptoHandle crypto =
                               (ProofTokenCryptoHandle)CryptoHandle.Create(nativeCryptoHandle))
                    {
                        proofToken = crypto.CreateProofToken();
                    }

                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(infocardToken.xmlToken);
                    SecurityKeyIdentifierClause internalTokenReference = null;
                    if (null != infocardToken.internalTokenReference)
                    {
                        internalTokenReference = tokenSerializer.ReadKeyIdentifierClause(
                            CreateReaderWithQuotas(infocardToken.internalTokenReference));
                    }
                    SecurityKeyIdentifierClause externalTokenReference = null;
                    if (null != infocardToken.externalTokenReference)
                    {
                        externalTokenReference = tokenSerializer.ReadKeyIdentifierClause(
                            CreateReaderWithQuotas(infocardToken.externalTokenReference));
                    }
                    IDT.TraceDebug("ICARDCLIENT: Constructing a new GenericXmlSecurityToken");
                    token = new GenericXmlSecurityToken(
                        xmlDoc.DocumentElement,
                        proofToken,
                        DateTime.FromFileTimeUtc(infocardToken.createDate),
                        DateTime.FromFileTimeUtc(infocardToken.expiryDate),
                        internalTokenReference,
                        externalTokenReference,
                        null);
                }
                else
                {
                    IDT.TraceDebug("ICARDCLIENT: The PInvoke of GetToken failed with a return code of {0}", result);

                    //
                    // Convert the HRESULTS to exceptions
                    //
                    ExceptionHelper.ThrowIfCardSpaceException((int)result);
                    throw IDT.ThrowHelperError(new CardSpaceException(SR.GetString(SR.ClientAPIInfocardError)));
                }
            }
            catch
            {
                if (null != nativeCryptoHandle)
                {
                    nativeCryptoHandle.Dispose();
                }

                if (null != proofToken)
                {
                    proofToken.Dispose();
                }
                throw;
            }
            finally
            {
                if (null != nativeToken)
                {
                    nativeToken.Dispose();
                }
            }

            return(token);
        }