-
Notifications
You must be signed in to change notification settings - Fork 3
/
DlgCertification.cpp
207 lines (180 loc) · 5.86 KB
/
DlgCertification.cpp
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
// CDlgCertification.cpp : 実装ファイル
//
#include "stdafx.h"
#include "Sazabi.h"
#include "DlgCertification.h"
#include "afxdialogex.h"
// CDlgCertification ダイアログ
IMPLEMENT_DYNAMIC(CDlgCertification, CDialogEx)
CDlgCertification::CDlgCertification(CWnd* pParent /*=nullptr*/)
: CDlgCertification(CefString(""),
std::vector<CefRefPtr<CefX509Certificate>>(),
pParent)
{
}
CDlgCertification::CDlgCertification(CefString host,
std::vector<CefRefPtr<CefX509Certificate>> certificates,
CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_DLG_CERTIFICATION, pParent)
{
m_selectedIndex = 0;
m_host = host;
m_certificates = certificates;
}
CDlgCertification::~CDlgCertification()
{
}
void CDlgCertification::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_CERTIFICATION_COMBO, certificationComboBox);
}
CString CDlgCertification::GetSerialNumberAsHexString(const CefRefPtr<CefX509Certificate> certificate)
{
auto serialNumber = certificate->GetSerialNumber();
auto size = serialNumber->GetSize();
CString serialNumberAsHexString;
for (size_t i = 0; i < size; i++)
{
short buf = 0;
int gotSize = serialNumber->GetData(&buf, 1, i);
if (gotSize == 0)
break;
CString hex;
hex.Format(_T("%02X"), buf);
if (i > 0)
{
serialNumberAsHexString += ":";
}
serialNumberAsHexString += hex;
}
return serialNumberAsHexString;
}
CString CDlgCertification::GetTimeString(const CefTime& value)
{
if (value.GetTimeT() == 0)
return "*";
if (value.month < 1 || value.month > 12)
return "*";
CString timeString;
// CefTime is UTC.
timeString.Format(_T("%d-%02d-%02dT%02d:%02d:%02dZ"),
value.year,
value.month,
value.day_of_month,
value.hour,
value.minute,
value.second);
return timeString;
}
CString CDlgCertification::GetPrincipalString(const CefRefPtr<CefX509CertPrincipal> principal)
{
CString principalString;
std::vector<CefString> values;
principalString += _T("CN=");
principalString += (LPCWSTR)principal->GetCommonName().c_str();
principalString += _T(", O=");
principal->GetOrganizationNames(values);
for (size_t i = 0; i < values.size(); i++)
{
CefString value = values[i];
principalString += (LPCWSTR)value.c_str();
if (i > 0)
{
principalString += _T(" ");
}
}
principalString += _T(", OU=");
principal->GetOrganizationUnitNames(values);
for (size_t i = 0; i < values.size(); i++)
{
CefString value = values[i];
principalString += (LPCWSTR)value.c_str();
if (i > 0)
{
principalString += _T(" ");
}
}
#if CHROME_VERSION_MAJOR < 115
principalString += _T(", STREET=");
principal->GetStreetAddresses(values);
for (size_t i = 0; i < values.size(); i++)
{
CefString value = values[i];
principalString += (LPCWSTR)value.c_str();
if (i > 0)
{
principalString += _T("-");
}
}
#endif
principalString += _T(", L=");
principalString += (LPCWSTR)principal->GetLocalityName().c_str();
principalString += _T(", ST=");
principalString += (LPCWSTR)principal->GetStateOrProvinceName().c_str();
principalString += _T(", C=");
principalString += (LPCWSTR)principal->GetCountryName().c_str();
return principalString;
}
int CDlgCertification::SelectedIndex()
{
return m_selectedIndex;
}
BEGIN_MESSAGE_MAP(CDlgCertification, CDialogEx)
ON_CBN_SELCHANGE(IDC_CERTIFICATION_COMBO, &CDlgCertification::OnCbnSelchangeCertificationCombo)
ON_BN_CLICKED(IDOK, &CDlgCertification::OnBnClickedOk)
END_MESSAGE_MAP()
// CDlgCertification メッセージ ハンドラー
void CDlgCertification::OnCbnSelchangeCertificationCombo()
{
int curSel = certificationComboBox.GetCurSel();
CefRefPtr<CefX509Certificate> certificate = m_certificates[curSel];
CString certificationDetail;
CString title;
title.LoadString(ID_CERTIFICATION_ISSUER);
certificationDetail += title;
certificationDetail += GetPrincipalString(certificate->GetIssuer());
certificationDetail += _T("\r\n");
title.LoadString(ID_CERTIFICATION_SUBJECT);
certificationDetail += title;
certificationDetail += GetPrincipalString(certificate->GetSubject());
certificationDetail += _T("\r\n");
title.LoadString(ID_CERTIFICATION_SERIAL_NUMBER);
certificationDetail += title;
certificationDetail += GetSerialNumberAsHexString(certificate);
CefTime validStart;
cef_time_from_basetime(certificate->GetValidStart(), &validStart);
CString validStartTimeString = GetTimeString(validStart);
CefTime validExpiry;
cef_time_from_basetime(certificate->GetValidExpiry(), &validExpiry);
CString validExpiryString = GetTimeString(validExpiry);
certificationDetail += _T("\r\n");
title.LoadString(ID_CERTIFICATION_VALID_PERIOD);
certificationDetail += title + validStartTimeString + _T(" - ") + validExpiryString;
SetDlgItemText(IDC_CERTIFICATION_EDIT, certificationDetail);
}
void CDlgCertification::OnBnClickedOk()
{
// DoModalなどで結果を返し終えた後、certificationComboBoxなどが解放されてしまって参照できない。
// なので、このタイミングでクラス変数に結果を代入しておく。
m_selectedIndex = certificationComboBox.GetCurSel();
CDialogEx::OnOK();
}
BOOL CDlgCertification::OnInitDialog()
{
BOOL superResult = CDialogEx::OnInitDialog();
SetDlgItemText(IDC_CERTIFICATE_STATIC_SITE_INFO, (LPCWSTR)m_host.c_str());
for (CefRefPtr<CefX509Certificate> certificate : m_certificates)
{
CString serialNumber = GetSerialNumberAsHexString(certificate);
CString displayItemName;
displayItemName.Format(
_T("%s [%s]"),
(LPCWSTR)certificate->GetSubject()->GetDisplayName().c_str(),
(LPCTSTR)serialNumber);
certificationComboBox.AddString(displayItemName);
}
certificationComboBox.SetCurSel(0);
OnCbnSelchangeCertificationCombo();
return superResult;
}