📄 genkeys.c
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/EnhPi3/genkeys.c,v $
* $Date: 2003/05/29 16:43:31 $
*
Description:
\*____________________________________________________________________________*/
/* $SourceTop:$ */
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "Shlwapi.h"
#include <openssl/pem.h>
#include <openssl/conf.h>
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include "resrc1.h"
#include "Dialogs.h"
#include "Config.h"
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static HWND hWndDlgOK = 0;
static HWND hWndDlgKeyFile = 0;
static HWND hWndDlgCertFile = 0;
static HWND hWndDlgKeyLen = 0;
static HWND hWndDlgDays = 0;
static HWND hWndDlgPassword = 0;
static HWND hWndDlgRetype = 0;
static HWND hWndDlgCountry = 0;
static HWND hWndDlgCN = 0;
static HWND hWndDlgO = 0;
static HWND hWndDlgOU = 0;
static HWND hWndDlgServer = 0;
static HWND hWndDlgComment = 0;
static HWND hWndDlgStatus = 0;
static HWND hWndDlgProgress = 0;
static DlgData *pDD = 0;
int do_generate_cert( void *password, DlgData *pDD );
static BOOL CALLBACK GenSslKeysDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam,
LPARAM lParam)
{
enum { BUF_SIZE=1023 };
char szBuf[BUF_SIZE+1];
FILE *fp;
switch (uMsg)
{
case WM_INITDIALOG:
pDD = (DlgData *)lParam;
hWndDlgOK = GetDlgItem( hDlg, IDOK );
hWndDlgKeyFile = GetDlgItem( hDlg, IDC_PKEYFILE );
hWndDlgCertFile = GetDlgItem( hDlg, IDC_CERTFILE );
hWndDlgKeyLen = GetDlgItem( hDlg, IDC_KEYLEN );
hWndDlgDays = GetDlgItem( hDlg, IDC_VALID );
hWndDlgPassword = GetDlgItem( hDlg, IDC_PASSWORD );
hWndDlgRetype = GetDlgItem( hDlg, IDC_CONFIRM );
hWndDlgCountry = GetDlgItem( hDlg, IDC_CUSTOM1 );
hWndDlgCN = GetDlgItem( hDlg, IDC_CUSTOM2 );
hWndDlgO = GetDlgItem( hDlg, IDC_CUSTOM3 );
hWndDlgOU = GetDlgItem( hDlg, IDC_CUSTOM4 );
hWndDlgServer = GetDlgItem( hDlg, IDC_SERVERSTAMP );
hWndDlgComment = GetDlgItem( hDlg, IDC_COMMENT );
hWndDlgStatus = GetDlgItem( hDlg, IDC_STATUS );
hWndDlgProgress = GetDlgItem( hDlg, IDC_PROGRESS );
assert( hWndDlgOK && hWndDlgKeyFile && hWndDlgCertFile );
assert( hWndDlgKeyLen && hWndDlgDays );
assert( hWndDlgPassword && hWndDlgRetype );
assert( hWndDlgCountry && hWndDlgCN );
assert( hWndDlgO && hWndDlgOU );
assert( hWndDlgServer && hWndDlgComment );
assert( hWndDlgStatus && hWndDlgProgress );
/*
** Limit edit field lengths, really just a sanity thing
*/
SendMessage( hWndDlgKeyFile, EM_SETLIMITTEXT, 256, 0 );
SendMessage( hWndDlgCertFile, EM_SETLIMITTEXT, 256, 0 );
SendMessage( hWndDlgCountry, EM_SETLIMITTEXT, 2, 0 );
SendMessage( hWndDlgCN, EM_SETLIMITTEXT, 256, 0 );
SendMessage( hWndDlgO, EM_SETLIMITTEXT, 256, 0 );
SendMessage( hWndDlgOU, EM_SETLIMITTEXT, 256, 0 );
SendMessage( hWndDlgServer, EM_SETLIMITTEXT, 256, 0 );
SendMessage( hWndDlgComment, EM_SETLIMITTEXT, 256, 0 );
SendMessage( hWndDlgStatus, EM_SETLIMITTEXT, 256, 0 );
SendMessage( hWndDlgKeyFile, WM_SETTEXT, 0, (LPARAM)pDD->pSslKeyFilename );
SendMessage( hWndDlgCertFile, WM_SETTEXT, 0, (LPARAM)pDD->pSslCertFilename );
SendMessage( hWndDlgCountry, WM_SETTEXT, 0, (LPARAM)"DE" );
SendMessage( hWndDlgCN, WM_SETTEXT, 0, (LPARAM)pDD->pSslServername );
SendMessage( hWndDlgO, WM_SETTEXT, 0, (LPARAM)"pi3.org" );
SendMessage( hWndDlgOU, WM_SETTEXT, 0, (LPARAM)"SSL demo certificate generator" );
SendMessage( hWndDlgServer, WM_SETTEXT, 0, (LPARAM)pDD->pSslServername );
SendMessage( hWndDlgComment, WM_SETTEXT, 0, (LPARAM)"This certificate has been generated by the Pi3Web Server" );
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"" );
SendMessage( hWndDlgKeyLen, TBM_SETRANGE, FALSE, (LPARAM)MAKELONG(512, 2048) );
SendMessage( hWndDlgKeyLen, TBM_SETTICFREQ, 256, 0 );
SendMessage( hWndDlgKeyLen, TBM_SETLINESIZE, 0, 512 );
SendMessage( hWndDlgKeyLen, TBM_SETPAGESIZE, 0, 512 );
SendMessage( hWndDlgKeyLen, TBM_SETPOS, TRUE, 1024 );
SendMessage( hWndDlgDays, TBM_SETRANGE, FALSE, (LPARAM)MAKELONG(365, (1095)) );
SendMessage( hWndDlgDays, TBM_SETTICFREQ, 182, 0 );
SendMessage( hWndDlgDays, TBM_SETLINESIZE, 0, 365 );
SendMessage( hWndDlgDays, TBM_SETPAGESIZE, 0, 365 );
SendMessage( hWndDlgDays, TBM_SETPOS, TRUE, 365 );
SendMessage( hWndDlgProgress, PBM_SETRANGE, 0, (LPARAM)MAKELONG(0, (100)) );
SendMessage( hWndDlgProgress, PBM_SETSTEP, 1, 0 ); // 0x00bbggrr
SendMessage( hWndDlgProgress, PBM_SETBARCOLOR, 0, (LPARAM)0x000000FF );
break;
case WM_COMMAND:
switch (GET_WM_COMMAND_ID(wParam, lParam))
{
case IDC_CUSTOM2: // CN
*szBuf = '\0';
SendMessage( hWndDlgCN, WM_GETTEXT, BUF_SIZE,(LPARAM)szBuf );
EnableWindow( hWndDlgOK, *szBuf );
break;
case IDOK:
/*
** check key file
*/
fp = fopen( pDD->pSslKeyFilename, "r" );
if ( fp )
{
MessageBox( hDlg, "Private key file with this name already exists. \
Please move away existing file.", "File Exists", MB_OK );
fclose( fp );
return (FALSE);
}
/*
** check certificate file
*/
fp = fopen( pDD->pSslCertFilename, "r" );
if ( fp )
{
MessageBox( hDlg, "Certificate file with this name already exists. \
Please move away existing file.", "File Exists", MB_OK );
fclose( fp );
return (FALSE);
}
/*
** check password
*/
*szBuf = '\0';
SendMessage( hWndDlgPassword, WM_GETTEXT, BUF_SIZE,(LPARAM)szBuf );
if ( *szBuf )
{
const char *pTmp = szBuf + strlen( szBuf ) + 1;
SendMessage( hWndDlgRetype, WM_GETTEXT, BUF_SIZE,(LPARAM)pTmp );
if (strcmp( szBuf, pTmp ))
{
MessageBox( hDlg, "You did not re-enter the correct password.",
"Password Error", MB_OK );
return (FALSE);
}
}
/*
** here we do the real stuff
*/
if ( do_generate_cert( szBuf, pDD ))
{
MessageBox( hDlg, "Generation of SSL key and certificate failed.",
"Key Generation Error", MB_OK );
return (FALSE);
}
else
{
MessageBox( hDlg, "SSL key and certificate has been successfully created!",
"Key Generation Success", MB_OK );
}
EndDialog( hDlg, 1 );
break;
case IDCANCEL:
EndDialog( hDlg, 0 );
break;
default:;
};
default:
return (FALSE);
};
return (TRUE);
}
/*____________________________________________________________________________*\
*
Description:
Definitions and global values
\*____________________________________________________________________________*/
DLGPROC fnGenSslKeys = (DLGPROC)GenSslKeysDlgProc;
int mkit(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int days);
int get_passwd(char *buf, int bufsize);
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int do_generate_cert( void *password, DlgData* pDD )
{
FILE *fp;
X509 *x509=NULL;
EVP_PKEY *pkey=NULL;
EVP_CIPHER *enc=NULL;
int iRet = 0;
int ec = 0;
enum { BUF_SIZE=1023 };
char szBuf[BUF_SIZE+1];
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Generate private key..." );
iRet = mkit(&x509,&pkey,
SendMessage( hWndDlgKeyLen, TBM_GETPOS, 0, 0 ),
0,
SendMessage( hWndDlgDays, TBM_GETPOS, 0, 0 ));
if ( iRet ) goto err;
if ( strlen( (const char *)password ) )
{
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Encrypt private key..." );
enc=EVP_des_ede3_cbc();
if ( !enc )
{
iRet = -1;
goto err;
}
}
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Write private key PEM file..." );
fp = fopen( pDD->pSslKeyFilename, "w" );
if ( fp )
{
iRet = (PEM_write_PrivateKey(fp,pkey,enc,NULL,0,NULL,password) == 1) ? 0 : -1;
fflush( fp );
fclose( fp );
}
else
{
iRet = -1;
goto err;
}
if ( iRet ) goto err;
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Write X.509 certificate PEM file..." );
fp = fopen( pDD->pSslCertFilename, "w" );
if ( fp )
{
iRet = (PEM_write_X509(fp,x509) == 1) ? 0 : -1;
fflush( fp );
fclose( fp );
}
else
{
iRet = -1;
goto err;
}
if (!iRet)
{
SendMessage( hWndDlgStatus, WM_SETTEXT, 0,
(LPARAM)"Key and certificate generation finished..." );
};
err:
if (iRet)
{
ec = ERR_get_error();
if (ec)
{
sprintf( szBuf, "Key and certificate generation failed with errorcode 0x%X.", ec );
}
else
{
ec = GetLastError();
sprintf( szBuf, "Key and certificate generation failed with system error 0x%X.", ec );
};
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)szBuf );
};
X509_free(x509);
EVP_PKEY_free(pkey);
return( iRet );
}
#ifdef WIN16
# define MS_CALLBACK _far _loadds
# define MS_FAR _far
#else
# define MS_CALLBACK
# define MS_FAR
#endif
static void MS_CALLBACK callback(int p, int n, void *arg)
{
static int primeq = 0;
switch ( p )
{
case 0:
if ( primeq )
{
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Generate prime Q..." );
}
else
{
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Generate prime P..." );
};
SendMessage( hWndDlgProgress, PBM_SETBARCOLOR, 0, (LPARAM)0x000000FF );
SendMessage( hWndDlgProgress, PBM_SETPOS, n, 0 );
break;
case 1:
if ( primeq )
{
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Generate prime Q..." );
}
else
{
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Generate prime P..." );
};
SendMessage( hWndDlgProgress, PBM_SETBARCOLOR, 0, (LPARAM)0x0000FF00 );
SendMessage( hWndDlgProgress, PBM_SETPOS, n, 0 );
break;
case 2:
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Rejected prime number..." );
SendMessage( hWndDlgProgress, PBM_SETPOS, 0, 0 );
break;
case 3:
if ( primeq )
{
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Found prime Q..." );
}
else
{
SendMessage( hWndDlgStatus, WM_SETTEXT, 0, (LPARAM)"Found prime P..." );
};
SendMessage( hWndDlgProgress, PBM_SETPOS, 100, 0 );
primeq = 1;
break;
default: ;
}
}
int mkit(x509p,pkeyp,bits,serial,days)
X509 **x509p;
EVP_PKEY **pkeyp;
int bits;
int serial;
int days;
{
X509 *x;
EVP_PKEY *pk;
RSA *rsa;
X509_NAME *name=NULL;
X509_NAME_ENTRY *ne=NULL;
X509_EXTENSION *ex=NULL;
enum { BUF_SIZE=1023 };
char szBuf[BUF_SIZE+1];
if ((pkeyp == NULL) || (*pkeyp == NULL))
{
if ((pk=EVP_PKEY_new()) == NULL)
{
abort();
return(-1);
}
}
else
pk= *pkeyp;
if ((x509p == NULL) || (*x509p == NULL))
{
if ((x=X509_new()) == NULL)
goto err;
}
else
x= *x509p;
rsa=RSA_generate_key(bits,RSA_F4,callback,NULL);
if (!EVP_PKEY_assign_RSA(pk,rsa))
{
abort();
goto err;
}
rsa=NULL;
X509_set_version(x,3);
ASN1_INTEGER_set(X509_get_serialNumber(x),serial);
X509_gmtime_adj(X509_get_notBefore(x),0);
X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days);
X509_set_pubkey(x,pk);
name=X509_get_subject_name(x);
/* This function creates and adds the entry, working out the
* correct string type and performing checks on its length.
* Normally we'd check the return value for errors...
*/
SendMessage( hWndDlgCountry, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
X509_NAME_add_entry_by_txt(name,"C",
MBSTRING_ASC, szBuf, -1, -1, 0);
SendMessage( hWndDlgO, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
X509_NAME_add_entry_by_txt(name,"O",
MBSTRING_ASC, szBuf, -1, -1, 0);
SendMessage( hWndDlgOU, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
X509_NAME_add_entry_by_txt(name,"OU",
MBSTRING_ASC, szBuf, -1, -1, 0);
SendMessage( hWndDlgCN, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
X509_NAME_add_entry_by_txt(name,"CN",
MBSTRING_ASC, szBuf, -1, -1, 0);
X509_set_issuer_name(x,name);
/* Add extension using V3 code: we can set the config file as NULL
* because we wont reference any other sections. We can also set
* the context to NULL because none of these extensions below will need
* to access it.
*/
ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_cert_type, "server");
X509_add_ext(x,ex,-1);
X509_EXTENSION_free(ex);
SendMessage( hWndDlgComment, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_comment, szBuf);
X509_add_ext(x,ex,-1);
X509_EXTENSION_free(ex);
SendMessage( hWndDlgServer, WM_GETTEXT, BUF_SIZE, (LPARAM)szBuf );
ex = X509V3_EXT_conf_nid(NULL, NULL, NID_netscape_ssl_server_name, szBuf);
X509_add_ext(x,ex,-1);
X509_EXTENSION_free(ex);
if (!X509_sign(x,pk,EVP_md5()))
goto err;
*x509p=x;
*pkeyp=pk;
return(0);
err:
return(-1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -