VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/demos/bio/sconnect.c@ 108358

最後變更 在這個檔案從108358是 108206,由 vboxsync 提交於 5 週 前

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 3.0 KB
 
1/*
2 * Copyright 1998-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10/*-
11 * A minimal program to do SSL to a passed host and port.
12 * It is actually using non-blocking IO but in a very simple manner
13 * sconnect host:port - it does a 'GET / HTTP/1.0'
14 *
15 * cc -I../../include sconnect.c -L../.. -lssl -lcrypto
16 */
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21#include <errno.h>
22#include <openssl/err.h>
23#include <openssl/ssl.h>
24
25#define HOSTPORT "localhost:4433"
26#define CAFILE "root.pem"
27
28int main(int argc, char *argv[])
29{
30 const char *hostport = HOSTPORT;
31 const char *CAfile = CAFILE;
32 const char *hostname;
33 BIO *out = NULL;
34 char buf[1024 * 10], *p;
35 SSL_CTX *ssl_ctx = NULL;
36 SSL *ssl;
37 BIO *ssl_bio;
38 int i, len, off, ret = EXIT_FAILURE;
39
40 if (argc > 1)
41 hostport = argv[1];
42 if (argc > 2)
43 CAfile = argv[2];
44
45#ifdef WATT32
46 dbug_init();
47 sock_init();
48#endif
49
50 ssl_ctx = SSL_CTX_new(TLS_client_method());
51
52 /* Enable trust chain verification */
53 SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
54 SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL);
55
56 /* Lets make a SSL structure */
57 ssl = SSL_new(ssl_ctx);
58 SSL_set_connect_state(ssl);
59
60
61 /* Use it inside an SSL BIO */
62 ssl_bio = BIO_new(BIO_f_ssl());
63 BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
64
65 /* Lets use a connect BIO under the SSL BIO */
66 out = BIO_new(BIO_s_connect());
67 BIO_set_conn_hostname(out, hostport);
68
69 /* The BIO has parsed the host:port and even IPv6 literals in [] */
70 hostname = BIO_get_conn_hostname(out);
71 if (!hostname || SSL_set1_host(ssl, hostname) <= 0)
72 goto err;
73
74 BIO_set_nbio(out, 1);
75 out = BIO_push(ssl_bio, out);
76
77 p = "GET / HTTP/1.0\r\n\r\n";
78 len = strlen(p);
79
80 off = 0;
81 for (;;) {
82 i = BIO_write(out, &(p[off]), len);
83 if (i <= 0) {
84 if (BIO_should_retry(out)) {
85 fprintf(stderr, "write DELAY\n");
86 sleep(1);
87 continue;
88 } else {
89 goto err;
90 }
91 }
92 off += i;
93 len -= i;
94 if (len <= 0)
95 break;
96 }
97
98 for (;;) {
99 i = BIO_read(out, buf, sizeof(buf));
100 if (i == 0)
101 break;
102 if (i < 0) {
103 if (BIO_should_retry(out)) {
104 fprintf(stderr, "read DELAY\n");
105 sleep(1);
106 continue;
107 }
108 goto err;
109 }
110 fwrite(buf, 1, i, stdout);
111 }
112
113 ret = EXIT_SUCCESS;
114 goto done;
115
116 err:
117 if (ERR_peek_error() == 0) { /* system call error */
118 fprintf(stderr, "errno=%d ", errno);
119 perror("error");
120 } else {
121 ERR_print_errors_fp(stderr);
122 }
123 done:
124 BIO_free_all(out);
125 SSL_CTX_free(ssl_ctx);
126 return ret;
127}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette