testtls.c
上传用户:knt0001
上传日期:2022-01-28
资源大小:264k
文件大小:2k
- #include <stdio.h>
- #include <dstring.h>
- #include <dnet.h>
- void
- getSmtpCommands(dsocket *sd)
- {
- int err;
- size_t wlen;
- char wbuf[4096]={0}, rbuf[4096]={0};
- while (1) {
- printf("smtp> ");
- fgets(wbuf, sizeof(wbuf), stdin);
- if (strncasecmp(wbuf, "quit", 4) == 0) {
- break;
- }
- wlen = strlen(wbuf);
- wbuf[wlen-1] = ' ';
- strncat(wbuf, "rn", sizeof(wbuf));
- wlen += 1;
- if (dnetWrite(sd, wbuf, wlen) == -1) {
- printf("SSL Write error.nt");
- break;
- }
- err = dnetRead(sd, rbuf, sizeof(rbuf)-1);
- if (err == -1) {
- printf("dnetRecv Error.n");
- break;
- }
- rbuf[err] = ' ';
- printf("<-- %sn", rbuf);
- memset(rbuf, ' ', sizeof(rbuf));
- memset(wbuf, ' ', sizeof(wbuf));
- }
- }
- int main(int argc, char **argv)
- {
- dsocket *sd=NULL;
- char *put=NULL;
- char *server=NULL;
- uint port;
- char buf[4096] = {0};
- if (argc != 3) {
- printf("%s server portn", argv[0]);
- exit(1);
- } else {
- server = argv[1];
- port = atoi(argv[2]);
- }
- // The next function doesn't have to be called unless we want
- // to specify our own get password function. Otherwise OpenSSL
- // will prompt the user on the cmd line.
- // SSL_CTX_set_default_passwd_cb(ctx, getPassword);
-
- /* END Init SSL/TLS */
- sd = dnetConnect(server, port);
- if (!sd) {
- perror("Coulnd't connect to server.");
- return 1;
- }
- dnetRead(sd, buf, sizeof(buf)-1);
- printf("<-- %s", buf);
- put = "EHLO snaghosting.comrn";
- dnetWrite(sd, put, strlen(put));
- printf("--> %s", put);
- memset(buf, ' ', sizeof(buf));
- dnetRead(sd, buf, sizeof(buf)-1);
- printf("<-- %sn", buf);
- put = "STARTTLSrn";
- dnetWrite(sd, put, strlen(put));
- printf("--> %s", put);
- memset(buf, ' ', sizeof(buf));
- dnetRead(sd, buf, sizeof(buf)-1);
- printf("--> %s", buf);
- memset(buf, ' ', sizeof(buf));
- if (dnetUseTls(sd) == ERROR) {
- printf("TLS Connection failed miserably.n");
- dnetClose(sd);
- return 1;
- }
- if (dnetVerifyCert(sd) == ERROR) {
- printf("Couldn't verify peer certificate.n");
- dnetClose(sd);
- return 1;
- }
- /* Do cert verification stuff here. */
- getSmtpCommands(sd);
- dnetWrite(sd, "QUITrn", 6);
- dnetClose(sd);
- return 0;
- }