122 lines
No EOL
3.8 KiB
C++
122 lines
No EOL
3.8 KiB
C++
#include <iostream>
|
|
#include <fox-1.6/fx.h>
|
|
|
|
// NSPR include files
|
|
#include <prerror.h>
|
|
#include <prinit.h>
|
|
#include <prnetdb.h>
|
|
#include <nspr.h>
|
|
|
|
// NSS include files
|
|
#include <nss.h>
|
|
#include <pk11pub.h>
|
|
#include <secmod.h>
|
|
#include <ssl.h>
|
|
#include <sslproto.h>
|
|
#include <prio.h>
|
|
|
|
// PK11 Password Function typedef
|
|
typedef char *(*PK11PasswordFunc)(
|
|
PK11SlotInfo *slot,
|
|
PRBool retry,
|
|
void *arg);
|
|
|
|
int main(int argc, char* argv[]) {
|
|
// NSPR Init
|
|
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
|
|
|
|
// NSS Init
|
|
NSSInitContext *const nssContext =
|
|
NSS_InitContext("sql:/etc/pki/nssdb", "", "", "", NULL, NSS_INIT_READONLY | NSS_INIT_PK11RELOAD);
|
|
|
|
if(nssContext == NULL) {
|
|
const PRErrorCode err = PR_GetError();
|
|
fprintf(stderr, "NSSInitContext failed: Error code %d: %s\n", err, PR_ErrorToName(err));
|
|
exit(1);
|
|
}
|
|
|
|
// Ciphers to enable
|
|
static const PRUint16 goodCiphers[] = {
|
|
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
SSL_NULL_WITH_NULL_NULL // Sentinel value
|
|
};
|
|
|
|
PRBool foundGoodCipher = PR_FALSE;
|
|
for(const PRUint16 *p = goodCiphers; *p != SSL_NULL_WITH_NULL_NULL; ++p) {
|
|
PRInt32 policy;
|
|
if(SSL_CipherPolicyGet(*p, &policy) != SECSuccess) {
|
|
const PRErrorCode err = PR_GetError();
|
|
fprintf(stderr, "Error: Policy for cipher %u: error %d: %s\n",
|
|
(unsigned)*p, err, PR_ErrorToName(err));
|
|
exit(1);
|
|
}
|
|
if(policy == SSL_ALLOWED) {
|
|
// Policy allowed this cipher choice
|
|
fprintf(stderr, "Info: found cipher %x\n", (unsigned) *p);
|
|
foundGoodCipher = PR_TRUE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(!foundGoodCipher) {
|
|
if(NSS_SetDomesticPolicy() != SECSuccess) {
|
|
const PRErrorCode err = PR_GetError();
|
|
fprintf(stderr, "Error: NSS_SetDomesticPolicy: error %d: %s\n",
|
|
err, PR_ErrorToName(err));
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
// Initialize trusted certificate store
|
|
char module_name[] = "library=libnssckbi.so name=\"Root Certs\"";
|
|
SECMODModule *builtInRootsMod = SECMOD_LoadUserModule(module_name, NULL, PR_FALSE);
|
|
if(builtInRootsMod == NULL || !builtInRootsMod->loaded) {
|
|
const PRErrorCode err = PR_GetError();
|
|
fprintf(stderr, "Error: Loading built in roots module failed, code %d: %s\n",
|
|
err, PR_ErrorToName(err));
|
|
exit(1);
|
|
}
|
|
|
|
// Attempt to resolve a name.
|
|
PRAddrInfo *addrInfos = PR_GetAddrInfoByName("buyvm.theoks.net", PR_AF_UNSPEC, PR_AI_ADDRCONFIG);
|
|
void *iter = nullptr;
|
|
PRNetAddr firstAddr;
|
|
PRNetAddr tempAddr;
|
|
do {
|
|
iter = PR_EnumerateAddrInfo(iter, addrInfos, 0, &tempAddr);
|
|
char ipString[80];
|
|
PRStatus status = PR_NetAddrToString(&tempAddr, ipString, 75);
|
|
if(status == PR_FAILURE) {
|
|
const PRErrorCode err = PR_GetError();
|
|
fprintf(stderr, "Error: Converting PRNetAddr to string %d: %s\n",
|
|
err, PR_ErrorToName(err));
|
|
exit(1);
|
|
}
|
|
printf("Found IP Address: %s\n", ipString);
|
|
firstAddr = tempAddr;
|
|
} while(iter);
|
|
|
|
PRFileDesc *tcpSocket = PR_NewTCPSocket();
|
|
firstAddr.inet.port = 443;
|
|
PRFileDesc *nssSocket;
|
|
SSL_ImportFD(NULL, tcpSocket);
|
|
|
|
char *sendBuf = (char *)PR_MALLOC( 65536 * sizeof(char));
|
|
char *toSend = (char *) "GET / HTTP/1.0\r\nHost: buyvm.theoks.net\r\nUser-Agent: oksirc_net_test\r\n\r\n";
|
|
PR_Send(tcpSocket, sendBuf, null, 0, PR_INTERVAL_NO_TIMEOUT);
|
|
|
|
FXApp app("Hello", "FoxTest");
|
|
app.init(argc, argv);
|
|
|
|
FXMainWindow *mainWindow = new FXMainWindow(&app, "Hello", NULL, NULL, DECOR_ALL);
|
|
new FXButton(mainWindow, "&Hello World!", NULL, &app, FXApp::ID_QUIT);
|
|
app.create();
|
|
mainWindow->show(PLACEMENT_SCREEN);
|
|
int foxRetCode = app.run();
|
|
|
|
SECMOD_DestroyModule(builtInRootsMod);
|
|
NSS_ShutdownContext(nssContext);
|
|
|
|
return app.run();
|
|
} |