Skip to content

Commit

Permalink
Fix X509Certificate constructors from string
Browse files Browse the repository at this point in the history
- When parsing a string with the certificate need to keep the terminator in the binary representation.
- X509Certificate constructor with password was unnecessarily parsing the string to byte array twice.

Signed-off-by: José Simões <[email protected]>
  • Loading branch information
josesimoes committed Dec 12, 2018
1 parent 3d4a1a3 commit c1190b5
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,16 @@ public X509Certificate(byte[] certificate, string password)
/// </remarks>
public X509Certificate(string certificate)
{
_certificate = Encoding.UTF8.GetBytes(certificate);
var tempCertificate = Encoding.UTF8.GetBytes(certificate);

//////////////////////////////////////////////
// because this is parsing from a string //
// we need to keep the terminator //
//////////////////////////////////////////////
_certificate = new byte[tempCertificate.Length + 1];
Array.Copy(tempCertificate, _certificate, tempCertificate.Length);
_certificate[_certificate.Length - 1] = 0;

_password = "";

ParseCertificate(_certificate, _password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate);
Expand All @@ -108,10 +117,19 @@ public X509Certificate(string certificate)
/// </remarks>
public X509Certificate(string certificate, string password)
{
_certificate = Encoding.UTF8.GetBytes(certificate);
_password = password;

ParseCertificate(Encoding.UTF8.GetBytes(certificate), _password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate);
var tempCertificate = Encoding.UTF8.GetBytes(certificate);

//////////////////////////////////////////////
// because this is parsing from a string //
// we need to keep the terminator //
//////////////////////////////////////////////
_certificate = new byte[tempCertificate.Length + 1];
Array.Copy(tempCertificate, _certificate, tempCertificate.Length);
_certificate[_certificate.Length - 1] = 0;

ParseCertificate(_certificate, _password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate);
}

/// <summary>
Expand Down

0 comments on commit c1190b5

Please sign in to comment.