示例#1
0
bool MaOpenSslSocket::getEof()
{
	bool	rc;

	rc = (BIO_eof(bio) != 0);
	return rc;
}
示例#2
0
文件: ssl_api.c 项目: ewust/tapdance
int ssl_decrypt(SSL *s, const char *in, int len, char **out)
{
    int i;
    
    // WARNING: we expect this to be a memory bio...
    BIO *rbio = SSL_get_rbio(s);
    if (!BIO_eof(rbio)) {
      fprintf(stderr, "ssl_decrypt: Someone left data in the rbio!");
      fflush(stderr);
      return -1;
    }

    if (BIO_write(rbio, in, len) != len) {
      fprintf(stderr, "ssl_decrypt: couldn't write to BIO!");
      fflush(stderr);
      return -1;
    }

    *out = malloc(len);
    if (!*out) {
        return -1;
    }

    i = 0;
    int ret = SSL_read(s, *out + i, len - i);
    i += ret;  // If <= 0, we return it; o.w. we should accumulate.
    //fprintf(stderr, "first SSL_read returned: %d\n", ret);
    //fflush(stderr);
    while (ret > 0 && !BIO_eof(rbio) && i < len) {
      ret = SSL_read(s, *out + i, len - i);
      fprintf(stderr, "SSL_read returned: %d\n", ret);
      fflush(stderr);
      if (ret > 0)
          i += ret;
    }

    if (!BIO_eof(rbio)) {
      fprintf(stderr, "We are leaving data in the rbio! ret: %d\n", ret);
      fprintf(stderr, "In particular, this data:\n");
      char *data;
      long data_len = BIO_get_mem_data(rbio, &data);
      //hexdump(data, data_len);
    }

    return i;
}
示例#3
0
/* 
 * Takes in an RSA object and PEM encodes it in out
 * @param key: the RSA private key
 * @param out: the string the PEM encoding goes to
 * @param pem_password: the password to unlock the pem encoding
 * @return: the length of the PEM encoding
 */
unsigned int rsa_privatekey_to_pem(RSA *key, unsigned char **out, unsigned char *password) {
    BIO *pubKey = BIO_new(BIO_s_mem());

    PEM_write_bio_RSAPrivateKey(pubKey, key, NULL, NULL, 0, NULL, NULL);

    unsigned char line[65];
    int len = 0;
    unsigned char *pem = NULL;
    unsigned char *new_pem = NULL;

    if (!BIO_eof(pubKey)) {
        BIO_gets(pubKey, line, sizeof *pubKey);

        len += strlen(line);

        new_pem = (unsigned char *)realloc(pem, len*sizeof(unsigned char));
        if (!new_pem) {
            printf("realloc failed at length:%d\n", len);
        } else {
            memcpy(new_pem, "-----BEGIN PRIVATE KEY-----\n", (size_t)len);
            pem = new_pem;
        }
    }

    while (!BIO_eof(pubKey)) {
        BIO_gets(pubKey, line, sizeof *pubKey);

        //  current length of PEM (including newlines)
        len += strlen(line);

        new_pem = (unsigned char *)realloc(pem, len*sizeof(unsigned char));
        if (!new_pem) {
            printf("realloc failed at length:%d\n", len);
            exit(EXIT_FAILURE);
        } else {
            memcpy(new_pem, strcat(new_pem, line), (size_t)len);
            pem = new_pem;
        }
    }

    *out = pem;

    return len;
}
示例#4
0
static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
{
    if (ctx->type == is_dir)
        return ctx->_.dir.end_reached;

    if (ctx->_.file.last_handler != NULL
        && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
        return 0;
    return BIO_eof(ctx->_.file.file);
}
CAMLprim value ocaml_ssl_get_rbio_eof(value socket) 
{
    CAMLparam1(socket);
    CAMLlocal1(ret);
    BIO *b;
    int eof;

    ssl_socket_t *ssl = ssl_socket_of_block(socket);
    b = SSL_get_rbio(ssl->handler);
    if (b == NULL) 
	failwith("Ssl.get_rbio_eof: No rbio found");
    eof = BIO_eof(b);
    ret = Val_bool(eof);

    CAMLreturn(ret);
}
示例#6
0
char *
x509_get_subject (X509 *cert, struct gc_arena *gc)
{
  BIO *subject_bio = NULL;
  BUF_MEM *subject_mem;
  char *subject = NULL;
  int maxlen = 0;

  /*
   * Generate the subject string in OpenSSL proprietary format,
   * when in --compat-names mode
   */
  if (compat_flag (COMPAT_FLAG_QUERY | COMPAT_NAMES))
    {
      subject = gc_malloc (256, false, gc);
      X509_NAME_oneline (X509_get_subject_name (cert), subject, 256);
      subject[255] = '\0';
      return subject;
    }

  subject_bio = BIO_new (BIO_s_mem ());
  if (subject_bio == NULL)
    goto err;

  X509_NAME_print_ex (subject_bio, X509_get_subject_name (cert),
                      0, XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN |
                      ASN1_STRFLGS_UTF8_CONVERT | ASN1_STRFLGS_ESC_CTRL);

  if (BIO_eof (subject_bio))
    goto err;

  BIO_get_mem_ptr (subject_bio, &subject_mem);

  maxlen = subject_mem->length + 1;
  subject = gc_malloc (maxlen, false, gc);

  memcpy (subject, subject_mem->data, maxlen);
  subject[maxlen - 1] = '\0';

err:
  if (subject_bio)
    BIO_free (subject_bio);

  return subject;
}
示例#7
0
int dtls_netsock_write(NETIO_SOCK_T *pnetsock, const unsigned char *pDataIn, unsigned int lenIn,
                       unsigned char *pDataOut, unsigned int lenOut) {
  int rc = 0;
  int code = 0;
  long sz0, sz;
  char *p = NULL;

  if(!pnetsock || !pDataIn || !pDataOut || !pnetsock->ssl.pCtxt || !pnetsock->ssl.pDtlsCtxt || !pnetsock->ssl.pBioWr) {
     return -1;
  }

  pthread_mutex_lock(&((STREAM_DTLS_CTXT_T *) pnetsock->ssl.pDtlsCtxt)->mtx);

  if((rc = SSL_write(pnetsock->ssl.pCtxt, pDataIn, lenIn)) < 0) {
    code = SSL_get_error(pnetsock->ssl.pCtxt, rc);
    LOG(X_DEBUG("DTLS SSL_write for len:%d, rc:%d, code:%d"), lenIn, rc, code);
  }

  sz0 = BIO_get_mem_data(pnetsock->ssl.pBioWr, &p);

  if((rc = BIO_read(pnetsock->ssl.pBioWr, pDataOut, lenOut)) < 0){
    LOG(X_ERROR("DTLS BIO_read (output data) failed with %d for %d -> %d bytes: %s"), 
                rc, lenIn, lenOut, ERR_reason_error_string(ERR_get_error()));
    rc = -1;
  } else {
    //LOG(X_DEBUG("DTLS BIO_read for lenIn:%d -> lenOut:%d/%d (buf:%d)"), lenIn, rc, sz0, lenOut);
  }

  if(rc >= 0) {

    if(!BIO_eof(pnetsock->ssl.pBioWr)) {
      sz = BIO_get_mem_data(pnetsock->ssl.pBioWr, &p);
      LOG(X_WARNING("DTLS output BIO discarding unread %lu/%ld bytes"), sz, sz0); 
    }

    (void) BIO_reset(pnetsock->ssl.pBioWr);

  }

  pthread_mutex_unlock(&((STREAM_DTLS_CTXT_T *) pnetsock->ssl.pDtlsCtxt)->mtx);

  return rc;
}
示例#8
0
	inline
	void DtlsTransport::SendPendingOutgoingDtlsData()
	{
		MS_TRACE();

		if (BIO_eof(this->sslBioToNetwork))
			return;

		long read;
		char* data = nullptr;

		read = BIO_get_mem_data(this->sslBioToNetwork, &data);
		if (read <= 0)
			return;

		MS_DEBUG("%ld bytes of DTLS data ready to sent to the peer", read);

		// Notify the listener.
		this->listener->onOutgoingDtlsData(this, (uint8_t*)data, (size_t)read);

		// Clear the BIO buffer.
		// NOTE: the (void) avoids the -Wunused-value warning.
		(void)BIO_reset(this->sslBioToNetwork);
	}
示例#9
0
文件: ssl_api.c 项目: ewust/tapdance
int ssl_encrypt(SSL *s, const char *in, int len, char **out)
{

    int i;

    if (!BIO_eof(SSL_get_wbio(s))) {
      fprintf(stderr, "ssl_encrypt: Someone left data in the wbio!\n");
      fprintf(stderr, "In particular, this data:\n");
      fflush(stderr);
      char *data;
      long data_len = BIO_get_mem_data(SSL_get_wbio(s), &data);
      //hexdump(data, data_len);
      return -1;
    }

    i = SSL_write(s, in, len);
    if (i < 0) {
      fprintf(stderr, "ssl_encrypt: SSL_write returned < 0\n");
      fflush(stderr);
      return -1;
    }

    return fetch_data_from_bio(s, out);
}
char *
_openssl_get_subject (X509 *cert, char *buf, int size)
{
  BIO *subject_bio = NULL;
  BUF_MEM *subject_mem;
  char *subject = buf;
  int maxlen = size;

  subject_bio = BIO_new (BIO_s_mem ());
  if (subject_bio == NULL)
    goto err;

  X509_NAME_print_ex (subject_bio, X509_get_subject_name (cert),
                      0, XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN |
                      ASN1_STRFLGS_UTF8_CONVERT | ASN1_STRFLGS_ESC_CTRL);

  if (BIO_eof (subject_bio))
    goto err;

  BIO_get_mem_ptr (subject_bio, &subject_mem);
  if (subject == NULL)
    {
      maxlen = subject_mem->length + 1;
      subject = malloc (maxlen);
      check_malloc_return (subject);
    }

  memcpy (subject, subject_mem->data, maxlen);
  subject[maxlen - 1] = '\0';

err:
  if (subject_bio)
    BIO_free (subject_bio);

  return subject;
}
示例#11
0
int HsOpenSSL_BIO_eof(BIO* bio) {
    return BIO_eof(bio);
}
示例#12
0
int BIO_eof_shim(BIO *b) {
    return BIO_eof(b);
}