Beispiel #1
0
ndn_Error
ndn_encodeTlvSignatureInfo
(const struct ndn_Signature *signatureInfo, struct ndn_TlvEncoder *encoder)
{
    if (signatureInfo->type == ndn_SignatureType_Generic) {
        // Handle a Generic signature separately since it has the entire encoding.
        const struct ndn_Blob *encoding = &signatureInfo->signatureInfoEncoding;
        ndn_Error error;
        size_t endOffset;
        uint64_t signatureType;

        // Do a test decoding to sanity check that it is valid TLV.
        struct ndn_TlvDecoder decoder;
        ndn_TlvDecoder_initialize(&decoder, encoding->value, encoding->length);
        error = ndn_TlvDecoder_readNestedTlvsStart
                (&decoder, ndn_Tlv_SignatureInfo, &endOffset);
        if (!error)
            error = ndn_TlvDecoder_readNonNegativeIntegerTlv
                    (&decoder, ndn_Tlv_SignatureType, &signatureType);
        if (!error)
            error = ndn_TlvDecoder_finishNestedTlvs(&decoder, endOffset);
        if (error)
            return NDN_ERROR_The_Generic_signature_encoding_is_not_a_valid_NDN_TLV_SignatureInfo;

        return ndn_TlvEncoder_writeArray(encoder, encoding->value, encoding->length);
    }

    if (signatureInfo->type == ndn_SignatureType_Sha256WithRsaSignature ||
            signatureInfo->type == ndn_SignatureType_Sha256WithEcdsaSignature ||
            signatureInfo->type == ndn_SignatureType_HmacWithSha256Signature)
        return ndn_TlvEncoder_writeNestedTlv
               (encoder, ndn_Tlv_SignatureInfo,
                encodeSignatureWithKeyLocatorAndValidityPeriodValue, signatureInfo, 0);
    else if (signatureInfo->type == ndn_SignatureType_DigestSha256Signature)
        return ndn_TlvEncoder_writeNestedTlv
               (encoder, ndn_Tlv_SignatureInfo, encodeDigestSha256Value,
                signatureInfo, 0);
    else
        return NDN_ERROR_encodeSignatureInfo_unrecognized_SignatureType;
}
Beispiel #2
0
/**
 * This private function is called by ndn_TlvEncoder_writeNestedTlv to write the TLVs in the body of the Interest value.
 * @param context This is the InterestValueContext struct pointer which was passed to writeTlv.
 * @param encoder the ndn_TlvEncoder which is calling this.
 * @return 0 for success, else an error code.
 */
static ndn_Error
encodeInterestValue(const void *context, struct ndn_TlvEncoder *encoder)
{
  const struct InterestValueContext *interestValueContext =
    (const struct InterestValueContext *)context;
  const struct ndn_Interest *interest = interestValueContext->interest;
  ndn_Error error;
  uint8_t nonceBuffer[4];
  struct ndn_Blob nonceBlob;

  if ((error = ndn_encodeTlvName
       (&interest->name, interestValueContext->signedPortionBeginOffset,
        interestValueContext->signedPortionEndOffset, encoder)))
    return error;
  // For Selectors, set omitZeroLength true.
  if ((error = ndn_TlvEncoder_writeNestedTlv(encoder, ndn_Tlv_Selectors, encodeSelectorsValue, interest, 1)))
    return error;

  // Encode the Nonce as 4 bytes.
  nonceBlob.length = sizeof(nonceBuffer);
  if (interest->nonce.length == 0) {
    // Generate a random nonce.
    if ((error = ndn_generateRandomBytes(nonceBuffer, sizeof(nonceBuffer))))
      return error;
    nonceBlob.value = nonceBuffer;
  }
  else if (interest->nonce.length < 4) {
    // TLV encoding requires 4 bytes, so pad out to 4 using random bytes.
    ndn_memcpy(nonceBuffer, interest->nonce.value, interest->nonce.length);
    if ((error = ndn_generateRandomBytes
         (nonceBuffer + interest->nonce.length,
          sizeof(nonceBuffer) - interest->nonce.length)))
      return error;
    nonceBlob.value = nonceBuffer;
  }
  else
    // TLV encoding requires 4 bytes, so truncate to 4.
    nonceBlob.value = interest->nonce.value;
  if ((error = ndn_TlvEncoder_writeBlobTlv(encoder, ndn_Tlv_Nonce, &nonceBlob)))
    return error;

  if ((error = ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlvFromDouble
      (encoder, ndn_Tlv_InterestLifetime, interest->interestLifetimeMilliseconds)))
    return error;

  if (interest->forwardingHintWireEncoding.value &&
      interest->forwardingHintWireEncoding.length > 0) {
    if (interest->selectedDelegationIndex >= 0)
      return NDN_ERROR_An_Interest_may_not_have_a_selected_delegation_when_encoding_a_forwarding_hint;
    if (interest->linkWireEncoding.value)
      return NDN_ERROR_An_Interest_may_not_have_a_link_object_when_encoding_a_forwarding_hint;

    // Add the encoded sequence of delegations as is.
    if ((error = ndn_TlvEncoder_writeBlobTlv
         (encoder, ndn_Tlv_ForwardingHint, &interest->forwardingHintWireEncoding)))
      return error;
  }

  if (interest->linkWireEncoding.value) {
    // Encode the entire link as is.
    if ((error = ndn_TlvEncoder_writeArray
        (encoder, interest->linkWireEncoding.value, interest->linkWireEncoding.length)))
      return error;
  }
  if ((error = ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlv
      (encoder, ndn_Tlv_SelectedDelegation, interest->selectedDelegationIndex)))
    return error;

  return NDN_ERROR_success;
}