struct X509_flat * flatten_X509(X509 *x) { struct X509_flat *out = NULL; int ret; BUF_MEM *bptr = NULL; BIO *mem = NULL; if (x == NULL) { return NULL; } mem = BIO_new(BIO_s_mem()); if (mem == NULL) { return NULL; } ret = PEM_write_bio_X509(mem, x); if (ret == 0) { BIO_free(mem); return NULL; } out = new_X509_flat(); if (out == NULL) { BIO_free(mem); return NULL; } BIO_get_mem_ptr(mem, &bptr); assert(BIO_set_close(mem, BIO_NOCLOSE) == 1); BIO_free(mem); out->len = bptr->length; if (bptr->length != 0 && (size_t) bptr->length <= SIZE_MAX/sizeof(*(out->data))) { out->data = malloc(bptr->length*sizeof(*(out->data))); } if (out->data == NULL) { BUF_MEM_free(bptr); return NULL; } memcpy(out->data, bptr->data, bptr->length); BUF_MEM_free(bptr); return out; }
char* encode_base64(unsigned char* data, size_t length) { BIO *bmem, *b64; BUF_MEM *bufmem; b64 = BIO_new(BIO_f_base64()); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); BIO_write(b64, data, length); BIO_flush(b64); BIO_get_mem_ptr(b64, &bufmem); BIO_set_close(b64, BIO_NOCLOSE); BIO_free_all(b64); return bufmem->data; }
int utils::Base64Encode(const unsigned char* buffer, size_t length, char** b64text) { //Encodes a binary safe base 64 string BIO *bio, *b64; BUF_MEM *bufferPtr; b64 = BIO_new(BIO_f_base64()); bio = BIO_new(BIO_s_mem()); bio = BIO_push(b64, bio); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Ignore newlines - write everything in one line BIO_write(bio, buffer, length); BIO_flush(bio); BIO_get_mem_ptr(bio, &bufferPtr); BIO_set_close(bio, BIO_NOCLOSE); BIO_free_all(bio); *b64text=(*bufferPtr).data; return (0); //success }
static LUA_FUNCTION(openssl_pkcs7_decrypt) { PKCS7 *p7 = CHECK_OBJECT(1, PKCS7, "openssl.pkcs7"); X509 *cert = CHECK_OBJECT(2, X509, "openssl.x509"); EVP_PKEY *key = CHECK_OBJECT(3, EVP_PKEY, "openssl.evp_pkey"); long flags = luaL_optint(L, 4, 0); BIO *out = BIO_new(BIO_s_mem()); if (PKCS7_decrypt(p7, key, cert, out, flags)) { BUF_MEM* mem; BIO_get_mem_ptr(out, &mem); lua_pushlstring(L, mem->data, mem->length); } else lua_pushnil(L); BIO_free(out); return 1; }
static LUA_FUNCTION(openssl_pkcs7_read) { BIO* bio = load_bio_object(L, 1); int fmt = luaL_checkoption(L, 2, "auto", format); PKCS7 *p7 = NULL; BIO* ctx = NULL; if (fmt == FORMAT_AUTO) { fmt = bio_is_der(bio) ? FORMAT_DER : FORMAT_PEM; } if (fmt == FORMAT_DER) { p7 = d2i_PKCS7_bio(bio, NULL); BIO_reset(bio); }else if (fmt == FORMAT_PEM) { p7 = PEM_read_bio_PKCS7(bio, NULL, NULL, NULL); BIO_reset(bio); }else if (fmt == FORMAT_SMIME) { p7 = SMIME_read_PKCS7(bio, &ctx); } BIO_free(bio); if (p7) { PUSH_OBJECT(p7, "openssl.pkcs7"); if (ctx) { BUF_MEM* mem; BIO_get_mem_ptr(ctx, &mem); lua_pushlstring(L, mem->data, mem->length); BIO_free(ctx); return 2; } return 1; } return openssl_pushresult(L, 0); }
//------------------------------------------------------------------------------ // helper function to encode a vector of bytes to a base64 string: static std::string b64_encode(const std::vector<unsigned char>& data) { BIO* b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); BIO* bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_write(b64, data.data(), data.size()); BIO_flush(b64); BUF_MEM* bptr = NULL; BIO_get_mem_ptr(b64, &bptr); std::string output(bptr->data, bptr->length); BIO_free_all(b64); return output; }
char *base64(const unsigned char *input, int length) { BIO *bmem, *b64; BUF_MEM *bptr; b64 = BIO_new(BIO_f_base64()); 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 *)malloc(bptr->length); memcpy(buff, bptr->data, bptr->length-1); buff[bptr->length-1] = 0; BIO_free_all(b64); return buff; }
passwand_error_t encode(const uint8_t *s, size_t len, char **e) { assert(s != NULL); assert(e != NULL); if (len > (size_t)INT_MAX) return PW_OVERFLOW; /* Create a base64 filter. */ BIO *b64 __attribute__((cleanup(autobiofree))) = BIO_new(BIO_f_base64()); if (b64 == NULL) return PW_NO_MEM; BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); /* Create an in-memory sink to encode data into. */ BIO *out = BIO_new(BIO_s_mem()); if (out == NULL) return PW_NO_MEM; BIO *pipe __attribute__((cleanup(autobiofree))) = BIO_push(b64, out); b64 = NULL; /* Encode the data. */ if (BIO_write(pipe, s, len) != (int)len) return PW_IO; BIO_flush(pipe); /* Extract it into a string. */ BUF_MEM *bptr; BIO_get_mem_ptr(out, &bptr); if (SIZE_MAX - 1 < bptr->length) return PW_OVERFLOW; *e = malloc(bptr->length + 1); if (*e == NULL) return PW_NO_MEM; memcpy(*e, bptr->data, bptr->length); (*e)[bptr->length] = '\0'; return PW_OK; }
//Function to use openssl to do BASE64 encoding static bool base_64_encode(const uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t *out_size) { BIO* bioMem = NULL; bool ret = false; BIO *bio64 = NULL; BIO_METHOD *bm = BIO_f_base64(); if(bm == NULL) goto ret_point; bio64 = BIO_new(bm); if(bio64 == NULL) goto ret_point; BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL); bm = BIO_s_mem(); if(bm == NULL) goto ret_point; bioMem = BIO_new(bm); if(bioMem == NULL) goto ret_point; (void)BIO_push(bio64, bioMem); if(BIO_write(bio64, in_buf, in_size) != (int)in_size){ goto ret_point; } (void)BIO_flush(bio64); BUF_MEM *bptr; BIO_get_mem_ptr(bio64, &bptr); if(bptr==NULL){ goto ret_point; } if(*out_size < bptr->length){ goto ret_point; } if(memcpy_s(out_buf, *out_size,bptr->data, bptr->length)!=0) goto ret_point; *out_size = static_cast<uint32_t>(bptr->length); ret = true; ret_point: BIO_free_all(bio64);//we need not free bioMem too because the free_all has free it. return ret; }
// ============================================================================ std::string Crypto::base64(const std::vector<uint8_t>& input) { BIO *bmem, *b64; BUF_MEM* bptr; std::string result; b64 = BIO_new(BIO_f_base64()); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL); BIO_write(b64, input.data(), (int)input.size()); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); result.resize(bptr->length - 1); memcpy(&result[0], bptr->data, bptr->length - 1); BIO_free_all(b64); return result; } // base64
static int base64_encode(char *str,int str_len,char *encode,int encode_len) { BIO *bmem,*b64; BUF_MEM *bptr; b64=BIO_new(BIO_f_base64()); 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); if(bptr->length>encode_len){ oss_log_write("encode_len too small\n"); return -1; } encode_len=bptr->length; memcpy(encode,bptr->data,bptr->length); // write(1,encode,bptr->length); BIO_free_all(b64); return encode_len; }
gchar* encodeBase64(gchar *input, gint length) { BIO *b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); BIO *bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_write(b64, input, length); BIO_flush(b64); BUF_MEM *buffer; BIO_get_mem_ptr(b64, &buffer); gchar *data = g_malloc(buffer->length); memcpy(data, buffer->data, buffer->length - 1); data[buffer->length - 1] = 0; BIO_free_all(b64); return data; }
char *Base64::encode(const char *input, int size) { BIO *bmem, *b64; BUF_MEM *bptr; b64 = BIO_new(BIO_f_base64()); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_write(b64, input, size); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); char *buff = (char *)Memory::Alloc(bptr->length); memcpy(buff, bptr->data, bptr->length-1); buff[bptr->length-1] = 0; BIO_free_all(b64); return buff; }
static char *base64(char *str, size_t *len) { BIO *b64, *bmem; BUF_MEM *bptr; b64 = BIO_new(BIO_f_base64()); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_write(b64, str, *len); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); char *buf = malloc(bptr->length-1); memcpy(buf, bptr->data, bptr->length-1); *len = bptr->length-1; BIO_free_all(b64); return buf; }
std::streamsize Connection::write(const char* buf, std::size_t n) { std::streambuf* sb = _ios->rdbuf(); if( ! sb) return 0; std::streamsize written = SSL_write(_ssl, buf, n); log_debug("encrypted " << written << " bytes"); BUF_MEM* bm = 0; BIO_get_mem_ptr(_out, &bm); if(bm->length > 0) { sb->sputn(bm->data, bm->length); log_debug("wrote " << bm->length << " bytes to output"); bm->length = 0; } return written; }
/** * BASE64编码 * * LUA示例: * local codec = require('codec') * local bs = [[...]] --源内容 * local result = codec.base64_encode(bs) */ static int codec_base64_encode(lua_State *L) { size_t len; const char *bs = luaL_checklstring(L, 1, &len); BIO *b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); BIO *bio = BIO_new(BIO_s_mem()); bio = BIO_push(b64, bio); BIO_write(bio, bs, len); BIO_flush(bio); BUF_MEM *p; BIO_get_mem_ptr(bio, &p); int n = p->length; char dst[n]; memcpy(dst, p->data, n); BIO_free_all(bio); lua_pushlstring(L, dst, n); return 1; }
static int openssl_ssl_session_export(lua_State*L) { SSL_SESSION* session = CHECK_OBJECT(1, SSL_SESSION, "openssl.ssl_session"); int pem = lua_isnoneornil(L, 2) ? 1 : auxiliar_checkboolean(L, 2); BIO* bio = BIO_new(BIO_s_mem()); BUF_MEM *bio_buf; if (pem) { PEM_write_bio_SSL_SESSION(bio, session); } else { i2d_SSL_SESSION_bio(bio, session); } BIO_get_mem_ptr(bio, &bio_buf); lua_pushlstring(L, bio_buf->data, bio_buf->length); BIO_free(bio); return 1; }
/** Helper function to implement crypto_pk_write_*_key_to_string. */ int crypto_pk_write_key_to_string_impl(crypto_pk_t *env, char **dest, size_t *len, int is_public) { BUF_MEM *buf; BIO *b; int r; // assert(env); // assert(env->key); // assert(dest); b = BIO_new(BIO_s_mem()); /* Create a memory BIO */ if (!b) return -1; /* Now you can treat b as if it were a file. Just use the * PEM_*_bio_* functions instead of the non-bio variants. */ if (is_public) r = PEM_write_bio_RSAPublicKey(b, env->key); else r = PEM_write_bio_RSAPrivateKey(b, env->key, NULL,NULL,0,NULL,NULL); if (!r) { err(1, "writing RSA key to string"); BIO_free(b); return -1; } BIO_get_mem_ptr(b, &buf); (void)BIO_set_close(b, BIO_NOCLOSE); /* so BIO_free doesn't free buf */ BIO_free(b); *dest = sgx_malloc(buf->length+1); sgx_memcpy(*dest, buf->data, buf->length); (*dest)[buf->length] = 0; /* nul terminate it */ *len = buf->length; BUF_MEM_free(buf); return 0; }
char *bin2Base64PlusSlashEqualsMultiLine(const unsigned char *input, int length) { // from http://www.ioncannon.net/programming/34/howto-base64-encode-with-cc-and-openssl/ BIO *bmem, *b64; BUF_MEM *bptr; b64 = BIO_new(BIO_f_base64()); 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 *)malloc(bptr->length); memcpy(buff, bptr->data, bptr->length-1); buff[bptr->length-1] = 0; BIO_free_all(b64); return buff; }
/** dsa_get_ppk Returns a dynamically allocated string like the following: -----BEGIN PUBLIC KEY----- ...Some, uh, public key... -----END PUBLIC KEY----- or NULL on error. Note that the pointer is dynamically allocated. */ char *dsa_get_ppk() { BIO *fsu = BIO_new(BIO_s_mem()); BUF_MEM *buf; char *realbuf = NULL; if(fsu == NULL) return NULL; PEM_write_bio_DSA_PUBKEY(fsu, probe); BIO_flush(fsu); BIO_get_mem_ptr(fsu, &buf); if((realbuf = malloc(53 + buf->length)) != NULL) { memcpy(realbuf, buf->data, buf->length); realbuf[buf->length] = '\0'; } BIO_free(fsu); return realbuf; }
char* CryptoHandler::base64encode(const unsigned char *input, const 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, input, length); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); char *buff = (char *)malloc(bptr->length); memcpy(buff, bptr->data, bptr->length-1); buff[bptr->length-1] = 0; BIO_free_all(b64); return buff; }
char *base64_enc(uint8_t *input, int length) { BIO *bmem, *b64; BUF_MEM *bptr; b64 = BIO_new(BIO_f_base64()); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); BIO_write(b64, input, length); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); char *buf = (char *)malloc(bptr->length); if (bptr->length) { memcpy(buf, bptr->data, bptr->length - 1); buf[bptr->length - 1] = 0; } BIO_free_all(bmem); return buf; }
static VALUE t_get_peer_cert (VALUE self UNUSED, VALUE signature) { VALUE ret = Qnil; X509 *cert = NULL; BUF_MEM *buf; BIO *out; cert = evma_get_peer_cert (NUM2BSIG (signature)); if (cert != NULL) { out = BIO_new(BIO_s_mem()); PEM_write_bio_X509(out, cert); BIO_get_mem_ptr(out, &buf); ret = rb_str_new(buf->data, buf->length); X509_free(cert); BIO_free(out); } return ret; }
/* * Trivial utility function to extract the string * value of the subject name from a cert. */ static void extract_sub_name (X509 *cert, char *name, int len) { X509_NAME *subject_nm; BIO *out; BUF_MEM *bm; subject_nm = X509_get_subject_name(cert); out = BIO_new(BIO_s_mem()); X509_NAME_print_ex(out, subject_nm, 0, XN_FLAG_SEP_SPLUS_SPC); BIO_get_mem_ptr(out, &bm); strncpy(name, bm->data, len); if (bm->length < len) { name[bm->length] = 0; } else { name[len] = 0; } BIO_free(out); }
void base64_encode(unsigned char *input, int length,char *output, int *output_len){ BIO *bmem, *b64; BUF_MEM *bptr; char *buff; 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); //buff = (char *)malloc(bptr->length); memcpy(output, bptr->data, bptr->length); output[bptr->length] = 0; *output_len = bptr->length; BIO_free_all(b64); }
static VALUE ossl_x509crl_get_signature_algorithm(VALUE self) { X509_CRL *crl; BIO *out; BUF_MEM *buf; VALUE str; GetX509CRL(self, crl); if (!(out = BIO_new(BIO_s_mem()))) { ossl_raise(eX509CRLError, NULL); } if (!i2a_ASN1_OBJECT(out, crl->sig_alg->algorithm)) { BIO_free(out); ossl_raise(eX509CRLError, NULL); } BIO_get_mem_ptr(out, &buf); str = rb_str_new(buf->data, buf->length); BIO_free(out); return str; }
/*This function will Base-64 encode your data.*/ char * base64encode (const void *b64_encode_me, int encode_this_many_bytes) { BIO *b64_bio, *mem_bio; //Declare two BIOs. One base64 encodes, the other stores memory. BUF_MEM *mem_bio_mem_ptr; //Pointer to the "memory BIO" structure holding the base64 data. b64_bio = BIO_new(BIO_f_base64()); //Initialize our base64 filter BIO. mem_bio = BIO_new(BIO_s_mem()); //Initialize our memory sink BIO. BIO_push(b64_bio, mem_bio); //Link the BIOs (i.e. create a filter-sink BIO chain.) BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL); //Don't add a newline every 64 characters. BIO_write(b64_bio, b64_encode_me, encode_this_many_bytes); //Encode and write our b64 data. BIO_flush(b64_bio); //Flush data. Necessary for b64 encoding, because of pad characters. BIO_get_mem_ptr(mem_bio, &mem_bio_mem_ptr); //Store address of mem_bio's memory structure. BIO_set_close(mem_bio,BIO_NOCLOSE); //Permit access to mem_ptr after BIOs are destroyed. BIO_free_all(b64_bio); //Destroys all BIOs in chain, starting with b64 (i.e. the 1st one). (*mem_bio_mem_ptr).data[(*mem_bio_mem_ptr).length] = '\0'; //Adds a null-terminator. return (*mem_bio_mem_ptr).data; //Returns base-64 encoded data. (See: "buf_mem_st" struct). }
int SocketClient::pinCertCallback(int pok, X509_STORE_CTX *ctx) { std::cout << "Checking pinned certificate" << std::endl; X509 *cert = NULL; BIO *b64 = NULL; BUF_MEM *bptr = NULL; char *szCert = NULL; cert = ctx->current_cert; assert(cert != NULL); b64 = BIO_new(BIO_s_mem()); assert(b64 != NULL); assert(1 == PEM_write_bio_X509(b64, cert)); BIO_get_mem_ptr(b64, &bptr); assert(NULL != (szCert = (char*)malloc(bptr->length + 1))); assert(0 < BIO_read(b64, szCert, bptr->length)); int ret = strncmp(szCert, AUTH_CERTIFICATE, strlen(AUTH_CERTIFICATE)); free(szCert); if (b64) { BIO_free(b64); } if(ret == 0) { std::cout << "pinned certificate verification passed..." << std::endl; return 1; } else { std::cout << "pinned certificate verification failed..." << std::endl; return 0; } }
PKI_MEM *PKI_MEM_new_func_bio ( void *obj, int (*func)(BIO *, void *) ) { BIO *bio_mem = NULL; PKI_MEM *ret = NULL; BUF_MEM *bio_mem_ptr = NULL; int i = 0; size_t size = 0; if (!obj || !func ) { return NULL; } if(( bio_mem = BIO_new(BIO_s_mem())) == NULL ) { return NULL; } // fprintf( stderr, "BIO=>%p -- OBJ=>%p\n", bio_mem, obj ); if((i = func( bio_mem, obj )) <= 0 ) { return NULL; } BIO_get_mem_ptr( bio_mem, &bio_mem_ptr ); if ( bio_mem_ptr == NULL ) { if( bio_mem ) BIO_free ( bio_mem ); return ( NULL ); } /* Adds the data to the return PKI_MEM */ size = (size_t) bio_mem_ptr->length; ret = PKI_MEM_new_data( size, (unsigned char *) bio_mem_ptr->data); /* Closes the BIO and frees the memory */ if( bio_mem ) BIO_free ( bio_mem ); return ( ret ); }
static VALUE ossl_x509crl_to_text(VALUE self) { X509_CRL *crl; BIO *out; BUF_MEM *buf; VALUE str; GetX509CRL(self, crl); if (!(out = BIO_new(BIO_s_mem()))) { ossl_raise(eX509CRLError, NULL); } if (!X509_CRL_print(out, crl)) { BIO_free(out); ossl_raise(eX509CRLError, NULL); } BIO_get_mem_ptr(out, &buf); str = rb_str_new(buf->data, buf->length); BIO_free(out); return str; }