예제 #1
0
        /// <summary>
        /// Creates a new <see cref="VirgilKey"/> with custom Public/Private key pair.
        /// </summary>
        /// <param name="keyName">Name of the key.</param>
        /// <param name="keyPair">The key pair.</param>
        /// <param name="password">The password.</param>
        /// <returns>The instance of <see cref="VirgilKey"/></returns>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="VirgilKeyIsAlreadyExistsException"></exception>
        public static VirgilKey Create(string keyName, KeyPair keyPair, string password = null)
        {
            if (string.IsNullOrWhiteSpace(keyName))
            {
                throw new ArgumentException(Localization.ExceptionArgumentIsNullOrWhitespace, nameof(keyName));
            }

            if (keyPair == null)
            {
                throw new ArgumentNullException(nameof(keyPair));
            }

            var crypto  = VirgilConfig.GetService <Crypto>();
            var storage = VirgilConfig.GetService <IKeyStorage>();

            if (storage.Exists(keyName))
            {
                throw new VirgilKeyIsAlreadyExistsException();
            }

            var virgilKey = new VirgilKey {
                KeyPair = keyPair, KeyName = keyName
            };
            var exportedPrivateKey = crypto.ExportPrivateKey(virgilKey.KeyPair.PrivateKey, password);

            storage.Store(new KeyEntry
            {
                Name  = keyName,
                Value = exportedPrivateKey
            });

            return(virgilKey);
        }
예제 #2
0
        /// <summary>
        /// Creates a <see cref="VirgilKey"/> with specified key name.
        /// </summary>
        /// <param name="keyName">Name of the key.</param>
        /// <param name="password">The password.</param>
        /// <returns>The instance of <see cref="VirgilKey"/></returns>
        public static VirgilKey Create(string keyName, string password = null)
        {
            var crypto  = VirgilConfig.GetService <Crypto>();
            var keyPair = crypto.GenerateKeys();

            return(Create(keyName, keyPair, password));
        }
예제 #3
0
        /// <summary>
        /// Creates a new <see cref="VirgilCard"/> by request.
        /// </summary>
        /// <param name="request">The request.</param>
        public static async Task <VirgilCard> CreateAsync(CreateCardRequest request)
        {
            var client = VirgilConfig.GetService <VirgilClient>();
            var card   = await client.CreateCardAsync(request).ConfigureAwait(false);

            return(new VirgilCard(card));
        }
예제 #4
0
        /// <summary>
        /// Loads the <see cref="VirgilKey"/> by specified key name.
        /// </summary>
        /// <param name="keyName">Name of the key.</param>
        /// <param name="password">The password.</param>
        /// <returns>The instance of <see cref="VirgilKey"/></returns>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="VirgilKeyIsNotFoundException"></exception>
        public static VirgilKey Load(string keyName, string password = null)
        {
            if (string.IsNullOrWhiteSpace(keyName))
            {
                throw new ArgumentException(Localization.ExceptionArgumentIsNullOrWhitespace, nameof(keyName));
            }

            var crypto  = VirgilConfig.GetService <Crypto>();
            var storage = VirgilConfig.GetService <IKeyStorage>();

            if (!storage.Exists(keyName))
            {
                throw new VirgilKeyIsNotFoundException();
            }

            var entry      = storage.Load(keyName);
            var privateKey = crypto.ImportPrivateKey(entry.Value, password);
            var publicKey  = crypto.ExtractPublicKey(privateKey);

            var virgilKey = new VirgilKey
            {
                KeyName = keyName,
                KeyPair = new KeyPair(publicKey, privateKey)
            };

            return(virgilKey);
        }
예제 #5
0
        /// <summary>
        /// Finds the <see cref="VirgilCard" />s by specified criteria.
        /// </summary>
        /// <param name="identities">The identities.</param>
        /// <param name="type">Type of the identity.</param>
        /// <returns>
        /// A list of found <see cref="VirgilCard" />s.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static async Task <IEnumerable <VirgilCard> > FindAsync
        (
            IEnumerable <string> identities,
            string type = null
        )
        {
            var identityList = identities as IList <string> ?? identities.ToList();

            if (identities == null || !identityList.Any())
            {
                throw new ArgumentNullException(nameof(identities));
            }

            var client = VirgilConfig.GetService <VirgilClient>();

            var criteria = new SearchCriteria
            {
                Identities   = identityList,
                IdentityType = type,
                Scope        = CardScope.Application
            };

            var cardModels = await client.SearchCardsAsync(criteria).ConfigureAwait(false);

            return(cardModels.Select(model => new VirgilCard(model)).ToList());
        }
예제 #6
0
        /// <summary>
        /// Decrypts and verifies the data.
        /// </summary>
        /// <param name="cipherData">The data to be decrypted.</param>
        /// <param name="signer">The signer's <see cref="VirgilCard"/>.</param>
        /// <returns>The decrypted data, which is the original plain text before encryption.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public byte[] DecryptThenVerify(byte[] cipherData, VirgilCard signer)
        {
            var crypto    = VirgilConfig.GetService <Crypto>();
            var publicKey = crypto.ImportPublicKey(signer.PublicKey);

            var cipherdata = crypto.DecryptThenVerify(cipherData, this.KeyPair.PrivateKey, publicKey);

            return(cipherdata);
        }
예제 #7
0
        /// <summary>
        /// Destroys the current <see cref="VirgilKey"/>.
        /// </summary>
        public void Destroy()
        {
            if (string.IsNullOrWhiteSpace(this.KeyName))
            {
                throw new NotSupportedException();
            }

            var storage = VirgilConfig.GetService <IKeyStorage>();

            storage.Delete(this.KeyName);
        }
예제 #8
0
        /// <summary>
        /// Signs the request as authority.
        /// </summary>
        public void SignRequest(SignableRequest request, string appId)
        {
            if (string.IsNullOrWhiteSpace(appId))
            {
                throw new ArgumentException(Localization.ExceptionArgumentIsNullOrWhitespace, nameof(appId));
            }

            var signer = VirgilConfig.GetService <RequestSigner>();

            signer.AuthoritySign(request, appId, this.KeyPair.PrivateKey);
        }
예제 #9
0
        /// <summary>
        /// Generates a digital signature for specified data using current <see cref="VirgilKey"/>.
        /// </summary>
        /// <param name="data">The data for which the digital signature will be generated.</param>
        /// <returns>A byte array containing the result from performing the operation.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public byte[] Sign(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var crypto    = VirgilConfig.GetService <VirgilCrypto>();
            var signature = crypto.Sign(data, this.KeyPair.PrivateKey);

            return(signature);
        }
예제 #10
0
        public CreateCardRequest BuildCardRequest(string identity, string type, Dictionary <string, string> data = null)
        {
            var crypto = VirgilConfig.GetService <Crypto>();
            var signer = VirgilConfig.GetService <RequestSigner>();

            var exportedPublicKey = crypto.ExportPublicKey(this.KeyPair.PublicKey);
            var request           = new CreateCardRequest(identity, type, exportedPublicKey, data);

            signer.SelfSign(request, this.KeyPair.PrivateKey);

            return(request);
        }
예제 #11
0
        /// <summary>
        /// Decrypts the specified cipherdata using <see cref="VirgilKey"/>.
        /// </summary>
        /// <param name="cipherData">The encrypted data.</param>
        /// <returns>A byte array containing the result from performing the operation.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public byte[] Decrypt(byte[] cipherData)
        {
            if (cipherData == null)
            {
                throw new ArgumentNullException(nameof(cipherData));
            }

            var crypto = VirgilConfig.GetService <VirgilCrypto>();
            var data   = crypto.Decrypt(cipherData, this.KeyPair.PrivateKey);

            return(data);
        }
예제 #12
0
        /// <summary>
        /// Gets the <see cref="VirgilCard"/> by specified identifier.
        /// </summary>
        /// <param name="cardId">The identifier that represents a <see cref="VirgilCard"/>.</param>
        public static async Task <VirgilCard> GetAsync(string cardId)
        {
            var client        = VirgilConfig.GetService <VirgilClient>();
            var virgilCardDto = await client.GetCardAsync(cardId);

            if (virgilCardDto == null)
            {
                throw new VirgilCardIsNotFoundException();
            }

            return(new VirgilCard(virgilCardDto));
        }
예제 #13
0
        public static VirgilKey FromFile(string keyPath, string password = null)
        {
            var crypto         = VirgilConfig.GetService <Crypto>();
            var privateKeyData = File.ReadAllBytes(keyPath);

            var privateKey = crypto.ImportPrivateKey(privateKeyData, password);
            var publicKey  = crypto.ExtractPublicKey(privateKey);

            return(new VirgilKey
            {
                KeyPair = new KeyPair(publicKey, privateKey)
            });
        }
예제 #14
0
        /// <summary>
        /// Verifies that a digital signature is valid for specified text.
        /// </summary>
        /// <param name="recipient">The <see cref="VirgilCard"/> recipient.</param>
        /// <param name="data">The text.</param>
        /// <param name="signature">The signature.</param>
        /// <returns><c>true</c> if the signature is valid; otherwise, <c>false</c>.</returns>
        /// <exception cref="ArgumentException"></exception>
        public static bool Verify(this VirgilCard recipient, byte[] data, byte[] signature)
        {
            if (recipient == null)
            {
                throw new ArgumentNullException(nameof(recipient));
            }

            var crypto    = VirgilConfig.GetService <Crypto>();
            var publicKey = crypto.ImportPublicKey(recipient.PublicKey);
            var isValid   = crypto.Verify(data, signature, publicKey);

            return(isValid);
        }
예제 #15
0
        /// <summary>
        /// Encrypts the data.
        /// </summary>
        /// <param name="recipients">The list of <see cref="VirgilCard"/> recipients.</param>
        /// <param name="data">The data to encrypt.</param>
        /// <returns>The encrypted data</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static byte[] Encrypt(this IEnumerable <VirgilCard> recipients, byte[] data)
        {
            if (recipients == null)
            {
                throw new ArgumentNullException(nameof(recipients));
            }

            var crypto     = VirgilConfig.GetService <Crypto>();
            var publicKeys = recipients.Select(p => crypto.ImportPublicKey(p.PublicKey)).ToArray();

            var cipherdata = crypto.Encrypt(data, publicKeys);

            return(cipherdata);
        }
예제 #16
0
        /// <summary>
        /// Encrypts the specified data for current <see cref="VirgilCard"/> recipient.
        /// </summary>
        /// <param name="data">The data to be encrypted.</param>
        public byte[] Encrypt(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var crypto    = VirgilConfig.GetService <Crypto>();
            var publicKey = crypto.ImportPublicKey(this.PublicKey);

            var cipherdata = crypto.Encrypt(data, publicKey);

            return(cipherdata);
        }
예제 #17
0
        /// <summary>
        /// Encrypts and signs the data.
        /// </summary>
        /// <param name="data">The data to be encrypted.</param>
        /// <param name="recipients">The list of <see cref="VirgilCard"/> recipients.</param>
        /// <returns>The encrypted data</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public byte[] SignThenEncrypt(byte[] data, IEnumerable <VirgilCard> recipients)
        {
            if (recipients == null)
            {
                throw new ArgumentNullException(nameof(recipients));
            }

            var crypto     = VirgilConfig.GetService <Crypto>();
            var publicKeys = recipients.Select(p => crypto.ImportPublicKey(p.PublicKey)).ToArray();

            var cipherdata = crypto.SignThenEncrypt(data, this.KeyPair.PrivateKey, publicKeys);

            return(cipherdata);
        }
예제 #18
0
        /// <summary>
        /// Finds the <see cref="VirgilCard" />s in global scope by specified criteria.
        /// </summary>
        /// <param name="identities">The identity.</param>
        /// <param name="type">Type of the identity.</param>
        /// <returns>
        /// A list of found <see cref="VirgilCard" />s.
        /// </returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static async Task <IEnumerable <VirgilCard> > FindGlobalAsync
        (
            IEnumerable <string> identities,
            GlobalIdentityType type = GlobalIdentityType.Email
        )
        {
            if (identities == null)
            {
                throw new ArgumentNullException(nameof(identities));
            }

            var client = VirgilConfig.GetService <VirgilClient>();

            var criteria = new SearchCriteria
            {
                Identities   = identities,
                IdentityType = type.ToString().ToLower(),
                Scope        = CardScope.Global
            };

            var cards = await client.SearchCardsAsync(criteria).ConfigureAwait(false);

            return(cards.Select(c => new VirgilCard(c)).ToList());
        }
예제 #19
0
        /// <summary>
        /// Exports the <see cref="VirgilKey"/> to default Virgil Security format.
        /// </summary>
        public byte[] Export(string password = null)
        {
            var crypto = VirgilConfig.GetService <Crypto>();

            return(crypto.ExportPrivateKey(this.KeyPair.PrivateKey, password));
        }
예제 #20
0
 /// <summary>
 /// Revokes a <see cref="VirgilCard"/> by revocation request.
 /// </summary>
 public static async Task RevokeAsync(RevokeCardRequest request)
 {
     var client = VirgilConfig.GetService <VirgilClient>();
     await client.RevokeCardAsync(request).ConfigureAwait(false);
 }