Ejemplo n.º 1
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Ok && data != null)
            {
                if (requestCode == CHOOSE_TRUST_CHAIN_FILE)
                {
                    var fullPath = data.Data.Path;

                    byte[] trustChainBytes = File.ReadAllBytes(fullPath);

                    try {
                        TrustChainNode[] nodes = TrustChainUtil.SegmentChain(trustChainBytes);

                        if (TrustChainUtil.ValidateTrustChain(nodes))
                        {
                            identity.AddTrustChain(trustChainBytes);
                        }
                        else
                        {
                            Toast.MakeText(this, "Could not validate the trust chain", ToastLength.Short).Show();
                        }
                    } catch (CryptographicException) {
                        Toast.MakeText(this, "Could not validate the trust chain", ToastLength.Short).Show();
                    }
                }
                else if (requestCode == CHOOSE_IDENTITY_FILE)
                {
                    var fullPath = data.Data.Path;
                    Console.WriteLine($"got path {fullPath}");

                    byte[] newIdentity = File.ReadAllBytes(fullPath);

                    if (newIdentity.Length != CryptoUtil.ASYM_KEY_SIZE_BYTES)
                    {
                        Toast.MakeText(this, "Identity is invalid length", ToastLength.Short).Show();
                    }
                    else
                    {
                        // TODO add changeable permissions
                        var newChainBytes = identity.GenerateNewChain(newIdentity, identity.PermissionsHeld,
                                                                      identity.PermissionsGrantable, "Child");

                        var trustChainFileName = string.Format(TRUST_CHAIN_FORMAT_STRING,
                                                               IdentityManager.GetIdentityString(newIdentity));
                        var trustChainFullPath = Path.Combine(privatePath, trustChainFileName);
                        File.WriteAllBytes(trustChainFullPath, newChainBytes);

                        startEmail("Trust chain", trustChainFullPath);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public object ToObject(byte[] buffer)
        {
            using (var ms = new MemoryStream(buffer))
                using (var reader = new BinaryReader(ms)) {
                    var packetType = (PacketType)reader.ReadUInt32();
                    switch (packetType)
                    {
                    case PacketType.Have:
                        return(new HavePacket {
                            MerkleRootHash = reader.ReadSha256Base64()
                        });

                    case PacketType.Need:
                        return(new NeedPacket {
                            MerkleRootHash = reader.ReadSha256Base64()
                        });

                    case PacketType.Give:
                        return(new GivePacket {
                            NodeHash = reader.ReadSha256Base64(),
                            Node = reader.ReadMerkleNode()
                        });

                    case PacketType.Whois:
                        return(new WhoisPacket {
                            IdHash = reader.ReadBytes(CryptoUtil.HASH_SIZE)
                        });

                    case PacketType.Ident:
                        return(new IdentPacket {
                            Id = reader.ReadBytes(CryptoUtil.ASYM_KEY_SIZE_BYTES),
                            TrustChain = TrustChainUtil.SegmentChain(reader.ReadBytes(reader.ReadInt32() * TrustChainNode.NODE_BLOCK_SIZE))
                        });

                    case PacketType.Done:
                        return(new DonePacket());

                    default:
                        throw new InvalidStateException();
                    }
                }
        }