c# - BouncyCastle IPasswordFinder memory management -
i'm writing small wrapper bouncycastle basic crypto tasks. have class reads pem file, , such supports password protected pem files. support passwords, overrode ipasswordfinder , pass pemreader. works fine, noticed 1 strange thing passwordfinder when analyzing code coverage report. class takes securestring, temporarily marshals bouncycastle can read it, , disposes of immediately. when dispose code gets called, however, buffer freed. wondering if bouncycastle disposing of buffer itself, or if else going on. code:
internal class securepasswordstore : ipasswordfinder, idisposable { private readonly securestring password; private char[] _chars; public securepasswordstore(securestring password) { this.password = password; } public unsafe char[] getpassword() { intptr ptr = intptr.zero; try { _chars = new char[password.length]; ptr = marshal.securestringtocotaskmemunicode(password); marshal.copy(ptr, _chars, 0, password.length); return _chars; } { if (ptr != intptr.zero) { marshal.zerofreecotaskmemunicode(ptr); } } } #region idisposable support private bool disposedvalue = false; // detect redundant calls protected virtual void dispose(bool disposing) { if (!disposedvalue) { if (disposing) { (var = 0; < _chars?.length; i++) { _chars[i] = default(char); } _chars = null; } disposedvalue = true; } } ~securepasswordstore() { dispose(false); } // code added correctly implement disposable pattern. public void dispose() { dispose(true); gc.suppressfinalize(this); } #endregion }
so essentially, getpassword gets called pemreader, securestring marshalled _chars array. when password store disposed, _chars array should nulled out , disposed of. however, time dispose method, _chars null again. can shed light on this?
Comments
Post a Comment