private static void encodeKeyLocator(int type, KeyLocator keyLocator,
				TlvEncoder encoder)
        {
            int saveLength = encoder.getLength();

            // Encode backwards.
            if (keyLocator.getType() != net.named_data.jndn.KeyLocatorType.NONE) {
                if (keyLocator.getType() == net.named_data.jndn.KeyLocatorType.KEYNAME)
                    encodeName(keyLocator.getKeyName(), new int[1], new int[1],
                            encoder);
                else if (keyLocator.getType() == net.named_data.jndn.KeyLocatorType.KEY_LOCATOR_DIGEST
                        && keyLocator.getKeyData().size() > 0)
                    encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.KeyLocatorDigest, keyLocator
                            .getKeyData().buf());
                else
                    throw new Exception("Unrecognized KeyLocatorType "
                            + keyLocator.getType());
            }

            encoder.writeTypeAndLength(type, encoder.getLength() - saveLength);
        }
 /// <summary>
 /// Look in the IdentityStorage for the public key with the name in the
 /// KeyLocator (if available). If the public key can't be found, return an
 /// empty Blob.
 /// </summary>
 ///
 /// <param name="keyLocator">The KeyLocator.</param>
 /// <param name="failureReason"></param>
 /// <returns>The public key DER or an empty Blob if not found.</returns>
 private Blob getPublicKeyDer(KeyLocator keyLocator, String[] failureReason)
 {
     if (keyLocator.getType() == net.named_data.jndn.KeyLocatorType.KEYNAME
             && identityStorage_ != null) {
         try {
             // Assume the key name is a certificate name.
             return identityStorage_
                     .getKey(net.named_data.jndn.security.certificate.IdentityCertificate
                             .certificateNameToPublicKeyName(keyLocator
                                     .getKeyName()));
         } catch (SecurityException ex) {
             failureReason[0] = "The identityStorage doesn't have the key named "
                     + keyLocator.getKeyName().toUri();
             return new Blob();
         }
     } else {
         // Can't find a key to verify.
         failureReason[0] = "The signature KeyLocator doesn't have a key name";
         return new Blob();
     }
 }
示例#3
0
        /// <summary>
        /// Check if the given Data packet can satisfy this Interest. This method
        /// considers the Name, MinSuffixComponents, MaxSuffixComponents,
        /// PublisherPublicKeyLocator, and Exclude. It does not consider the
        /// ChildSelector or MustBeFresh. This uses the given wireFormat to get the
        /// Data packet encoding for the full Name.
        /// </summary>
        ///
        /// <param name="data">The Data packet to check.</param>
        /// <param name="wireFormat"></param>
        /// <returns>True if the given Data packet can satisfy this Interest.</returns>
        public bool matchesData(Data data, WireFormat wireFormat)
        {
            // Imitate ndn-cxx Interest::matchesData.
            int  interestNameLength = getName().size();
            Name dataName           = data.getName();
            int  fullNameLength     = dataName.size() + 1;

            // Check MinSuffixComponents.
            bool hasMinSuffixComponents = getMinSuffixComponents() >= 0;
            int  minSuffixComponents    = (hasMinSuffixComponents) ? getMinSuffixComponents()
                                        : 0;

            if (!(interestNameLength + minSuffixComponents <= fullNameLength))
            {
                return(false);
            }

            // Check MaxSuffixComponents.
            bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0;

            if (hasMaxSuffixComponents &&
                !(interestNameLength + getMaxSuffixComponents() >= fullNameLength))
            {
                return(false);
            }

            // Check the prefix.
            if (interestNameLength == fullNameLength)
            {
                if (getName().get(-1).isImplicitSha256Digest())
                {
                    if (!getName().equals(data.getFullName(wireFormat)))
                    {
                        return(false);
                    }
                }
                else
                {
                    // The Interest Name is the same length as the Data full Name, but the
                    //   last component isn't a digest so there's no possibility of matching.
                    return(false);
                }
            }
            else
            {
                // The Interest Name should be a strict prefix of the Data full Name,
                if (!getName().isPrefixOf(dataName))
                {
                    return(false);
                }
            }

            // Check the Exclude.
            // The Exclude won't be violated if the Interest Name is the same as the
            //   Data full Name.
            if (getExclude().size() > 0 && fullNameLength > interestNameLength)
            {
                if (interestNameLength == fullNameLength - 1)
                {
                    // The component to exclude is the digest.
                    if (getExclude().matches(
                            data.getFullName(wireFormat).get(interestNameLength)))
                    {
                        return(false);
                    }
                }
                else
                {
                    // The component to exclude is not the digest.
                    if (getExclude().matches(dataName.get(interestNameLength)))
                    {
                        return(false);
                    }
                }
            }

            // Check the KeyLocator.
            KeyLocator publisherPublicKeyLocator = getKeyLocator();

            if (publisherPublicKeyLocator.getType() != net.named_data.jndn.KeyLocatorType.NONE)
            {
                Signature signature = data.getSignature();
                if (!net.named_data.jndn.KeyLocator.canGetFromSignature(signature))
                {
                    // No KeyLocator in the Data packet.
                    return(false);
                }
                if (!publisherPublicKeyLocator.equals(net.named_data.jndn.KeyLocator
                                                      .getFromSignature(signature)))
                {
                    return(false);
                }
            }

            return(true);
        }