/* * write an reop encrypted message header, followed by base64 data */ static void writeencfile(const char *filename, const void *hdr, size_t hdrlen, const char *ident, uint8_t *msg, unsigned long long msglen) { char header[1024]; char b64[1024]; char *b64data; size_t b64len; int fd; b64len = (msglen + 2) / 3 * 4 + 1; b64data = xmalloc(b64len); if (b64_ntop(msg, msglen, b64data, b64len) == -1) errx(1, "b64 encode failed"); fd = xopen(filename, O_CREAT|O_TRUNC|O_NOFOLLOW|O_WRONLY, 0666); snprintf(header, sizeof(header), "-----BEGIN REOP ENCRYPTED MESSAGE-----\n"); writeall(fd, header, strlen(header), filename); snprintf(header, sizeof(header), "ident:%s\n", ident); writeall(fd, header, strlen(header), filename); if (b64_ntop(hdr, hdrlen, b64, sizeof(b64)) == -1) errx(1, "b64 encode failed"); writeb64data(fd, filename, b64); explicit_bzero(b64, sizeof(b64)); snprintf(header, sizeof(header), "-----BEGIN REOP ENCRYPTED MESSAGE DATA-----\n"); writeall(fd, header, strlen(header), filename); writeb64data(fd, filename, b64data); xfree(b64data, b64len); snprintf(header, sizeof(header), "-----END REOP ENCRYPTED MESSAGE-----\n"); writeall(fd, header, strlen(header), filename); close(fd); }
bool asignify_pubkey_write(struct asignify_public_data *pk, FILE *f) { char *b64data, *b64id; bool ret = false; if (pk == NULL || f == NULL) { return (false); } if (pk->version == 1) { b64id = xmalloc(pk->id_len * 2); b64_ntop(pk->id, pk->id_len, b64id, pk->id_len * 2); b64data = xmalloc(pk->data_len * 2); b64_ntop(pk->data, pk->data_len, b64data, pk->data_len * 2); ret = (fprintf(f, "%s1:%s:%s\n", PUBKEY_MAGIC, b64id, b64data) > 0); free(b64id); free(b64data); } else if (pk->version == 0) { /* XXX: support openbsd pubkeys format */ } return (ret); }
hash_stat hash_plugin_check_hash_dictionary(const char *hash, const char *password[VECTORSIZE], const char *salt, char *salt2[VECTORSIZE], const char *username, int *num, int threadid) { char *dest[VECTORSIZE]; int a; char *salted[VECTORSIZE]; int lens[VECTORSIZE]; for (a=0;a<vectorsize;a++) { salted[a]=alloca(128); bzero(salted[a],40); memcpy(salted[a],password[a], strlen(password[a])); memcpy(salted[a]+strlen(password[a]), salt,strlen(salt)); dest[a] = alloca(128); lens[a] = strlen(password[a])+strlen(salt); } (void)hash_sha1_unicode((const char **)salted, dest, lens); for (a=0;a<vectorsize;a++) { memcpy(dest[a]+20,salt,8); memcpy(salt2[a],"{SSHA}",6); b64_ntop((const unsigned char *)dest[a], 28, salt2[a]+6, 128); } for (a=0;a<vectorsize;a++) { if (fastcompare((const char *)salt2[a],hash,strlen(hash)-1)==0) {*num=a;return hash_ok;} } return hash_err; }
static void hybi10_encode(int in, int out) { unsigned char inbuf[MAXOFRAME / 4 * 3]; char outbuf[MAXOFRAME + 5]; /* Four-byte header + nul. */ ssize_t len, wlen; for (;;) { len = read(in, inbuf, sizeof inbuf); if (len == -1) { perror("read"); die(1); } else if (len == 0) die(0); /* Encode data as Base64. */ len = b64_ntop(inbuf, len, outbuf + 4, sizeof outbuf - 4); assert(len > 0 && len <= MAXOFRAME); /* Frame header. */ outbuf[0] = 0x81; outbuf[1] = 126; outbuf[2] = len >> 8; outbuf[3] = len; len += 4; wlen = write(out, outbuf, len); if (wlen == -1) { perror("write"); die(1); } else if (wlen != len) die(0); } }
static void hixie76_encode(int in, int out) { unsigned char inbuf[MAXOFRAME / 4 * 3]; char outbuf[MAXOFRAME + 2]; ssize_t len, wlen; for (;;) { len = read(in, inbuf, sizeof inbuf); if (len == -1) { perror("read"); die(1); } else if (len == 0) die(0); /* Frame header. */ outbuf[0] = 0x00; /* Encode data as Base64. */ len = b64_ntop(inbuf, len, outbuf + 1, sizeof outbuf - 1); assert(len > 0 && len <= MAXOFRAME); /* Frame trailer. */ outbuf[len + 1] = 0xff; wlen = write(out, outbuf, len + 2); if (wlen == -1) { perror("write"); die(1); } else if (wlen != len + 2) die(0); } }
/* * assume that ersatz_pw is raw. * salt = HDF(pw) xor ersatz_pw * output: out_salt is base64 length 16 */ int py_ersatz_salt(char *password, char *ersatz_pw, char *out_salt) { /* HDF on password */ char hmac_digest[HMAC_LEN]; int ret = py_hsm_hmac(password, hmac_digest); if(ret != HSM_HMAC_OK) return ERSATZ_SALT_FAIL; /* todo: check len of ersatz pw*/ /* pad ersatz pw with nulls */ char ersatz_pw_padded[HMAC_LEN]; memset(ersatz_pw_padded, 0, HMAC_LEN); strcpy(ersatz_pw_padded, ersatz_pw); /* xor the hmac_digest and padded ersatz */ char raw_salt[HMAC_LEN]; int i; for(i = 0; i < HMAC_LEN; i++) raw_salt[i] = hmac_digest[i] ^ ersatz_pw_padded[i]; /* encode base64, replace with '+' with '.' to maintain formating in passwd.master. Also, trim the salt down */ b64_ntop((unsigned char*) raw_salt, RAW_SALT_LEN, out_salt, SALT_SIZE); for(i = 0; i < SALT_SIZE; i++) if(out_salt[i] == '+') out_salt[i] = '.'; out_salt[SALT_SIZE] = '\0'; return ERSATZ_SALT_OK; }
TEST(resolv, b64_ntop) { char buf[128]; memset(buf, 'x', sizeof(buf)); ASSERT_EQ(static_cast<int>(strlen("aGVsbG8=")), b64_ntop(reinterpret_cast<u_char const*>("hello"), strlen("hello"), buf, sizeof(buf))); ASSERT_STREQ(buf, "aGVsbG8="); }
static int dst_s_write_public_key(const DST_KEY *key) { FILE *fp; char filename[PATH_MAX]; u_char out_key[RAW_KEY_SIZE]; char enc_key[RAW_KEY_SIZE]; int len = 0; int mode; memset(out_key, 0, sizeof(out_key)); if (key == NULL) { EREPORT(("dst_write_public_key(): No key specified \n")); return (0); } else if ((len = dst_key_to_dnskey(key, out_key, sizeof(out_key)))< 0) return (0); /* Make the filename */ if (dst_s_build_filename(filename, key->dk_key_name, key->dk_id, key->dk_alg, PUBLIC_KEY, PATH_MAX) == -1) { EREPORT(("dst_write_public_key(): Cannot make filename from %s, %d, and %s\n", key->dk_key_name, key->dk_id, PUBLIC_KEY)); return (0); } /* XXX in general this should be a check for symmetric keys */ mode = (key->dk_alg == KEY_HMAC_MD5) ? 0600 : 0644; /* create public key file */ if ((fp = dst_s_fopen(filename, "w+", mode)) == NULL) { EREPORT(("DST_write_public_key: open of file:%s failed (errno=%d)\n", filename, errno)); return (0); } /*write out key first base64 the key data */ if (key->dk_flags & DST_EXTEND_FLAG) b64_ntop(&out_key[6], len - 6, enc_key, sizeof(enc_key)); else b64_ntop(&out_key[4], len - 4, enc_key, sizeof(enc_key)); fprintf(fp, "%s IN KEY %d %d %d %s\n", key->dk_key_name, key->dk_flags, key->dk_proto, key->dk_alg, enc_key); fclose(fp); return (1); }
int encode_hixie(u_char const *src, size_t srclength, char *target, size_t targsize) { int sz = 0, len = 0; target[sz++] = '\x00'; len = b64_ntop(src, srclength, target+sz, targsize-sz); if (len < 0) { return len; } sz += len; target[sz++] = '\xff'; return sz; }
int encode_hybi(u_char const *src, size_t srclength, char *target, size_t targsize, char opcode) { int sz = 0, len = 0; if ((int)srclength <= 0) { return 0; } if (srclength < 126) { target[sz] = (char)(opcode & 0x0F | 0x80); sz += sizeof(char); sz += sizeof(char); } else if (srclength > 125 && srclength < 65536) { target[sz] = (char)(opcode & 0x0F | 0x80); sz += sizeof(char); sz += sizeof(char); sz += sizeof(short); } else { target[sz] = (char)(opcode & 0x0F | 0x80); sz += sizeof(char); sz += sizeof(char); sz += sizeof(long); } len = b64_ntop(src, srclength, target+sz, targsize-sz); if (len < 0) { return len; } if (len + 3 < 126) { target[1] = (char)(len); } else if (len > 125 && len < 65536) { target[1] = (char)126; *(u_short*)&(target[2]) = htons(len); } else { target[1] = (char)127; *(u_long*)&(target[2]) = htonl(len); } sz += len; return sz; }
static void gen_sha1(headers_t *headers, char *target) { SHA_CTX c; unsigned char hash[SHA_DIGEST_LENGTH]; int r; SHA1_Init(&c); SHA1_Update(&c, headers->key1, strlen(headers->key1)); SHA1_Update(&c, "EpfvMdcoQCUdHswgSBh9", 20); SHA1_Final(hash, &c); r = b64_ntop(hash, sizeof hash, target, 29); }
sstring *b64stringForSString(const sstring &str) { sstring *ostr = new sstring; int buflen = str.size()*2; char *buf = (char *)malloc(buflen); int len = b64_ntop((const unsigned char *)str.data(), str.size(), buf,buflen); ostr->assign(buf,len); free(buf); return ostr; }
static void gen_sha1(headers_t *headers, char *target) { SHA_CTX c; unsigned char hash[SHA_DIGEST_LENGTH]; int r; SHA1_Init(&c); SHA1_Update(&c, headers->key1, strlen(headers->key1)); SHA1_Update(&c, HYBI_GUID, 36); SHA1_Final(hash, &c); r = b64_ntop(hash, sizeof hash, target, HYBI10_ACCEPTHDRLEN); //assert(r == HYBI10_ACCEPTHDRLEN - 1); }
/* Base64 encode string. */ char * imap_base64_encode(char *in) { char *out; size_t size; size = (strlen(in) * 2) + 1; out = xcalloc(1, size); if (b64_ntop(in, strlen(in), out, size) < 0) { xfree(out); return (NULL); } return (out); }
static int dst_hmac_md5_key_to_file_format(const DST_KEY *dkey, char *buff, const int buff_len) { char *bp; int len, i, key_len; u_char key[HMAC_LEN]; HMAC_Key *hkey; if (dkey == NULL || dkey->dk_KEY_struct == NULL) return (0); /* * Using snprintf() would be so much simpler here. */ if (buff == NULL || buff_len <= (int)(strlen(key_file_fmt_str) + strlen(KEY_FILE_FORMAT) + 4)) return (-1); /*%< no OR not enough space in output area */ hkey = (HMAC_Key *) dkey->dk_KEY_struct; memset(buff, 0, buff_len); /*%< just in case */ /* write file header */ sprintf(buff, key_file_fmt_str, KEY_FILE_FORMAT, KEY_HMAC_MD5, "HMAC"); bp = buff + strlen(buff); memset(key, 0, HMAC_LEN); for (i = 0; i < HMAC_LEN; i++) key[i] = hkey->hk_ipad[i] ^ HMAC_IPAD; for (i = HMAC_LEN - 1; i >= 0; i--) if (key[i] != 0) break; key_len = i + 1; if (buff_len - (bp - buff) < 6) return (-1); strcat(bp, "Key: "); bp += strlen("Key: "); len = b64_ntop(key, key_len, bp, buff_len - (bp - buff)); if (len < 0) return (-1); bp += len; if (buff_len - (bp - buff) < 2) return (-1); *(bp++) = '\n'; *bp = '\0'; return (bp - buff); }
static void hybi10_calcaccepthdr(const char *key, char *out) { SHA_CTX c; unsigned char hash[SHA_DIGEST_LENGTH]; int r; SHA1_Init(&c); SHA1_Update(&c, key, strlen(key)); SHA1_Update(&c, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", 36); SHA1_Final(hash, &c); r = b64_ntop(hash, sizeof hash, out, HYBI10_ACCEPTHDRLEN); assert(r == HYBI10_ACCEPTHDRLEN - 1); }
static string encode(const char *aws_secret_access_key,string str) { unsigned char md[20]; uint32_t md_len = sizeof(md); /* Note: This MUST be sha1() */ HMAC(EVP_sha1(),aws_secret_access_key,strlen(aws_secret_access_key), (const unsigned char *)str.c_str(),str.size(), md,&md_len); /* Now encode this to base64 */ char b64str[64]; memset(b64str,0,sizeof(b64str)); b64_ntop(md,md_len,b64str,sizeof(b64str)); return string(b64str); }
/* * Generate an MD5 cancel key. */ unsigned char * md5_key(char *secret, int seclen, char *message, int msglen) { unsigned char *cankey; unsigned char *canlock; unsigned char *hmacbuff; MD5_CTX hash_ctx; cankey = (char *) malloc(BUFFSIZE); hmacbuff = hmac_md5(secret, seclen, message, msglen); strcpy(cankey, "md5:"); (void) b64_ntop(hmacbuff, MD5_LENGTH, cankey + 4, BUFFSIZE); return (cankey); }
void visit_value(const types::b_binary& value) { std::size_t b64_len; b64_len = (value.size / 3 + 1) * 4 + 1; std::unique_ptr<char> b64(reinterpret_cast<char*>(operator new(b64_len))); b64_ntop(value.bytes, value.size, b64.get(), b64_len); out << "{" << std::endl; pad(1); out << "\"$type\" : " << value.sub_type << "," << std::endl; pad(1); out << "\"$binary\" : " << b64.get() << "," << std::endl; pad(); out << "}"; }
static int rdata_base64_to_string(buffer_type *output, rdata_atom_type rdata, rr_type* ATTR_UNUSED(rr)) { int length; size_t size = rdata_atom_size(rdata); if(size == 0) return 1; buffer_reserve(output, size * 2 + 1); length = b64_ntop(rdata_atom_data(rdata), size, (char *) buffer_current(output), size * 2); if (length > 0) { buffer_skip(output, length); } return length != -1; }
static int urlsafe_base64_encode(u_char const *src, size_t srclength, char *target, size_t targsize) { char *pos; int len; if ((len = b64_ntop(src, srclength, target, targsize)) == -1) return -1; while ((pos = strchr(target, '+')) != NULL) *pos = '-'; while ((pos = strchr(target, '/')) != NULL) *pos = '_'; return len; }
static char * chap_bin2hex(const char *bin, size_t bin_len) { unsigned char *b64, *tmp; size_t b64_len; b64_len = (bin_len + 2) / 3 * 4 + 3; /* +2 for "0b", +1 for '\0'. */ b64 = malloc(b64_len); if (b64 == NULL) log_err(1, "malloc"); tmp = b64; tmp += sprintf(tmp, "0b"); b64_ntop(bin, bin_len, tmp, b64_len - 2); return (b64); }
void tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx) { char *buf; size_t off; if (!tty_term_has(tty->term, TTYC_MS)) return; off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */ buf = xmalloc(off); b64_ntop(ctx->ptr, ctx->num, buf, off); tty_putcode_ptr2(tty, TTYC_MS, "", buf); xfree(buf); }
static int dst_hmac_md5_key_to_file_format(const DST_KEY *dkey, char *buff, const unsigned buff_len) { char *bp; int i; unsigned len, b_len, key_len; u_char key[HMAC_LEN]; HMAC_Key *hkey; if (dkey == NULL || dkey->dk_KEY_struct == NULL) return (0); if (buff == NULL || buff_len <= (int) strlen(key_file_fmt_str)) return (-1); /* no OR not enough space in output area */ hkey = (HMAC_Key *) dkey->dk_KEY_struct; memset(buff, 0, buff_len); /* just in case */ /* write file header */ sprintf(buff, key_file_fmt_str, KEY_FILE_FORMAT, KEY_HMAC_MD5, "HMAC"); bp = (char *) strchr(buff, '\0'); b_len = buff_len - (bp - buff); memset(key, 0, HMAC_LEN); for (i = 0; i < HMAC_LEN; i++) key[i] = hkey->hk_ipad[i] ^ HMAC_IPAD; for (i = HMAC_LEN - 1; i >= 0; i--) if (key[i] != 0) break; key_len = i + 1; strcat(bp, "Key: "); bp += strlen("Key: "); b_len = buff_len - (bp - buff); len = b64_ntop(key, key_len, bp, b_len); if (len < 0) return (-1); bp += len; *(bp++) = '\n'; *bp = '\0'; b_len = buff_len - (bp - buff); return (buff_len - b_len); }
static void ldif_print_value(const char *attr, const char *value, FILE *fp) { char *enc; size_t sz, encsz; if (ldif_need_encoding(value)) { sz = strlen(value); encsz = (sz * 4) / 3 + 4; if ((enc = malloc(encsz)) == NULL) err(4, "malloc"); if (b64_ntop(value, sz, enc, encsz) == -1) err(4, "base64 encode"); ldif_print_folded(attr, enc, 1, fp); free(enc); } else ldif_print_folded(attr, value, 0, fp); }
void xmlseg(BIO *bp,AFFILE *af,const char *segname) { BIO_printf(bp," <segment>\n"); BIO_printf(bp," <name>%s</name>\n",segname); /* Get the signature and base64 it (if we can) */ u_char sigbuf[1024]; size_t sigbuf_len = sizeof(sigbuf); char segname_sig[1024]; strlcpy(segname_sig,segname,sizeof(segname_sig)); strlcat(segname_sig,AF_SIG256_SUFFIX,sizeof(segname_sig)); if(af_get_seg(af,segname_sig,0,sigbuf,&sigbuf_len)==0){ char sigbuf48[2048]; int sigbuf48_len = b64_ntop(sigbuf,sigbuf_len,sigbuf48,sizeof(sigbuf48)); sigbuf48[sigbuf48_len] = 0; // null terminate BIO_printf(bp," <sig>%s</sig>\n",sigbuf48); } BIO_printf(bp," </segment>\n"); }
/* * Generate an MD5 cancel lock. */ unsigned char * md5_lock(char *secret, int seclen, char *message, int msglen) { unsigned char *cankey, *canlock; unsigned char hmacbuff[MD5_LENGTH]; unsigned char junk[MD5_LENGTH]; MD5_CTX hash_ctx; cankey = lock_strip_alpha(md5_key(secret, seclen, message, msglen), junk); canlock = (char *) malloc(BUFFSIZE); MD5Init(&hash_ctx); MD5Update(&hash_ctx, cankey, strlen(cankey)); MD5Final(hmacbuff, &hash_ctx); strcpy(canlock, "md5:"); (void) b64_ntop(hmacbuff, MD5_LENGTH, canlock + 4, BUFFSIZE); return (canlock); }
/* * Verify an SHA cancel key against a cancel lock. * Returns 0 on success, nonzero on failure. */ int md5_verify(unsigned char *key, unsigned char *lock) { unsigned char binkey[MD5_LENGTH + 4]; unsigned char templock[BUFFSIZE]; unsigned char hmacbuff[MD5_LENGTH]; MD5_CTX hash_ctx; int verified; /* Convert the key back into binary */ (void) b64_pton(key, binkey, (MD5_LENGTH + 4)); MD5Init(&hash_ctx); MD5Update(&hash_ctx, key, strlen(key)); MD5Final(hmacbuff, &hash_ctx); (void) b64_ntop(hmacbuff, MD5_LENGTH, templock, BUFFSIZE); verified = strcmp(templock, lock); return (verified); }
char * sshbuf_dtob64(struct sshbuf *buf) { size_t len = sshbuf_len(buf), plen; const u_char *p = sshbuf_ptr(buf); char *ret; int r; if (len == 0) return strdup(""); plen = ((len + 2) / 3) * 4 + 1; if (SIZE_MAX / 2 <= len || (ret = malloc(plen)) == NULL) return NULL; if ((r = b64_ntop(p, len, ret, plen)) == -1) { bzero(ret, plen); free(ret); return NULL; } return ret; }
/* * can write a few different file types */ static void writekeyfile(const char *filename, const char *info, const void *key, size_t keylen, const char *ident, int oflags, mode_t mode) { char header[1024]; char b64[1024]; int fd; fd = xopen(filename, O_CREAT|oflags|O_NOFOLLOW|O_WRONLY, mode); snprintf(header, sizeof(header), "-----BEGIN REOP %s-----\nident:%s\n", info, ident); writeall(fd, header, strlen(header), filename); if (b64_ntop(key, keylen, b64, sizeof(b64)) == -1) errx(1, "b64 encode failed"); writeb64data(fd, filename, b64); explicit_bzero(b64, sizeof(b64)); snprintf(header, sizeof(header), "-----END REOP %s-----\n", info); writeall(fd, header, strlen(header), filename); close(fd); }