Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2013-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;
 
 
 public class MemoryPrivateKeyStorage extends PrivateKeyStorage {
  
Set the public key for the keyName.

Parameters:
keyName The key name.
keyType The KeyType, such as KeyType.RSA.
publicKeyDer The public key DER byte buffer.
Throws:
net.named_data.jndn.security.SecurityException if can't decode the key DER.
 
   public final void
     (Name keyNameKeyType keyTypeByteBuffer publicKeyDerthrows SecurityException
   {
       (keyName.toUri(), new PublicKey(new Blob(publicKeyDertrue)));
   }

  
Set the private key for the keyName.

Parameters:
keyName The key name.
keyType The KeyType, such as KeyType.RSA.
privateKeyDer The private key DER byte buffer.
Throws:
net.named_data.jndn.security.SecurityException if can't decode the key DER.
 
   public final void
     (Name keyNameKeyType keyTypeByteBuffer privateKeyDerthrows SecurityException
   {
       (keyName.toUri(), new PrivateKey(keyTypeprivateKeyDer));
   }

  
Set the public and private key for the keyName.

Parameters:
keyName The key name.
keyType The KeyType, such as KeyType.RSA.
publicKeyDer The public key DER byte buffer.
privateKeyDer The private key DER byte buffer.
Throws:
net.named_data.jndn.security.SecurityException if can't decode the key DER.
 
   public final void
     (Name keyNameKeyType keyTypeByteBuffer publicKeyDer,
      ByteBuffer privateKeyDerthrows SecurityException
   {
     setPublicKeyForKeyName(keyNamekeyTypepublicKeyDer);
     setPrivateKeyForKeyName(keyNamekeyTypeprivateKeyDer);
   }

  

Deprecated:
Use setKeyPairForKeyName(keyName, KeyType.RSA, publicKeyDer, privateKeyDer).
Parameters:
keyName The key name.
publicKeyDer The public key DER byte buffer.
privateKeyDer The private key DER byte buffer.
Throws:
net.named_data.jndn.security.SecurityException if can't decode the key DER.
 
   public final void
    (Name keyNameByteBuffer publicKeyDerByteBuffer privateKeyDerthrows SecurityException
  {
    setKeyPairForKeyName(keyName.publicKeyDerprivateKeyDer);
  }

  
Generate a pair of asymmetric keys.

Parameters:
keyName The name of the key pair.
params The parameters of the key.
Throws:
net.named_data.jndn.security.SecurityException
  public void
  generateKeyPair(Name keyNameKeyParams paramsthrows SecurityException
  {
    if (doesKeyExist(keyName.))
      throw new SecurityException("Public Key already exists");
    if (doesKeyExist(keyName.))
      throw new SecurityException("Private Key already exists");
    String keyAlgorithm;
    int keySize;
    if (params.getKeyType() == .) {
      keyAlgorithm = "RSA";
      keySize = ((RsaKeyParams)params).getKeySize();
    }
    else if (params.getKeyType() == .) {
      keyAlgorithm = "EC";
      keySize = ((EcdsaKeyParams)params).getKeySize();
    }
    else
      throw new SecurityException
        ("Cannot generate a key pair of type " + params.getKeyType());
    KeyPairGenerator generator = null;
    try{
      generator = KeyPairGenerator.getInstance(keyAlgorithm);
    }
    catch(NoSuchAlgorithmException e){
      throw new SecurityException
        ("FilePrivateKeyStorage: Could not create the key generator: " + e.getMessage());
    }
    generator.initialize(keySize);
    KeyPair pair = generator.generateKeyPair();
      (keyNameparams.getKeyType(), ByteBuffer.wrap(pair.getPublic().getEncoded()),
       ByteBuffer.wrap(pair.getPrivate().getEncoded()));
  }

  
Delete a pair of asymmetric keys. If the key doesn't exist, do nothing.

Parameters:
keyName The name of the key pair.
  public void
  deleteKeyPair(Name keyNamethrows SecurityException
  {
    String keyUri = keyName.toUri();
    .remove(keyUri);
    .remove(keyUri);
  }

  
Get the public key

Parameters:
keyName The name of public key.
Returns:
The public key.
Throws:
net.named_data.jndn.security.SecurityException
  public PublicKey
  getPublicKey(Name keyNamethrows SecurityException
  {
    PublicKey publicKey = (PublicKey).get(keyName.toUri());
    if (publicKey == null)
      throw new SecurityException
        ("MemoryPrivateKeyStorage: Cannot find public key " + keyName.toUri());
    return publicKey;
  }

  
Fetch the private key for keyName and sign the data, returning a signature Blob.

Parameters:
data Pointer the input byte buffer to sign.
keyName The name of the signing key.
digestAlgorithm the digest algorithm.
Returns:
The signature Blob.
Throws:
net.named_data.jndn.security.SecurityException
  public Blob
  sign(ByteBuffer dataName keyNameDigestAlgorithm digestAlgorithm)
       throws SecurityException
  {
    if (digestAlgorithm != .)
      throw new SecurityException
        ("MemoryPrivateKeyStorage.sign: Unsupported digest algorithm");
    // Find the private key and sign.
    PrivateKey privateKey = (PrivateKey).get(keyName.toUri());
    if (privateKey == null)
      throw new SecurityException
        ("MemoryPrivateKeyStorage: Cannot find private key " + keyName.toUri());
    Signature signature = null;
    if (privateKey.getKeyType() == .) {
      try {
        signature = Signature.getInstance("SHA256withRSA");
      }
      catch (NoSuchAlgorithmException e) {
        // Don't expect this to happen.
        throw new SecurityException("SHA256withRSA algorithm is not supported");
      }
    }
    else if (privateKey.getKeyType() == .) {
      try {
        signature = Signature.getInstance("SHA256withECDSA");
      }
      catch (NoSuchAlgorithmException e) {
        // Don't expect this to happen.
        throw new SecurityException("SHA256withECDSA algorithm is not supported");
      }
    }
    else
      // We don't expect this to happen.
      throw new SecurityException("Unrecognized private key type");
    try {
      signature.initSign(privateKey.getPrivateKey());
    }
    catch (InvalidKeyException exception) {
      throw new SecurityException
        ("InvalidKeyException: " + exception.getMessage());
    }
    try {
      signature.update(data);
      return new Blob(signature.sign());
    }
    catch (SignatureException exception) {
      throw new SecurityException
        ("SignatureException: " + exception.getMessage());
    }
  }

  
Decrypt data.

Parameters:
keyName The name of the decrypting key.
data The byte buffer to be decrypted, from its position to its limit.
isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption is used.
Returns:
The decrypted data.
Throws:
net.named_data.jndn.security.SecurityException
  public Blob
  decrypt(Name keyNameByteBuffer databoolean isSymmetric)
          throws SecurityException
  {
      ("MemoryPrivateKeyStorage.decrypt is not implemented");
  }

  
Encrypt data.

Parameters:
keyName The name of the encrypting key.
data The byte buffer to be encrypted, from its position to its limit.
isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption is used.
Returns:
The encrypted data.
Throws:
net.named_data.jndn.security.SecurityException
  public Blob
  encrypt(Name keyNameByteBuffer databoolean isSymmetric)
         throws SecurityException
  {
      ("MemoryPrivateKeyStorage.encrypt is not implemented");
  }

  
Generate a symmetric key.

Parameters:
keyName The name of the key.
params The parameters of the key.
Throws:
net.named_data.jndn.security.SecurityException
  public void
  generateKey(Name keyNameKeyParams paramsthrows SecurityException
  {
      ("MemoryPrivateKeyStorage.generateKey is not implemented");
  }

  
Check if a particular key exists.

Parameters:
keyName The name of the key.
keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC.
Returns:
True if the key exists, otherwise false.
  public boolean
  doesKeyExist(Name keyNameKeyClass keyClassthrows SecurityException
  {
    if (keyClass == .)
      return .containsKey(keyName.toUri());
    else if (keyClass == .)
      return .containsKey(keyName.toUri());
    else
      // KeyClass.SYMMETRIC not implemented yet.
      return false;
  }

  
PrivateKey is a simple class to hold a java.security.PrivateKey along with a KeyType.
  class PrivateKey {
    public PrivateKey(KeyType keyTypeByteBuffer keyDerthrows SecurityException {
       = keyType;
      if (keyType == .) {
        KeyFactory keyFactory = null;
        try {
          keyFactory = KeyFactory.getInstance("RSA");
        }
        catch (NoSuchAlgorithmException exception) {
          // Don't expect this to happen.
          throw new SecurityException
            ("KeyFactory: RSA is not supported: " + exception.getMessage());
        }
        try {
           =
            keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyDer.array()));
        }
        catch (InvalidKeySpecException exception) {
          // Don't expect this to happen.
          throw new SecurityException
            ("KeyFactory: PKCS8EncodedKeySpec is not supported for RSA: " +
             exception.getMessage());
        }
      }
      else if (keyType == .) {
        KeyFactory keyFactory = null;
        try {
          keyFactory = KeyFactory.getInstance("EC");
        }
        catch (NoSuchAlgorithmException exception) {
          // Don't expect this to happen.
          throw new SecurityException
            ("KeyFactory: EC is not supported: " + exception.getMessage());
        }
        try {
           =
            keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyDer.array()));
        }
        catch (InvalidKeySpecException exception) {
          // Don't expect this to happen.
          throw new SecurityException
            ("KeyFactory: PKCS8EncodedKeySpec is not supported for EC: " +
             exception.getMessage());
        }
      }
      else
        throw new SecurityException
          ("PrivateKey constructor: Unrecognized keyType");
    }
    public KeyType getKeyType() { return ; }
    public java.security.PrivateKey getPrivateKey() { return ; }
    private KeyType keyType_;
  }
  // Use HashMap without generics so it works with older Java compilers.
  private final HashMap publicKeyStore_ =
    new HashMap(); 
< The map key is the keyName.toUri(). The value is security.certificate.PublicKey.
  private final HashMap privateKeyStore_ =
    new HashMap(); 
< The map key is the keyName.toUri(). The value is MemoryPrivateKeyStorage.PrivateKey.
New to GrepCode? Check out our FAQ X