/// <summary> /// Create a new KeyLocator with a copy of the fields in keyLocator. /// </summary> /// /// <param name="keyLocator">The KeyLocator to copy.</param> public KeyLocator(KeyLocator keyLocator) { this.type_ = net.named_data.jndn.KeyLocatorType.NONE; this.keyData_ = new Blob(); this.keyName_ = new ChangeCounter(new Name()); this.changeCount_ = 0; type_ = keyLocator.type_; keyData_ = keyLocator.keyData_; keyName_.set(new Name(keyLocator.getKeyName())); }
/// <summary> /// Check if this key locator has the same values as the given key locator. /// </summary> /// /// <param name="other">The other key locator to check.</param> /// <returns>true if the key locators are equal, otherwise false.</returns> public bool equals(KeyLocator other) { if (type_ != other.type_) { return(false); } if (type_ == net.named_data.jndn.KeyLocatorType.KEYNAME) { if (!getKeyName().equals(other.getKeyName())) { return(false); } } else if (type_ == net.named_data.jndn.KeyLocatorType.KEY_LOCATOR_DIGEST) { if (!getKeyData().equals(other.getKeyData())) { return(false); } } return(true); }
/// <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(); } }
/// <summary> /// Check if this key locator has the same values as the given key locator. /// </summary> /// /// <param name="other">The other key locator to check.</param> /// <returns>true if the key locators are equal, otherwise false.</returns> public bool equals(KeyLocator other) { if (type_ != other.type_) return false; if (type_ == net.named_data.jndn.KeyLocatorType.KEYNAME) { if (!getKeyName().equals(other.getKeyName())) return false; } else if (type_ == net.named_data.jndn.KeyLocatorType.KEY_LOCATOR_DIGEST) { if (!getKeyData().equals(other.getKeyData())) return false; } return true; }
private static void decodeKeyLocator(int expectedType, KeyLocator keyLocator, TlvDecoder decoder, bool copy) { int endOffset = decoder.readNestedTlvsStart(expectedType); keyLocator.clear(); if (decoder.getOffset() == endOffset) // The KeyLocator is omitted, so leave the fields as none. return; if (decoder.peekType(net.named_data.jndn.encoding.tlv.Tlv.Name, endOffset)) { // KeyLocator is a Name. keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEYNAME); decodeName(keyLocator.getKeyName(), new int[1], new int[1], decoder, copy); } else if (decoder.peekType(net.named_data.jndn.encoding.tlv.Tlv.KeyLocatorDigest, endOffset)) { // KeyLocator is a KeyLocatorDigest. keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEY_LOCATOR_DIGEST); keyLocator.setKeyData(new Blob(decoder .readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.KeyLocatorDigest), copy)); } else throw new EncodingException( "decodeKeyLocator: Unrecognized key locator type"); decoder.finishNestedTlvs(endOffset); }
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); }
public void testConstructor() { // Check default settings. EncryptedContent content = new EncryptedContent(); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.NONE, content.getAlgorithmType()); Assert.AssertEquals(true, content.getPayload().isNull()); Assert.AssertEquals(true, content.getInitialVector().isNull()); Assert.AssertEquals(net.named_data.jndn.KeyLocatorType.NONE, content.getKeyLocator().getType()); // Check an encrypted content with IV. KeyLocator keyLocator = new KeyLocator(); keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEYNAME); keyLocator.getKeyName().set("/test/key/locator"); EncryptedContent rsaOaepContent = new EncryptedContent(); rsaOaepContent.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep) .setKeyLocator(keyLocator).setPayload(new Blob(message, false)) .setInitialVector(new Blob(iv, false)); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, rsaOaepContent.getAlgorithmType()); Assert.AssertTrue(rsaOaepContent.getPayload().equals(new Blob(message, false))); Assert.AssertTrue(rsaOaepContent.getInitialVector() .equals(new Blob(iv, false))); Assert.AssertTrue(rsaOaepContent.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE); Assert.AssertTrue(rsaOaepContent.getKeyLocator().getKeyName() .equals(new Name("/test/key/locator"))); // Encoding. Blob encryptedBlob = new Blob(encrypted, false); Blob encoded = rsaOaepContent.wireEncode(); Assert.AssertTrue(encryptedBlob.equals(encoded)); // Decoding. EncryptedContent rsaOaepContent2 = new EncryptedContent(); rsaOaepContent2.wireDecode(encryptedBlob); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, rsaOaepContent2.getAlgorithmType()); Assert.AssertTrue(rsaOaepContent2.getPayload() .equals(new Blob(message, false))); Assert.AssertTrue(rsaOaepContent2.getInitialVector().equals( new Blob(iv, false))); Assert.AssertTrue(rsaOaepContent2.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE); Assert.AssertTrue(rsaOaepContent2.getKeyLocator().getKeyName() .equals(new Name("/test/key/locator"))); // Check the no IV case. EncryptedContent rsaOaepContentNoIv = new EncryptedContent(); rsaOaepContentNoIv.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep) .setKeyLocator(keyLocator).setPayload(new Blob(message, false)); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, rsaOaepContentNoIv.getAlgorithmType()); Assert.AssertTrue(rsaOaepContentNoIv.getPayload().equals( new Blob(message, false))); Assert.AssertTrue(rsaOaepContentNoIv.getInitialVector().isNull()); Assert.AssertTrue(rsaOaepContentNoIv.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE); Assert.AssertTrue(rsaOaepContentNoIv.getKeyLocator().getKeyName() .equals(new Name("/test/key/locator"))); // Encoding. Blob encryptedBlob2 = new Blob(encryptedNoIv, false); Blob encodedNoIV = rsaOaepContentNoIv.wireEncode(); Assert.AssertTrue(encryptedBlob2.equals(encodedNoIV)); // Decoding. EncryptedContent rsaOaepContentNoIv2 = new EncryptedContent(); rsaOaepContentNoIv2.wireDecode(encryptedBlob2); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, rsaOaepContentNoIv2.getAlgorithmType()); Assert.AssertTrue(rsaOaepContentNoIv2.getPayload().equals( new Blob(message, false))); Assert.AssertTrue(rsaOaepContentNoIv2.getInitialVector().isNull()); Assert.AssertTrue(rsaOaepContentNoIv2.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE); Assert.AssertTrue(rsaOaepContentNoIv2.getKeyLocator().getKeyName() .equals(new Name("/test/key/locator"))); }
public void testSetterGetter() { EncryptedContent content = new EncryptedContent(); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.NONE, content.getAlgorithmType()); Assert.AssertEquals(true, content.getPayload().isNull()); Assert.AssertEquals(true, content.getInitialVector().isNull()); Assert.AssertEquals(net.named_data.jndn.KeyLocatorType.NONE, content.getKeyLocator().getType()); content.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, content.getAlgorithmType()); Assert.AssertEquals(true, content.getPayload().isNull()); Assert.AssertEquals(true, content.getInitialVector().isNull()); Assert.AssertEquals(net.named_data.jndn.KeyLocatorType.NONE, content.getKeyLocator().getType()); KeyLocator keyLocator = new KeyLocator(); keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEYNAME); keyLocator.getKeyName().set("/test/key/locator"); content.setKeyLocator(keyLocator); Assert.AssertTrue(content.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE); Assert.AssertTrue(content.getKeyLocator().getKeyName() .equals(new Name("/test/key/locator"))); Assert.AssertEquals(true, content.getPayload().isNull()); Assert.AssertEquals(true, content.getInitialVector().isNull()); content.setPayload(new Blob(message, false)); Assert.AssertTrue(content.getPayload().equals(new Blob(message, false))); content.setInitialVector(new Blob(iv, false)); Assert.AssertTrue(content.getInitialVector().equals(new Blob(iv, false))); Blob encoded = content.wireEncode(); Blob contentBlob = new Blob(encrypted, false); Assert.AssertTrue(contentBlob.equals(encoded)); }