/// <summary> /// Set the key name in keyHandle according to identityName and params. /// </summary> /// protected static internal void setKeyName(TpmKeyHandle keyHandle, Name identityName, KeyParams paras) { Name.Component keyId; if (paras.getKeyIdType() == net.named_data.jndn.security.KeyIdType.USER_SPECIFIED) { keyId = paras.getKeyId(); } else if (paras.getKeyIdType() == net.named_data.jndn.security.KeyIdType.SHA256) { byte[] digest = net.named_data.jndn.util.Common.digestSha256(keyHandle.derivePublicKey() .buf()); keyId = new Name.Component(digest); } else if (paras.getKeyIdType() == net.named_data.jndn.security.KeyIdType.RANDOM) { if (paras.getKeyId().getValue().size() == 0) { throw new TpmBackEnd.Error( "setKeyName: The keyId is empty for type RANDOM"); } keyId = paras.getKeyId(); } else { throw new TpmBackEnd.Error("setKeyName: unrecognized params.getKeyIdType()"); } keyHandle.setKeyName(net.named_data.jndn.security.pib.PibKey.constructKeyName(identityName, keyId)); }
/// <summary> /// Create a key for the identityName according to params. /// </summary> /// /// <param name="identityName">The name if the identity.</param> /// <param name="params">The KeyParams for creating the key.</param> /// <returns>The handle of the created key.</returns> /// <exception cref="Tpm.Error">if params is invalid.</exception> /// <exception cref="TpmBackEnd.Error">if the key cannot be created.</exception> public TpmKeyHandle createKey(Name identityName, KeyParams paras) { // Do key name checking. if (paras.getKeyIdType() == net.named_data.jndn.security.KeyIdType.USER_SPECIFIED) { // The keyId is pre-set. Name keyName = net.named_data.jndn.security.pib.PibKey.constructKeyName(identityName, paras.getKeyId()); if (hasKey(keyName)) { throw new Tpm.Error("Key `" + keyName.toUri() + "` already exists"); } } else if (paras.getKeyIdType() == net.named_data.jndn.security.KeyIdType.SHA256) { // The key name will be assigned in setKeyName after the key is generated. } else if (paras.getKeyIdType() == net.named_data.jndn.security.KeyIdType.RANDOM) { Name keyName_0; Name.Component keyId; ByteBuffer random = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(8); do { net.named_data.jndn.util.Common.getRandom().nextBytes(random.array()); keyId = new Name.Component(new Blob(random, false)); keyName_0 = net.named_data.jndn.security.pib.PibKey.constructKeyName(identityName, keyId); } while (hasKey(keyName_0)); paras.setKeyId(keyId); } else { throw new Tpm.Error("Unsupported key id type"); } return(doCreateKey(identityName, paras)); }