public CallerChain ImportChain(byte[] token, string domain)
        {
            ConnectionImpl conn = (ConnectionImpl)GetCurrentConnection();

            if (conn == null)
            {
                throw new NO_PERMISSION(NoLoginCode.ConstVal, CompletionStatus.Completed_No);
            }
            AccessControl          acs    = conn.Acs;
            AsymmetricKeyParameter busKey = conn.BusKey;

            if (busKey == null)
            {
                throw new NO_PERMISSION(NoLoginCode.ConstVal, CompletionStatus.Completed_No);
            }
            byte[]     encryptedToken = Crypto.Encrypt(busKey, token);
            SignedData signedChain    = acs.signChainByToken(encryptedToken, domain);

            try {
                CallChain callChain = CallerChainImpl.UnmarshalCallChain(signedChain);
                return(new CallerChainImpl(callChain.bus, callChain.caller, callChain.target,
                                           callChain.originators, signedChain));
            }
            catch (GenericUserException e) {
                const string message = "Falha inesperada ao importar uma nova cadeia.";
                Logger.Error(message, e);
                throw new OpenBusInternalException(message, e);
            }
        }
 public CallerChain DecodeChain(byte[] encoded)
 {
     try {
         VersionedData[] versions = DecodeExportedVersions(encoded,
                                                           _magicTagCallChain);
         CallerChainImpl decodedChain = null;
         for (int i = 0; i < versions.Length; i++)
         {
             // Se houver duas versões, a versão atual virá antes da versão legacy.
             if (versions[i].version == ExportVersion.ConstVal)
             {
                 TypeCode signedDataTypeCode =
                     ORB.create_tc_for_type(typeof(SignedData));
                 SignedData exportedChain =
                     (SignedData)
                     _codec.decode_value(versions[i].encoded, signedDataTypeCode);
                 CallChain chain = CallerChainImpl.UnmarshalCallChain(exportedChain);
                 if (decodedChain == null)
                 {
                     decodedChain = new CallerChainImpl(chain.bus, chain.caller,
                                                        chain.target, chain.originators, exportedChain);
                 }
                 else
                 {
                     decodedChain.Signed.Chain = exportedChain;
                 }
             }
             if (versions[i].version == CurrentVersion.ConstVal)
             {
                 TypeCode exportedChainTypeCode =
                     ORB.create_tc_for_type(typeof(ExportedCallChain));
                 ExportedCallChain exportedChain =
                     (ExportedCallChain)
                     _codec.decode_value(versions[i].encoded, exportedChainTypeCode);
                 core.v2_0.services.access_control.CallChain chain =
                     CallerChainImpl.UnmarshalLegacyCallChain(exportedChain.signedChain);
                 if (decodedChain == null)
                 {
                     decodedChain = new CallerChainImpl(exportedChain.bus, chain.caller,
                                                        chain.target, chain.originators, exportedChain.signedChain);
                 }
                 else
                 {
                     decodedChain.Signed.LegacyChain = exportedChain.signedChain;
                 }
             }
         }
         if (decodedChain != null)
         {
             return(decodedChain);
         }
     }
     catch (GenericUserException e) {
         const string message =
             "Falha inesperada ao decodificar uma cadeia exportada.";
         Logger.Error(message, e);
         throw new InvalidEncodedStreamException(message, e);
     }
     throw new InvalidEncodedStreamException("Versão de cadeia incompatível.");
 }
        public CallerChain MakeChainFor(string entity)
        {
            ConnectionImpl conn = (ConnectionImpl)GetCurrentConnection();

            if (conn == null)
            {
                Logger.Error("Não há conexão para executar a chamada MakeChainFor.");
                throw new NO_PERMISSION(NoLoginCode.ConstVal, CompletionStatus.Completed_No);
            }
            LoginInfo?myLogin = conn.Login;

            if (!myLogin.HasValue)
            {
                Logger.Error("Não há login para executar a chamada MakeChainFor.");
                throw new NO_PERMISSION(NoLoginCode.ConstVal, CompletionStatus.Completed_No);
            }

            try {
                AccessControl acs         = conn.Acs;
                SignedData    signedChain = acs.signChainFor(entity);
                CallChain     callChain   = CallerChainImpl.UnmarshalCallChain(signedChain);
                if (conn.Legacy)
                {
                    SignedCallChain legacySigned = conn.LegacyConverter.signChainFor(entity);
                    return(new CallerChainImpl(callChain.bus, callChain.caller, callChain.target,
                                               callChain.originators, signedChain, legacySigned));
                }
                return(new CallerChainImpl(callChain.bus, callChain.caller, callChain.target,
                                           callChain.originators, signedChain));
            }
            catch (GenericUserException e) {
                Logger.Error("Falha inesperada ao criar uma nova cadeia.", e);
                throw;
            }
        }