RSAKey EVPPKey::rsaKey() const { if (type() == EVP_PKEY_RSA) { boost::shared_ptr<RSA> rsa(EVP_PKEY_get1_RSA(d_pkey.get()), RSA_free); return RSAKey(rsa, d_has_private_compound); } throw Exception::bad_function_call("Key type is not RSA"); }
RSAKey RSAKey::createFromPEMPrivateKey(const std::vector<unsigned char>& data, PEMPassphraseCallback callback, void* userdata) { std::vector<unsigned char> datacopy = data; std::shared_ptr<BIO> pbio(BIO_new_mem_buf(datacopy.data(), (int)datacopy.size()), BIO_free); RSA* prsa = PEM_read_bio_RSAPrivateKey(pbio.get(), NULL, callback, userdata); EXCEPTION_ASSERT_WITH_LOG(prsa, OpenSSLException, "Unable to parse the RSA public key PEM data"); std::shared_ptr<RSA> sprsa(prsa, RSA_free); return RSAKey(sprsa, true); }
RSAKey RSAKey::createFromRaw(RSA* raw, bool has_private_key) { std::shared_ptr<RSA> sprsa; if (has_private_key) { sprsa.reset(RSAPrivateKey_dup(raw), RSA_free); } else { sprsa.reset(RSAPublicKey_dup(raw), RSA_free); } return RSAKey(sprsa, has_private_key); }
RSAKey RSAKey::createFromPrivateCompound(const std::vector<unsigned char>& data) { const unsigned char* buf = reinterpret_cast<const unsigned char*>(&data[0]); RSA* prsa = NULL; if (!d2i_RSAPrivateKey(&prsa, &buf, static_cast<long>(data.size()))) { THROW_EXCEPTION_WITH_LOG(OpenSSLException, "Unable to read private key compound from the specified buffer."); } else { std::shared_ptr<RSA> sprsa(prsa, RSA_free); return RSAKey(sprsa, true); } }
RSAKey RSAKey::createFromPEMPrivateKeyFile(const std::string& filename, PEMPassphraseCallback callback, void* userdata) { FILE* fp = fopen(filename.c_str(), "r"); if (fp == NULL) { THROW_EXCEPTION_WITH_LOG(Exception::exception, "Cannot open the file."); } RSA* prsa = PEM_read_RSAPrivateKey(fp, NULL, callback, userdata); fclose(fp); EXCEPTION_ASSERT_WITH_LOG(prsa, OpenSSLException, "Unable to parse the RSA private key PEM file"); std::shared_ptr<RSA> sprsa(prsa, RSA_free); return RSAKey(sprsa, true); }
bool RSAKey::isValid() const noexcept { return operator!= (RSAKey()); }
const Result CtrlrWindows::exportWithDefaultPanel(CtrlrPanel* panelToWrite, const bool isRestricted, const bool signPanel, RSAKey privateKey) { if (panelToWrite == nullptr) { return (Result::fail("Windows Native: exportWithDefaultPanel got nullptr for panel")); } File me = File::getSpecialLocation(File::currentExecutableFile); File newMe; HANDLE hResource; MemoryBlock panelExportData,panelResourcesData; MemoryBlock iconData(BinaryData::ico_midi_png, BinaryData::ico_midi_pngSize); FileChooser fc(CTRLR_NEW_INSTANCE_DIALOG_TITLE, me.getParentDirectory().getChildFile(File::createLegalFileName(panelToWrite->getProperty(Ids::name))).withFileExtension(me.getFileExtension()), "*"+me.getFileExtension(), panelToWrite->getOwner().getProperty(Ids::ctrlrNativeFileDialogs)); if (fc.browseForFileToSave(true)) { newMe = fc.getResult(); if (!newMe.hasFileExtension(me.getFileExtension())) { newMe = newMe.withFileExtension(me.getFileExtension()); } if (!me.copyFileTo (newMe)) { return (Result::fail("Windows Native: exportMeWithNewResource can't copy \""+me.getFullPathName()+"\" to \""+newMe.getFullPathName()+"\"")); } } else { return (Result::fail("Windows Native: exportMeWithNewResource \"Save file\" dialog failed")); } hResource = BeginUpdateResource(newMe.getFullPathName().toUTF8(), FALSE); if (hResource) { String error; if ( (error = CtrlrPanel::exportPanel (panelToWrite, File::nonexistent, newMe, &panelExportData, &panelResourcesData, isRestricted)) == String::empty) { if ( writeResource (hResource, MAKEINTRESOURCE(CTRLR_INTERNAL_PANEL_RESID), RT_RCDATA, panelExportData) && writeResource (hResource, MAKEINTRESOURCE(CTRLR_INTERNAL_RESOURCES_RESID), RT_RCDATA, panelResourcesData) ) { EndUpdateResource(hResource, FALSE); } else { return (Result::fail("Windows Native: exportMeWithNewResource writeResource[panel] failed")); } if (isRestricted && privateKey != RSAKey()) { /* Sign the panel */ MemoryBlock signature = signData (panelResourcesData, privateKey); } } else { return (Result::fail("Windows Native: exportMeWithNewResource exportPanel error: \""+error+"\"")); } return (Result::ok()); } return (Result::fail("Windows Native: exportMeWithNewResource BeginUpdateResource failed")); }