Beispiel #1
0
        /// <summary>
        /// Imports the <see cref="VirgilKey"/> from buffer.
        /// </summary>
        /// <param name="keyBuffer">The buffer with Key.</param>
        /// <param name="keyPassword">The Key password.</param>
        /// <returns>An instance of <see cref="VirgilKey"/> class.</returns>
        public VirgilKey Import(VirgilBuffer keyBuffer, string keyPassword = null)
        {
            var privateKey = this.context.Crypto.ImportPrivateKey(keyBuffer.GetBytes(), keyPassword);
            var virgilKey  = new VirgilKey(this.context, privateKey);

            return(virgilKey);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new <see cref="VirgilCard"/> that is representing user's Public key and information
        /// about identity. This card has to be published to the Virgil's services.
        /// </summary>
        /// <param name="identity">The user's identity.</param>
        /// <param name="identityType">Type of the identity.</param>
        /// <param name="ownerKey">The owner's <see cref="VirgilKey"/>.</param>
        /// <param name="customFields">The custom fields (optional).</param>
        /// <returns>A new instance of <see cref="VirgilCard"/> class, that is unpublished and
        /// representing user's Public key.</returns>
        public VirgilCard Create(string identity, VirgilKey ownerKey,
                                 string identityType = "unknown",
                                 Dictionary <string, string> customFields = null)
        {
            var cardModel = this.BuildCardModel(identity, identityType, customFields,
                                                CardScope.Application, ownerKey);

            return(new VirgilCard(this.context, cardModel));
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new global <see cref="VirgilCard"/> that is representing user's
        /// Public key and information about identity.
        /// </summary>
        /// <param name="identity">The user's identity value.</param>
        /// <param name="identityType">Type of the identity.</param>
        /// <param name="ownerKey">The owner's <see cref="VirgilKey"/>.</param>
        /// <param name="customFields">The custom fields (optional).</param>
        /// <returns>A new instance of <see cref="VirgilCard"/> class, that is representing user's Public key.</returns>
        public VirgilCard CreateGlobal(string identity, IdentityType identityType, VirgilKey ownerKey,
                                       Dictionary <string, string> customFields = null)
        {
            var identityTypeString = Enum.GetName(typeof(IdentityType), identityType)?.ToLower();

            var cardModel = this.BuildCardModel(identity, identityTypeString, customFields,
                                                CardScope.Global, ownerKey);

            return(new VirgilCard(this.context, cardModel));
        }
        /// <summary>
        /// Revokes a global <see cref="VirgilCard"/> from Virgil Security services.
        /// </summary>
        /// <param name="card">The Card to be revoked.</param>
        /// <param name="key">The Key associated with the revoking Card.</param>
        /// <param name="identityToken">The identity token.</param>
        public async Task RevokeGlobalAsync(VirgilCard card, VirgilKey key, IdentityValidationToken identityToken)
        {
            var revokeRequest = new RevokeGlobalCardRequest(card.Id, RevocationReason.Unspecified, identityToken.Value);

            var fingerprint = this.context.Crypto.CalculateFingerprint(revokeRequest.Snapshot);
            var signature   = key.Sign(fingerprint.GetValue());

            revokeRequest.AppendSignature(card.Id, signature.GetBytes());

            await this.context.Client.RevokeGlobalCardAsync(revokeRequest);
        }
Beispiel #5
0
        private CardModel BuildCardModel
        (
            string identity,
            string identityType,
            Dictionary <string, string> customFields,
            CardScope scope,
            VirgilKey ownerKey
        )
        {
            var cardSnapshotModel = new PublishCardSnapshotModel
            {
                Identity     = identity,
                IdentityType = identityType,
                Info         = new CardInfoModel
                {
                    DeviceName = this.context.DeviceManager.GetDeviceName(),
                    Device     = this.context.DeviceManager.GetSystemName()
                },
                PublicKeyData = ownerKey.ExportPublicKey().GetBytes(),
                Scope         = scope,
                Data          = customFields
            };

            var snapshot = new Snapshotter().Capture(cardSnapshotModel);

            var snapshotFingerprint = this.context.Crypto.CalculateFingerprint(snapshot);
            var cardId        = snapshotFingerprint.ToHEX();
            var selfSignature = ownerKey.Sign(VirgilBuffer.From(snapshotFingerprint.GetValue()));

            var signatures = new Dictionary <string, byte[]>
            {
                [cardId] = selfSignature.GetBytes()
            };

            var cardModel = new CardModel(cardSnapshotModel)
            {
                Id       = cardId,
                Snapshot = snapshot,
                Meta     = new CardMetaModel
                {
                    Signatures = signatures
                }
            };

            return(cardModel);
        }
Beispiel #6
0
        /// <summary>
        /// Loads the <see cref="VirgilKey"/> from current storage by specified key name.
        /// </summary>
        /// <param name="keyName">The name of the Key.</param>
        /// <param name="keyPassword">The Key password.</param>
        /// <returns>An instance of <see cref="VirgilKey"/> class.</returns>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="VirgilKeyIsNotFoundException"></exception>
        public VirgilKey Load(string keyName, string keyPassword = null)
        {
            if (string.IsNullOrWhiteSpace(keyName))
            {
                throw new ArgumentException(Localization.ExceptionArgumentIsNullOrWhitespace, nameof(keyName));
            }

            if (!this.context.KeyStorage.Exists(keyName))
            {
                throw new VirgilKeyIsNotFoundException();
            }

            var entry      = this.context.KeyStorage.Load(keyName);
            var privateKey = this.context.Crypto.ImportPrivateKey(entry.Value, keyPassword);

            var virgilKey = new VirgilKey(this.context, privateKey);

            return(virgilKey);
        }
Beispiel #7
0
 /// <summary>
 /// To check if current Virgil Card was generated for <paramref name="virgilKey"/>.
 /// </summary>
 /// <param name="virgilKey">An instance of <see cref="VirgilKey"/>.</param>
 public bool IsPairFor(VirgilKey virgilKey)
 {
     return(this.PublicKey.Get().Value.SequenceEqual(virgilKey.ExportPublicKey().GetBytes()));
 }
Beispiel #8
0
 /// <summary>
 /// Signs a plaintext using current <see cref="VirgilKey"/>.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="plaintext"/></param>
 /// <param name="plaintext">The plaintext to be signed.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with generated signature.</returns>
 public static VirgilBuffer Sign(this VirgilKey virgilKey, string plaintext)
 {
     return(virgilKey.Sign(VirgilBuffer.From(plaintext)));
 }
Beispiel #9
0
 /// <summary>
 /// Encrypts and signs the specified data for current enumeration of <see cref="VirgilCard"/> recipients.
 /// </summary>
 /// <param name="task">The list of <see cref="VirgilCard"/> recipients.</param>
 /// <param name="data">The data to be encrypted.</param>
 /// <param name="key">The signer's <see cref="VirgilKey"/></param>
 /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns>
 /// <exception cref="ArgumentNullException"></exception>
 public static Task <VirgilBuffer> SignThenEncrypt(this Task <IEnumerable <VirgilCard> > task, byte[] data, VirgilKey key)
 {
     return(SignThenEncrypt(task, VirgilBuffer.From(data), key));
 }
Beispiel #10
0
 /// <summary>
 /// Signs a byte array data using current <see cref="VirgilKey"/>.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="data"/></param>
 /// <param name="data">The plaintext to be signed.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with generated signature.</returns>
 public static VirgilBuffer Sign(this VirgilKey virgilKey, byte[] data)
 {
     return(virgilKey.Sign(VirgilBuffer.From(data)));
 }
Beispiel #11
0
        /// <summary>
        /// Encrypts and signs the specified buffer with data for current enumeration of <see cref="VirgilCard"/> recipients.
        /// </summary>
        /// <param name="task">The list of <see cref="VirgilCard"/> recipients.</param>
        /// <param name="buffer">The buffer data to be encrypted.</param>
        /// <param name="key">The signer's <see cref="VirgilKey"/></param>
        /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static Task <VirgilBuffer> SignThenEncrypt(this Task <IEnumerable <VirgilCard> > task, VirgilBuffer buffer, VirgilKey key)
        {
            return(task.ContinueWith(t =>
            {
                if (t.Exception == null && !t.Result.Any())
                {
                    throw new RecipientsNotFoundException();
                }

                return key.SignThenEncrypt(buffer, t.Result);
            }));
        }
Beispiel #12
0
 /// <summary>
 /// Encrypts and signs the specified plaintext for current enumeration of <see cref="VirgilCard"/> recipients.
 /// </summary>
 /// <param name="task">The list of <see cref="VirgilCard"/> recipients.</param>
 /// <param name="plaintext">The plaintext to be encrypted.</param>
 /// <param name="key">The signer's <see cref="VirgilKey"/></param>
 /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns>
 /// <exception cref="ArgumentNullException"></exception>
 public static Task <VirgilBuffer> SignThenEncrypt(this Task <IEnumerable <VirgilCard> > task, string plaintext, VirgilKey key)
 {
     return(SignThenEncrypt(task, VirgilBuffer.From(plaintext), key));
 }
Beispiel #13
0
 /// <summary>
 /// Decrypts a ciphertext using current <see cref="VirgilKey"/> and verifies one
 /// using specified <see cref="VirgilCard"/>.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/>, that represents a Private key.</param>
 /// <param name="ciphertext">The ciphertext in base64 encoded string.</param>
 /// <param name="signerCard">The signer's <see cref="VirgilCard"/>, that represents a
 /// Public key and user/device information.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with decrypted data.</returns>
 public static VirgilBuffer DecryptThenVerify(this VirgilKey virgilKey, string ciphertext, VirgilCard signerCard)
 {
     return(virgilKey.DecryptThenVerify(VirgilBuffer.From(ciphertext, StringEncoding.Base64), signerCard));
 }
Beispiel #14
0
 /// <summary>
 /// Signs a byte array data using current <see cref="VirgilKey"/> and then encrypt it
 /// using multiple recipient's <see cref="VirgilCard"/>s.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="data"/>.</param>
 /// <param name="data">The data to be encrypted.</param>
 /// <param name="recipients">A list of recipient's <see cref="VirgilCard"/>s used to
 /// encrypt the <paramref name="data"/>.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with encrypted data.</returns>
 public static VirgilBuffer SignThenEncrypt(this VirgilKey virgilKey, byte[] data, IEnumerable <VirgilCard> recipients)
 {
     return(virgilKey.SignThenEncrypt(VirgilBuffer.From(data), recipients));
 }
Beispiel #15
0
 /// <summary>
 /// Signs the plaintext using current <see cref="VirgilKey"/> and then encrypt it
 /// using recipient's <see cref="VirgilCard"/>.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="plaintext"/>.</param>
 /// <param name="plaintext">The plaintext to be encrypted.</param>
 /// <param name="recipient">The recipient's <see cref="VirgilCard"/> used to
 /// encrypt the <paramref name="plaintext"/>.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with encrypted data.</returns>
 public static VirgilBuffer SignThenEncrypt(this VirgilKey virgilKey, string plaintext, VirgilCard recipient)
 {
     return(virgilKey.SignThenEncrypt(VirgilBuffer.From(plaintext), new [] { recipient }));
 }
Beispiel #16
0
 /// <summary>
 /// Signs a byte array data using current <see cref="VirgilKey"/> and then encrypt it
 /// using recipient's <see cref="VirgilCard"/>.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="data"/>.</param>
 /// <param name="data">The plaintext to be encrypted.</param>
 /// <param name="recipient">The recipient's <see cref="VirgilCard"/> used to
 /// encrypt the <paramref name="data"/>.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with encrypted data.</returns>
 public static VirgilBuffer SignThenEncrypt(this VirgilKey virgilKey, byte[] data, VirgilCard recipient)
 {
     return(virgilKey.SignThenEncrypt(VirgilBuffer.From(data), new[] { recipient }));
 }
Beispiel #17
0
 /// <summary>
 /// Signs a plaintext using current <see cref="VirgilKey"/> and then encrypt it
 /// using multiple recipient's <see cref="VirgilCard"/>s.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="plaintext"/>.</param>
 /// <param name="plaintext">The plaintext to be encrypted.</param>
 /// <param name="recipients">A list of recipient's <see cref="VirgilCard"/>s used to
 /// encrypt the <paramref name="plaintext"/>.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with encrypted data.</returns>
 public static VirgilBuffer SignThenEncrypt(this VirgilKey virgilKey, string plaintext, IEnumerable <VirgilCard> recipients)
 {
     return(virgilKey.SignThenEncrypt(VirgilBuffer.From(plaintext), recipients));
 }
Beispiel #18
0
 /// <summary>
 /// Encrypts the specified data for current <see cref="VirgilCard" /> recipient.
 /// </summary>
 /// <param name="task">The <see cref="VirgilCard" /> recipient</param>
 /// <param name="buffer">The buffer with data to be encrypted.</param>
 /// <param name="key">The signer's key.</param>
 /// <returns>
 /// A new <see cref="VirgilBuffer" /> with encrypted data.
 /// </returns>
 /// <exception cref="ArgumentNullException"></exception>
 public static Task <VirgilBuffer> SignThenEncrypt(this Task <VirgilCard> task, VirgilBuffer buffer, VirgilKey key)
 {
     return(task.ContinueWith(t => key.SignThenEncrypt(buffer, new[] { t.Result })));
 }
Beispiel #19
0
 /// <summary>
 /// Decrypts a ciphertext using current <see cref="VirgilKey"/> and verifies one
 /// using specified <see cref="VirgilCard"/>.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/>, that represents a Private key.</param>
 /// <param name="cipherdata">The ciphertext in base64 encoded string.</param>
 /// <param name="signerCard">The signer's <see cref="VirgilCard"/>, that represents a
 /// Public key and user/device information.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with decrypted data.</returns>
 public static VirgilBuffer DecryptThenVerify(this VirgilKey virgilKey, byte[] cipherdata, VirgilCard signerCard)
 {
     return(virgilKey.DecryptThenVerify(new VirgilBuffer(cipherdata), signerCard));
 }