/// <summary> /// Get the protected data as a byte array. Please note that the returned /// byte array is not protected and can therefore been read by any other /// applications. Make sure that your clear it properly after usage. /// </summary> /// <returns>Unprotected byte array. This is always a copy of the internal /// protected data and can therefore be cleared safely.</returns> public byte[] ReadData() { if (m_xbEncrypted != null) { byte[] pb = m_xbEncrypted.ReadPlainText(); SetData(pb); // Clear the XorredBuffer object return(pb ?? new byte[0]); } if (m_pbData.Length == 0) { return(new byte[0]); } if (m_bDoProtect && m_bProtectionSupported) { Debug.Assert((m_pbData.Length % 16) == 0); ProtectedMemory.Unprotect(m_pbData, MemoryProtectionScope.SameProcess); } byte[] pbReturn = new byte[m_uDataLen]; if (m_uDataLen > 0) { Array.Copy(m_pbData, pbReturn, (int)m_uDataLen); } if (m_bDoProtect && m_bProtectionSupported) { ProtectedMemory.Protect(m_pbData, MemoryProtectionScope.SameProcess); } return(pbReturn); }
/// <summary> /// Construct a new protected binary data object. Copy the data from /// a <c>XorredBuffer</c> object. /// </summary> /// <param name="bEnableProtection">Enable protection or not.</param> /// <param name="xbProtected"><c>XorredBuffer</c> object used to /// initialize the <c>ProtectedBinary</c> object.</param> /// <exception cref="System.ArgumentNullException">Thrown if the input /// parameter is <c>null</c>.</exception> public ProtectedBinary(bool bEnableProtection, XorredBuffer xbProtected) { Debug.Assert(xbProtected != null); if (xbProtected == null) { throw new ArgumentNullException("xbProtected"); } byte[] pb = xbProtected.ReadPlainText(); Init(bEnableProtection, pb); MemUtil.ZeroByteArray(pb); }
/// <summary> /// Construct a new protected binary data object. /// Copy the data from a <c>XorredBuffer</c> object. /// </summary> /// <param name="bEnableProtection">Enable protection or not.</param> /// <param name="xb"><c>XorredBuffer</c> object containing the data.</param> public ProtectedBinary(bool bEnableProtection, XorredBuffer xb) { if (xb == null) { Debug.Assert(false); throw new ArgumentNullException("xb"); } byte[] pb = xb.ReadPlainText(); try { Init(bEnableProtection, pb, 0, pb.Length); } finally { if (bEnableProtection) { MemUtil.ZeroByteArray(pb); } } }
/// <summary> /// Convert the protected string to a normal string object. /// Be careful with this function, the returned string object /// isn't protected any more and stored in plain-text in the /// process memory. /// </summary> /// <returns>Plain-text string. Is never <c>null</c>.</returns> public string ReadString() { if (m_xbEncrypted != null) { byte[] pb = m_xbEncrypted.ReadPlainText(); string str = StrUtil.Utf8.GetString(pb, 0, pb.Length); SetString(str); // Clear the XorredBuffer object // No need to erase the pb buffer, the plain text is // now readable in memory anyway (in str). return(str ?? string.Empty); } if (m_bIsProtected) { if (m_secString != null) { #if !KeePassLibSD IntPtr p = Marshal.SecureStringToGlobalAllocUnicode(m_secString); string str = Marshal.PtrToStringUni(p); Marshal.ZeroFreeGlobalAllocUnicode(p); #else string str = m_secString.ReadAsString(); #endif return(str ?? string.Empty); } else { return(m_strAlternativeSecString); } } return(m_strPlainText); // Unprotected string }
/// <summary> /// Construct a new protected string. The string is initialized /// to the value passed in the <c>XorredBuffer</c> object. /// </summary> /// <param name="bEnableProtection">Enable protection or not.</param> /// <param name="xbProtected"><c>XorredBuffer</c> object containing the /// string in UTF-8 representation. The UTF-8 string must not /// be <c>null</c>-terminated.</param> public ProtectedString(bool bEnableProtection, XorredBuffer xbProtected) { Debug.Assert(xbProtected != null); if(xbProtected == null) throw new ArgumentNullException("xbProtected"); byte[] pb = xbProtected.ReadPlainText(); Init(bEnableProtection, pb); if(bEnableProtection) MemUtil.ZeroByteArray(pb); }