summaryrefslogtreecommitdiff
path: root/src/windows/csp/gui.cpp
blob: 848217b1e8c4824c0bdcf9e0b8e7a643feeb9d8a (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
/** BEGIN COPYRIGHT BLOCK
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; version 2 of the License.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*
* Copyright (C) 2003-2004 Identity Alliance

* All rights reserved.
* END COPYRIGHT BLOCK **/

/*****************************************************************
/
/ File   :   gui.cpp
/ Date   :   December 3, 2002
/ Purpose:   Crypto API CSP->PKCS#11 Module
/ License:   Copyright (C) 2003-2004 Identity Alliance
/
******************************************************************/

#include "resource.h"
#include "csp.h"
#include <tchar.h>

using namespace std;

namespace MCSP {

static BOOL DoInitDialog(HWND hDlg, LPARAM lParam)
{
   RECT rect;
   int width, height;
   int screen_x, screen_y;
   int x, y;

   GetWindowRect(hDlg, &rect);
   width = rect.right - rect.left;
   height = rect.bottom - rect.top;

   screen_x = GetSystemMetrics(SM_CXSCREEN);
   screen_y = GetSystemMetrics(SM_CYSCREEN);

   x = screen_x/2 - width/2;
   y = screen_y/2 - height/2;

   SetWindowPos(hDlg, HWND_TOPMOST, x, y, width, height, SWP_NOSIZE);
   SetFocus(GetDlgItem(hDlg, IDC_PIN_EDIT));

   if (lParam == 0)
   {
      SetLastError(ERROR_INVALID_BLOCK);
      EndDialog(hDlg, 0);
      return TRUE;
   }

   // FIXME: Why does lParam need to be type-cast to LONG?
   //        The parameter is suppose to be LONG_PTR (LPARAM)
   SetWindowLongPtr(hDlg, GWLP_USERDATA, static_cast<LONG>(lParam));
   EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);

   return FALSE;
}

static BOOL DoPINChanged(HWND hDlg)
{
   HWND pinCtrl;
   BOOL enable = TRUE;

   pinCtrl = GetDlgItem(hDlg, IDC_PIN_EDIT);
   int len = GetWindowTextLength(pinCtrl);

   if (len == 0)
      enable = FALSE;

   EnableWindow(GetDlgItem(hDlg, IDOK), enable);

   return TRUE;
}

static BOOL DoPIN(HWND hDlg, WPARAM wParam)
{
   switch(HIWORD(wParam))
   {
   case EN_UPDATE:
      return DoPINChanged(hDlg);
      break;
   default:
      break;
   }

   return TRUE;
}

static void OnOK(HWND hDlg, LPARAM lParam)
{
   BinStr* s = reinterpret_cast<BinStr*>((LPARAM)GetWindowLongPtr(hDlg, GWLP_USERDATA));
   if (!s)
      return;

   HWND pinCtrl = GetDlgItem(hDlg, IDC_PIN_EDIT);
   int len = GetWindowTextLength(pinCtrl);

   s->resize(len + 1);
   GetWindowText(pinCtrl, reinterpret_cast<LPSTR>(&(*s)[0]), static_cast<int>(s->size()));
   
   // Chop off null cause we don't need it
   s->resize(s->size() - 1);

   EndDialog(hDlg, IDOK);
}

static BOOL DoCommand(HWND hDlg, WPARAM wParam, LPARAM lParam)
{
   switch(LOWORD(wParam))
   {
   case IDCANCEL:
      EndDialog(hDlg, IDCANCEL);
      break;
   case IDOK:
      OnOK(hDlg, lParam);
      break;
   case IDC_PIN_EDIT:
      return DoPIN(hDlg, wParam);
      break;
   default:
      break;
   }

   return TRUE;
}

static
INT_PTR CALLBACK PINDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
   switch (message)
   {
   case WM_COMMAND:
      return DoCommand(hDlg, wParam, lParam);
      break;
   case WM_INITDIALOG:
      return DoInitDialog(hDlg, lParam);
      break;
   default:
      break;
   }

   return FALSE;
}

// asks the user for a pin
bool DisplayPINDialog(BinStr* pin)
{
   INT_PTR result;

   result = DialogBoxParam(g_hModule, MAKEINTRESOURCE(IDD_PIN_DIALOG_1), NULL,
      PINDialogProc, reinterpret_cast<LPARAM>(pin));

   switch(result)
   {
   case 0:
      return false;
      break;
   case IDCANCEL:
      return false;
      break;
   case IDOK:
      if (pin->empty())
         return false;
      else
         return true;
      break;
   default:
      break;
   }

   return false;
}

// for debugging
void DisplayError(const Session* context, const string& str)
{
   if (!context->silent_)
      MessageBox(NULL, str.c_str(), PROVIDER_NAME" Error", MB_OK | MB_ICONERROR | MB_TASKMODAL);

   LOG("ERROR: \"%s\"\n", str.c_str());
}

// for debugging
void DisplayWin32Error(const Session* context)
{
   LPVOID lpMsgBuf;
   
   FormatMessage( 
      FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
      NULL,
      GetLastError(),
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
      (LPTSTR) &lpMsgBuf,
      0,
      NULL 
      );
   
   // Display the string
   if (!context->silent_)
      MessageBox(NULL, (LPCSTR)lpMsgBuf, PROVIDER_NAME" Win32 Error", MB_OK | MB_ICONERROR | MB_TASKMODAL);

   LOG("WIN32 error: \"%s\"\n", lpMsgBuf);
   
   // Free the buffer.
   LocalFree( lpMsgBuf );
}

} // namespace MCSP