1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2017 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using KeePassLib.Interfaces;
using KeePassLib.Utility;
namespace KeePassLib.Serialization
{
public enum IOCredSaveMode
{
/// <summary>
/// Do not remember user name or password.
/// </summary>
NoSave = 0,
/// <summary>
/// Remember the user name only, not the password.
/// </summary>
UserNameOnly,
/// <summary>
/// Save both user name and password.
/// </summary>
SaveCred
}
public enum IOCredProtMode
{
None = 0,
Obf
}
/* public enum IOFileFormatHint
{
None = 0,
Deprecated
} */
public sealed class IOConnectionInfo : IDeepCloneable<IOConnectionInfo>
{
// private IOFileFormatHint m_ioHint = IOFileFormatHint.None;
private string m_strUrl = string.Empty;
public string Path
{
get { return m_strUrl; }
set
{
Debug.Assert(value != null);
if(value == null) throw new ArgumentNullException("value");
m_strUrl = value;
}
}
private string m_strUser = string.Empty;
[DefaultValue("")]
public string UserName
{
get { return m_strUser; }
set
{
Debug.Assert(value != null);
if(value == null) throw new ArgumentNullException("value");
m_strUser = value;
}
}
private string m_strPassword = string.Empty;
[DefaultValue("")]
public string Password
{
get { return m_strPassword; }
set
{
Debug.Assert(value != null);
if(value == null) throw new ArgumentNullException("value");
m_strPassword = value;
}
}
private IOCredProtMode m_ioCredProtMode = IOCredProtMode.None;
public IOCredProtMode CredProtMode
{
get { return m_ioCredProtMode; }
set { m_ioCredProtMode = value; }
}
private IOCredSaveMode m_ioCredSaveMode = IOCredSaveMode.NoSave;
public IOCredSaveMode CredSaveMode
{
get { return m_ioCredSaveMode; }
set { m_ioCredSaveMode = value; }
}
private bool m_bComplete = false;
[XmlIgnore]
public bool IsComplete // Credentials etc. fully specified
{
get { return m_bComplete; }
set { m_bComplete = value; }
}
/* public IOFileFormatHint FileFormatHint
{
get { return m_ioHint; }
set { m_ioHint = value; }
} */
private IocProperties m_props = new IocProperties();
[XmlIgnore]
public IocProperties Properties
{
get { return m_props; }
set
{
if(value == null) throw new ArgumentNullException("value");
m_props = value;
}
}
/// <summary>
/// For serialization only; use <c>Properties</c> in code.
/// </summary>
[DefaultValue("")]
public string PropertiesEx
{
get { return m_props.Serialize(); }
set
{
if(value == null) throw new ArgumentNullException("value");
IocProperties p = IocProperties.Deserialize(value);
Debug.Assert(p != null);
m_props = (p ?? new IocProperties());
}
}
public IOConnectionInfo CloneDeep()
{
IOConnectionInfo ioc = (IOConnectionInfo)this.MemberwiseClone();
ioc.m_props = m_props.CloneDeep();
return ioc;
}
#if DEBUG // For debugger display only
public override string ToString()
{
return GetDisplayName();
}
#endif
/*
/// <summary>
/// Serialize the current connection info to a string. Credentials
/// are serialized based on the <c>CredSaveMode</c> property.
/// </summary>
/// <param name="iocToCompile">Input object to be serialized.</param>
/// <returns>Serialized object as string.</returns>
public static string SerializeToString(IOConnectionInfo iocToCompile)
{
Debug.Assert(iocToCompile != null);
if(iocToCompile == null) throw new ArgumentNullException("iocToCompile");
string strUrl = iocToCompile.Path;
string strUser = TransformUnreadable(iocToCompile.UserName, true);
string strPassword = TransformUnreadable(iocToCompile.Password, true);
string strAll = strUrl + strUser + strPassword + "CUN";
char chSep = StrUtil.GetUnusedChar(strAll);
if(chSep == char.MinValue) throw new FormatException();
StringBuilder sb = new StringBuilder();
sb.Append(chSep);
sb.Append(strUrl);
sb.Append(chSep);
if(iocToCompile.CredSaveMode == IOCredSaveMode.SaveCred)
{
sb.Append('C');
sb.Append(chSep);
sb.Append(strUser);
sb.Append(chSep);
sb.Append(strPassword);
}
else if(iocToCompile.CredSaveMode == IOCredSaveMode.UserNameOnly)
{
sb.Append('U');
sb.Append(chSep);
sb.Append(strUser);
sb.Append(chSep);
}
else // Don't remember credentials
{
sb.Append('N');
sb.Append(chSep);
sb.Append(chSep);
}
return sb.ToString();
}
public static IOConnectionInfo UnserializeFromString(string strToDecompile)
{
Debug.Assert(strToDecompile != null);
if(strToDecompile == null) throw new ArgumentNullException("strToDecompile");
if(strToDecompile.Length <= 1) throw new ArgumentException();
char chSep = strToDecompile[0];
string[] vParts = strToDecompile.Substring(1, strToDecompile.Length -
1).Split(new char[]{ chSep });
if(vParts.Length < 4) throw new ArgumentException();
IOConnectionInfo s = new IOConnectionInfo();
s.Path = vParts[0];
if(vParts[1] == "C")
s.CredSaveMode = IOCredSaveMode.SaveCred;
else if(vParts[1] == "U")
s.CredSaveMode = IOCredSaveMode.UserNameOnly;
else
s.CredSaveMode = IOCredSaveMode.NoSave;
s.UserName = TransformUnreadable(vParts[2], false);
s.Password = TransformUnreadable(vParts[3], false);
return s;
}
*/
/*
/// <summary>
/// Very simple string protection. Doesn't really encrypt the input
/// string, only encodes it that it's not readable on the first glance.
/// </summary>
/// <param name="strToEncode">The string to encode/decode.</param>
/// <param name="bEncode">If <c>true</c>, the string will be encoded,
/// otherwise it'll be decoded.</param>
/// <returns>Encoded/decoded string.</returns>
private static string TransformUnreadable(string strToEncode, bool bEncode)
{
Debug.Assert(strToEncode != null);
if(strToEncode == null) throw new ArgumentNullException("strToEncode");
if(bEncode)
{
byte[] pbUtf8 = StrUtil.Utf8.GetBytes(strToEncode);
unchecked
{
for(int iPos = 0; iPos < pbUtf8.Length; ++iPos)
pbUtf8[iPos] += (byte)(iPos * 11);
}
return Convert.ToBase64String(pbUtf8);
}
else // Decode
{
byte[] pbBase = Convert.FromBase64String(strToEncode);
unchecked
{
for(int iPos = 0; iPos < pbBase.Length; ++iPos)
pbBase[iPos] -= (byte)(iPos * 11);
}
return StrUtil.Utf8.GetString(pbBase, 0, pbBase.Length);
}
}
*/
public string GetDisplayName()
{
string str = m_strUrl;
if(m_strUser.Length > 0)
str += (" (" + m_strUser + ")");
return str;
}
public bool IsEmpty()
{
return (m_strUrl.Length == 0);
}
public static IOConnectionInfo FromPath(string strPath)
{
IOConnectionInfo ioc = new IOConnectionInfo();
ioc.Path = strPath;
ioc.CredSaveMode = IOCredSaveMode.NoSave;
return ioc;
}
public bool CanProbablyAccess()
{
if(IsLocalFile()) return File.Exists(m_strUrl);
return true;
}
public bool IsLocalFile()
{
// Not just ":/", see e.g. AppConfigEx.ChangePathRelAbs
return (m_strUrl.IndexOf("://") < 0);
}
public void ClearCredentials(bool bDependingOnRememberMode)
{
if((bDependingOnRememberMode == false) ||
(m_ioCredSaveMode == IOCredSaveMode.NoSave))
{
m_strUser = string.Empty;
}
if((bDependingOnRememberMode == false) ||
(m_ioCredSaveMode == IOCredSaveMode.NoSave) ||
(m_ioCredSaveMode == IOCredSaveMode.UserNameOnly))
{
m_strPassword = string.Empty;
}
}
public void Obfuscate(bool bObf)
{
if(bObf && (m_ioCredProtMode == IOCredProtMode.None))
{
m_strPassword = StrUtil.Obfuscate(m_strPassword);
m_ioCredProtMode = IOCredProtMode.Obf;
}
else if(!bObf && (m_ioCredProtMode == IOCredProtMode.Obf))
{
m_strPassword = StrUtil.Deobfuscate(m_strPassword);
m_ioCredProtMode = IOCredProtMode.None;
}
}
}
}
|