bool NBFFileOsImageWriter::Close() { if (m_bootloaderBytes!=0x40000) { g_err.Set("erased target file because I could not find a bootloader to add"); // make sure file cannot be used, when no bootloader found ClearFile(); LinearFileImageWriter::Close(); DeleteFile(); return false; } // todo: calculate checksum const char*header= "PW10A1-ENG-3.16-007"; UpdateNBFChecksum(m_checksum, ByteVector(header, header+strlen(header))); string headercheck=stringformat("%s-%04x------------", header, m_checksum&0xffff); if (!LinearFileImageWriter::WriteData(0x7fffffe0, ByteVector(headercheck.begin(), headercheck.begin()+0x20))) { g_err.Set("error writing nbf header"); return false; } if (!LinearFileImageWriter::Close()) return false; return true; }
ByteVector X509_digest(::X509* x509, const ::EVP_MD* md) { unsigned int n; unsigned char buf[EVP_MAX_MD_SIZE]; if (!X509_digest(x509, md, buf, &n)) { return ByteVector(); // Throw instead? } return ByteVector(buf, buf+n); }
ByteVector SSLSocket::getKeyprint() const noexcept { if(!ssl) return ByteVector(); X509* x509 = SSL_get_peer_certificate(ssl); if(!x509) return ByteVector(); ByteVector res = ssl::X509_digest(x509, EVP_sha256()); X509_free(x509); return res; }
static void setIpAddr(ByteVector &result,mg_con_t *mgp) { // This is the IP address, only IPv4 supported. // If the MS asks for IPv6, it is supposed to accept IPv4 anyway. result = ByteVector(6);// IPv4 address + 2 byte header. // 3GPP 24.008 10.5.6.4 result.setByte(0,0x01); // IETF allocated address, which is all we support. result.setByte(1,0x21); // IPv4. // This is a special case - we cannot use setUIint32 because it converts to network order, // but the ip address is already in network order, which is what 3GPP requires, so just copy it. memcpy(result.begin()+2, &mgp->mg_ip, 4); printf("IP address: %0x\n",mgp->mg_ip); }
ByteVector generate_install_config(const SfxInstallConfig& config) { wstring text; text += L";!@Install@!UTF-8!\n"; if (!config.title.empty()) text += L"Title=\"" + config.title + L"\"\n"; if (!config.begin_prompt.empty()) text += L"BeginPrompt=\"" + config.begin_prompt + L"\"\n"; if (!config.progress.empty()) text += L"Progress=\"" + config.progress + L"\"\n"; if (!config.run_program.empty()) text += L"RunProgram=\"" + config.run_program + L"\"\n"; if (!config.directory.empty()) text += L"Directory=\"" + config.directory + L"\"\n"; if (!config.execute_file.empty()) text += L"ExecuteFile=\"" + config.execute_file + L"\"\n"; if (!config.execute_parameters.empty()) text += L"ExecuteParameters=\"" + config.execute_parameters + L"\"\n"; text += L";!@InstallEnd@!\n"; string utf8_text = unicode_to_ansi(text, CP_UTF8); return ByteVector(utf8_text.begin(), utf8_text.end()); }
ByteVector String::data(Type t) const { switch(t) { case Latin1: { ByteVector v(size(), 0); char *p = v.data(); for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) *p++ = static_cast<char>(*it); return v; } case UTF8: { ByteVector v(size() * 4 + 1, 0); UTF16toUTF8(&d->data[0], d->data.size(), v.data(), v.size()); v.resize(::strlen(v.data())); return v; } case UTF16: { ByteVector v(2 + size() * 2, 0); char *p = v.data(); // Assume that if we're doing UTF16 and not UTF16BE that we want little // endian encoding. (Byte Order Mark) *p++ = '\xff'; *p++ = '\xfe'; for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) { *p++ = static_cast<char>(*it & 0xff); *p++ = static_cast<char>(*it >> 8); } return v; } case UTF16BE: { ByteVector v(size() * 2, 0); char *p = v.data(); for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) { *p++ = static_cast<char>(*it >> 8); *p++ = static_cast<char>(*it & 0xff); } return v; } case UTF16LE: { ByteVector v(size() * 2, 0); char *p = v.data(); for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) { *p++ = static_cast<char>(*it & 0xff); *p++ = static_cast<char>(*it >> 8); } return v; } default: { debug("String::data() - Invalid Type value."); return ByteVector(); } } }
TagLib::ByteVector TagLib::DecodeBase64(const TagLib::ByteVector& input) { #if USE_SECURITY_FRAMEWORK ByteVector result; CFErrorRef error; SecTransformRef decoder = SecDecodeTransformCreate(kSecBase64Encoding, &error); if(nullptr == decoder) { CFShow(error); return TagLib::ByteVector::null; } CFDataRef sourceData = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, (const UInt8 *)input.data(), input.size(), kCFAllocatorNull); if(nullptr == sourceData) { CFRelease(decoder), decoder = nullptr; return TagLib::ByteVector::null; } if(!SecTransformSetAttribute(decoder, kSecTransformInputAttributeName, sourceData, &error)) { CFShow(error); CFRelease(sourceData), sourceData = nullptr; CFRelease(decoder), decoder = nullptr; return TagLib::ByteVector::null; } CFTypeRef decodedData = SecTransformExecute(decoder, &error); if(nullptr == decodedData) { CFShow(error); CFRelease(sourceData), sourceData = nullptr; CFRelease(decoder), decoder = nullptr; return TagLib::ByteVector::null; } result.setData((const char *)CFDataGetBytePtr((CFDataRef)decodedData), (TagLib::uint)CFDataGetLength((CFDataRef)decodedData)); CFRelease(decodedData), decodedData = nullptr; CFRelease(sourceData), sourceData = nullptr; CFRelease(decoder), decoder = nullptr; return result; #else ByteVector result; BIO *b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); BIO *bio = BIO_new_mem_buf(reinterpret_cast<void *>(const_cast<char *>(input.data())), input.size()); bio = BIO_push(b64, bio); char inbuf [512]; int inlen; while(0 < (inlen = BIO_read(bio, inbuf, 512))) result.append(ByteVector(inbuf, inlen)); BIO_free_all(bio); return result; #endif }
void CryptoProxy::selfTest() { // test HMAC MD 5 ByteVector key; ByteVector data; ByteVector digest; // case 1 key.assign(16, 0x0b); data.copy((byte_t*)"Hi There", 8); const byte_t dig1[] = { 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d }; hmac(CryptoProxy::HMAC_MD5, data, key, digest); LOG_LODEBUG << "MD5 1: " << (memcmp(digest.c_ptr(), dig1, 16) == 0 ? "OK" : "FAILED"); // case 2 key.copy((byte_t*)"Jefe", 4); data.copy((byte_t*)"what do ya want for nothing?", 28); const byte_t dig2[] = { 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, 0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 }; hmac(CryptoProxy::HMAC_MD5, data, key, digest); LOG_LODEBUG << "MD5 2: " << (memcmp(digest.c_ptr(), dig2, 16) == 0 ? "OK" : "FAILED"); // case 3 key.assign(16, 0xaa); data.assign(50, 0xdd); const byte_t dig3[] = {0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88, 0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6}; hmac(CryptoProxy::HMAC_MD5, data, key, digest); LOG_LODEBUG << "MD5 3: " << (memcmp(digest.c_ptr(), dig3, 16) == 0 ? "OK" : "FAILED"); // test AES CBC 128 ByteVector plainText; ByteVector encryptedText; ByteVector decryptedText; // case 1 - one block const byte_t key1[] = {0x06, 0xa9, 0x21, 0x40, 0x36, 0xb8, 0xa1, 0x5b, 0x51, 0x2e, 0x03, 0xd5, 0x34, 0x12, 0x00, 0x06}; const byte_t iv1[] = {0x3d, 0xaf, 0xba, 0x42, 0x9d, 0x9e, 0xb4, 0x30, 0xb4, 0x22, 0xda, 0x80, 0x2c, 0x9f, 0xac, 0x41}; const byte_t cipher1[] = {0xe3, 0x53, 0x77, 0x9c, 0x10, 0x79, 0xae, 0xb8, 0x27, 0x08, 0x94, 0x2d, 0xbe, 0x77, 0x18, 0x1a}; plainText.copy((byte_t*)"Single block msg", 16); encrypt(ByteVector(key1, 16), ByteVector(iv1, 16), plainText, encryptedText); LOG_LODEBUG << "AES 1: length " << (encryptedText.length() == 16 ? "OK" : "FAILED"); LOG_LODEBUG << "AES 1: content " << (memcmp(encryptedText.c_ptr(), cipher1, 16) == 0 ? "OK" : "FAILED"); decrypt(ByteVector(key1, 16), ByteVector(iv1, 16), encryptedText, decryptedText); LOG_LODEBUG << "AES 1: decrypt " << (memcmp(decryptedText.c_ptr(), plainText.c_ptr(), 16) == 0 ? "OK" : "FAILED"); // case 2 - two blocks const byte_t key2[] = {0xc2, 0x86, 0x69, 0x6d, 0x88, 0x7c, 0x9a, 0xa0, 0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x25, 0xa4, 0x5a}; const byte_t iv2[] = {0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58}; const byte_t cipher2[] = {0xd2, 0x96, 0xcd, 0x94, 0xc2, 0xcc, 0xcf, 0x8a, 0x3a, 0x86, 0x30, 0x28, 0xb5, 0xe1, 0xdc, 0x0a, 0x75, 0x86, 0x60, 0x2d, 0x25, 0x3c, 0xff, 0xf9, 0x1b, 0x82, 0x66, 0xbe, 0xa6, 0xd6, 0x1a, 0xb1}; plainText.clear(); for (byte_t i = 0; i < 32; plainText += i++); encrypt(ByteVector(key2, 16), ByteVector(iv2, 16), plainText, encryptedText); LOG_LODEBUG << "AES 2: length " << (encryptedText.length() == 32 ? "OK" : "FAILED"); LOG_LODEBUG << "AES 2: content " << (memcmp(encryptedText.c_ptr(), cipher2, 32) == 0 ? "OK" : "FAILED"); decrypt(ByteVector(key2, 16), ByteVector(iv2, 16), encryptedText, decryptedText); LOG_LODEBUG << "AES 2: decrypt " << (memcmp(decryptedText.c_ptr(), plainText.c_ptr(), 32) == 0 ? "OK" : "FAILED"); // case 3 - three blocks const byte_t key3[] = {0x6c, 0x3e, 0xa0, 0x47, 0x76, 0x30, 0xce, 0x21, 0xa2, 0xce, 0x33, 0x4a, 0xa7, 0x46, 0xc2, 0xcd}; const byte_t iv3[] = {0xc7, 0x82, 0xdc, 0x4c, 0x09, 0x8c, 0x66, 0xcb, 0xd9, 0xcd, 0x27, 0xd8, 0x25, 0x68, 0x2c, 0x81}; const byte_t cipher3[] = {0xd0, 0xa0, 0x2b, 0x38, 0x36, 0x45, 0x17, 0x53, 0xd4, 0x93, 0x66, 0x5d, 0x33, 0xf0, 0xe8, 0x86, 0x2d, 0xea, 0x54, 0xcd, 0xb2, 0x93, 0xab, 0xc7, 0x50, 0x69, 0x39, 0x27, 0x67, 0x72, 0xf8, 0xd5, 0x02, 0x1c, 0x19, 0x21, 0x6b, 0xad, 0x52, 0x5c, 0x85, 0x79, 0x69, 0x5d, 0x83, 0xba, 0x26, 0x84}; plainText.copy((byte_t*)"This is a 48-byte message (exactly 3 AES blocks)", 48); encrypt(ByteVector(key3, 16), ByteVector(iv3, 16), plainText, encryptedText); LOG_LODEBUG << "AES 3: length " << (encryptedText.length() == 48 ? "OK" : "FAILED"); LOG_LODEBUG << "AES 3: content " << (memcmp(encryptedText.c_ptr(), cipher3, 48) == 0 ? "OK" : "FAILED"); decrypt(ByteVector(key3, 16), ByteVector(iv3, 16), encryptedText, decryptedText); LOG_LODEBUG << "AES 3: decrypt " << (memcmp(decryptedText.c_ptr(), plainText.c_ptr(), 48) == 0 ? "OK" : "FAILED"); }
ByteVector String::data(Type t) const { switch(t) { case Latin1: { ByteVector v(size(), 0); char *p = v.data(); for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) *p++ = static_cast<char>(*it); return v; } case UTF8: { ByteVector v(size() * 4 + 1, 0); #ifdef HAVE_CODECVT std::mbstate_t st = 0; const wchar_t *source; char *target; std::codecvt_base::result result = utf8_utf16_t().out( st, &d->data[0], &d->data[d->data.size()], source, v.data(), v.data() + v.size(), target); if(result != utf8_utf16_t::ok) { debug("String::data() - Unicode conversion error."); } #else const Unicode::UTF16 *source = &d->data[0]; Unicode::UTF8 *target = reinterpret_cast<Unicode::UTF8*>(v.data()); Unicode::ConversionResult result = Unicode::ConvertUTF16toUTF8( &source, source + d->data.size(), &target, target + v.size(), Unicode::lenientConversion); if(result != Unicode::conversionOK) { debug("String::data() - Unicode conversion error."); } #endif v.resize(::strlen(v.data())); return v; } case UTF16: { ByteVector v(2 + size() * 2, 0); char *p = v.data(); // Assume that if we're doing UTF16 and not UTF16BE that we want little // endian encoding. (Byte Order Mark) *p++ = '\xff'; *p++ = '\xfe'; for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) { *p++ = static_cast<char>(*it & 0xff); *p++ = static_cast<char>(*it >> 8); } return v; } case UTF16BE: { ByteVector v(size() * 2, 0); char *p = v.data(); for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) { *p++ = static_cast<char>(*it >> 8); *p++ = static_cast<char>(*it & 0xff); } return v; } case UTF16LE: { ByteVector v(size() * 2, 0); char *p = v.data(); for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) { *p++ = static_cast<char>(*it & 0xff); *p++ = static_cast<char>(*it >> 8); } return v; } default: { debug("String::data() - Invalid Type value."); return ByteVector(); } } }
ByteVector Key::getBytes() const { return ByteVector(getData(), getData() + getLength()); }