Metadata for a keyset
Ejemplo n.º 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)));
        }
Ejemplo n.º 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)));
        }
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 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.");
     }
 }
Ejemplo n.º 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.");
     }
 }
Ejemplo n.º 6
0
 /// <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();
 }
Ejemplo n.º 7
0
 /// <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();
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 11
0
        /// <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;
        }
Ejemplo n.º 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();
        }
Ejemplo n.º 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);
 }
Ejemplo n.º 14
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;
     }
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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));
        }
Ejemplo n.º 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;
 }
Ejemplo n.º 18
0
 public static KeyVersion GetPrimaryKeyVersion(this KeyMetadata metadata)
 {
     return(metadata.Versions.SingleOrDefault(it => it.Status == KeyStatus.Primary));
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 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;
 }