home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / pwgenera.zip / pmkey.c < prev    next >
C/C++ Source or Header  |  1996-07-12  |  5KB  |  175 lines

  1.  
  2. #define INCL_WIN
  3. #define INCL_GPI
  4. #define    INCL_WINWINDOWMGR
  5. #define    INCL_WINMESSAGEMGR
  6. #define ID_WINDOW   256
  7. #define WM_USER_PROC_DLGS (WM_USER + 0)
  8. #define MAXCHALLENGELENGTH    256
  9. #define MAXPASSWORDLENGTH    256
  10. #define MAXRESPONSELENGTH    256
  11. #define MAXSEEDLENGTH        256
  12.  
  13.  
  14. #include <os2.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "key.h"
  19. #include "resource.h"
  20.  
  21. MRESULT EXPENTRY WndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  22. static void ProcessDialogBoxes(HWND hwnd);
  23. MRESULT EXPENTRY PMKeyDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  24. void CenterWindow(HWND hwnd);
  25.  
  26. HAB    hab;
  27.  
  28. int
  29. main(void)
  30. {
  31.     HMQ  hmq;                             /* Message queue handle         */
  32.     HWND hwndClient = NULLHANDLE;         /* Client area window handle    */
  33.     HWND hwndFrame = NULLHANDLE;          /* Frame window handle          */
  34.     QMSG qmsg;                            /* Message from message queue   */
  35.     ULONG flCreate;                       /* Window creation control flags*/
  36.     char szClientClass[] = "PMKey";
  37.  
  38.     hab = WinInitialize(0);
  39.     hmq = WinCreateMsgQueue(hab, 0);
  40.  
  41.     if (!WinRegisterClass(hab, szClientClass, (PFNWP)WndProc,
  42.             CS_SIZEREDRAW, 0))
  43.         return 0;
  44.  
  45.     flCreate = 0;
  46.  
  47.     if ((hwndFrame = WinCreateStdWindow(HWND_DESKTOP, 0, &flCreate,
  48.             szClientClass, "", 0, (HMODULE)0L, ID_WINDOW, &hwndClient)) == 0L)
  49.         return 0;
  50.  
  51.     WinSetWindowPos(hwndFrame, HWND_TOP, 0, 0, 0, 0, SWP_SIZE);
  52.  
  53.     while (WinGetMsg(hab, &qmsg, 0L, 0, 0))
  54.         WinDispatchMsg(hab, &qmsg);
  55.     WinDestroyWindow(hwndFrame);
  56.     WinDestroyMsgQueue(hmq);
  57.     WinTerminate(hab);
  58.     return 0;
  59. }
  60.  
  61. MRESULT EXPENTRY
  62. WndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  63. {
  64.     switch(msg)
  65.     {
  66.         case WM_CREATE:
  67.         {
  68.             /* Post a user defined message that will execute the dialog boxes*/
  69.             WinPostMsg( hwnd, WM_USER_PROC_DLGS, MPVOID, MPVOID); 
  70.             break;
  71.         }
  72.         case WM_USER_PROC_DLGS:
  73.         {
  74.             ProcessDialogBoxes(hwnd);
  75.             WinPostMsg(hwnd, WM_QUIT, (MPARAM)0, (MPARAM)0);
  76.             break;
  77.         }
  78.     }
  79.     return WinDefWindowProc(hwnd, msg, mp1, mp2);
  80. }
  81.  
  82. static void ProcessDialogBoxes(HWND hwnd)
  83. {
  84.     WinDlgBox(HWND_DESKTOP, hwnd, PMKeyDlgProc, 0L, ID_PMKEY_DLG, 0);
  85.     return;
  86. }
  87.  
  88. MRESULT EXPENTRY PMKeyDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  89. {
  90.     switch (msg) {
  91.         case WM_INITDLG:
  92.         {
  93.             /* Position the dialog box in the center of the screen */
  94.             CenterWindow(hwnd);
  95.             /* Set the maximum length for the challenge and the password */
  96.             WinSendDlgItemMsg(hwnd, ID_CHALLENGE_EDIT, EM_SETTEXTLIMIT,MPFROMSHORT(MAXCHALLENGELENGTH-1),MPVOID);
  97.             WinSendDlgItemMsg(hwnd, ID_PASSWORD_EDIT, EM_SETTEXTLIMIT,MPFROMSHORT(MAXPASSWORDLENGTH-1),MPVOID);
  98.             WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd, ID_CHALLENGE_EDIT));
  99.             return (MRESULT) TRUE;
  100.         }
  101.  
  102.         case WM_COMMAND:
  103.             switch(SHORT1FROMMP(mp1)) {
  104.  
  105.                 case ID_COMPUTE_BTN:
  106.                 {
  107.                     static char challenge[MAXCHALLENGELENGTH];
  108.                     static char password[MAXPASSWORDLENGTH];
  109.                     static char response[MAXRESPONSELENGTH];
  110.                     static char seed[MAXSEEDLENGTH];
  111.                     int algorithm;
  112.                     int keynum;
  113.  
  114.                     WinQueryDlgItemText(hwnd, ID_CHALLENGE_EDIT, 
  115.                         MAXCHALLENGELENGTH, challenge);
  116.                     WinQueryDlgItemText(hwnd, ID_PASSWORD_EDIT, 
  117.                         MAXPASSWORDLENGTH, password);
  118.  
  119.                     if (!KeyParseChallenge(challenge, 
  120.                             &algorithm, &keynum, seed))
  121.                     {
  122.                         WinMessageBox(HWND_DESKTOP, hwnd, 
  123.                             "Invalid OTP challenge.", "Error", 
  124.                             0, MB_OK | MB_ERROR);
  125.                         return (MRESULT) TRUE;
  126.                     }
  127.  
  128.                     if (algorithm == -1)
  129.                         algorithm = 4;
  130.  
  131.                     if (!KeyGenerateResponse(algorithm, keynum, seed, password,
  132.                             response))
  133.                     {
  134.                         WinMessageBox(HWND_DESKTOP, hwnd, 
  135.                             "Key crunch failed.", "Error", 
  136.                             0, MB_OK | MB_ERROR);
  137.                         return (MRESULT) TRUE;
  138.                     }
  139.  
  140.                     WinSetDlgItemText(hwnd, ID_RESPONSE_EDIT, response);
  141.  
  142.                     return (MRESULT) TRUE;
  143.                 }
  144.  
  145.                 case ID_EXIT_BTN:
  146.                 {
  147.                     WinDismissDlg(hwnd, 0);
  148.                     return (MRESULT) TRUE;
  149.                 }
  150.  
  151.                 default: return WinDefDlgProc(hwnd,msg,mp1,mp2);
  152.             }
  153.  
  154.  
  155.         default:
  156.             return WinDefDlgProc(hwnd, msg, mp1, mp2);
  157.         }
  158. }
  159.  
  160. void CenterWindow(HWND hwnd)
  161. {
  162.     RECTL rclRect1;
  163.     RECTL rclRect2;
  164.     int x,y;
  165.  
  166.     /* Position the dialog box in the center of the screen */
  167.     WinQueryWindowRect(HWND_DESKTOP, &rclRect1);
  168.     WinQueryWindowRect(hwnd, &rclRect2);
  169.     x = ((rclRect1.xRight - rclRect1.xLeft) - (rclRect2.xRight - rclRect2.xLeft)) / 2;
  170.     y = ((rclRect1.yTop - rclRect1.yBottom) - (rclRect2.yTop - rclRect2.yBottom)) / 2;
  171.     WinSetWindowPos(hwnd, HWND_TOP, x, y, (rclRect2.xRight - rclRect2.xLeft),
  172.     (rclRect2.yTop - rclRect2.yBottom), SWP_SIZE | SWP_MOVE | SWP_SHOW);
  173.     return;
  174. }
  175.