Skip to content

Commit

Permalink
keychain: NullKey
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Jun 20, 2020
1 parent 33aad03 commit b207294
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ KeyChain
* ECDSA: P-256 curve only
* HMAC-SHA256: no
* RSA: no
* Null: yes
* [NDN certificates](https://named-data.net/doc/ndn-cxx/0.7.0/specs/certificate-format.html): basic support

Application layer services
Expand Down
1 change: 1 addition & 0 deletions src/NDNph.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "ndnph/keychain/ecdsa-private-key.hpp"
#include "ndnph/keychain/ecdsa-public-key.hpp"
#include "ndnph/keychain/helper.hpp"
#include "ndnph/keychain/null-key.hpp"
#include "ndnph/keychain/private-key.hpp"
#include "ndnph/keychain/public-key.hpp"
#include "ndnph/keychain/validity-period.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/ndnph/an.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ enum
Sha256WithRsa = 0x01,
Sha256WithEcdsa = 0x03,
HmacWithSha256 = 0x04,
Null = 0xC8,
};
} // namespace SigType

Expand Down
4 changes: 2 additions & 2 deletions src/ndnph/app/segment-consumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define NDNPH_APP_SEGMENT_CONSUMER_HPP

#include "../face/packet-handler.hpp"
#include "../keychain/digest-key.hpp"
#include "../keychain/null-key.hpp"
#include "../port/clock/port.hpp"

namespace ndnph {
Expand All @@ -12,7 +12,7 @@ class SegmentConsumerBase : public PacketHandler
public:
struct Options
{
const PublicKey& verifier = DigestKey::get();
const PublicKey& verifier = NullKey::get();

/** @brief Maximum retransmission of an Interest, not counting initial Interest. */
int retxLimit = 5;
Expand Down
58 changes: 58 additions & 0 deletions src/ndnph/keychain/null-key.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef NDNPH_KEYCHAIN_NULL_KEY_HPP
#define NDNPH_KEYCHAIN_NULL_KEY_HPP

#include "private-key.hpp"
#include "public-key.hpp"

namespace ndnph {

/**
* @brief Null signature: packet is not signed.
*
* Signing produces an empty signature.
* Verification does nothing and accepts any signature type.
*
* @note This is intended in testing environments or for experimental purposes.
* @sa https://redmine.named-data.net/projects/ndn-tlv/wiki/NullSignature
*/
class NullKey
: public PrivateKey
, public PublicKey
{
public:
static const NullKey& get()
{
static NullKey instance;
return instance;
}

size_t getMaxSigLen() const final
{
return 0;
}

void updateSigInfo(SigInfo& sigInfo) const final
{
sigInfo.sigType = SigType::Null;
sigInfo.name = Name();
}

ssize_t sign(std::initializer_list<tlv::Value>, uint8_t*) const final
{
return 0;
}

bool matchSigInfo(const SigInfo&) const final
{
return true;
}

bool verify(std::initializer_list<tlv::Value>, const uint8_t*, size_t) const final
{
return true;
}
};

} // namespace ndnph

#endif // NDNPH_KEYCHAIN_NULL_KEY_HPP
16 changes: 0 additions & 16 deletions tests/mock/mock-key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@

namespace ndnph {

class NullPrivateKey : public PrivateKey
{
public:
size_t getMaxSigLen() const final
{
return 0;
}

void updateSigInfo(SigInfo&) const final {}

ssize_t sign(std::initializer_list<tlv::Value>, uint8_t*) const final
{
return 0;
}
};

class MockKeyBase
{
protected:
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/app/ping.t.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ndnph/app/ping-client.hpp"
#include "ndnph/app/ping-server.hpp"
#include "ndnph/keychain/null-key.hpp"

#include "mock/bridge-fixture.hpp"
#include "mock/mock-key.hpp"
Expand Down Expand Up @@ -33,14 +34,14 @@ TEST(Ping, Client)
Data data = region.create<Data>();
data.setName(interest.getName().getPrefix(-1));
data.setFreshnessPeriod(1);
transport.receive(data.sign(NullPrivateKey()));
transport.receive(data.sign(NullKey::get()));
} else if (nInterests == 4) {
// no response
} else {
Data data = region.create<Data>();
data.setName(interest.getName());
data.setFreshnessPeriod(1);
transport.receive(data.sign(NullPrivateKey()));
transport.receive(data.sign(NullKey::get()));
}
return true;
});
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/face/face.t.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ndnph/face/face.hpp"
#include "ndnph/keychain/null-key.hpp"

#include "mock/mock-key.hpp"
#include "mock/mock-packet-handler.hpp"
Expand Down Expand Up @@ -40,7 +41,7 @@ TEST(Face, Receive)
EXPECT_CALL(hA, processData(matchDataName)).WillOnce(g::Return(true));
EXPECT_CALL(hB, processData).Times(0);
}
ASSERT_TRUE(transport.receive(data.sign(NullPrivateKey())));
ASSERT_TRUE(transport.receive(data.sign(NullKey::get())));

Nack nack = Nack::create(interest, NackReason::Congestion);
ASSERT_FALSE(!nack);
Expand Down Expand Up @@ -70,7 +71,7 @@ class MyPacketHandler : public MockPacketHandler
{
EXPECT_CALL(*this, processInterest(g::Property(&Interest::getName, g::Eq(request.getName()))))
.WillOnce([this](Interest) {
send(data.sign(NullPrivateKey()));
send(data.sign(NullKey::get()));
reply(nack);
send(interest, WithEndpointId(2035), WithPitToken(0xA31A71CE4C365FF4));
return true;
Expand Down Expand Up @@ -100,7 +101,7 @@ TEST(Face, Send)
ASSERT_FALSE(!hA.data);
hA.data.setName(Name::parse(region, "/A/1"));
Encoder encoderD(region);
encoderD.prepend(hA.data.sign(NullPrivateKey()));
encoderD.prepend(hA.data.sign(NullKey::get()));
encoderD.trim();

hA.nack = Nack::create(hA.request, NackReason::NoRoute);
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/packet/data.t.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ndnph/packet/data.hpp"
#include "ndnph/keychain/null-key.hpp"
#include "ndnph/packet/lp.hpp"

#include "mock/mock-key.hpp"
Expand All @@ -21,13 +22,13 @@ TEST(Data, EncodeMinimal)
std::vector<uint8_t> wire({
0x06, 0x0C, // Data
0x07, 0x03, 0x08, 0x01, 0x41, // Name
0x16, 0x03, 0x1B, 0x01, 0x00, // DSigInfo
0x16, 0x03, 0x1B, 0x01, 0xC8, // DSigInfo
0x17, 0x00, // DSigValue
});
data.setName(Name(&wire[4], 3));

Encoder encoder(region);
ASSERT_TRUE(encoder.prepend(data.sign(NullPrivateKey())));
ASSERT_TRUE(encoder.prepend(data.sign(NullKey::get())));
EXPECT_THAT(std::vector<uint8_t>(encoder.begin(), encoder.end()), g::ElementsAreArray(wire));
encoder.discard();

Expand Down

0 comments on commit b207294

Please sign in to comment.