public async Task Create(Credentials credentials, string name, DatabaseVersion version = DatabaseVersion.V4)
        {
            try
            {
                await Task.Run(() =>
                {
                    var compositeKey = CreateCompositeKey(credentials);
                    var ioConnection = IOConnectionInfo.FromByteArray(new byte[] {});

                    _pwDatabase.New(ioConnection, compositeKey);
                    _pwDatabase.Name           = name;
                    _pwDatabase.RootGroup.Name = name;

                    _credentials = credentials;

                    switch (version)
                    {
                    case DatabaseVersion.V4:
                        _pwDatabase.KdfParameters  = KdfPool.Get("Argon2").GetDefaultParameters();
                        _pwDatabase.DataCipherUuid = CipherPool.GlobalPool[1].CipherUuid;
                        break;
                    }
                });
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message, ex);
            }
        }
        private CompositeKey CreateCompositeKey(Credentials credentials)
        {
            var compositeKey = new CompositeKey();

            if (credentials.Password != null)
            {
                compositeKey.AddUserKey(new KcpPassword(credentials.Password));
            }
            if (credentials.KeyFileContents != null)
            {
                compositeKey.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromByteArray(credentials.KeyFileContents)));
            }
            return(compositeKey);
        }
Esempio n. 3
0
        private static void CreateXmlKeyFile(string strFile, byte[] pbKeyData)
        {
            Debug.Assert(strFile != null);
            if (strFile == null)
            {
                throw new ArgumentNullException("strFile");
            }
#endif
            Debug.Assert(pbKeyData != null);
            if (pbKeyData == null)
            {
                throw new ArgumentNullException("pbKeyData");
            }

#if ModernKeePassLib
            var fileContents = new byte[0];
            var ioc          = IOConnectionInfo.FromByteArray(fileContents);
#else
            IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile);
#endif
            using (Stream s = IOConnection.OpenWrite(ioc))
            {
                using (XmlWriter xw = XmlUtilEx.CreateXmlWriter(s))
                {
                    xw.WriteStartDocument();
                    xw.WriteStartElement(RootElementName);    // <KeyFile>

                    xw.WriteStartElement(MetaElementName);    // <Meta>
                    xw.WriteStartElement(VersionElementName); // <Version>
                    xw.WriteString("1.00");
                    xw.WriteEndElement();                     // </Version>
                    xw.WriteEndElement();                     // </Meta>

                    xw.WriteStartElement(KeyElementName);     // <Key>

                    xw.WriteStartElement(KeyDataElementName); // <Data>
                    xw.WriteString(Convert.ToBase64String(pbKeyData));
                    xw.WriteEndElement();                     // </Data>

                    xw.WriteEndElement();                     // </Key>

                    xw.WriteEndElement();                     // </KeyFile>
                    xw.WriteEndDocument();
                }
#if ModernKeePassLib
                return(((MemoryStream)s).ToArray());
#endif
            }
        }
        public async Task Open(byte[] file, Credentials credentials)
        {
            try
            {
                await Task.Run(() =>
                {
                    var compositeKey = CreateCompositeKey(credentials);
                    var ioConnection = IOConnectionInfo.FromByteArray(file);

                    _pwDatabase.Open(ioConnection, compositeKey, new NullStatusLogger());

                    _credentials = credentials;
                });
            }
            catch (InvalidCompositeKeyException ex)
            {
                throw new ArgumentException(ex.Message, ex);
            }
        }
Esempio n. 5
0
 public KcpKeyFile(byte[] keyFile)
 {
     Construct(IOConnectionInfo.FromByteArray(keyFile), false);
 }