Beispiel #1
0
        /// <summary>
        /// Load the private key with name keyName from the key file directory.
        /// </summary>
        ///
        /// <param name="keyName">The name of the key.</param>
        /// <returns>The key loaded into a TpmPrivateKey.</returns>
        internal TpmPrivateKey loadKey(Name keyName)
        {
            TpmPrivateKey key    = new TpmPrivateKey();
            StringBuilder base64 = new StringBuilder();

            try {
                TextReader reader = new FileReader(
                    toFilePath(keyName).FullName);
                // Use "try/finally instead of "try-with-resources" or "using"
                // which are not supported before Java 7.
                try {
                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        base64.append(line);
                    }
                } finally {
                    reader.close();
                }
            } catch (FileNotFoundException ex) {
                throw new TpmBackEnd.Error("Error reading private key file: " + ex);
            } catch (IOException ex_0) {
                throw new TpmBackEnd.Error("Error reading private key file: " + ex_0);
            }

            byte[] pkcs = net.named_data.jndn.util.Common.base64Decode(base64.toString());

            try {
                key.loadPkcs1(ILOG.J2CsMapping.NIO.ByteBuffer.wrap(pkcs), default(KeyType) /* was: null */);
            } catch (TpmPrivateKey.Error ex_1) {
                throw new TpmBackEnd.Error("Error decoding private key file: " + ex_1);
            }
            return(key);
        }
Beispiel #2
0
        /// <summary>
        /// Save the private key using keyName into the key file directory.
        /// </summary>
        ///
        /// <param name="keyName">The name of the key.</param>
        /// <param name="key">The private key to save.</param>
        private void saveKey(Name keyName, TpmPrivateKey key)
        {
            String filePath = toFilePath(keyName).FullName;
            String base64;

            try {
                base64 = net.named_data.jndn.util.Common.base64Encode(key.toPkcs1().getImmutableArray(),
                                                                      true);
            } catch (TpmPrivateKey.Error ex) {
                throw new TpmBackEnd.Error("Error encoding private key file: " + ex);
            }

            try {
                var writer = (new StreamWriter(filePath));
                // Use "try/finally instead of "try-with-resources" or "using"
                // which are not supported before Java 7.
                try {
                    writer.Write(base64, 0, base64.Substring(0, base64.Length));
                    writer.flush();
                } finally {
                    writer.close();
                }
            } catch (IOException ex_0) {
                throw new TpmBackEnd.Error("Error writing private key file: " + ex_0);
            }
        }
        /// <summary>
        /// Create a TpmKeyHandleMemory to use the given in-memory key.
        /// </summary>
        ///
        /// <param name="key">The in-memory key.</param>
        public TpmKeyHandleMemory(TpmPrivateKey key)
        {
            if (key == null)
            {
                throw new AssertionError("The key is null");
            }

            key_ = key;
        }
        /// <summary>
        /// Get the handle of the key with name keyName.
        /// </summary>
        ///
        /// <param name="keyName">The name of the key.</param>
        /// <returns>The handle of the key, or null if the key does not exist.</returns>
        protected internal override TpmKeyHandle doGetKeyHandle(Name keyName)
        {
            TpmPrivateKey key = ILOG.J2CsMapping.Collections.Collections.Get(keys_, keyName);

            if (key == null)
            {
                return(null);
            }

            return(new TpmKeyHandleMemory(key));
        }
 /// <summary>
 /// Import an encoded private key with name keyName in PKCS #8 format, possibly
 /// password-encrypted.
 /// </summary>
 ///
 /// <param name="keyName">The name of the key to use in the TPM.</param>
 /// <param name="pkcs8">unencrypted PKCS #8 PrivateKeyInfo.</param>
 /// <param name="password">it to decrypt the PKCS #8 EncryptedPrivateKeyInfo. If the password is null, import an unencrypted PKCS #8 PrivateKeyInfo.</param>
 /// <exception cref="TpmBackEnd.Error">for an error importing the key.</exception>
 protected internal override void doImportKey(Name keyName, ByteBuffer pkcs8,
                                              ByteBuffer password)
 {
     try {
         TpmPrivateKey key = new TpmPrivateKey();
         if (password != null)
         {
             key.loadEncryptedPkcs8(pkcs8, password);
         }
         else
         {
             key.loadPkcs8(pkcs8);
         }
         // Copy the Name.
         ILOG.J2CsMapping.Collections.Collections.Put(keys_, new Name(keyName), key);
     } catch (TpmPrivateKey.Error ex) {
         throw new TpmBackEnd.Error("Cannot import private key: " + ex);
     }
 }
Beispiel #6
0
        /// <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);
        }
Beispiel #7
0
        /// <summary>
        /// Import an encoded private key with name keyName in PKCS #8 format, possibly
        /// password-encrypted.
        /// </summary>
        ///
        /// <param name="keyName">The name of the key to use in the TPM.</param>
        /// <param name="pkcs8">unencrypted PKCS #8 PrivateKeyInfo.</param>
        /// <param name="password">it to decrypt the PKCS #8 EncryptedPrivateKeyInfo. If the password is null, import an unencrypted PKCS #8 PrivateKeyInfo.</param>
        /// <exception cref="TpmBackEnd.Error">for an error importing the key.</exception>
        protected internal override void doImportKey(Name keyName, ByteBuffer pkcs8,
                                                     ByteBuffer password)
        {
            TpmPrivateKey key = new TpmPrivateKey();

            try {
                if (password != null)
                {
                    key.loadEncryptedPkcs8(pkcs8, password);
                }
                else
                {
                    key.loadPkcs8(pkcs8);
                }
            } catch (TpmPrivateKey.Error ex) {
                throw new TpmBackEnd.Error("Cannot import private key: " + ex);
            }

            saveKey(keyName, key);
        }