-
Notifications
You must be signed in to change notification settings - Fork 135
Draft: #690: encrypt message #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lidaobing
wants to merge
11
commits into
main
Choose a base branch
from
bf_690_encrypt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0bcc42e
1
lidaobing 453193c
1
lidaobing e5e5536
1
lidaobing ad58d74
1
lidaobing 51a4d4b
1
lidaobing 6e3b0b5
1
lidaobing 7a9f8c4
1
lidaobing b98e306
1
lidaobing ff37f0b
1
lidaobing 1d62473
1
lidaobing e1a1f09
1
lidaobing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace iptux { | ||
|
|
||
| class Encrypt { | ||
| public: | ||
| static std::string genRsaPrivPem(int bits); | ||
| }; | ||
|
|
||
| } // namespace iptux |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
| #include "Encrypt.h" | ||
| #include "iptux-utils/output.h" | ||
| #include <gnutls/abstract.h> | ||
| #include <gnutls/gnutls.h> | ||
| #include <gnutls/x509.h> | ||
|
|
||
| using namespace std; | ||
|
|
||
| namespace iptux { | ||
|
|
||
| std::string Encrypt::genRsaPrivPem(int bits) { | ||
| int ret; | ||
| gnutls_datum_t out = {NULL, 0}; | ||
|
|
||
| if (bits < 512) { | ||
| LOG_WARN("Invalid RSA key bits: %d", bits); | ||
| return ""; | ||
| } | ||
|
|
||
| gnutls_global_init(); | ||
|
|
||
| gnutls_x509_privkey_t privkey; | ||
| ret = gnutls_x509_privkey_init(&privkey); | ||
| if (ret < 0) { | ||
| LOG_WARN("gnutls_x509_privkey_init failed: %s", gnutls_strerror(ret)); | ||
| return ""; | ||
| } | ||
|
|
||
| ret = gnutls_x509_privkey_generate(privkey, GNUTLS_PK_RSA, bits, 0); | ||
| if (ret < 0) { | ||
| LOG_WARN("gnutls_x509_privkey_generate failed: %s", gnutls_strerror(ret)); | ||
| gnutls_x509_privkey_deinit(privkey); | ||
| return ""; | ||
| } | ||
|
|
||
| ret = gnutls_x509_privkey_export2_pkcs8(privkey, GNUTLS_X509_FMT_PEM, NULL, 0, | ||
| &out); | ||
| if (ret < 0) { | ||
| LOG_WARN("gnutls_x509_privkey_export2_pkcs8 failed: %s", | ||
| gnutls_strerror(ret)); | ||
| gnutls_x509_privkey_deinit(privkey); | ||
| return ""; | ||
| } | ||
|
|
||
| std::string keypair((char*)out.data, out.size); | ||
| gnutls_free(out.data); | ||
| gnutls_x509_privkey_deinit(privkey); | ||
| return keypair; | ||
| } | ||
|
|
||
| } // namespace iptux |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #include "Encrypt.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| struct GenRsaPrivPemTest { | ||
| int bits; | ||
| bool success; | ||
| bool fail; | ||
| }; | ||
|
|
||
| TEST(Encrypt, GenRsaPrivPem) { | ||
| struct GenRsaPrivPemTest tests[] = { | ||
| {1024, .success = 1}, {2048, .success = 1}, {4096, .success = 1}, | ||
| {512, .success = 1}, {0, .fail = 1}, {-1, .fail = 1}, | ||
| {1025, .success = 1}, {1032, .success = 1}, | ||
| }; | ||
|
|
||
| for (const auto& test : tests) { | ||
| std::string priv_pem = iptux::Encrypt::genRsaPrivPem(test.bits); | ||
| if (test.success) { | ||
| ASSERT_FALSE(priv_pem.empty()) << "bits=" << test.bits; | ||
| } | ||
|
|
||
| if (test.fail) { | ||
| ASSERT_TRUE(priv_pem.empty()) << "bits=" << test.bits; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚨 suggestion (security): Persisting the private key in plain text config may be a security concern
Persisting the private key in the general config store means any user/process with access to that file can read it. Depending on your threat model, consider a more secure storage mechanism (OS keyring/keystore, separate restricted file, or encrypting the key with a passphrase) rather than plain text in the config. At minimum, ensure the config file location is tightly permissioned if you keep this approach.
Suggested implementation:
To fully address the concern, you should:
private_key(e.g. OS keyring/keystore integration, or a separate file created with restrictive permissions such as 0600).ProgramData::initPrivateKey()to load the private key from that secure storage, and ensure it never falls back to reading it from the general config.private_keyviaconfigand migrate them to the secure storage path instead.