Metadata for a keyset
コード例 #1
0
        /// <summary>
        /// Returns keyset with only the public keys.
        /// </summary>
        /// <returns></returns>
        public MutableKeySet PublicKey()
        {
            if (Metadata.Kind != KeyKind.Private)
            {
                return(null);
            }

            var newMeta = new KeyMetadata(Metadata);

            newMeta.Purpose = newMeta.Purpose == KeyPurpose.SignAndVerify
                                  ? KeyPurpose.Verify
                                  : KeyPurpose.Encrypt;

#pragma warning disable 618
            newMeta.KeyType = null; //Keytype only matters to old style empty keysets
#pragma warning restore 618

            newMeta.Kind = KeyKind.Public;

            var copiedKeys = _keys.Select(p => new { p.Key, ((IPrivateKey)p.Value).PublicKey })
                             .Select(p => new { p.Key, Type = p.PublicKey.KeyType, Value = this.GetConfig().RawStringEncoding.GetBytes(p.PublicKey.ToJson()) })
                             .Select(p => new { p.Key, Value = Key.Read(p.Type, p.Value, Config) }).ToList();


            //Update versions to public key type
            foreach (var key in copiedKeys)
            {
                var newVersion = newMeta.Versions.Single(it => it.VersionNumber == key.Key);
                newVersion.KeyType = key.Value.KeyType;
            }

            return(new MutableKeySet(newMeta, copiedKeys.ToDictionary(k => k.Key, v => v.Value)));
        }
コード例 #2
0
        /// <summary>
        /// Returns keyset with only the public keys.
        /// </summary>
        /// <returns></returns>
        public MutableKeySet PublicKey()
        {
            if (!typeof(IPrivateKey).IsAssignableFrom(Metadata.KeyType.RepresentedType))
            {
                return(null);
            }

            var newMeta = new KeyMetadata(Metadata);

            newMeta.Purpose = newMeta.Purpose == KeyPurpose.SignAndVerify
                                  ? KeyPurpose.Verify
                                  : KeyPurpose.Encrypt;

            var copiedKeys = _keys.Select(p => new { p.Key, ((IPrivateKey)p.Value).PublicKey })
                             .Select(
                p =>
                new
            {
                p.Key,
                Type  = p.PublicKey.KeyType,
                Value = Keyczar.RawStringEncoding.GetBytes(p.PublicKey.ToJson())
            })
                             .Select(p => new { p.Key, Value = Key.Read(p.Type, p.Value) });

            newMeta.KeyType = copiedKeys.Select(it => it.Value.KeyType).First();

            return(new MutableKeySet(newMeta, copiedKeys.ToDictionary(k => k.Key, v => v.Value)));
        }
コード例 #3
0
        public void Write(KeyMetadata metadata)
        {
            CreateDir();
            var meta_file = Path.Combine(_location, "meta");
            var file      = meta_file + ".temp";

            if (!_overwrite && File.Exists(meta_file))
            {
                success = false;
                return;
            }
            try
            {
                _filePaths.Add(file);
                using (var stream = new FileStream(file, FileMode.Create))
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(metadata.ToJson());
                    }
            }
            catch (Exception ex)
            {
                _exceptions.Add(ex);
                success = false;
            }
        }
コード例 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MutableKeySet" /> class.
 /// </summary>
 /// <param name="emptyKeySet">The metadata of an empty key set.</param>
 /// <exception cref="InvalidKeySetException">Only empty key sets can be created using just the KeyMetadata.</exception>
 public MutableKeySet(KeyMetadata emptyKeySet)
 {
     _metadata = emptyKeySet;
     if (_metadata.Versions.Any())
     {
         throw new InvalidKeySetException("Only empty key sets can be created using just the KeyMetadata.");
     }
 }
コード例 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MutableKeySet" /> class.
 /// </summary>
 /// <param name="emptyKeySet">The metadata of an empty key set.</param>
 /// <exception cref="InvalidKeySetException">Only empty key sets can be created using just the KeyMetadata.</exception>
 public MutableKeySet(KeyMetadata emptyKeySet)
 {
     _metadata = emptyKeySet;
     if (_metadata.Versions.Any())
     {
         throw new InvalidKeySetException("Only empty key sets can be created using just the KeyMetadata.");
     }
 }
コード例 #6
0
ファイル: KeyMetadata.cs プロジェクト: dwat001/keyczar-dotnet
 /// <summary>
 /// Initializes a new instance of the <see cref="KeyMetadata"/> class.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 public KeyMetadata(KeyMetadata metadata)
 {
     Name      = metadata.Name ?? String.Empty;
     Purpose   = metadata.Purpose;
     KeyType   = metadata.KeyType;
     Encrypted = metadata.Encrypted;
     Versions  = metadata.Versions.Select(it => new KeyVersion(it)).ToList();
 }
コード例 #7
0
ファイル: KeyMetadata.cs プロジェクト: jbtule/keyczar-dotnet
 /// <summary>
 /// Initializes a new instance of the <see cref="KeyMetadata"/> class.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 public KeyMetadata(KeyMetadata metadata)
 {
     Name = metadata.Name ?? String.Empty;
     Purpose = metadata.Purpose;
     KeyType = metadata.KeyType;
     Encrypted = metadata.Encrypted;
     Versions = metadata.Versions.Select(it => new KeyVersion(it)).ToList();
 }
コード例 #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)
 {
     foreach (var keyPair in _keys)
     {
         keyPair.Value.SafeDispose();
     }
     _keys.Clear();
     _metadata = null;
 }
コード例 #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MutableKeySet"/> class.
        /// </summary>
        /// <param name="keySet">The key set.</param>
        public MutableKeySet(IKeySet keySet)
        {
            _metadata = keySet.Metadata;

            foreach (var version in keySet.Metadata.Versions)
            {
                //Easy way to deep copy keys
                var keyData = keySet.GetKeyData(version.VersionNumber);
                var key     = Key.Read(_metadata.KeyType, keyData);
                keyData.Clear();
                _keys.Add(version.VersionNumber, key);
            }
        }
コード例 #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MutableKeySet"/> class.
        /// </summary>
        /// <param name="keySet">The key set.</param>
        public MutableKeySet(IKeySet keySet)
        {
            _metadata = keySet.Metadata;

            foreach (var version in keySet.Metadata.Versions)
            {
                //Easy way to deep copy keys
                var keyData = keySet.GetKeyData(version.VersionNumber);
                var key = Key.Read(_metadata.KeyType, keyData);
                keyData.Clear();
                _keys.Add(version.VersionNumber, key);
            }
        }
コード例 #11
0
ファイル: KeyMetadata.cs プロジェクト: jbtule/keyczar-dotnet
        /// <summary>
        /// Initializes a new instance of the <see cref="KeyMetadata"/> class.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        public KeyMetadata(KeyMetadata metadata)
        {
            Name      = metadata.Name ?? String.Empty;
            Purpose   = metadata.Purpose;
            Encrypted = metadata.Encrypted;
            Versions  = metadata.Versions.Select(it => new KeyVersion(it)).ToList();
#pragma warning disable 618
            KeyType = metadata.KeyType;
#pragma warning restore 618
            OriginallyOfficial = metadata.OriginallyOfficial;
            Kind = metadata.Kind;

            Format = KeyczarConst.MetaDataFormat;
        }
コード例 #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="KeyMetadata"/> class.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        public OfficialKeyMetadata(KeyMetadata metadata)
        {
            Name    = metadata.Name ?? String.Empty;
            Purpose = metadata.Purpose;
#pragma warning disable 618
            KeyType = metadata.KeyType;
#pragma warning restore 618

            if (!metadata.ValidOfficial())
            {
                throw new InvalidDataException("Official KeySet must only have one keytype");
            }

            KeyType = metadata.OfficialKeyType();


            Encrypted = metadata.Encrypted;
            Versions  = metadata.Versions.Select(it => new OfficialKeyVersion(it)).ToList();
        }
コード例 #13
0
 /// <summary>
 /// Writes the specified metadata.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 public void Write(KeyMetadata metadata)
 {
     metadata.Encrypted = true;
     _writer.Write(metadata);
 }
コード例 #14
0
ファイル: KeySetWriter.cs プロジェクト: jbtule/keyczar-dotnet
 public void Write(KeyMetadata metadata)
 {
     CreateDir();
     var meta_file = Path.Combine(_location, "meta");
     var file = meta_file + ".temp";
     if (!_overwrite && File.Exists(meta_file))
     {
         success = false;
         return;
     }
     try
     {
         _filePaths.Add(file);
         using (var stream = new FileStream(file, FileMode.Create))
         using (var writer = new StreamWriter(stream))
         {
             writer.Write(metadata.ToJson());
         }
     }
     catch (Exception ex)
     {
         _exceptions.Add(ex);
         success = false;
     }
 }
コード例 #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MutableKeySet"/> class.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 /// <param name="keys">The keys.</param>
 protected MutableKeySet(KeyMetadata metadata, IDictionary <int, Key> keys)
 {
     _metadata       = metadata;
     _keys           = keys;
     onlyMetaChanged = false;
 }
コード例 #16
0
        /// <summary>
        /// Returns keyset with only the public keys.
        /// </summary>
        /// <returns></returns>
        public MutableKeySet PublicKey()
        {
            if (!typeof (IPrivateKey).IsAssignableFrom(Metadata.KeyType.RepresentedType))
            {
                return null;
            }

            var newMeta = new KeyMetadata(Metadata);
            newMeta.Purpose = newMeta.Purpose == KeyPurpose.SignAndVerify
                                  ? KeyPurpose.Verify
                                  : KeyPurpose.Encrypt;

            var copiedKeys = _keys.Select(p => new {p.Key, ((IPrivateKey) p.Value).PublicKey})
                                  .Select(
                                      p =>
                                      new
                                          {
                                              p.Key,
                                              Type = p.PublicKey.KeyType,
                                              Value = Keyczar.RawStringEncoding.GetBytes(p.PublicKey.ToJson())
                                          })
                                  .Select(p => new {p.Key, Value = Key.Read(p.Type, p.Value)});

            newMeta.KeyType = copiedKeys.Select(it => it.Value.KeyType).First();

            return new MutableKeySet(newMeta, copiedKeys.ToDictionary(k => k.Key, v => v.Value));
        }
コード例 #17
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)
 {
     foreach (var keyPair in _keys)
     {
         keyPair.Value.SafeDispose();
     }
     _keys.Clear();
     _metadata = null;
 }
コード例 #18
0
ファイル: IKeySet.cs プロジェクト: jbtule/keyczar-dotnet
 public static KeyVersion GetPrimaryKeyVersion(this KeyMetadata metadata)
 {
     return(metadata.Versions.SingleOrDefault(it => it.Status == KeyStatus.Primary));
 }
コード例 #19
0
 /// <summary>
 /// Writes the specified metadata.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 public void Write(KeyMetadata metadata)
 {
     metadata.Encrypted = true;
     _writer.Write(metadata);
 }
コード例 #20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MutableKeySet"/> class.
 /// </summary>
 /// <param name="metadata">The metadata.</param>
 /// <param name="keys">The keys.</param>
 protected MutableKeySet(KeyMetadata metadata, IDictionary<int, Key> keys)
 {
     _metadata = metadata;
     _keys = keys;
     onlyMetaChanged = false;
 }