本文整理汇总了C++中BIO_write函数的典型用法代码示例。如果您正苦于以下问题:C++ BIO_write函数的具体用法?C++ BIO_write怎么用?C++ BIO_write使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了BIO_write函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: checkout_file
// This is a function that checks out a file from the server
// - RETURNS 0 in case of success, 1 otherwise
int checkout_file(SSL* ssl, BIO *sslbio, char *buffer) {
FILE* file; // pointer to the file to be received
int ret;
int length;
// Sending the client name
// BIO_write(sslbio, CLIENT_NAME, strlen(CLIENT_NAME));
// Receive number of options
memset(buffer,0,4096);
length = BIO_read(sslbio, buffer, BUF_SIZE);
if(length <= 0)
{
strcpy(buffer, "No message");
length = strlen(buffer);
}
buffer[length] = '\0';
printf("\nSelect file number to check out file.\n");
printf("You have %s option(s):\n", buffer);
int opns = atoi(buffer);
if ( opns < 1 ) {
//No files to choose from.
printf("You have ZERO files stored at server...\n");
printf("Unable to check-out file: Choose a different option.\n");
exit(1);
}
// If files are present, receive file options
printf("\nFILE UID | FILE NAME | FILE OWNER\n");
int i;
for (i = 0; i < opns; i++) {
memset(buffer,0,4096);
length = BIO_read(sslbio, buffer, BUF_SIZE);
if(length <= 0)
{
strcpy(buffer, "No message");
length = strlen(buffer);
}
buffer[length] = '\0';
printf("%s\n", buffer);
}
// Receive deliminated list of option numbers
memset(buffer,0,4096);
length = BIO_read(sslbio, buffer, BUF_SIZE);
if(length <= 0)
{
strcpy(buffer, "No message");
length = strlen(buffer);
}
buffer[length] = '\0';
// Tokenize options
int options[opns];
int numtokens = str_to_ints(buffer, options);
// Select Option, i.e., file UID choice
int filechoice = getintchoice("Select FILE UID to checkout", options, opns);
length = floor(log10(abs(filechoice))) + 1;
char sfile[length];
sprintf(sfile, "%d", filechoice);
// Send File UID to Server
BIO_write(sslbio, sfile, length);
// Get file name from Server
memset(buffer,0,4096);
length = BIO_read(sslbio, buffer, BUF_SIZE);
if(length <= 0)
{
strcpy(buffer, "No message");
length = strlen(buffer);
}
buffer[length] = '\0';
printf("Saving file '%s' to local disk.\n", buffer);
// Open file
file = fopen(buffer, "w+");
if(file == NULL) {
fprintf(stderr, "File not found: '%s'\n", buffer);
return 1;
}
// Get file data from Server
memset(buffer,0,4096);
length = BIO_read(sslbio, buffer, BUF_SIZE);
if(length <= 0)
{
strcpy(buffer, "No message");
length = strlen(buffer);
}
buffer[length] = '\0';
printf("Plain text received:\n%s\n",buffer);
// Writing to file
fwrite(buffer, length, 1, file);
//.........这里部分代码省略.........
示例2: STACK_OF
//.........这里部分代码省略.........
if (pcert == NULL)
{
for (i=0; i<sk_PKCS7_RECIP_INFO_num(rsk); i++)
{
ri=sk_PKCS7_RECIP_INFO_value(rsk,i);
if (pkcs7_decrypt_rinfo(&ek, &eklen,
ri, pkey) > 0)
break;
ERR_clear_error();
ri = NULL;
}
if (ri == NULL)
{
PKCS7err(PKCS7_F_PKCS7_DATADECODE,
PKCS7_R_NO_RECIPIENT_MATCHES_KEY);
goto err;
}
}
else
{
if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) <= 0)
goto err;
}
evp_ctx=NULL;
BIO_get_cipher_ctx(etmp,&evp_ctx);
if (EVP_CipherInit_ex(evp_ctx,evp_cipher,NULL,NULL,NULL,0) <= 0)
goto err;
if (EVP_CIPHER_asn1_to_param(evp_ctx,enc_alg->parameter) < 0)
goto err;
if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) {
/* Some S/MIME clients don't use the same key
* and effective key length. The key length is
* determined by the size of the decrypted RSA key.
*/
if(!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen))
{
PKCS7err(PKCS7_F_PKCS7_DATADECODE,
PKCS7_R_DECRYPTED_KEY_IS_WRONG_LENGTH);
goto err;
}
}
if (EVP_CipherInit_ex(evp_ctx,NULL,NULL,ek,NULL,0) <= 0)
goto err;
if (ek)
{
OPENSSL_cleanse(ek,eklen);
OPENSSL_free(ek);
}
if (out == NULL)
out=etmp;
else
BIO_push(out,etmp);
etmp=NULL;
}
#if 1
if (PKCS7_is_detached(p7) || (in_bio != NULL))
{
bio=in_bio;
}
else
{
#if 0
bio=BIO_new(BIO_s_mem());
/* We need to set this so that when we have read all
* the data, the encrypt BIO, if present, will read
* EOF and encode the last few bytes */
BIO_set_mem_eof_return(bio,0);
if (data_body->length > 0)
BIO_write(bio,(char *)data_body->data,data_body->length);
#else
if (data_body->length > 0)
bio = BIO_new_mem_buf(data_body->data,data_body->length);
else {
bio=BIO_new(BIO_s_mem());
BIO_set_mem_eof_return(bio,0);
}
if (bio == NULL)
goto err;
#endif
}
BIO_push(out,bio);
bio=NULL;
#endif
if (0)
{
err:
if (out != NULL) BIO_free_all(out);
if (btmp != NULL) BIO_free_all(btmp);
if (etmp != NULL) BIO_free_all(etmp);
if (bio != NULL) BIO_free_all(bio);
out=NULL;
}
return(out);
}
示例3: s_client_main
//.........这里部分代码省略.........
if (mbuf_len == -1) {
BIO_printf(bio_err, "BIO_read failed\n");
goto end;
}
}
for (;;) {
struct pollfd pfd[3]; /* stdin, stdout, socket */
int ptimeout = -1;
if ((SSL_version(con) == DTLS1_VERSION) &&
DTLSv1_get_timeout(con, &timeout))
ptimeout = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {
in_init = 1;
tty_on = 0;
} else {
tty_on = 1;
if (in_init) {
in_init = 0;
if (sess_out) {
BIO *stmp = BIO_new_file(sess_out, "w");
if (stmp) {
PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));
BIO_free(stmp);
} else
BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
}
print_stuff(bio_c_out, con, full_log);
if (full_log > 0)
full_log--;
if (starttls_proto) {
BIO_write(bio_err, mbuf, mbuf_len);
/* We don't need to know any more */
starttls_proto = PROTO_OFF;
}
if (reconnect) {
reconnect--;
BIO_printf(bio_c_out, "drop connection and then reconnect\n");
SSL_shutdown(con);
SSL_set_connect_state(con);
shutdown(SSL_get_fd(con), SHUT_RD);
close(SSL_get_fd(con));
goto re_start;
}
}
}
ssl_pending = read_ssl && SSL_pending(con);
pfd[0].fd = -1;
pfd[1].fd = -1;
if (!ssl_pending) {
if (tty_on) {
if (read_tty) {
pfd[0].fd = fileno(stdin);
pfd[0].events = POLLIN;
}
if (write_tty) {
pfd[1].fd = fileno(stdout);
pfd[1].events = POLLOUT;
}
}
pfd[2].fd = SSL_get_fd(con);
示例4: RSA_print
int RSA_print(BIO *bp, RSA *x, int off)
{
char str[128];
const char *s;
unsigned char *m=NULL;
int ret=0;
size_t buf_len=0, i;
if (x->n)
buf_len = (size_t)BN_num_bytes(x->n);
if (x->e)
if (buf_len < (i = (size_t)BN_num_bytes(x->e)))
buf_len = i;
if (x->d)
if (buf_len < (i = (size_t)BN_num_bytes(x->d)))
buf_len = i;
if (x->p)
if (buf_len < (i = (size_t)BN_num_bytes(x->p)))
buf_len = i;
if (x->q)
if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
buf_len = i;
if (x->dmp1)
if (buf_len < (i = (size_t)BN_num_bytes(x->dmp1)))
buf_len = i;
if (x->dmq1)
if (buf_len < (i = (size_t)BN_num_bytes(x->dmq1)))
buf_len = i;
if (x->iqmp)
if (buf_len < (i = (size_t)BN_num_bytes(x->iqmp)))
buf_len = i;
m=(unsigned char *)OPENSSL_malloc(buf_len+10);
if (m == NULL)
{
RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
goto err;
}
if (off)
{
if (off > 128) off=128;
memset(str,' ',off);
}
if (x->d != NULL)
{
if (off && (BIO_write(bp,str,off) <= 0)) goto err;
if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n))
<= 0) goto err;
}
if (x->d == NULL)
sprintf(str,"Modulus (%d bit):",BN_num_bits(x->n));
else
strcpy(str,"modulus:");
if (!print(bp,str,x->n,m,off)) goto err;
s=(x->d == NULL)?"Exponent:":"publicExponent:";
if (!print(bp,s,x->e,m,off)) goto err;
if (!print(bp,"privateExponent:",x->d,m,off)) goto err;
if (!print(bp,"prime1:",x->p,m,off)) goto err;
if (!print(bp,"prime2:",x->q,m,off)) goto err;
if (!print(bp,"exponent1:",x->dmp1,m,off)) goto err;
if (!print(bp,"exponent2:",x->dmq1,m,off)) goto err;
if (!print(bp,"coefficient:",x->iqmp,m,off)) goto err;
ret=1;
err:
if (m != NULL) OPENSSL_free(m);
return(ret);
}
示例5: process_files
int process_files(char **parameters, int param_count){
int i, success = 0;
size_t siglen;
long int filesize=0, bytes_written=0;
unsigned char *signature, *file_mem;
char *boincname, *filename;
EVP_MD_CTX *mdctx = NULL;
FILE *current_file;
BIO *b64, *bio_out;
printf("Boinc filename Filename Status\n");
printf("--------------------------------------------------------\n");
for(i = 0; i<param_count; i+=2){
boincname = parameters[i];
filename = parameters[i+1];
printf("%-18.18s %-18.18s ", boincname, filename);
if(!(mdctx = EVP_MD_CTX_create())) goto err;
if(1 != EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, private_key)) goto err;
/* Open file and get size */
current_file = fopen(filename, "rb");
if(current_file == NULL)
{print_error("file_signing", "unable to open file", filename); goto err;}
fseek(current_file, 0L, SEEK_END);
filesize = ftell(current_file);
file_mem = (unsigned char *)OPENSSL_malloc(filesize);
if(file_mem == NULL)
{print_error("file_signing", "unable to reserve memory for file", filename); goto err;}
fseek(current_file, 0L, SEEK_SET); //rewind to beginning of file
/* read file to memory */
bytes_written = fread(file_mem, 1, filesize, current_file);
if(bytes_written != filesize){
if(ferror(current_file))
{print_error("file_signing", "Error reading file", filename); goto err;}
else if(feof(current_file))
{printf("File: %s wrote %li of filesize %li", filename, bytes_written, filesize); goto err;}
else
{print_error("file_signing", "something strange happened", filename); goto err;}
}
if(1 != EVP_DigestSignUpdate(mdctx, file_mem, filesize)) goto err;
if(1 != EVP_DigestSignFinal(mdctx, NULL, &siglen)) goto err; // Get signature size
if(!(signature = OPENSSL_malloc(sizeof(unsigned char) * siglen))) goto err;
if(1 != EVP_DigestSignFinal(mdctx, signature, &siglen)) goto err;
fwrite(boincname, sizeof(char), strlen(boincname), outputfile);
fputc(' ', outputfile);
b64 = BIO_new(BIO_f_base64());
bio_out = BIO_new_fp(outputfile, BIO_NOCLOSE);
BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
BIO_push(b64, bio_out);
BIO_write(b64, signature, sizeof(unsigned char) * siglen);
BIO_flush(b64);
BIO_free_all(b64);
fputc('\n', outputfile);
success = 1;
printf("%-18.18s\n", "Success!");
err:
if(mdctx) EVP_MD_CTX_destroy(mdctx);
if(current_file) fclose(current_file);
if(file_mem) OPENSSL_free(file_mem);
if(signature) OPENSSL_free(signature);
if(!success)
printf("%-18.18s\n", "Failed!");
}
}
示例6: do_fp
int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
EVP_PKEY *key, unsigned char *sigin, int siglen,
const char *sig_name, const char *md_name,
const char *file, BIO *bmd)
{
size_t len;
int i;
for (;;) {
i = BIO_read(bp, (char *)buf, BUFSIZE);
if (i < 0) {
BIO_printf(bio_err, "Read Error in %s\n", file);
ERR_print_errors(bio_err);
return 1;
}
if (i == 0)
break;
}
if (sigin) {
EVP_MD_CTX *ctx;
BIO_get_md_ctx(bp, &ctx);
i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen);
if (i > 0)
BIO_printf(out, "Verified OK\n");
else if (i == 0) {
BIO_printf(out, "Verification Failure\n");
return 1;
} else {
BIO_printf(bio_err, "Error Verifying Data\n");
ERR_print_errors(bio_err);
return 1;
}
return 0;
}
if (key) {
EVP_MD_CTX *ctx;
BIO_get_md_ctx(bp, &ctx);
len = BUFSIZE;
if (!EVP_DigestSignFinal(ctx, buf, &len)) {
BIO_printf(bio_err, "Error Signing Data\n");
ERR_print_errors(bio_err);
return 1;
}
} else {
len = BIO_gets(bp, (char *)buf, BUFSIZE);
if ((int)len < 0) {
ERR_print_errors(bio_err);
return 1;
}
}
if (binout)
BIO_write(out, buf, len);
else if (sep == 2) {
for (i = 0; i < (int)len; i++)
BIO_printf(out, "%02x", buf[i]);
BIO_printf(out, " *%s\n", file);
} else {
if (sig_name) {
BIO_puts(out, sig_name);
if (md_name)
BIO_printf(out, "-%s", md_name);
BIO_printf(out, "(%s)= ", file);
} else if (md_name)
BIO_printf(out, "%s(%s)= ", md_name, file);
else
BIO_printf(out, "(%s)= ", file);
for (i = 0; i < (int)len; i++) {
if (sep && (i != 0))
BIO_printf(out, ":");
BIO_printf(out, "%02x", buf[i]);
}
BIO_printf(out, "\n");
}
return 0;
}
示例7: b64_write
static int b64_write(BIO *b, const char *in, int inl)
{
int ret = 0;
int n;
int i;
BIO_B64_CTX *ctx;
ctx = (BIO_B64_CTX *)b->ptr;
BIO_clear_retry_flags(b);
if (ctx->encode != B64_ENCODE) {
ctx->encode = B64_ENCODE;
ctx->buf_len = 0;
ctx->buf_off = 0;
ctx->tmp_len = 0;
EVP_EncodeInit(ctx->base64);
}
OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
n = ctx->buf_len - ctx->buf_off;
while (n > 0) {
i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
if (i <= 0) {
BIO_copy_next_retry(b);
return (i);
}
OPENSSL_assert(i <= n);
ctx->buf_off += i;
OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
n -= i;
}
/* at this point all pending data has been written */
ctx->buf_off = 0;
ctx->buf_len = 0;
if ((in == NULL) || (inl <= 0))
return (0);
while (inl > 0) {
n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
if (ctx->tmp_len > 0) {
OPENSSL_assert(ctx->tmp_len <= 3);
n = 3 - ctx->tmp_len;
/*
* There's a theoretical possibility for this
*/
if (n > inl)
n = inl;
memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
ctx->tmp_len += n;
ret += n;
if (ctx->tmp_len < 3)
break;
ctx->buf_len =
EVP_EncodeBlock((unsigned char *)ctx->buf,
(unsigned char *)ctx->tmp, ctx->tmp_len);
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
/*
* Since we're now done using the temporary buffer, the
* length should be 0'd
*/
ctx->tmp_len = 0;
} else {
if (n < 3) {
memcpy(ctx->tmp, in, n);
ctx->tmp_len = n;
ret += n;
break;
}
n -= n % 3;
ctx->buf_len =
EVP_EncodeBlock((unsigned char *)ctx->buf,
(const unsigned char *)in, n);
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
ret += n;
}
} else {
EVP_EncodeUpdate(ctx->base64,
(unsigned char *)ctx->buf, &ctx->buf_len,
(unsigned char *)in, n);
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
ret += n;
}
inl -= n;
in += n;
ctx->buf_off = 0;
n = ctx->buf_len;
while (n > 0) {
i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
if (i <= 0) {
BIO_copy_next_retry(b);
//.........这里部分代码省略.........
示例8: buffer_write
static int buffer_write(BIO *b, const char *in, int inl)
{
int i, num = 0;
BIO_F_BUFFER_CTX *ctx;
if ((in == NULL) || (inl <= 0))
return (0);
ctx = (BIO_F_BUFFER_CTX *)b->ptr;
if ((ctx == NULL) || (b->next_bio == NULL))
return (0);
BIO_clear_retry_flags(b);
start:
i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
/* add to buffer and return */
if (i >= inl) {
memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
ctx->obuf_len += inl;
return (num + inl);
}
/* else */
/* stuff already in buffer, so add to it first, then flush */
if (ctx->obuf_len != 0) {
if (i > 0) { /* lets fill it up if we can */
memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
in += i;
inl -= i;
num += i;
ctx->obuf_len += i;
}
/* we now have a full buffer needing flushing */
for (;;) {
i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
ctx->obuf_len);
if (i <= 0) {
BIO_copy_next_retry(b);
if (i < 0)
return ((num > 0) ? num : i);
if (i == 0)
return (num);
}
ctx->obuf_off += i;
ctx->obuf_len -= i;
if (ctx->obuf_len == 0)
break;
}
}
/*
* we only get here if the buffer has been flushed and we still have
* stuff to write
*/
ctx->obuf_off = 0;
/* we now have inl bytes to write */
while (inl >= ctx->obuf_size) {
i = BIO_write(b->next_bio, in, inl);
if (i <= 0) {
BIO_copy_next_retry(b);
if (i < 0)
return ((num > 0) ? num : i);
if (i == 0)
return (num);
}
num += i;
in += i;
inl -= i;
if (inl == 0)
return (num);
}
/*
* copy the rest into the buffer since we have only a small amount left
*/
goto start;
}
示例9: buffer_ctrl
//.........这里部分代码省略.........
memcpy(ctx->ibuf, ptr, (int)num);
ret = 1;
break;
case BIO_C_SET_BUFF_SIZE:
if (ptr != NULL) {
ip = (int *)ptr;
if (*ip == 0) {
ibs = (int)num;
obs = ctx->obuf_size;
} else { /* if (*ip == 1) */
ibs = ctx->ibuf_size;
obs = (int)num;
}
} else {
ibs = (int)num;
obs = (int)num;
}
p1 = ctx->ibuf;
p2 = ctx->obuf;
if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
p1 = OPENSSL_malloc((int)num);
if (p1 == NULL)
goto malloc_error;
}
if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
p2 = OPENSSL_malloc((int)num);
if (p2 == NULL) {
if (p1 != ctx->ibuf)
OPENSSL_free(p1);
goto malloc_error;
}
}
if (ctx->ibuf != p1) {
OPENSSL_free(ctx->ibuf);
ctx->ibuf = p1;
ctx->ibuf_off = 0;
ctx->ibuf_len = 0;
ctx->ibuf_size = ibs;
}
if (ctx->obuf != p2) {
OPENSSL_free(ctx->obuf);
ctx->obuf = p2;
ctx->obuf_off = 0;
ctx->obuf_len = 0;
ctx->obuf_size = obs;
}
break;
case BIO_C_DO_STATE_MACHINE:
if (b->next_bio == NULL)
return (0);
BIO_clear_retry_flags(b);
ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
BIO_copy_next_retry(b);
break;
case BIO_CTRL_FLUSH:
if (b->next_bio == NULL)
return (0);
if (ctx->obuf_len <= 0) {
ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
}
for (;;) {
BIO_clear_retry_flags(b);
if (ctx->obuf_len > 0) {
r = BIO_write(b->next_bio,
&(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
BIO_copy_next_retry(b);
if (r <= 0)
return ((long)r);
ctx->obuf_off += r;
ctx->obuf_len -= r;
} else {
ctx->obuf_len = 0;
ctx->obuf_off = 0;
ret = 1;
break;
}
}
ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
case BIO_CTRL_DUP:
dbio = (BIO *)ptr;
if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
ret = 0;
break;
default:
if (b->next_bio == NULL)
return (0);
ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
}
return (ret);
malloc_error:
BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
return (0);
}
示例10: do_fp
int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
const char *file)
{
int len;
int i;
for (;;)
{
i=BIO_read(bp,(char *)buf,BUFSIZE);
if(i < 0)
{
BIO_printf(bio_err, "Read Error in %s\n",file);
ERR_print_errors(bio_err);
return 1;
}
if (i == 0) break;
}
if(sigin)
{
EVP_MD_CTX *ctx;
BIO_get_md_ctx(bp, &ctx);
i = EVP_VerifyFinal(ctx, sigin, (unsigned int)siglen, key);
if(i > 0)
BIO_printf(out, "Verified OK\n");
else if(i == 0)
{
BIO_printf(out, "Verification Failure\n");
return 1;
}
else
{
BIO_printf(bio_err, "Error Verifying Data\n");
ERR_print_errors(bio_err);
return 1;
}
return 0;
}
if(key)
{
EVP_MD_CTX *ctx;
BIO_get_md_ctx(bp, &ctx);
if(!EVP_SignFinal(ctx, buf, (unsigned int *)&len, key))
{
BIO_printf(bio_err, "Error Signing Data\n");
ERR_print_errors(bio_err);
return 1;
}
}
else
len=BIO_gets(bp,(char *)buf,BUFSIZE);
if(binout) BIO_write(out, buf, len);
else
{
BIO_write(out,title,strlen(title));
for (i=0; i<len; i++)
{
if (sep && (i != 0))
BIO_printf(out, ":");
BIO_printf(out, "%02x",buf[i]);
}
BIO_printf(out, "\n");
}
return 0;
}
示例11: loge
//.........这里部分代码省略.........
chan = cur_chan;
int flags = enc_buf [1]; // Flags
int frame_len = be16toh(*((uint16_t*)&enc_buf[2]));
logd("Frame flags %i len %i", flags, frame_len);
if (frame_len > MAX_FRAME_PAYLOAD_SIZE)
{
loge ("Too big");
return (-1);
}
int header_size = 4;
bool has_total_size_header = false;
if ((flags & HU_FRAME_FIRST_FRAME) & !(flags & HU_FRAME_LAST_FRAME))
{
//if first but not last, next 4 is total size
has_total_size_header = true;
header_size += 4;
}
int remaining_bytes_in_frame = (frame_len + header_size) - have_len;
while(remaining_bytes_in_frame > 0)
{
logd("Getting more %i", remaining_bytes_in_frame);
int got_bytes = hu_aap_tra_recv (&enc_buf[have_len], remaining_bytes_in_frame, tmo); // Get Rx packet from Transport
if (got_bytes < 0) { // If we don't have a full 6 byte header at least...
loge ("Recv got_bytes: %d", got_bytes);
return (-1);
}
have_len += got_bytes;
remaining_bytes_in_frame -= got_bytes;
}
if (!has_first && !(flags & HU_FRAME_FIRST_FRAME))
{
loge ("No HU_FRAME_FIRST_FRAME");
return (-1);
}
has_first = true;
has_last = (flags & HU_FRAME_LAST_FRAME) != 0;
if (has_total_size_header)
{
uint32_t total_size = be32toh(*((uint32_t*)&enc_buf[4]));
logd("First only, total len %u", total_size);
temp_assembly_buffer.reserve(total_size);
}
else
{
temp_assembly_buffer.reserve(frame_len);
}
if (flags & HU_FRAME_ENCRYPTED)
{
size_t cur_vec = temp_assembly_buffer.size();
temp_assembly_buffer.resize(cur_vec + frame_len); //just incase
int bytes_written = BIO_write (hu_ssl_rm_bio, &enc_buf[header_size], frame_len); // Write encrypted to SSL input BIO
if (bytes_written <= 0) {
loge ("BIO_write() bytes_written: %d", bytes_written);
return (-1);
}
if (bytes_written != frame_len)
loge ("BIO_write() len: %d bytes_written: %d chan: %d %s", frame_len, bytes_written, chan, chan_get (chan));
else if (ena_log_verbo)
logd ("BIO_write() len: %d bytes_written: %d chan: %d %s", frame_len, bytes_written, chan, chan_get (chan));
int bytes_read = SSL_read (hu_ssl_ssl, &temp_assembly_buffer[cur_vec], frame_len); // Read decrypted to decrypted rx buf
if (bytes_read <= 0 || bytes_read > frame_len) {
loge ("SSL_read() bytes_read: %d errno: %d", bytes_read, errno);
hu_ssl_ret_log (bytes_read);
return (-1); // Fatal so return error and de-initialize; Should we be able to recover, if Transport data got corrupted ??
}
if (ena_log_verbo)
logd ("SSL_read() bytes_read: %d", bytes_read);
temp_assembly_buffer.resize(cur_vec + bytes_read);
}
else
{
temp_assembly_buffer.insert(temp_assembly_buffer.end(), &enc_buf[header_size], &enc_buf[frame_len+header_size]);
}
}
const int buf_len = temp_assembly_buffer.size();
if (buf_len >= 2)
{
uint16_t msg_type = be16toh(*reinterpret_cast<uint16_t*>(temp_assembly_buffer.data()));
ret = iaap_msg_process (chan, msg_type, &temp_assembly_buffer[2], buf_len - 2); // Decrypt & Process 1 received encrypted message
if (ret < 0 && iaap_state != hu_STATE_STOPPED) { // If error...
loge ("Error iaap_msg_process() ret: %d ", ret);
return (ret);
}
}
return (ret); // Return value from the last iaap_recv_dec_process() call; should be 0
}
示例12: pkeyutl_main
//.........这里部分代码省略.........
}
/* Raw input data is handled elsewhere */
if (in != NULL && !rawin) {
/* Read the input data */
buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
if (buf_inlen < 0) {
BIO_printf(bio_err, "Error reading input Data\n");
goto end;
}
if (rev) {
size_t i;
unsigned char ctmp;
size_t l = (size_t)buf_inlen;
for (i = 0; i < l / 2; i++) {
ctmp = buf_in[i];
buf_in[i] = buf_in[l - 1 - i];
buf_in[l - 1 - i] = ctmp;
}
}
}
/* Sanity check the input if the input is not raw */
if (!rawin
&& buf_inlen > EVP_MAX_MD_SIZE
&& (pkey_op == EVP_PKEY_OP_SIGN
|| pkey_op == EVP_PKEY_OP_VERIFY
|| pkey_op == EVP_PKEY_OP_VERIFYRECOVER)) {
BIO_printf(bio_err,
"Error: The input data looks too long to be a hash\n");
goto end;
}
if (pkey_op == EVP_PKEY_OP_VERIFY) {
if (rawin) {
rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, sig, siglen,
NULL, 0);
} else {
rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
buf_in, (size_t)buf_inlen);
}
if (rv == 1) {
BIO_puts(out, "Signature Verified Successfully\n");
ret = 0;
} else {
BIO_puts(out, "Signature Verification Failure\n");
}
goto end;
}
if (kdflen != 0) {
buf_outlen = kdflen;
rv = 1;
} else {
if (rawin) {
/* rawin allocates the buffer in do_raw_keyop() */
rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, NULL, 0,
&buf_out, (size_t *)&buf_outlen);
} else {
rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
buf_in, (size_t)buf_inlen);
if (rv > 0 && buf_outlen != 0) {
buf_out = app_malloc(buf_outlen, "buffer output");
rv = do_keyop(ctx, pkey_op,
buf_out, (size_t *)&buf_outlen,
buf_in, (size_t)buf_inlen);
}
}
}
if (rv <= 0) {
if (pkey_op != EVP_PKEY_OP_DERIVE) {
BIO_puts(bio_err, "Public Key operation error\n");
} else {
BIO_puts(bio_err, "Key derivation failed\n");
}
ERR_print_errors(bio_err);
goto end;
}
ret = 0;
if (asn1parse) {
if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
ERR_print_errors(bio_err);
} else if (hexdump) {
BIO_dump(out, (char *)buf_out, buf_outlen);
} else {
BIO_write(out, buf_out, buf_outlen);
}
end:
EVP_PKEY_CTX_free(ctx);
release_engine(e);
BIO_free(in);
BIO_free_all(out);
OPENSSL_free(buf_in);
OPENSSL_free(buf_out);
OPENSSL_free(sig);
sk_OPENSSL_STRING_free(pkeyopts);
sk_OPENSSL_STRING_free(pkeyopts_passin);
return ret;
}
示例13: cas_validate
//.........这里部分代码省略.........
BIO_set_conn_port(bio, config->port);
if(BIO_do_connect(bio) <= 0)
{
DEBUG_LOG("Error attempting to connect : %s\n", config->host);
END(CAS_ERROR_CONN);
}
}
/* build request */
full_request = malloc(strlen(CAS_METHOD) + strlen(" ")
+ strlen(config->uriValidate) + strlen("?ticket=") + strlen(ticket) +
+ strlen("&service=") + strlen(service) + strlen(" ")
+ strlen(GENERIC_HEADERS) + strlen ("\r\n")
#ifdef HEADER_HOST_NAME
+ strlen(HEADER_HOST_NAME) + strlen (": ") + strlen (config->host)
#endif
+ strlen("\r\n\r\n") + 1);
if (full_request == NULL)
{
DEBUG_LOG("Error memory allocation%s\n", "");
END(CAS_ERROR_MEMORY_ALLOC);
}
#ifdef HEADER_HOST_NAME
sprintf(full_request, "%s %s?ticket=%s&service=%s %s\r\n%s: %s\r\n\r\n",
CAS_METHOD, config->uriValidate, ticket, service, GENERIC_HEADERS,
HEADER_HOST_NAME,config->host);
#else
sprintf(full_request, "%s %s?ticket=%s&service=%s %s\r\n\r\n",
CAS_METHOD, config->uriValidate, ticket, service, GENERIC_HEADERS);
#endif
/* send request */
DEBUG_LOG("---- request :\n%s\n", full_request);
if (BIO_write(bio, full_request, strlen(full_request)) != strlen(full_request))
{
DEBUG_LOG("Unable to correctly send request to %s\n", config->host);
END(CAS_ERROR_HTTP);
}
/* Read the response */
total = 0;
b = 0;
do
{
b = BIO_read(bio, buf + total, (sizeof(buf) - 1) - total);
total += b;
} while (b > 0);
buf[total] = '\0';
if (b != 0 || total >= sizeof(buf) - 1)
{
DEBUG_LOG("Unexpected read error or response too large from %s\n", config->host);
DEBUG_LOG("b = %d\n", b);
DEBUG_LOG("total = %d\n", total);
DEBUG_LOG("buf = %s\n", buf);
END(CAS_ERROR_HTTP); // unexpected read error or response too large
}
DEBUG_LOG("---- response :\n%s\n", buf);
str = (char *)strstr(buf, "\r\n\r\n"); // find the end of the header
if (!str)
{
DEBUG_LOG("no header in response%s\n", "");
END(CAS_ERROR_HTTP); // no header
}
示例14: main
//.........这里部分代码省略.........
/* Check the validity of Private Key */
if (!SSL_CTX_check_private_key(ctx)) {
ERR_print_errors_fp(stdout);
printf("ssl_ctx_check_private_key failed.\r\n");
//goto free_ctx;
BIO_free_all(sslbio);
SSL_CTX_free(ctx);
return 0;
}
/* Create the connection */
sslbio = BIO_new_ssl_connect(ctx);
/* Get SSL from sslbio */
BIO_get_ssl(sslbio, &ssl);
/* Set the SSL mode into SSL_MODE_AUTO_RETRY */
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
//////////////////////////////////////////////////
// NOTE: Port# hardcoded here; change if necessary
//////////////////////////////////////////////////
BIO_set_conn_port(sslbio, "7777");
BIO_set_conn_hostname(sslbio, server);
/* Request Connection */
if(BIO_do_connect(sslbio) <= 0)
{
fprintf(stderr, "Error attempting to connect\n");
ERR_print_errors_fp(stderr);
BIO_free_all(sslbio);
SSL_CTX_free(ctx);
return 0;
}
else
{
printf("Connection to server successful!\n");
}
/* Verify Server Certificate Validity */
if(SSL_get_verify_result(ssl) != X509_V_OK)
{
printf("Certificate Verification Error: %ld\n", SSL_get_verify_result(ssl));
BIO_free_all(sslbio);
SSL_CTX_free(ctx);
return 0;
}
else
{
printf("verify server cert successful\n");
}
//Send hostname to server
printf("Sending client name to server.\n");
BIO_write(sslbio, CLIENT_NAME, strlen(CLIENT_NAME));
do
{
choice = getchoice("Please select an action", menu);
printf("You have chosen: %c\n", choice);
if (choice == 'a')
{
printf("Check-in function will be executed\n");
choiceProcess (sslbio, buffer, choice);
ret = checkin_file(ssl, sslbio, buffer);
}
else if (choice == 'b')
{
printf("Check-out function will be executed\n");
choiceProcess (sslbio, buffer, choice);
ret = checkout_file(ssl, sslbio, buffer);
}
else if (choice == 'c')
{
printf("Delegate function will be executed\n");
choiceProcess (sslbio, buffer, choice);
}
else if (choice == 'd')
{
printf("Safe-delete function will be executed\n");
choiceProcess (sslbio, buffer, choice);
}
else
{
printf("Terminate function will be executed\n");
}
} while (choice != 'q');
/* Terminate the connection by sending message */
clientTerminate (sslbio, buffer);
/* Close the connection and free the context */
BIO_ssl_shutdown(sslbio);
BIO_free_all(sslbio);
SSL_CTX_free(ctx);
}
return 0;
}
示例15: process_output_ssl
static ssize_t process_output_ssl( pn_transport_t *transport, unsigned int layer, char *buffer, size_t max_len)
{
pni_ssl_t *ssl = transport->ssl;
if (!ssl) return PN_EOS;
if (ssl->ssl == NULL && init_ssl_socket(transport, ssl)) return PN_EOS;
ssize_t written = 0;
bool work_pending;
do {
work_pending = false;
// first, get any pending application output, if possible
if (!ssl->app_output_closed && ssl->out_count < ssl->out_size) {
ssize_t app_bytes = transport->io_layers[layer+1]->process_output(transport, layer+1, &ssl->outbuf[ssl->out_count], ssl->out_size - ssl->out_count);
if (app_bytes > 0) {
ssl->out_count += app_bytes;
work_pending = true;
ssl_log(transport, "Gathered %d bytes from app to send to peer", app_bytes );
} else {
if (app_bytes < 0) {
ssl_log(transport, "Application layer closed its output, error=%d (%d bytes pending send)",
(int) app_bytes, (int) ssl->out_count);
ssl->app_output_closed = app_bytes;
}
}
}
// now push any pending app data into the socket
if (!ssl->ssl_closed) {
char *data = ssl->outbuf;
if (ssl->out_count > 0) {
int wrote = BIO_write( ssl->bio_ssl, data, ssl->out_count );
if (wrote > 0) {
data += wrote;
ssl->out_count -= wrote;
work_pending = true;
ssl_log( transport, "Wrote %d bytes from app to socket", wrote );
} else {
if (!BIO_should_retry(ssl->bio_ssl)) {
int reason = SSL_get_error( ssl->ssl, wrote );
switch (reason) {
case SSL_ERROR_ZERO_RETURN:
// SSL closed cleanly
ssl_log(transport, "SSL connection has closed");
start_ssl_shutdown(transport); // KAG: not sure - this may not be necessary
ssl->out_count = 0; // can no longer write to socket, so erase app output data
ssl->ssl_closed = true;
break;
default:
// unexpected error
return (ssize_t)ssl_failed(transport);
}
} else {
if (BIO_should_read( ssl->bio_ssl )) {
ssl->read_blocked = true;
ssl_log(transport, "Detected read-blocked");
}
if (BIO_should_write( ssl->bio_ssl )) {
ssl->write_blocked = true;
ssl_log(transport, "Detected write-blocked");
}
}
}
}
if (ssl->out_count == 0) {
if (ssl->app_input_closed && ssl->app_output_closed) {
// application is done sending/receiving data, and all buffered output data has
// been written to the SSL socket
start_ssl_shutdown(transport);
}
} else if (data != ssl->outbuf) {
memmove( ssl->outbuf, data, ssl->out_count );
}
}
// read from the network bio as much as possible, filling the buffer
if (max_len) {
int available = BIO_read( ssl->bio_net_io, buffer, max_len );
if (available > 0) {
max_len -= available;
buffer += available;
written += available;
ssl->write_blocked = false;
work_pending = work_pending || max_len > 0;
ssl_log(transport, "Read %d bytes from BIO Layer", available );
}
}
} while (work_pending);
//_log(ssl, "written=%d ssl_closed=%d in_count=%d app_input_closed=%d app_output_closed=%d bio_pend=%d",
// written, ssl->ssl_closed, ssl->in_count, ssl->app_input_closed, ssl->app_output_closed, BIO_pending(ssl->bio_net_io) );
// PROTON-82: close the output side as soon as we've sent the SSL close_notify.
// We're not requiring the response, as some implementations never reply.
// ----
// Once no more data is available "below" the SSL socket, tell the transport we are
//.........这里部分代码省略.........