1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2014 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "CheckCertificateDlg.h"
26 IMPLEMENT_DYNAMIC(CCheckCertificateDlg
, CStandAloneDialog
)
27 CCheckCertificateDlg::CCheckCertificateDlg(CWnd
* pParent
/*=NULL*/)
28 : CStandAloneDialog(CCheckCertificateDlg::IDD
, pParent
)
33 void CCheckCertificateDlg::DoDataExchange(CDataExchange
* pDX
)
35 CStandAloneDialog::DoDataExchange(pDX
);
36 DDX_Text(pDX
, IDC_ERROR
, m_sError
);
37 DDX_Text(pDX
, IDC_COMMONNAME
, m_sCertificateCN
);
38 DDX_Text(pDX
, IDC_ISSUER
, m_sCertificateIssuer
);
39 DDX_Text(pDX
, IDC_SHA1
, m_sSHA1
);
40 DDX_Text(pDX
, IDC_SHA256
, m_sSHA256
);
43 BEGIN_MESSAGE_MAP(CCheckCertificateDlg
, CStandAloneDialog
)
44 ON_BN_CLICKED(IDOK
, OnBnClickedOk
)
45 ON_BN_CLICKED(IDC_OPENCERT
, &CCheckCertificateDlg::OnBnClickedOpencert
)
48 void CCheckCertificateDlg::OnBnClickedOk()
53 static CString
getCertificateHash(HCRYPTPROV hCryptProv
, ALG_ID algId
, BYTE
* certificate
, size_t len
)
55 CString readable
= _T("unknown");
56 std::unique_ptr
<BYTE
[]> pHash(nullptr);
57 HCRYPTHASH hHash
= NULL
;
62 if (!CryptCreateHash(hCryptProv
, algId
, 0, 0, &hHash
))
65 if (!CryptHashData(hHash
, certificate
, (DWORD
)len
, 0))
69 DWORD hashLenLen
= sizeof(DWORD
);
70 if (!CryptGetHashParam(hHash
, HP_HASHSIZE
, (BYTE
*)&hashLen
, &hashLenLen
, 0))
73 pHash
.reset(new BYTE
[hashLen
]);
74 if (!CryptGetHashParam(hHash
, HP_HASHVAL
, pHash
.get(), &hashLen
, 0))
78 for (const BYTE
* it
= pHash
.get(); it
< pHash
.get() + hashLen
; ++it
)
81 tmp
.Format(L
"%02X", *it
);
82 if (!readable
.IsEmpty())
89 CryptDestroyHash(hHash
);
94 BOOL
CCheckCertificateDlg::OnInitDialog()
96 CStandAloneDialog::OnInitDialog();
97 CAppUtils::MarkWindowAsUnpinnable(m_hWnd
);
99 HCRYPTPROV hCryptProv
= 0;
100 CryptAcquireContext(&hCryptProv
, nullptr, nullptr, PROV_RSA_AES
, CRYPT_VERIFYCONTEXT
);
102 m_sSHA1
= getCertificateHash(hCryptProv
, CALG_SHA1
, (BYTE
*)cert
->data
, cert
->len
);
103 m_sSHA256
= getCertificateHash(hCryptProv
, CALG_SHA_256
, (BYTE
*)cert
->data
, cert
->len
);
104 if (m_sSHA256
.GetLength() > 57)
105 m_sSHA256
= m_sSHA256
.Left(57) + L
"\r\n" + m_sSHA256
.Mid(57);
107 CryptReleaseContext(hCryptProv
, 0);
110 error
.Format(IDS_ERR_SSL_VALIDATE
, m_sHostname
);
111 SetDlgItemText(IDC_ERRORDESC
, error
);
115 GetDlgItem(IDCANCEL
)->SetFocus();
120 void CCheckCertificateDlg::OnBnClickedOpencert()
122 CTGitPath tempFile
= CTempFiles::Instance().GetTempFilePath(true, CTGitPath(_T("certificate.der")));
126 CFile
file(tempFile
.GetWinPathString(), CFile::modeReadWrite
);
127 file
.Write(cert
->data
, (UINT
)cert
->len
);
130 catch (CFileException
* e
)
132 MessageBox(_T("Could not write to file."), _T("TortoiseGit"), MB_ICONERROR
);
137 CAppUtils::ShellOpen(tempFile
.GetWinPathString(), GetSafeHwnd());