static int bio_rdp_tls_write(BIO* bio, const char* buf, int size) { int status; BIO_RDP_TLS* tls = (BIO_RDP_TLS*) bio->ptr; if (!buf || !tls) return 0; BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL); status = SSL_write(tls->ssl, buf, size); if (status <= 0) { switch (SSL_get_error(tls->ssl, status)) { case SSL_ERROR_NONE: BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); break; case SSL_ERROR_WANT_WRITE: BIO_set_flags(bio, BIO_FLAGS_WRITE); break; case SSL_ERROR_WANT_READ: BIO_set_flags(bio, BIO_FLAGS_READ); break; case SSL_ERROR_WANT_X509_LOOKUP: BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); bio->retry_reason = BIO_RR_SSL_X509_LOOKUP; break; case SSL_ERROR_WANT_CONNECT: BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL); bio->retry_reason = BIO_RR_CONNECT; break; case SSL_ERROR_SYSCALL: BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); break; case SSL_ERROR_SSL: BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); break; } } return status; }
ustring b64_encode (const char *buf, const size_t length) { bool succeeded = false; ustring rv; auto_BIO mem (BIO_new (BIO_s_mem ())); auto_BIO filter (BIO_new (BIO_f_base64())); if (!mem || !filter) throw SSL_ERROR; BIO_set_flags(((BIO *) filter), BIO_FLAGS_BASE64_NO_NL); BIO_push (filter, mem); if ((BIO_write (filter, buf, length) >= 0) && (1 == BIO_flush (filter))) { const int to_read = BIO_pending (mem); unsigned char *tmp_buf = (unsigned char *) VHTI_alloc (to_read + 1); if (BIO_read(mem, tmp_buf, to_read) >= 0) { tmp_buf[to_read] = '\0'; rv.assign (tmp_buf); succeeded = true; } free (tmp_buf); } if (!succeeded) throw SSL_ERROR; return rv; }
gchar* s3_base64_encode(const GByteArray *to_enc) { BIO *bio_b64 = NULL, *bio_buff = NULL; long bio_b64_len; char *bio_b64_data = NULL, *ret = NULL; if (!to_enc) return NULL; /* Initialize base64 encoding filter */ bio_b64 = BIO_new(BIO_f_base64()); g_assert(bio_b64); BIO_set_flags(bio_b64, BIO_FLAGS_BASE64_NO_NL); /* Initialize memory buffer for the base64 encoding */ bio_buff = BIO_new(BIO_s_mem()); g_assert(bio_buff); bio_buff = BIO_push(bio_b64, bio_buff); /* Write the MD5 hash into the buffer to encode it in base64 */ BIO_write(bio_buff, to_enc->data, to_enc->len); /* BIO_flush is a macro and GCC 4.1.2 complains without this cast*/ (void) BIO_flush(bio_buff); /* Pull out the base64 encoding of the MD5 hash */ bio_b64_len = BIO_get_mem_data(bio_buff, &bio_b64_data); g_assert(bio_b64_data); ret = g_strndup(bio_b64_data, bio_b64_len); /* If bio_b64 is freed separately, freeing bio_buff will * invalidly free memory and potentially segfault. */ BIO_free_all(bio_buff); return ret; }
static int transport_bio_simple_write(BIO* bio, const char* buf, int size) { int error; int status = 0; if (!buf) return 0; BIO_clear_flags(bio, BIO_FLAGS_WRITE); status = _send((SOCKET) bio->num, buf, size, 0); if (status <= 0) { error = WSAGetLastError(); if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) || (error == WSAEALREADY)) { BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY)); } else { BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); } } return status; }
char* encodeBase64(const char *input, size_t length) { if(length > 0) { BIO *bmem, *b64; BUF_MEM *bptr; b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_write(b64, input, length); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); char *buff = (char *)NFCD_MALLOC(bptr->length+1); memcpy(buff, bptr->data, bptr->length); buff[bptr->length] = 0; BIO_free_all(b64); return buff; } else { return ""; } }
/* caller must free the returned string */ char *base64_dec(unsigned char *in, int size) { BIO *bio64, *biomem; char *buf=NULL; buf = malloc(sizeof(char) * size); bzero(buf, size); if ((bio64 = BIO_new(BIO_f_base64())) == NULL) { logprintfl(EUCAERROR, "BIO_new(BIO_f_base64()) failed\n"); } else { BIO_set_flags (bio64, BIO_FLAGS_BASE64_NO_NL); /* no long-line wrapping */ if ((biomem = BIO_new_mem_buf(in, size)) == NULL) { logprintfl(EUCAERROR, "BIO_new_mem_buf() failed\n"); } else { biomem = BIO_push(bio64, biomem); if ((BIO_read(biomem, buf, size)) <= 0) { logprintfl(EUCAERROR, "BIO_read() read failed\n"); } // BIO_free_all(biomem); } BIO_free_all(bio64); } return buf; }
uint8_t* OT_base64_decode(const char *input, size_t* out_len, int bLineBreaks) { BIO *bmem = NULL, *b64 = NULL; OT_ASSERT(NULL != input); int in_len = strlen(input); int out_max_len=(in_len*6+7)/8; unsigned char *buf = new unsigned char [out_max_len]; OT_ASSERT(NULL != buf); memset(buf, 0, out_max_len); b64 = BIO_new(BIO_f_base64()); if (b64) { if (!bLineBreaks) { BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); } bmem = BIO_new_mem_buf((char*)input, in_len); b64 = BIO_push(b64, bmem); *out_len = BIO_read(b64, buf, out_max_len); BIO_free_all(b64); } else { OT_ASSERT_MSG(false, "Failed creating new Bio in base64_decode.\n"); } return buf; }
uint8_t *base64_dec(char *input, int *outlen) { BIO *bmem, *b64; int inlen = strlen(input); b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); // Apple cut the padding off their challenges; restore it BIO_write(bmem, input, inlen); while (inlen++ & 3) BIO_write(bmem, "=", 1); BIO_flush(bmem); int bufsize = strlen(input) * 3 / 4 + 1; uint8_t *buf = malloc(bufsize); int nread; nread = BIO_read(b64, buf, bufsize); BIO_free_all(bmem); *outlen = nread; return buf; }
void fromBase64(const string& input, vector<uint8_t>& output) { // openssl doesn't like whitespace, so remove it. string cleanInput(input); cleanInput.erase (remove_if(cleanInput.begin(), cleanInput.end(), ::isspace), cleanInput.end()); BIO *base64 = BIO_new(BIO_f_base64()); if (!base64) throw runtime_error("fromBase64: BIO_new failed"); BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL); BIO *inputBuffer = BIO_new_mem_buf((char*)&cleanInput[0], cleanInput.size()); if (!inputBuffer) { BIO_free(base64); throw runtime_error("fromBase64: BIO_new_mem_buf failed"); } inputBuffer = BIO_push(base64, inputBuffer); // The output will be shorter than the input. output.resize(cleanInput.size()); int outputLength = BIO_read(inputBuffer, &output[0], cleanInput.size()); if (outputLength < 0) { BIO_free_all(inputBuffer); throw runtime_error("fromBase64: BIO_read failed to decode"); } output.resize(outputLength); BIO_free_all(inputBuffer); }
string toBase64(const uint8_t* array, size_t arrayLength, bool addNewlines) { BIO *base64 = BIO_new(BIO_f_base64()); if (!base64) throw runtime_error("toBase64: BIO_new failed"); if (!addNewlines) BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL); BIO *outputBuffer = BIO_new(BIO_s_mem()); if (!outputBuffer) { BIO_free(base64); throw runtime_error("toBase64: BIO_new failed"); } outputBuffer = BIO_push(base64, outputBuffer); if (BIO_write(base64, array, arrayLength) <= 0) { BIO_free_all(outputBuffer); throw runtime_error("toBase64: BIO_write failed"); } BIO_flush(base64); char *bufferPointer; size_t bufferSize = BIO_get_mem_data(outputBuffer, &bufferPointer); string result(bufferPointer, bufferSize); BIO_free_all(outputBuffer); return result; }
static int transport_bio_simple_read(BIO* bio, char* buf, int size) { int error; int status = 0; if (!buf) return 0; BIO_clear_flags(bio, BIO_FLAGS_READ); status = _recv((SOCKET) bio->num, buf, size, 0); if (status > 0) return status; if (status == 0) { BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); return 0; } error = WSAGetLastError(); if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) || (error == WSAEALREADY)) { BIO_set_flags(bio, (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY)); } else { BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); } return -1; }
void encode(const type& ascii, type& base64) { BIO *bio, *b64; BUF_MEM *bptr; b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bio = BIO_new(BIO_s_mem()); BIO_push(b64, bio); BIO_get_mem_ptr(b64, &bptr); //Write directly to base64-buffer to avoid copy int base64_length=static_cast<int>(round(4*ceil((double)ascii.size()/3.0))); base64.resize(base64_length); bptr->length=0; bptr->max=base64_length+1; bptr->data=(char*)&base64[0]; BIO_write(b64, &ascii[0], static_cast<int>(ascii.size())); BIO_flush(b64); //To keep &base64[0] through BIO_free_all(b64) bptr->length=0; bptr->max=0; bptr->data=nullptr; BIO_free_all(b64); }
char *base64_decode(const char *str) { BIO *bio, *base64_filter, *bio_out; char inbuf[512]; int inlen; base64_filter = BIO_new(BIO_f_base64()); BIO_set_flags(base64_filter, BIO_FLAGS_BASE64_NO_NL); bio = BIO_new_mem_buf((void*)str, strlen(str)); bio = BIO_push(base64_filter, bio); bio_out = BIO_new(BIO_s_mem()); while((inlen = BIO_read(bio, inbuf, 512)) > 0 ) { BIO_write(bio_out, inbuf, inlen); } BIO_flush(bio_out); char *new_data; long bytes_written = BIO_get_mem_data(bio_out, &new_data); BIO_free_all(bio); BIO_free_all(bio_out); return new_data; }
static std::string encode(const std::string &ascii) noexcept { std::string base64; BIO *bio, *b64; BUF_MEM *bptr = BUF_MEM_new(); b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bio = BIO_new(BIO_s_mem()); BIO_push(b64, bio); BIO_set_mem_buf(b64, bptr, BIO_CLOSE); // Write directly to base64-buffer to avoid copy auto base64_length = static_cast<size_t>(round(4 * ceil(static_cast<double>(ascii.size()) / 3.0))); base64.resize(base64_length); bptr->length = 0; bptr->max = base64_length + 1; bptr->data = &base64[0]; if(BIO_write(b64, &ascii[0], static_cast<int>(ascii.size())) <= 0 || BIO_flush(b64) <= 0) base64.clear(); // To keep &base64[0] through BIO_free_all(b64) bptr->length = 0; bptr->max = 0; bptr->data = nullptr; BIO_free_all(b64); return base64; }
ustring b64_decode (const ustring &message) { bool succeeded = false; auto_BIO mem (BIO_new (BIO_s_mem ())); auto_BIO filter (BIO_new (BIO_f_base64())); ustring rv; BIO_set_flags(((BIO *) filter), BIO_FLAGS_BASE64_NO_NL); BIO_push (filter, mem); if ((BIO_write (mem, message.data (), message.size ()) >= 0) && (1 == BIO_flush (mem))) { const unsigned int Length = BIO_pending (filter); { unsigned char *tmp_buf = (unsigned char *) VHTI_alloc (Length); const int nread = BIO_read(filter, tmp_buf, Length); if (nread >= 0) { succeeded = true; rv.assign (tmp_buf, nread); } free (tmp_buf); } } if (!succeeded) throw SSL_ERROR; return rv; }
static int rdg_bio_read(BIO* bio, char* buf, int size) { int status; rdpRdg* rdg = (rdpRdg*) bio->ptr; status = rdg_read_data_packet(rdg, (BYTE*) buf, size); if (status < 0) { BIO_clear_retry_flags(bio); return -1; } else if (status == 0) { BIO_set_retry_read(bio); WSASetLastError(WSAEWOULDBLOCK); return -1; } else { BIO_set_flags(bio, BIO_FLAGS_READ); } return status; }
int dtls1_read_failed(SSL *s, int code) { DTLS1_STATE *state; BIO *bio; int send_alert = 0; if ( code > 0) { fprintf( stderr, "invalid state reached %s:%d", __FILE__, __LINE__); return 1; } bio = SSL_get_rbio(s); if ( ! BIO_dgram_recv_timedout(bio)) { /* not a timeout, none of our business, let higher layers handle this. in fact it's probably an error */ return code; } if ( ! SSL_in_init(s)) /* done, no need to send a retransmit */ { BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ); return code; } state = s->d1; state->timeout.num_alerts++; if ( state->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) { /* fail the connection, enough alerts have been sent */ SSLerr(SSL_F_DTLS1_READ_FAILED,SSL_R_READ_TIMEOUT_EXPIRED); return 0; } state->timeout.read_timeouts++; if ( state->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) { send_alert = 1; state->timeout.read_timeouts = 1; } #if 0 /* for now, each alert contains only one record number */ item = pqueue_peek(state->rcvd_records); if ( item ) { /* send an alert immediately for all the missing records */ } else #endif #if 0 /* no more alert sending, just retransmit the last set of messages */ if ( send_alert) ssl3_send_alert(s,SSL3_AL_WARNING, DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); #endif return dtls1_retransmit_buffered_messages(s) ; }
void LoginSession::StartSsoLogin(XMLPacket *Packet) { rapidxml::xml_node<>* requestNode = Packet->m_XMLDocument.first_node("Request"); char password[1024]; memset(password, 0, 1024); int passwordLength = -1; char* username = nullptr; char* passwordBase64 = nullptr; try { username = requestNode->first_node("LoginName")->value(); if (requestNode->first_node("Password") != nullptr) { passwordBase64 = requestNode->first_node("Password")->value(); auto bio = BIO_new_mem_buf(passwordBase64, -1); auto b64 = BIO_new(BIO_f_base64()); bio = BIO_push(b64, bio); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); passwordLength = BIO_read(bio, password, strlen(passwordBase64)); BIO_free_all(bio); } } catch(std::exception ex) { printf("Password tokens not supported.\n"); return; } // TODO: add support for resume tokens if (passwordBase64 == nullptr) { printf("Resume tokens not supported yet\n"); m_TSLReady = false; return; } printf("Login >> %s with %s\n", username, password); int sequence = Packet->m_Meta[2] - '0'; const char* temporary_guid = "0687C32C-0331-E611-80C3-ECB1D78A5C75"; const char* temporary_resumeToken = "22236HTR-CCCC-CCCC-CCCC-2310CCCCC93A"; const char* temporary_username = "******"; GW2Packet replyPacket("", sequence, PT_REPLY); replyPacket.AddElement("UserId", temporary_guid); replyPacket.AddElement("UserCenter", "5"); replyPacket.AddElement("UserName", temporary_username); replyPacket.AddElement("Parts", ""); replyPacket.AddElement("ResumeToken", temporary_resumeToken); replyPacket.AddElement("EmailVerified", "1"); SessionSendPacket packet; memset(packet.m_TLSSendBuffer, 0, 4096); sprintf(packet.m_TLSSendBuffer, replyPacket.Payload()); packet.m_TLSSendBufferLength = strlen(packet.m_TLSSendBuffer); packet.m_TLSSendNeeded = true; m_SendPackets.push_back(packet); }
/** * @brief Base64 decode input string \p in of size \p insize. * @returns -1 on invalid Base64, or 0 on successes in which case a * newly allocated binary string is set in out (and size). */ static int rd_base64_decode (const rd_chariov_t *in, rd_chariov_t *out) { size_t asize; BIO *b64, *bmem; if (in->size == 0 || (in->size % 4) != 0) return -1; asize = (in->size * 3) / 4; /* allocation size */ out->ptr = rd_malloc(asize+1); b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bmem = BIO_new_mem_buf(in->ptr, (int)in->size); bmem = BIO_push(b64, bmem); out->size = BIO_read(bmem, out->ptr, (int)asize+1); assert(out->size <= asize); BIO_free_all(bmem); #if ENABLE_DEVEL /* Verify that decode==encode */ { char *encoded = rd_base64_encode(out); assert(strlen(encoded) == in->size); assert(!strncmp(encoded, in->ptr, in->size)); rd_free(encoded); } #endif return 0; }
/* * des - base64编码,将二进制字符与64个可打印字符进行对应转化。 2^6 = 64,6bits对应一个字符,三个字节对应四个可见字符 * param - str : 需编码的数据 * str_len : str的长度 * encode : 编码后数据存储 * encode_len : encode缓冲区的长度,要求大小需大于str_len,建议为(str_len/3) * 4 + 8 * ret - success : 编码后数据实际长度 * fail : -1 */ int base64_encode(char *str,int str_len,char *encode, u_int encode_len) { BIO *bmem,*b64; BUF_MEM *bptr; b64=BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bmem=BIO_new(BIO_s_mem()); b64=BIO_push(b64, bmem); BIO_write(b64, str, str_len); //encode BIO_flush(b64); BIO_get_mem_ptr(b64,&bptr); // printf("%d\n", bptr->length); if(bptr->length > encode_len){ printf("encode_len too small\n"); return -1; } encode_len=bptr->length; memcpy(encode,bptr->data,bptr->length); encode[bptr->length] = '\0'; BIO_free_all(b64); return encode_len; }
/* caller must free the returned string */ char * base64_enc (unsigned char * in, int size) { char * out_str = NULL; BIO * biomem, * bio64; if ( (bio64 = BIO_new (BIO_f_base64 ())) == NULL) { logprintfl (EUCAERROR, "error: BIO_new(BIO_f_base64()) failed\n"); } else { BIO_set_flags (bio64, BIO_FLAGS_BASE64_NO_NL); /* no long-line wrapping */ if ( (biomem = BIO_new (BIO_s_mem ())) == NULL) { logprintfl (EUCAERROR, "error: BIO_new(BIO_s_mem()) failed\n"); } else { bio64 = BIO_push (bio64, biomem); if ( BIO_write (bio64, in, size)!=size) { logprintfl (EUCAERROR, "error: BIO_write() failed\n"); } else { BUF_MEM * buf; (void) BIO_flush (bio64); BIO_get_mem_ptr (bio64, &buf); if ( (out_str = malloc(buf->length+1)) == NULL ) { logprintfl (EUCAERROR, "error: out of memory for Base64 buf\n"); } else { memcpy (out_str, buf->data, buf->length); out_str [buf->length] = '\0'; } } } BIO_free_all (bio64); /* frees both bio64 and biomem */ } return out_str; }
static char *base64(const char *bytes, size_t len) { BIO *memory, *b64; BUF_MEM *buffer; char *output; b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); memory = BIO_new(BIO_s_mem()); if (!b64 || !memory) goto error; b64 = BIO_push(b64, memory); if (!b64) goto error; if (BIO_write(b64, bytes, len) < 0 || BIO_flush(b64) < 0) goto error; BIO_get_mem_ptr(b64, &buffer); output = xmalloc(buffer->length + 1); memcpy(output, buffer->data, buffer->length); output[buffer->length] = '\0'; BIO_free_all(b64); return output; error: die("Could not base64 the given bytes."); }
static Buffer::Ptr base64Decode(T t) { if (!t) return Buffer::null(); if (!t->length()) return Buffer::create(); std::string src = t->toStdString(); Size len = src.length(); if (len != t->length()) return Buffer::null(); BIO* bio = BIO_new(BIO_f_base64()); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); BIO* bioMem = BIO_new_mem_buf(const_cast<char*>(src.c_str()), len); bio = BIO_push(bio, bioMem); char* dst = new char[len + 1]; int readLen = BIO_read(bio, dst, len); Buffer::Ptr decoded; if (readLen > 0) { decoded = Buffer::create(dst, readLen); } else { decoded = Buffer::null(); } BIO_free_all(bio); delete[] dst; return decoded; }
static size_t unbase64(const char *bytes, char **unbase64) { size_t len; BIO *memory, *b64; char *buffer; len = strlen(bytes); if (!len) goto error; b64 = BIO_new(BIO_f_base64()); memory = BIO_new_mem_buf((char *)bytes, len); if (!b64 || !memory) goto error; b64 = BIO_push(b64, memory); if (!b64) goto error; BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); buffer = xcalloc(len + 1, 1); len = BIO_read(b64, buffer, len); if ((int)len <= 0) goto error; buffer[len] = '\0'; BIO_free_all(b64); *unbase64 = buffer; return len; error: die("Could not unbase64 the given bytes."); }
char* decodeBase64(unsigned char *input, size_t length, size_t *out_length) { BIO *b64, *bmem; size_t decodedLen; char *buffer = (char*)NFCD_MALLOC(length+1); if (buffer == NULL) { return NULL; } memset(buffer, 0, length); b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) { return NULL; } BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bmem = BIO_new_mem_buf(input, length); bmem = BIO_push(b64, bmem); decodedLen = BIO_read(bmem, buffer, length); buffer[decodedLen] = 0; *out_length = decodedLen; BIO_free_all(bmem); return buffer; }
static int base64Encode(char* output, const unsigned char* data, size_t length) { BIO* base64 = BIO_new(BIO_f_base64()); if (base64 == NULL) { return -1; } BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL); BIO* memory = BIO_new(BIO_s_mem()); if (memory == NULL) { BIO_free_all(base64); return -1; } BIO* bio = BIO_push(base64, memory); BIO_write(bio, data, length); if (BIO_flush(bio) == -1) { return -1; } const char* p; size_t n = BIO_get_mem_data(memory, &p); int i; for (i = 0; i < n; ++i) { if (*(p+i) == '+') { *(output+i) = '-'; } else if (*(p+i) == '/') { *(output+i) = '_'; } else if (*(p+i) == '=') { break; } else { *(output+i) = *(p+i); } } BIO_free_all(bio); return n; }
char *base64(const unsigned char *input, int length) { BIO *bmem, *b64; BUF_MEM *bptr; b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_write(b64, magic, sizeof(magic)-1); BIO_write(b64, (char*)salt, sizeof(salt)); BIO_write(b64, input, length); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); char *buff = (char *)malloc(bptr->length); // memcpy(buff, bptr->data, bptr->length-1); int chars = 0; for (unsigned int i = 0; i < bptr->length-1; i++) { if (bptr->data[i] == '+') { buff[chars] = '-'; chars++;} else if (bptr->data[i] == '/') { buff[chars] = '_'; chars++; } else if (bptr->data[i] != '=') { buff[chars] = bptr->data[i]; chars++; } } buff[chars] = 0; BIO_free_all(b64); return buff; }
json_t * clevis_buf_encode(const clevis_buf_t *buf) { json_t *out = NULL; BIO *mem = NULL; BIO *b64 = NULL; char *c = NULL; int r = 0; mem = BIO_new(BIO_s_mem()); if (!mem) goto egress; b64 = BIO_new(BIO_f_base64()); if (!b64) goto egress; BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); if (!BIO_push(b64, mem)) goto egress; r = BIO_write(b64, buf->buf, buf->len); if (r != (int) buf->len) goto egress; BIO_flush(b64); r = BIO_get_mem_data(mem, &c); out = json_stringn(c, r); egress: BIO_free(b64); BIO_free(mem); return out; }
static int bio_rdp_tls_free(BIO* bio) { BIO_RDP_TLS* tls; if (!bio) return 0; tls = (BIO_RDP_TLS*) BIO_get_data(bio); if (!tls) return 0; if (BIO_get_shutdown(bio)) { if (BIO_get_init(bio) && tls->ssl) { SSL_shutdown(tls->ssl); SSL_free(tls->ssl); } BIO_set_init(bio, 0); BIO_set_flags(bio, 0); } DeleteCriticalSection(&tls->lock); free(tls); return 1; }
static int transport_bio_simple_uninit(BIO* bio) { WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*) BIO_get_data(bio); if (BIO_get_shutdown(bio)) { if (BIO_get_init(bio)) { _shutdown(ptr->socket, SD_BOTH); closesocket(ptr->socket); ptr->socket = 0; } } if (ptr->hEvent) { CloseHandle(ptr->hEvent); ptr->hEvent = NULL; } BIO_set_init(bio, 0); BIO_set_flags(bio, 0); return 1; }