예제 #1
0
 public CertEncryptedKeySetWriter(IKeySetWriter writer, Stream certStream, Func <string> passwordPrompt = null)
 {
     _writer        = writer;
     _certKeySet    = ImportedKeySet.Import.Pkcs12Keys(KeyPurpose.DecryptAndEncrypt, certStream, passwordPrompt);
     _encrypter     = new Crypter(_certKeySet);
     _sessionPacker = new BsonSessionKeyPacker();
 }
예제 #2
0
        public static IKeySetWriter LayerSecurity(Func <IRootProviderKeySetWriter> rootKeySetWriterCreator,
                                                  params Func <IKeySetWriter, ILayeredKeySetWriter>[] layeredKeySetWriterCreators)
        {
            IKeySetWriter writer = rootKeySetWriterCreator();

            return(layeredKeySetWriterCreators.Aggregate(writer, (current, layered) => layered(current)));
        }
        private void HelperCryptCreate(IKeySetWriter writer, IKeySet keySet, string kspath,
                                       IKeySet nonEncryptedKS = null, IKeySetWriter nonEncryptedWriter = null)
        {
            using (var ks = new MutableKeySet(nonEncryptedKS ?? keySet))
            {
                var ver = ks.AddKey(KeyStatus.Primary);
                Expect(ver, Is.EqualTo(1));

                var success = ks.Save(nonEncryptedWriter ?? writer);
                Expect(success, Is.True);
            }

            using (var encrypter = new Encrypter(nonEncryptedKS ?? keySet))
            {
                var ciphertext = encrypter.Encrypt(input);
                File.WriteAllText(Path.Combine(kspath, "1.out"), ciphertext);
            }

            using (var ks = new MutableKeySet(keySet))
            {
                var ver = ks.AddKey(KeyStatus.Primary);
                Expect(ver, Is.EqualTo(2));
                var success = ks.Save(writer);
                Expect(success, Is.True);
            }

            using (var encrypter = new Encrypter(keySet))
            {
                var ciphertext = encrypter.Encrypt(input);
                File.WriteAllText(Path.Combine(kspath, "2.out"), ciphertext);
            }
        }
예제 #4
0
        /// <summary>
        /// Saves using the specified writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <returns>true if successful</returns>
        public bool Save(IKeySetWriter writer)
        {
            writer.Write(_metadata);

            if (!onlyMetaChanged || writer is INonSeparatedMetadataAndKey)
            {
                for (int i = 1; i <= _keys.Count; i++)
                {
                    var key = _keys[i];
                    writer.Write(key, i);
                }
            }
            return(writer.Finish());
        }
예제 #5
0
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    _certKeySet = _certKeySet.SafeDispose();
                    _encrypter  = _encrypter.SafeDispose();
                    _writer     = null;
                }

                disposedValue = true;
            }
        }
예제 #6
0
        public CertEncryptedKeySetWriter(IKeySetWriter writer, string thumbPrint)
        {
            var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);

            certStore.Open(OpenFlags.ReadOnly);
            var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, false);
            var cert           = certCollection.OfType <X509Certificate2>().FirstOrDefault();
            var privKey        = cert?.GetRSAPrivateKey();

            if (privKey == null)
            {
                throw new InvalidKeyException("Could not find cert that matched thumbprint.");
            }

            var keyParam = DotNetUtilities.GetRsaKeyPair(privKey).Private as RsaPrivateCrtKeyParameters;
            var key      = CertEncryptedKeySet.KeyFromBouncyCastle(keyParam);

            _certKeySet = new ImportedKeySet(key, KeyPurpose.DecryptAndEncrypt, "imported from X509Store");

            _writer        = writer;
            _encrypter     = new Encrypter(_certKeySet);
            _sessionPacker = new BsonSessionKeyPacker();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="EncryptedKeySetWriter"/> class.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="encrypter">The encrypter.</param>
 public EncryptedKeySetWriter(IKeySetWriter writer, Encrypter encrypter)
 {
     _encrypter = encrypter;
     _writer = writer;
 }
예제 #8
0
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources.
 /// </summary>
 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 protected virtual void Dispose(bool disposing)
 {
     _writer   = null;
     _password = _password.SafeDispose();
 }
예제 #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PbeKeySetWriter"/> class.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="passwordPrompt">The password prompt.</param>
 /// <param name="iterationCount">The interations count.</param>
 public PbeKeySetWriter(IKeySetWriter writer, Func <string> passwordPrompt, int iterationCount = 4096)
 {
     _password       = CachedPrompt.Password(passwordPrompt);
     _writer         = writer;
     _iterationCount = iterationCount;
 }
예제 #10
0
        /// <summary>
        /// Saves using the specified writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <returns>true if successful</returns>
        public bool Save(IKeySetWriter writer)
        {
            writer.Write(_metadata);

            if (!onlyMetaChanged || writer is INonSeparatedMetadataAndKey)
            {
                for (int i = 1; i <= _keys.Count; i++)
                {
                    var key = _keys[i];
                    writer.Write(key, i);
                }
            }
            return writer.Finish();
        }
예제 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PbeKeySetWriter"/> class.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="passwordPrompt">The password prompt.</param>
 /// <param name="iterationCount">The interations count.</param>
 public PbeKeySetWriter(IKeySetWriter writer, Func<string> passwordPrompt, int iterationCount = 4096)
 {
     _password = CachedPrompt.Password(passwordPrompt);
     _writer = writer;
     _iterationCount = iterationCount;
 }
예제 #12
0
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources.
 /// </summary>
 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 protected virtual void Dispose(bool disposing)
 {
     _writer = null;
     _password = _password.SafeDispose();
 }
예제 #13
0
 public static KeyczarConfig GetConfig(this IKeySetWriter writer)
 {
     return(writer.Config ?? KeyczarDefaults.DefaultConfig);
 }
예제 #14
0
 /// <summary>
 /// Writes the specified key.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="key">The key.</param>
 /// <param name="version">The version.</param>
 public static void Write(this IKeySetWriter writer, Key key, int version)
 {
     writer.Write(writer.GetConfig().RawStringEncoding.GetBytes(key.ToJson()), version);
 }
예제 #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EncryptedKeySetWriter"/> class.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="encrypter">The encrypter.</param>
 public EncryptedKeySetWriter(IKeySetWriter writer, Encrypter encrypter)
 {
     _encrypter = encrypter;
     _writer    = writer;
 }