Example #1
0
        public Identity(IdentityManager identityManager, RSAParameters privateKey, string name)
        {
            this.privateKey = privateKey;
            identityHash    = CryptoUtil.GetHash(privateKey.Modulus);

            this.identityManager = identityManager;

            TrustChain           = null;
            PermissionsHeld      = Permission.None;
            PermissionsGrantable = Permission.None;
            Name = name;
        }
Example #2
0
        public Identity(RSACryptoServiceProvider rsa, IdentityManager identityManager, string name)
        {
            privateKey   = rsa.ExportParameters(true);
            identityHash = CryptoUtil.GetHash(privateKey.Modulus);

            this.identityManager = identityManager;

            TrustChain           = null;
            PermissionsHeld      = Permission.None;
            PermissionsGrantable = Permission.None;
            Name = name;
        }
Example #3
0
 // returns the canonical identity string of a public key
 public static string GetIdentityString(byte[] publicKey)
 {
     if (publicKey.Length == CryptoUtil.HASH_SIZE)
     {
         return(BitConverter.ToString(publicKey).Replace("-", ""));
     }
     else if (publicKey.Length == CryptoUtil.ASYM_KEY_SIZE_BYTES)
     {
         return(BitConverter.ToString(CryptoUtil.GetHash(publicKey)).Replace("-", ""));
     }
     else
     {
         throw new CryptographicException("Invalid key size");
     }
 }
Example #4
0
        // unicast/broadcast
        // () asymmetric encrypt
        // <[sender hash][recipient hash]([sender hash][message])[signature]>
        //  [32         ][32            ] [32         ][msg len] [256      ]
        //
        // multicast
        // () symmetric encrypt
        // <[sender hash][recipient hash][IV](<[message][signature]>)[signature]>
        public BroadcastMessageDto EncodePacket(byte[] message, byte[] remoteKey = null)
        {
            if (remoteKey != null && remoteKey.Length != CryptoUtil.ASYM_KEY_SIZE_BYTES && remoteKey.Length != CryptoUtil.SYM_KEY_SIZE)
            {
                throw new CryptographicException("Bad key size");
            }

            byte[] senderHash    = CryptoUtil.GetHash(privateKey.Modulus);
            byte[] recipientHash = CryptoUtil.GetHash(remoteKey);;
            byte[] processedMessage;

            if (remoteKey == null)
            {
                // broadcast
                recipientHash    = BROADCAST_ID;
                processedMessage = message;
            }
            else if (remoteKey.Length == CryptoUtil.ASYM_KEY_SIZE_BYTES)
            {
                // unicast
                using (var ms = new MemoryStream())
                    using (var writer = new BinaryWriter(ms)) {
                        writer.Write(senderHash);
                        writer.Write(message);
                        processedMessage = ms.ToArray();
                    }

                processedMessage = CryptoUtil.AsymmetricEncrypt(processedMessage, remoteKey);
            }
            else
            {
                // multicast
                using (var ms = new MemoryStream())
                    using (var writer = new BinaryWriter(ms)) {
                        writer.Write(message);
                        writer.Write(CryptoUtil.Sign(message, privateKey));
                        processedMessage = ms.ToArray();
                    }

                byte[] iv = CryptoUtil.GenerateIV();
                processedMessage = CryptoUtil.SymmetricEncrypt(processedMessage, remoteKey, iv);

                using (var ms = new MemoryStream())
                    using (var writer = new BinaryWriter(ms)) {
                        writer.Write(iv);
                        writer.Write(processedMessage);
                        processedMessage = ms.ToArray();
                    }
            }

            byte[] payload;
            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms)) {
                    writer.Write(senderHash);
                    writer.Write(recipientHash);
                    writer.Write(processedMessage);
                    payload = ms.ToArray();
                }

            byte[] signature = CryptoUtil.Sign(payload, privateKey);

            return(new BroadcastMessageDto {
                SourceIdHash = senderHash,
                DestinationIdHash = recipientHash,
                Payload = processedMessage,
                Signature = signature
            });
        }