Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014-2015 Regents of the University of California.

Author(s):
Jeff Thompson <jefft0@remap.ucla.edu> This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. A copy of the GNU Lesser General Public License is in the file COPYING.
 
 
 package net.named_data.jndn.security.identity;
 
MemoryIdentityStorage extends IdentityStorage and implements its methods to store identity, public key and certificate objects in memory. The application must get the objects through its own means and add the objects to the MemoryIdentityStorage object. To use permanent file-based storage, see BasicIdentityStorage.
 
 public class MemoryIdentityStorage extends IdentityStorage {
  
Check if the specified identity already exists.

Parameters:
identityName The identity name.
Returns:
True if the identity exists, otherwise false.
 
   public boolean
   doesIdentityExist(Name identityName)
   {
     return .containsKey(identityName.toUri());
   }

  
Add a new identity. Do nothing if the identity already exists.

Parameters:
identityName The identity name to be added.
 
   public void
   addIdentity(Name identityNamethrows SecurityException
   {
     String identityUri = identityName.toUri();
     if (.containsKey(identityUri))
       return;
 
     .put(identityUrinew IdentityRecord());
   }

  
Revoke the identity.

Returns:
True if the identity was revoked, false if not.
 
   public boolean
   {
     throw new UnsupportedOperationException
       ("MemoryIdentityStorage.revokeIdentity not implemented");
   }

  
Check if the specified key already exists.

Parameters:
keyName The name of the key.
Returns:
true if the key exists, otherwise false.
 
   public boolean
   doesKeyExist(Name keyNamethrows SecurityException
   {
     return .containsKey(keyName.toUri());
   }

  
Add a public key to the identity storage. Also call addIdentity to ensure that the identityName for the key exists.

Parameters:
keyName The name of the public key to be added.
keyType Type of the public key to be added.
publicKeyDer A blob of the public key DER to be added.
Throws:
net.named_data.jndn.security.SecurityException if a key with the keyName already exists.
 
   public void
   addKey(Name keyNameKeyType keyTypeBlob publicKeyDerthrows SecurityException
   {
     Name identityName = keyName.getSubName(0, keyName.size() - 1);
 
     addIdentity(identityName);
    if (doesKeyExist(keyName))
      throw new SecurityException("a key with the same name already exists!");
     .put(keyName.toUri(), new KeyRecord(keyTypepublicKeyDer));
  }

  
Get the public key DER blob from the identity storage.

Parameters:
keyName The name of the requested public key.
Returns:
The DER Blob. If not found, return a Blob with a null pointer.
  public Blob
  getKey(Name keyName)
  {
    KeyRecord keyRecord = (KeyRecord).get(keyName.toUri());
    if (keyRecord == null)
      // Not found.  Silently return a null Blob.
      return new Blob();
    return keyRecord.getKeyDer();
  }

  
Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.

Parameters:
keyName The name of the key.
  public void
  activateKey(Name keyName)
  {
      ("MemoryIdentityStorage.activateKey not implemented");
  }

  
Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.

Parameters:
keyName The name of the key.
  public void
  deactivateKey(Name keyName)
  {
      ("MemoryIdentityStorage.deactivateKey not implemented");
  }

  
Check if the specified certificate already exists.

Parameters:
certificateName The name of the certificate.
Returns:
True if the certificate exists, otherwise false.
  public boolean
  doesCertificateExist(Name certificateName)
  {
    return .containsKey(certificateName.toUri());
  }

  
Add a certificate to the identity storage.

Parameters:
certificate The certificate to be added. This makes a copy of the certificate.
Throws:
net.named_data.jndn.security.SecurityException if the certificate is already installed.
  public void
  {
    Name certificateName = certificate.getName();
    Name keyName = certificate.getPublicKeyName();
    if (!doesKeyExist(keyName))
      throw new SecurityException
        ("No corresponding Key record for certificate! " + keyName.toUri() +
         " " + certificateName.toUri());
    // Check if certificate already exists.
    if (doesCertificateExist(certificateName))
      throw new SecurityException("Certificate has already been installed!");
    // Check if the public key of certificate is the same as the key record.
    Blob keyBlob = getKey(keyName);
    if (keyBlob.isNull() ||
        !keyBlob.equals(certificate.getPublicKeyInfo().getKeyDer()))
      throw new SecurityException("Certificate does not match the public key!");
    // Insert the certificate.
    .put(certificateName.toUri(), certificate.wireEncode());
  }

  
Get a certificate from the identity storage.

Parameters:
certificateName The name of the requested certificate.
allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
Returns:
The requested certificate. If not found, return null.
  getCertificate(Name certificateNameboolean allowAny)
  {
    Blob certificateDer = (Blob).get(certificateName.toUri());
    if (certificateDer == null)
      // Not found.  Silently return null.
      return new IdentityCertificate();
    IdentityCertificate certificate = new IdentityCertificate();
    try {
      certificate.wireDecode(certificateDer);
    }
    catch (EncodingException ex) {
      // Don't expect this to happen. Silently return null.
      return new IdentityCertificate();
    }
    return certificate;
  }

  
Get/Set Default * /


  
Get the default identity.

Returns:
The name of default identity.
Throws:
net.named_data.jndn.security.SecurityException if the default identity is not set.
  public Name
  {
    if (.length() == 0)
      throw new SecurityException("MemoryIdentityStorage.getDefaultIdentity: The default identity is not defined");
    return new Name();
  }

  
Get the default key name for the specified identity.

Parameters:
identityName The identity name.
Returns:
The default key name.
Throws:
net.named_data.jndn.security.SecurityException if the default key name for the identity is not set.
  public Name
  {
    String identity = identityName.toUri();
    if(.containsKey(identity)){
      if(((IdentityRecord).get(identity)).hasDefaultKey()){
        return ((IdentityRecord).get(identity)).getDefaultKey();
      }
      else{
        throw new SecurityException("No default key set.");
      }
    }
    else{
      throw new SecurityException("Identity not found.");
    }
  }

  
Get the default certificate name for the specified key.

Parameters:
keyName The key name.
Returns:
The default certificate name.
Throws:
net.named_data.jndn.security.SecurityException if the default certificate name for the key name is not set.
  public Name
  {
    String key = keyName.toUri();
    if(.containsKey(key)){
      if(((KeyRecord).get(key)).hasDefaultCertificate()){
        return ((KeyRecord).get(key)).getDefaultCertificate();
      }
      else{
        throw new SecurityException("No default certificate set.");
      }
    }
    else{
      throw new SecurityException("Key not found.");
    }
  }

  
Append all the key names of a particular identity to the nameList.

Parameters:
identityName The identity name to search for.
nameList Append result names to nameList.
isDefault If true, add only the default key name. If false, add only the non-default key names.
  public void
    (Name identityNameArrayList nameListboolean isDefault)
  {
      ("MemoryIdentityStorage.getAllKeyNamesOfIdentity not implemented");
  }

  
Set the default identity. If the identityName does not exist, then clear the default identity so that getDefaultIdentity() throws an exception.

Parameters:
identityName The default identity name.
  public void
  setDefaultIdentity(Name identityName)
  {
    String identityUri = identityName.toUri();
    if (.containsKey(identityUri))
       = identityUri;
    else
      // The identity doesn't exist, so clear the default.
       = "";
  }

  
Set the default key name for the specified identity.

Parameters:
keyName The key name.
identityNameCheck The identity name to check the keyName.
  public void
  setDefaultKeyNameForIdentity(Name keyNameName identityNameCheck)
  {
    String identity = identityNameCheck.toUri();
    if(.containsKey(identity)){
      ((IdentityRecord).get(identity)).setDefaultKey(keyName);
    }
  }

  
Set the default key name for the specified identity.

Parameters:
keyName The key name.
certificateName The certificate name.
  public void
  setDefaultCertificateNameForKey(Name keyNameName certificateName)
  {
    String key = keyName.toUri();
    if(.containsKey(key)){
      ((KeyRecord).get(key)).setDefaultCertificate(certificateName);
    }
  }

  
Delete Methods * /


  
Delete a certificate.

Parameters:
certificateName The certificate name.
  public void
  deleteCertificateInfo(Name certificateNamethrows SecurityException
  {
      ("MemoryIdentityStorage.deleteCertificateInfo is not implemented");
  }

  
Delete a public key and related certificates.

Parameters:
keyName The key name.
  public void
  {
      ("MemoryIdentityStorage.deletePublicKeyInfo is not implemented");
  }

  
Delete an identity and related public keys and certificates.

Parameters:
identity The identity name.
  public void
  {
      ("MemoryIdentityStorage.deleteIdentityInfo is not implemented");
  }
  private static class IdentityRecord {
    void setDefaultKey(Name key){  = key; }
    boolean hasDefaultKey(){ return  != null; }
    Name getDefaultKey(){ return ; }
    private Name defaultKey_;
  };
  private static class KeyRecord {
    public KeyRecord(KeyType keyTypeBlob keyDer)
    {
       = keyType;
       = keyDer;
    }
    KeyType getKeyType() { return ; }
    Blob getKeyDer() { return ; }
    void setDefaultCertificate(Name certificate){  =
            certificate; }
    boolean hasDefaultCertificate(){ return  != null; }
    private KeyType keyType_;
    private Blob keyDer_;
    private Name defaultCertificate_;
  };
 // Use HashMap without generics so it works with older Java compilers.
  private final HashMap identityStore_ =
    new HashMap(); 
< The map key is the identityName.toUri(). The value is an IdentityRecord.
  private String defaultIdentity_ =
    ""
< The default identity in identityStore_, or "" if not defined.
  private final HashMap keyStore_ =
    new HashMap(); 
< The map key is the keyName.toUri(). The value is a KeyRecord.
  private final HashMap certificateStore_ =
    new HashMap(); 
< The map key is the certificateName.toUri(). The value is the certificate Blob.
New to GrepCode? Check out our FAQ X