您的位置:首页 > 编程语言 > C语言/C++

c++builder 编译 openssl

2016-09-10 18:25 253 查看
原文地址:http://www.rudeserver.com/ssl/openssltutorial.html

防止原文丢失,特在此拷贝一份,thanksMattFlood

NOTES:

Here'sexactlywhatIdidfromstarttofinishinordertobuildasimpleHTTPSclientusingopensslandBorlandC++Builder.InstallingopensslisdocumentedinINSTALL.W32,includedinthesourcedistribution,butI'vedocumentedmyexperience
here.....


YouShouldhaveBorlandC++Builderinstalledonyoursystem(I'musingC++Builder5).
YouShouldhaveautilitytodecode.tar.gzfiles(winzip,zipmagic,tar,gzip,etc...)
Downloadtheopensslsourcefromhere:

http://www.openssl.org/source/
Idownloadedhttp://www.openssl.org/source/openssl-0.9.6c.tar.gz

unzipanduntarthesourcecode.

afterthat,mysourcecodeendedupinafolder:

C:\DocumentsandSettings\poopoohead\Desktop\ssl\openssl\openssl-0.9.6c

YouneedtoinstallActiveStateperlifyoudon'talreadyhaveit...

DownloadthelateststablereleaseActivePerlfromhttp://www.activestate.com/
Whenyouinstallit,justmakesureeveryboxischeckedoneverydialoguethatpopsuptomakesurethe

environmentissetupcompletely

openupacommandprompt(>).
ThisstepistomakesureyouhavepathssetupforBorland'smakeandcompilerprograms.Runthefollowingtwocommandsandmakesuretheyarerecognizedcommands.Ifyoudon'tknowwhatImean-type>dingo666,andyou'llseewhatanunrecognizedcommand
lookslike.Ifthefollowingcommandsareunrecognized,you'llneedtomodify

yoursystem'sPATHvariable.Ididn'thaveto.

>make
>bcc32

Navigatetothebasedirectoryoftheopensslsource

>cdC:\DocumentsandSettings\poopoohead\Desktop\ssl\openssl\openssl-0.9.6c

Runthefollowingtocreateyourmakefile:

>ms\bcb4.bat

runmaketocompletethebuild.

>make-fbcb.mak

ifmakeransuccessfully,youshouldnowhave3newfoldersundertheinstalldir:

inc32(thisiswheretheincludefilesare....)
out32(thisiswherethe.libsare)
tmp32(thisisnothingtobeconcernedwith)

Nowyou'rereadytobuildansslapplication!!
PowerupBorlandC++Builder
Createanewapp(shouldalreadybeanewappreadytoworkwith...)
AddaMemoobjecttothemainform(leavedefaultname:Memo1),thisisjustsowecanseetheresultsortherequest.
Youneedtoaddtheopensslincludedirectoryandlibrariestoyourproject

Fromthemenu:Project->Options(orjusttypeshift+ctrl+F11)
Clickthe"Directories/Conditionals"Tab
Addtheincludedirectory:

clicktheelipses[...]buttontothefarrightof"includepath"
clicktheelipses[...]buttonthatappearsonthepopupdialogthatappears
navigatetoyourinc32folder
makesure"inc32"ishighlighted,NOTthe"openssl"subdirectory.....
click[OK]toselectthefolder
Clickthe[Add]button,then[OK]

Addtheopenssllibrarypath(I'mnotsurethisstepisneccesary,butIdiditanyway....):

Dothesamethingyoudidfortheincludedirectoryabove,adding"out32"to"LibraryPath"

Click[OK]toclosethepropertiesdialog
Nowaddthessllibrariestoyourproject:

Fromthemenu:Project->AddtoProject(orjusttypeshift+F11)
Makesure"FilesofType"issetto"LibrayFile(.lib)"
Navigatetoyourout32folder
selectalllibraryfilesthatappearinthatdirectorylisting,thenclick[Open]button

Addthefollowingincludedirectivestotheapplication:
#include<openssl/crypto.h>

#include<openssl/x509.h>

#include<openssl/pem.h>

#include<openssl/ssl.h>

#include<openssl/err.h>
CreateaSOCKETandconnecttoanhttpsserveronport443likeyounormallywould....
Assumingyoursocketisnamedsock,youthenperformyoursslcommunicationslikethis:



SOCKETsock;
//buildregularSOCK_STREAMsockandconnecttoserver
//...(I'mnotgoingintothesedetails)
//pretendsockisnowconnected....

//here'sthedataitemswe'lluse....
charbuf[1000];
charrequest[1000];

SSL_CTX*ctx;
SSL*ssl;
interr;

//initializeSSLstuff
//
SSL_load_error_strings();
SSL_library_init();

//buildtheSSLobjects...
//
ctx=SSL_CTX_new(SSLv2_client_method());
ssl=SSL_new(ctx);

//assignthesocketyoucreatedforSSLtouse
//
SSL_set_fd(ssl,[b]sock);

//communicate!!
/////////////////////////////////////////////
err=SSL_connect(ssl);
sprintf(request,
"GET%sHTTP/1.0\r\nHost:%s\r\n\r\n","/",
"www.theserver.com");
err=SSL_write(ssl,request,strlen(request));
while(1)
{
intread_size;
read_size=SSL_read(ssl,buf,sizeof(buf)-1);
buf[read_size]='\0';
if(read_size>0)
{
//I'massumingyouhaveaMemoobject
//onyourapplicationform...
//
Memo1->SetSelTextBuf(buf);
}
else
{
break;
}
}

//freetheSSLstuff....
//
SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx);[/b]


That'sit,buildandruntheprogram...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ builder openssl