Skip to content

Commit

Permalink
Bug 1355422 - NSS tool for encryption, r=ttaubert
Browse files Browse the repository at this point in the history
Summary:
Command line tool to encrypt files with aes-gcm and chacha.
Can also be used to measure performance of the cipher.

Reviewers: ttaubert

Differential Revision: https://nss-review.dev.mozaws.net/D245

--HG--
extra : rebase_source : 22afaf20901fab84ffc21f6c66601d17e849bdfb
extra : amend_source : 5f04fd9e3fbe6b85f8661fffeca3a6cb64454e28
extra : histedit_source : e90fad41df2e1a6fe055a96061d9ed0700d420f9%2Cefb3b14c62e05059a478b2aa2535d2640dc71291
  • Loading branch information
franziskuskiefer committed Apr 10, 2017
1 parent 87d18bf commit f8e9d87
Show file tree
Hide file tree
Showing 9 changed files with 593 additions and 18 deletions.
20 changes: 20 additions & 0 deletions nss-tool/common/tool.h
@@ -0,0 +1,20 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef tool_h__
#define tool_h__

#include <string>
#include <vector>

class Tool {
public:
virtual bool Run(const std::vector<std::string>& arguments) = 0;
virtual ~Tool() {}

private:
virtual void Usage() = 0;
};

#endif // tool_h__
12 changes: 6 additions & 6 deletions nss-tool/common/util.cc
Expand Up @@ -74,15 +74,15 @@ static char *GetModulePassword(PK11SlotInfo *slot, int retry, void *arg) {
return nullptr;
}

static std::vector<char> ReadFromIstream(std::istream &is) {
std::vector<char> certData;
static std::vector<uint8_t> ReadFromIstream(std::istream &is) {
std::vector<uint8_t> data;
while (is) {
char buf[1024];
is.read(buf, sizeof(buf));
certData.insert(certData.end(), buf, buf + is.gcount());
data.insert(data.end(), buf, buf + is.gcount());
}

return certData;
return data;
}

static std::string GetNewPasswordFromUser(void) {
Expand Down Expand Up @@ -181,8 +181,8 @@ std::string StringToHex(const ScopedSECItem &input) {
return ss.str();
}

std::vector<char> ReadInputData(std::string &dataPath) {
std::vector<char> data;
std::vector<uint8_t> ReadInputData(std::string dataPath) {
std::vector<uint8_t> data;
if (dataPath.empty()) {
std::cout << "No input file path given, using stdin." << std::endl;
data = ReadFromIstream(std::cin);
Expand Down
7 changes: 6 additions & 1 deletion nss-tool/common/util.h
Expand Up @@ -5,12 +5,17 @@
#ifndef util_h__
#define util_h__

#include "nspr.h"
#include "scoped_ptrs.h"

#include <secmodt.h>
#include <string>
#include <vector>

#ifndef PORT_Malloc
#define PORT_Malloc PR_Malloc
#endif

enum PwDataType { PW_NONE = 0, PW_FROMFILE = 1, PW_PLAINTEXT = 2 };
typedef struct {
PwDataType source;
Expand All @@ -21,6 +26,6 @@ bool InitSlotPassword(void);
bool ChangeSlotPassword(void);
bool DBLoginIfNeeded(const ScopedPK11SlotInfo &slot);
std::string StringToHex(const ScopedSECItem &input);
std::vector<char> ReadInputData(std::string &dataPath);
std::vector<uint8_t> ReadInputData(std::string dataPath);

#endif // util_h__
8 changes: 4 additions & 4 deletions nss-tool/db/dbtool.cc
Expand Up @@ -264,10 +264,10 @@ bool DBTool::ImportCertificate(const ArgParser &parser) {
return false;
}

std::vector<char> certData = ReadInputData(derFilePath);
std::vector<uint8_t> certData = ReadInputData(derFilePath);

ScopedCERTCertificate cert(
CERT_DecodeCertFromPackage(certData.data(), certData.size()));
ScopedCERTCertificate cert(CERT_DecodeCertFromPackage(
reinterpret_cast<char *>(certData.data()), certData.size()));
if (cert.get() == nullptr) {
std::cerr << "Error: Could not decode certificate!" << std::endl;
return false;
Expand Down Expand Up @@ -379,7 +379,7 @@ bool DBTool::ImportKey(const ArgParser &parser) {
return false;
}

std::vector<char> privKeyData = ReadInputData(privKeyFilePath);
std::vector<uint8_t> privKeyData = ReadInputData(privKeyFilePath);
if (privKeyData.empty()) {
return false;
}
Expand Down
7 changes: 4 additions & 3 deletions nss-tool/db/dbtool.h
Expand Up @@ -8,13 +8,14 @@
#include <string>
#include <vector>
#include "argparse.h"
#include "tool.h"

class DBTool {
class DBTool : public Tool {
public:
bool Run(const std::vector<std::string>& arguments);
bool Run(const std::vector<std::string>& arguments) override;

private:
void Usage();
void Usage() override;
bool PathHasDBFiles(std::string path);
void ListCertificates();
bool ImportCertificate(const ArgParser& parser);
Expand Down

0 comments on commit f8e9d87

Please sign in to comment.