Skip to content

Commit

Permalink
packet: Interest::Signed and Data::Signed typedef
Browse files Browse the repository at this point in the history
These types shall have public typenames, so that they can be used as
return types in external libraries.
  • Loading branch information
yoursunny committed Jan 5, 2021
1 parent f193939 commit 5e94215
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
26 changes: 12 additions & 14 deletions src/ndnph/app/ndncert/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ class NewRequest : public packet_struct::NewRequest
* @param signer private key corresponding to @c this->certRequest .
* @return an Encodable object, or a falsy value upon failure.
*/
detail::SignedInterestRef toInterest(Region& region, const CaProfile& profile,
detail::ISigPolicy& signingPolicy,
const EcPrivateKey& signer) const
Interest::Signed toInterest(Region& region, const CaProfile& profile,
detail::ISigPolicy& signingPolicy, const EcPrivateKey& signer) const
{
Encoder encoder(region);
encoder.prepend([this](Encoder& encoder) { encoder.prependTlv(TT::EcdhPub, ecdhPub); },
Expand All @@ -112,7 +111,7 @@ class NewRequest : public packet_struct::NewRequest
Name name = profile.prefix.append(region, getNewComponent());
Interest interest = region.create<Interest>();
if (!encoder || !name || !interest) {
return detail::SignedInterestRef();
return Interest::Signed();
}
interest.setName(name);
interest.setMustBeFresh(true);
Expand Down Expand Up @@ -180,10 +179,9 @@ class ChallengeRequest : public packet_struct::ChallengeRequest<Challenge>
* @param signer private key corresponding to @c newRequest->certReqest .
* @return an Encodable object, or a falsy value upon failure.
*/
detail::SignedInterestRef toInterest(Region& region, const CaProfile& profile,
const uint8_t* requestId, detail::SessionKey& sessionKey,
detail::ISigPolicy& signingPolicy,
const EcPrivateKey& signer) const
Interest::Signed toInterest(Region& region, const CaProfile& profile, const uint8_t* requestId,
detail::SessionKey& sessionKey, detail::ISigPolicy& signingPolicy,
const EcPrivateKey& signer) const
{
assert(challenge != nullptr);
Encoder encoder(region);
Expand All @@ -192,7 +190,7 @@ class ChallengeRequest : public packet_struct::ChallengeRequest<Challenge>
params);
encoder.trim();
if (!encoder) {
return detail::SignedInterestRef();
return Interest::Signed();
}
auto encrypted = sessionKey.encrypt(region, tlv::Value(encoder), requestId);

Expand All @@ -201,7 +199,7 @@ class ChallengeRequest : public packet_struct::ChallengeRequest<Challenge>
{ getChallengeComponent(), Component(region, detail::RequestIdLen::value, requestId) }, true);
Interest interest = region.create<Interest>();
if (!encrypted || !name || !interest) {
return detail::SignedInterestRef();
return Interest::Signed();
}
interest.setName(name);
interest.setMustBeFresh(true);
Expand Down Expand Up @@ -277,8 +275,8 @@ class Session
return m_state;
}

detail::SignedInterestRef makeNewRequest(Region& packetRegion, const EcPublicKey& pub,
const EcPrivateKey& pvt)
Interest::Signed makeNewRequest(Region& packetRegion, const EcPublicKey& pub,
const EcPrivateKey& pvt)
{
if (m_state != State::NEW_REQ) {
return setFailure();
Expand Down Expand Up @@ -332,7 +330,7 @@ class Session
return m_state == State::CHALLENGE_EXEC;
}

detail::SignedInterestRef makeChallengeRequest(Region& packetRegion)
Interest::Signed makeChallengeRequest(Region& packetRegion)
{
if (m_state != State::CHALLENGE_REQ) {
return setFailure();
Expand Down Expand Up @@ -370,7 +368,7 @@ class Session
}

private:
template<typename T = detail::SignedInterestRef>
template<typename T = Interest::Signed>
T setFailure(T&& value = T())
{
if (!value) {
Expand Down
29 changes: 14 additions & 15 deletions src/ndnph/app/ndncert/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CaProfile : public packet_struct::CaProfile
* @param signer private key corresponding to @c cert .
* @return an Encodable object, or a falsy value upon failure.
*/
detail::SignedDataRef toData(Region& region, EcPrivateKey& signer) const
Data::Signed toData(Region& region, EcPrivateKey& signer) const
{
Encoder encoder(region);
encoder.prepend([this](Encoder& encoder) { encoder.prependTlv(TT::CaPrefix, prefix); },
Expand All @@ -76,7 +76,7 @@ class CaProfile : public packet_struct::CaProfile

Data data = region.create<Data>();
if (!encoder || !version || !segment || !name || !data) {
return detail::SignedDataRef();
return Data::Signed();
}
data.setName(name);
data.setFreshnessPeriod(30000);
Expand Down Expand Up @@ -132,8 +132,8 @@ class NewResponse : public packet_struct::NewResponse
* @param signer private key corresponding to CA certificate.
* @return an Encodable object, or a falsy value upon failure.
*/
detail::SignedDataRef toData(Region& region, const Interest& newRequest,
const ChallengeList& challenges, const EcPrivateKey& signer) const
Data::Signed toData(Region& region, const Interest& newRequest, const ChallengeList& challenges,
const EcPrivateKey& signer) const
{
Encoder encoder(region);
encoder.prepend(
Expand All @@ -154,7 +154,7 @@ class NewResponse : public packet_struct::NewResponse

Data data = region.create<Data>();
if (!encoder || !data || !newRequest) {
return detail::SignedDataRef();
return Data::Signed();
}
data.setName(newRequest.getName());
data.setFreshnessPeriod(4000);
Expand Down Expand Up @@ -237,9 +237,8 @@ class ChallengeResponse : public packet_struct::ChallengeResponse
* @param signer private key corresponding to CA certificate.
* @return an Encodable object, or a falsy value upon failure.
*/
detail::SignedDataRef toData(Region& region, const Interest& challengeRequest,
const uint8_t* requestId, detail::SessionKey& sessionKey,
const EcPrivateKey& signer) const
Data::Signed toData(Region& region, const Interest& challengeRequest, const uint8_t* requestId,
detail::SessionKey& sessionKey, const EcPrivateKey& signer) const
{
Encoder encoder(region);
switch (status) {
Expand All @@ -263,13 +262,13 @@ class ChallengeResponse : public packet_struct::ChallengeResponse
encoder.prepend(tlv::NniElement<>(TT::Status, status));
encoder.trim();
if (!encoder) {
return detail::SignedDataRef();
return Data::Signed();
}
auto encrypted = sessionKey.encrypt(region, tlv::Value(encoder), requestId);

Data data = region.create<Data>();
if (!encrypted || !data || !challengeRequest) {
return detail::SignedDataRef();
return Data::Signed();
}
data.setName(challengeRequest.getName());
data.setFreshnessPeriod(4000);
Expand All @@ -278,7 +277,7 @@ class ChallengeResponse : public packet_struct::ChallengeResponse
}
};

inline detail::SignedDataRef
inline Data::Signed
makeError(Region& region, const Interest& interest, uint8_t errorCode, const EcPrivateKey& signer)
{
Encoder encoder(region);
Expand All @@ -288,7 +287,7 @@ makeError(Region& region, const Interest& interest, uint8_t errorCode, const EcP

Data data = region.create<Data>();
if (!encoder || !data || !interest) {
return detail::SignedDataRef();
return Data::Signed();
}
data.setName(interest.getName());
data.setFreshnessPeriod(4000);
Expand Down Expand Up @@ -316,7 +315,7 @@ class Session
}
}

detail::SignedDataRef handleNewRequest(Region& packetRegion, const Interest& interest)
Data::Signed handleNewRequest(Region& packetRegion, const Interest& interest)
{
if (!m_newRequest.fromInterest(m_region, interest, m_profile, m_signingPolicy)) {
return makeError(packetRegion, interest, ErrorCode::BadParameterFormat, m_signer);
Expand All @@ -329,12 +328,12 @@ class Session
!port::RandomSource::generate(m_newResponse.requestId, sizeof(m_newResponse.requestId)) ||
!m_sessionKey.makeKey(ecdhPvt, m_newRequest.ecdhPub, m_newResponse.salt,
m_newResponse.requestId)) {
return detail::SignedDataRef();
return Data::Signed();
}
return m_newResponse.toData(packetRegion, interest, m_challenges, m_signer);
}

detail::SignedDataRef handleChallengeRequest(Region& packetRegion, const Interest& interest)
Data::Signed handleChallengeRequest(Region& packetRegion, const Interest& interest)
{
m_challengeRegion->reset();
Challenge* prevChallenge = m_challengeRequest.challenge;
Expand Down
7 changes: 5 additions & 2 deletions src/ndnph/packet/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,18 @@ class Data : public detail::RefRegion<detail::DataObj>
return Decoder(encoder.begin(), encoder.size()).decode(*this);
}

/** @brief Result of Data::sign operation. */
using Signed = detail::SignedDataRef;

/**
* @brief Sign the packet with a private key.
* @return an Encodable object. This object is valid only if Data and PrivateKey are kept alive.
* It's recommended to pass it to Encoder immediately without saving as variable.
* @note Unrecognized fields found during decoding are not preserved in encoding output.
*/
detail::SignedDataRef sign(const PrivateKey& key, DSigInfo sigInfo = DSigInfo()) const
Signed sign(const PrivateKey& key, DSigInfo sigInfo = DSigInfo()) const
{
return detail::SignedDataRef(obj, key, std::move(sigInfo));
return Signed(obj, key, std::move(sigInfo));
}

/**
Expand Down
14 changes: 9 additions & 5 deletions src/ndnph/packet/interest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,22 +347,26 @@ class Interest : public detail::InterestRefBase
[this](Encoder& encoder) { encodeMiddle(encoder); });
}

/** @brief Result of Interest::sign operation. */
using Signed = detail::SignedInterestRef;

/** @brief Result of Interest::parameterize operation. */
class Parameterized : public detail::ParameterizedInterestRef
{
public:
using detail::ParameterizedInterestRef::ParameterizedInterestRef;

detail::SignedInterestRef sign(const PrivateKey& key, ISigInfo sigInfo = ISigInfo()) const
Signed sign(const PrivateKey& key, ISigInfo sigInfo = ISigInfo()) const
{
return detail::SignedInterestRef(obj, m_appParameters, key, std::move(sigInfo));
return Signed(obj, m_appParameters, key, std::move(sigInfo));
}

template<typename ISigPolicy>
detail::SignedInterestRef sign(const PrivateKey& key, Region& region, ISigPolicy& policy) const
Signed sign(const PrivateKey& key, Region& region, ISigPolicy& policy) const
{
ISigInfo si;
if (!policy.create(region, si)) {
return detail::SignedInterestRef();
return Signed();
}
return sign(key, si);
}
Expand Down Expand Up @@ -395,7 +399,7 @@ class Interest : public detail::InterestRefBase
* call sign() on its return value.
*/
template<typename... Arg>
detail::SignedInterestRef sign(Arg&&... arg) const
Signed sign(Arg&&... arg) const
{
return parameterize(tlv::Value()).sign(std::forward<Arg>(arg)...);
}
Expand Down

0 comments on commit 5e94215

Please sign in to comment.