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 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 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;
            }
        }
 public byte[] EncodeChain(CallerChain chain)
 {
     try {
         CallerChainImpl chainImpl     = (CallerChainImpl)chain;
         int             i             = 0;
         VersionedData?  actualVersion = null;
         VersionedData?  legacyVersion = null;
         if (!chainImpl.Legacy)
         {
             // se não é legacy, tem a versão atual. Pode ter a versão legacy ou não.
             TypeCode signedChainTC = _orb.create_tc_for_type(typeof(SignedData));
             Any      any           = new Any(chainImpl.Signed.Chain, signedChainTC);
             byte[]   encoded       = _codec.encode_value(any);
             actualVersion = new VersionedData(ExportVersion.ConstVal, encoded);
             i++;
         }
         if (chainImpl.Signed.LegacyChain.encoded != null)
         {
             ExportedCallChain exported = new ExportedCallChain(chainImpl.BusId,
                                                                chainImpl.Signed.LegacyChain);
             TypeCode exportedChainTC = _orb.create_tc_for_type(typeof(ExportedCallChain));
             Any      any             = new Any(exported, exportedChainTC);
             byte[]   legacyEncoded   = _codec.encode_value(any);
             legacyVersion = new VersionedData(CurrentVersion.ConstVal, legacyEncoded);
             i++;
         }
         VersionedData[] versions = new VersionedData[i];
         // A ordem das versões exportadas IMPORTA. A 2.1 deve vir antes da 2.0.
         if (legacyVersion != null)
         {
             versions[--i] = legacyVersion.Value;
         }
         if (actualVersion != null)
         {
             versions[--i] = actualVersion.Value;
         }
         return(EncodeExportedVersions(versions, _magicTagCallChain));
     }
     catch (InvalidTypeForEncoding e) {
         const string message =
             "Falha inesperada ao codificar uma cadeia para exportação.";
         Logger.Error(message, e);
         throw new OpenBusInternalException(message, e);
     }
 }
        internal bool IsJoinedToLegacyChain()
        {
            CallerChainImpl joined = (CallerChainImpl)JoinedChain;

            return((joined != null) && (joined.Legacy));
        }