void RTPSessionInterface::UpdateDigestAuthChallengeParams(bool newNonce, bool createOpaque, UInt32 qop) { if (newNonce || (fAuthNonce.Ptr == NULL)) this->CreateDigestAuthenticationNonce(); if (createOpaque) { // Generate a random UInt32 and convert it to a string // The base64 encoded form of the string is made the opaque value SInt64 theMicroseconds = OS::Microseconds(); ::srand((unsigned int)theMicroseconds); UInt32 randomNum = ::rand(); char* randomNumStr = new char[128]; qtss_sprintf(randomNumStr, "%" _U32BITARG_ "", randomNum); int len = ::strlen(randomNumStr); fAuthOpaque.Len = Base64encode_len(len); char *opaqueStr = new char[fAuthOpaque.Len]; (void)Base64encode(opaqueStr, randomNumStr, len); delete[] randomNumStr; // Don't need this anymore if (fAuthOpaque.Ptr != NULL) // Delete existing pointer before assigning new delete[] fAuthOpaque.Ptr; // one fAuthOpaque.Ptr = opaqueStr; fAuthOpaque.Len = ::strlen(opaqueStr); } else { if (fAuthOpaque.Ptr != NULL) delete[] fAuthOpaque.Ptr; fAuthOpaque.Len = 0; } fAuthNonceCount++; fAuthQop = qop; }
LUA_STRING Crypto::encodingBase64Lua(bool isDecoding, const char* input, int inputLength) { LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->clean(); int bufferSize = isDecoding ? Base64decode_len(input) : Base64encode_len(inputLength); char *buffer = bufferSize ? (char*)malloc(bufferSize) : NULL; int size = 0; if (buffer) { size = isDecoding ? Base64decode(buffer, input) : Base64encode(buffer, input, inputLength); } if (size) { stack->pushString(buffer, size); } else { stack->pushNil(); } if (buffer) { free(buffer); } return 1; }
int main(int argc, char *argv[]) { int bytesLen = 0; char *bytes = hexStrToBytes(sInputStr, &bytesLen); if (!bytes) { printf("Failure! Couldn't convert hex to bytes.\n"); return 1; } char *base64Str = malloc(Base64encode_len(bytesLen)); if (!base64Str) { printf("Failure! Couldn't alloc buffer for base64 string.\n"); return 1; } Base64encode(base64Str, bytes, bytesLen); if (strcmp(base64Str, sOutputStr) == 0) { printf("Success!\n"); } else { printf("Failure!\n"); } free(base64Str); return 0; }
char * cellophane_generateKey(int length) { char * key; int c = 0; char * tmp = malloc(16); unsigned char * digest = malloc(16); char random_number[16]; strcpy(tmp, ""); while( (c * 16) < length) { tmp = realloc(tmp,(c+1)*16); srand(time(NULL)); sprintf(random_number,"%d",rand()); cellophane_compute_md5((char *)random_number,digest); strcat(tmp, (const char *)digest); c++; } key = malloc(Base64encode_len(c*16)); Base64encode(key, tmp, c*16); return key; }
int CACrypto::encodeBase64(const char* input, int inputLength, char* output, int outputBufferLength) { CCAssert(Base64encode_len(inputLength) <= outputBufferLength, "CACrypto::encodeBase64() - outputBufferLength too small"); return Base64encode(output, input, inputLength); }
fzBuffer Data::B64Encode(const char *input, fzUInt inLength) { FZ_ASSERT(input, "Input pointer cannot be NULL"); FZ_ASSERT(inLength > 0, "Input data length cannot be 0."); //should be enough to store 6-bit buffers in 8-bit buffers fzUInt outputSize = Base64encode_len(inLength); char *output = new(std::nothrow) char[outputSize]; if( output ) { Base64encode(output, input, inLength); return fzBuffer(output, outputSize); }else{ FZLOGERROR("Base64: error allocating memory."); return fzBuffer::empty(); } }
void RTPSessionInterface::SetChallengeParams(QTSS_AuthScheme scheme, UInt32 qop, bool newNonce, bool createOpaque) { // Set challenge params // Set authentication scheme fAuthScheme = scheme; if (fAuthScheme == qtssAuthDigest) { // Set Quality of Protection // auth-int (Authentication with integrity) not supported yet fAuthQop = qop; if (newNonce || (fAuthNonce.Ptr == NULL)) this->CreateDigestAuthenticationNonce(); if (createOpaque) { // Generate a random UInt32 and convert it to a string // The base64 encoded form of the string is made the opaque value SInt64 theMicroseconds = OS::Microseconds(); ::srand((unsigned int)theMicroseconds); UInt32 randomNum = ::rand(); char* randomNumStr = new char[128]; qtss_sprintf(randomNumStr, "%" _U32BITARG_ "", randomNum); int len = ::strlen(randomNumStr); fAuthOpaque.Len = Base64encode_len(len); char *opaqueStr = new char[fAuthOpaque.Len]; (void)Base64encode(opaqueStr, randomNumStr, len); delete[] randomNumStr; // Don't need this anymore if (fAuthOpaque.Ptr != NULL) // Delete existing pointer before assigning new delete[] fAuthOpaque.Ptr; // one fAuthOpaque.Ptr = opaqueStr; } else { if (fAuthOpaque.Ptr != NULL) delete[] fAuthOpaque.Ptr; fAuthOpaque.Len = 0; } // Increase the Nonce Count by one // This number is a count of the next request the server // expects with this nonce. (Implies that the server // has already received nonce count - 1 requests that // sent authorization with this nonce fAuthNonceCount++; } }
void printOid(gss_OID oidElem) { OBJECT_IDENTIFIER_t *objId = NULL; asn_dec_rval_t ret; int b64len; char *b64; int r; b64len = Base64encode_len(oidElem->length); b64 = (char *)malloc(sizeof(char) * b64len); r = Base64encode(b64, (char *)(oidElem->elements), oidElem->length); // ret = asn_DEF_OBJECT_IDENTIFIER.ber_decoder(0, &asn_DEF_OBJECT_IDENTIFIER, &objId, oidElem->elements, oidElem->length,0); //if(ret.code != RC_OK) { // printf("\n**COULD NOT CREATE OID**\n"); // } print_arcs(objId); //free(objId); }
extern "C" int Base64EncodedLength(int byteDataSrcLength) { return Base64encode_len(byteDataSrcLength); }
int CACrypto::encodeBase64Len(const char* input, int inputLength) { return Base64encode_len(inputLength); }
void nc_otoc_start(nc_opts *opts, int pair_raw_sock) { int shell_fd_sock; int pair_fd_sock; fd_set readfds; int maxfds; char time_str[NOW_STR_LEN]; enum {init, none, text_sent, text_received, pkey_sent, pkey_received} last_action = init; shell_fd_sock = fileno(stdin); pair_fd_sock = nc_utils_get_rec_sockfd(pair_raw_sock); maxfds = pair_fd_sock + 1; /* --- start of sending public key --- */ if(opts->secure) { char *msg = NULL; char *msg_type = OTOC_MTYPE_PKEY; char *msg_body = NULL; /* --- start of encoding public key --- */ const char *plain_pkey = (const char*) my_publickey; int plain_pkey_len = crypto_box_PUBLICKEYBYTES; char encoded_pkey[Base64encode_len(plain_pkey_len)]; Base64encode(encoded_pkey, plain_pkey, plain_pkey_len); nc_log_writef("debug", "encode my public key: %s", encoded_pkey); /* --- end of encoding public key --- */ msg_body = encoded_pkey; nc_json_make_otoc_msg(&msg_type, &msg_body, plain_pkey_len, &msg); nn_send(pair_raw_sock, msg, strlen(msg), 0); nc_log_writef("debug", "one to one chat sent public key: %s", msg); last_action = pkey_sent; } /* --- end of sending public key --- */ /* --- start of shel command registration --- */ nc_otoc_register_cmd("/help", func_cmd_help); nc_otoc_register_cmd("/leave", func_cmd_leave); /* --- end of shell commmand registration --- */ for(;;) { switch(last_action) { case init: case pkey_sent: fprintf(stdout, ">> Entering (room code %d) ...\n", pair_raw_sock); fprintf(stdout, ">>> "); fflush(stdout); break; case text_received: fprintf(stdout, ">>> "); fflush(stdout); break; case text_sent: fprintf(stdout, ">>> "); fflush(stdout); break; case none: fprintf(stdout, ">>> "); fflush(stdout); break; case pkey_received: break; } FD_ZERO(&readfds); FD_SET(pair_fd_sock, &readfds); FD_SET(shell_fd_sock, &readfds); select(maxfds, &readfds, NULL, NULL, NULL); if(FD_ISSET(shell_fd_sock, &readfds)) { char *buf = NULL; size_t buf_sz = 1024; int i; nc_utils_now_str(time_str); getline(&buf, &buf_sz, stdin); if(buf[0] == '\n') { last_action = none; } else if(buf[0] == '/') { nc_utils_del_new_line(buf); for(i = 0; i < cmd_current_code; i++) { if(strstr(buf, cmds[i].name)) { if(cmds[i].func(buf) < 0) { return; } break; } } last_action = none; } else { if(opts->secure) { /* --- make ciphermsg with key pairs --- */ int ciphermsg_len = crypto_box_MACBYTES + strlen(buf); unsigned char nonce[crypto_box_NONCEBYTES]; unsigned char ciphermsg[ciphermsg_len]; /* @TODO: use random nonce */ //randombytes_buf(nonce, sizeof nonce); memset(nonce, '\0', sizeof nonce); crypto_box_easy(ciphermsg, (const unsigned char*) buf, strlen(buf), nonce, peers_publickey[pair_raw_sock], my_secretkey); /* --- make msg encoded with base64 --- */ const char *plain_ciphermsg = (const char*) ciphermsg; int plain_ciphermsg_len = ciphermsg_len; int encoded_ciphermsg_len = Base64encode_len(plain_ciphermsg_len); char encoded_ciphermsg[encoded_ciphermsg_len]; Base64encode(encoded_ciphermsg, plain_ciphermsg, plain_ciphermsg_len); nc_log_writef("debug", "encode ciphermsg: %s", encoded_ciphermsg); /* --- serialize msg with json --- */ char *msg = NULL; char *msg_type = OTOC_MTYPE_STXT; char *msg_body = NULL; msg_body = encoded_ciphermsg; nc_json_make_otoc_msg(&msg_type, &msg_body, strlen(buf), &msg); nc_log_writef("debug", "serialize encoded ciphermsg: %s", msg); /* --- send msg --- */ nn_send(pair_raw_sock, msg, strlen(msg), 0); } else { char *msg = NULL; char *msg_type = OTOC_MTYPE_RTXT; nc_json_make_otoc_msg(&msg_type, &buf, strlen(buf), &msg); nn_send(pair_raw_sock, msg, strlen(msg), 0); fprintf(stdout, "[%s] >>> %s", time_str, buf); fflush(stdout); nc_utils_del_new_line(buf); nc_log_writef("debug", "one to one chat sent: %s", buf); nc_utils_empty_string(buf); last_action = text_sent; } } } else if(FD_ISSET(pair_fd_sock, &readfds)) { char *buf = NULL; char *msg_body = NULL; char *msg_type = NULL; int original_msg_body_len; nn_recv(pair_raw_sock, &buf, NN_MSG, 0); nc_json_extract_otoc_msg(&buf, &msg_type, &original_msg_body_len, &msg_body); nc_utils_del_new_line(buf); nc_log_writef("debug", "one to one chat received: %s", msg_type); nn_freemsg(buf); if(strncmp(msg_type, OTOC_MTYPE_PKEY, OTOC_MTYPE_LEN) == 0) { /* public key message */ /* --- start of decoding public key --- */ int plain_pkey_len = Base64decode_len(msg_body); char plain_pkey[plain_pkey_len]; Base64decode(plain_pkey, (const char*) msg_body); strncpy(peers_publickey[pair_raw_sock], plain_pkey, crypto_box_PUBLICKEYBYTES); nc_log_writef("debug", "decoded peer's public key was stored."); /* --- end of decoding public key --- */ last_action = pkey_received; } else if(strncmp(msg_type, OTOC_MTYPE_RTXT, OTOC_MTYPE_LEN) == 0) { /* raw text message */ nc_utils_now_str(time_str); fprintf(stdout, "\r[%s] <<< %s", time_str, msg_body); fflush(stdout); last_action = text_received; } else if(strncmp(msg_type, OTOC_MTYPE_STXT, OTOC_MTYPE_LEN) == 0) { /* secure text message */ if(!opts->secure) { char *alert = "\r[%s] <<< { ... encrypted message ... }\n" "==========================================\n" "Your peer uses NanoChat undre secure mode.\n" "Use -s flag to see his encrypted messages.\n" "==========================================\n"; nc_utils_now_str(time_str); fprintf(stdout, alert, time_str); fflush(stdout); last_action = text_received; } else { /* --- decode secure msg body --- */ int plain_ciphermsg_len = Base64decode_len(msg_body); char plain_ciphermsg[plain_ciphermsg_len]; Base64decode(plain_ciphermsg, (const char*) msg_body); /* --- decrypt ciphermsg --- */ int decrypted_len = original_msg_body_len; int ciphermsg_len = crypto_box_MACBYTES + decrypted_len; unsigned char decrypted[decrypted_len]; unsigned char nonce[crypto_box_NONCEBYTES]; memset(nonce, '\0', sizeof nonce); crypto_box_open_easy(decrypted, plain_ciphermsg, ciphermsg_len, nonce, peers_publickey[pair_raw_sock], my_secretkey); /* --- show decrypted msg body --- */ decrypted[original_msg_body_len] = '\0'; nc_utils_now_str(time_str); fprintf(stdout, "\r[%s] <<< %s", time_str, decrypted); fflush(stdout); last_action = text_received; } } } } }
static bool writeDictToFileRecursive(CFDictionaryRef dict, int level, FILE *fp) { for (int i = 0; i < level; i++) fwrite("\t", 1, 1, fp); fwrite("<dict>\n", 1, 7, fp); CFIndex len = CFDictionaryGetCount(dict); if (len == 0) { for (int i = 0; i < level; i++) fwrite("\t", 1, 1, fp); fwrite("</dict>\n", 1, 8, fp); return true; } CFStringRef *keys = (CFStringRef*)malloc(len * sizeof(CFStringRef)); CFTypeRef *values = (CFTypeRef*)malloc(len * sizeof(CFTypeRef)); CFDictionaryGetKeysAndValues(dict, (const void**)keys, (const void**)values); for (CFIndex ci = 0; ci < len; ci++) { for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("<key>", 1, 5, fp); CFIndex cflen = CFStringGetLength(keys[ci]); if (cflen > 0) { char buf[cflen+1]; if (CFStringGetCString(keys[ci], buf, cflen+1, kCFStringEncodingUTF8) == false) { free(keys); free(values); return false; } fwrite(buf, 1, cflen, fp); } fwrite("</key>\n", 1, 7, fp); CFTypeID valtype = CFGetTypeID(values[ci]); if (valtype == CFStringGetTypeID()) { for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("<string>", 1, 8, fp); cflen = CFStringGetLength((CFStringRef)values[ci]); if (cflen > 0) { char buf[cflen+1]; if (CFStringGetCString((CFStringRef)values[ci], buf, cflen+1, kCFStringEncodingUTF8) == false) { free(keys); free(values); return false; } fwrite(buf, 1, cflen, fp); } fwrite("</string>\n", 1, 10, fp); } else if (valtype == CFDictionaryGetTypeID()) { if (!writeDictToFileRecursive((CFDictionaryRef)values[ci], level+1, fp)) { free(keys); free(values); return false; } } else if (valtype == CFDataGetTypeID()) { for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("<data>\n", 1, 7, fp); CFIndex datalen = CFDataGetLength((CFDataRef)values[ci]); if (datalen > 0) { int encodedlen = Base64encode_len((int)datalen); char encodeddata[encodedlen]; Base64encode(encodeddata, (const char*)CFDataGetBytePtr((CFDataRef)values[ci]), (int)datalen); encodedlen = strlen(encodeddata); int count = 0; while (count < encodedlen) { for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); if ( (encodedlen-count) > 60 ) { fwrite(encodeddata+count, 1, 60, fp); count += 60; } else { fwrite(encodeddata+count, 1, encodedlen-count, fp); count = encodedlen; } fwrite("\n", 1, 1, fp); } } for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("</data>\n", 1, 8, fp); } else if (valtype == CFBooleanGetTypeID()) { if (CFBooleanGetValue((CFBooleanRef)values[ci]) == true) { for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("<true/>\n", 1, 8, fp); } else { for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("<false/>\n", 1, 9, fp); } } else if (valtype == CFArrayGetTypeID()) { // TODO: Array output is not supported yet for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("<array>\n", 1, 8, fp); for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("</array>\n", 1, 9, fp); } #if defined(__APPLE__) else if (valtype == CFDateGetTypeID()) { // TODO: Date output is not supported yet for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("<date>\n", 1, 7, fp); for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("</date>\n", 1, 8, fp); } #endif else if (valtype == CFNumberGetTypeID()) { // TODO: Number output is not supported yet for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("<real>\n", 1, 7, fp); for (int i = 0; i <= level; i++) fwrite("\t", 1, 1, fp); fwrite("</real>\n", 1, 8, fp); } else { // unknown type free(keys); free(values); return false; } } free(keys); free(values); for (int i = 0; i < level; i++) fwrite("\t", 1, 1, fp); fwrite("</dict>\n", 1, 8, fp); return true; }