Exemple #1
0
 /// <summary>
 /// Create a key for the identityName according to params. The created key is
 /// named /{identityName}/[keyId]/KEY .
 /// This should only be called by KeyChain.
 /// </summary>
 ///
 /// <param name="identityName">The name if the identity.</param>
 /// <param name="params">The KeyParams for creating the key.</param>
 /// <returns>The name of the created key.</returns>
 /// <exception cref="Tpm.Error">if params is invalid or the key type is unsupported.</exception>
 /// <exception cref="TpmBackEnd.Error">if the key already exists or cannot be created.</exception>
 public Name createKey_(Name identityName, KeyParams paras)
 {
     if (paras.getKeyType() == net.named_data.jndn.security.KeyType.RSA ||
         paras.getKeyType() == net.named_data.jndn.security.KeyType.EC)
     {
         TpmKeyHandle keyHandle = backEnd_.createKey(identityName, paras);
         Name         keyName   = keyHandle.getKeyName();
         ILOG.J2CsMapping.Collections.Collections.Put(keys_, keyName, keyHandle);
         return(keyName);
     }
     else
     {
         throw new Tpm.Error("createKey: Unsupported key type");
     }
 }
Exemple #2
0
        /// <summary>
        /// Generate a pair of asymmetric keys.
        /// </summary>
        ///
        /// <param name="keyName">The name of the key pair.</param>
        /// <param name="params">The parameters of the key.</param>
        /// <exception cref="System.Security.SecurityException"></exception>
        public sealed override void generateKeyPair(Name keyName, KeyParams paras)
        {
            if (doesKeyExist(keyName, net.named_data.jndn.security.KeyClass.PUBLIC))
            {
                throw new SecurityException("Public Key already exists");
            }
            if (doesKeyExist(keyName, net.named_data.jndn.security.KeyClass.PRIVATE))
            {
                throw new SecurityException("Private Key already exists");
            }

            String keyAlgorithm;
            int    keySize;

            if (paras.getKeyType() == net.named_data.jndn.security.KeyType.RSA)
            {
                keyAlgorithm = "RSA";
                keySize      = ((RsaKeyParams)paras).getKeySize();
            }
            else if (paras.getKeyType() == net.named_data.jndn.security.KeyType.EC)
            {
                keyAlgorithm = "EC";
                keySize      = ((EcKeyParams)paras).getKeySize();
            }
            else
            {
                throw new SecurityException("Cannot generate a key pair of type "
                                            + paras.getKeyType());
            }

            KeyPairGenerator generator = null;

            try {
                generator = System.KeyPairGenerator.getInstance(keyAlgorithm);
            } catch (Exception e) {
                throw new SecurityException(
                          "FilePrivateKeyStorage: Could not create the key generator: "
                          + e.Message);
            }

            // generate
            generator.initialize(keySize);
            KeyPair pair = generator.generateKeyPair();

            // save
            this.write(keyName, net.named_data.jndn.security.KeyClass.PRIVATE, pair.getPrivate().getEncoded());
            this.write(keyName, net.named_data.jndn.security.KeyClass.PUBLIC, pair.getPublic().getEncoded());
        }
        /// <summary>
        /// Generate a key pair according to keyParams and return a new TpmPrivateKey
        /// with the private key. You can get the public key with derivePublicKey.
        /// </summary>
        ///
        /// <param name="keyParams">The parameters of the key.</param>
        /// <returns>A new TpmPrivateKey.</returns>
        /// <exception cref="System.ArgumentException">if the key type is not supported.</exception>
        /// <exception cref="TpmPrivateKey.Error">for an invalid key size, or an error generating.</exception>
        public static TpmPrivateKey generatePrivateKey(KeyParams keyParams)
        {
            String keyAlgorithm;
            int    keySize;

            if (keyParams.getKeyType() == net.named_data.jndn.security.KeyType.RSA)
            {
                keyAlgorithm = "RSA";
                keySize      = ((RsaKeyParams)keyParams).getKeySize();
            }
            else if (keyParams.getKeyType() == net.named_data.jndn.security.KeyType.EC)
            {
                keyAlgorithm = "EC";
                keySize      = ((EcKeyParams)keyParams).getKeySize();
            }
            else
            {
                throw new ArgumentException(
                          "Cannot generate a key pair of type "
                          + keyParams.getKeyType());
            }

            KeyPairGenerator generator = null;

            try {
                generator = System.KeyPairGenerator.getInstance(keyAlgorithm);
            } catch (Exception e) {
                throw new TpmPrivateKey.Error(
                          "TpmPrivateKey: Could not create the key generator: "
                          + e.Message);
            }

            generator.initialize(keySize);
            KeyPair pair = generator.generateKeyPair();

            TpmPrivateKey result = new TpmPrivateKey();

            result.keyType_    = keyParams.getKeyType();
            result.privateKey_ = pair.getPrivate();

            return(result);
        }