GUACAMOLE-362: Move PrivateKeyGuacamoleProperty into CAS extension and use ByteArrayOutputStream for reading thefile.
Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/commit/b968e073 Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/tree/b968e073 Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/diff/b968e073 Branch: refs/heads/master Commit: b968e073c26d2bcfeeb6ce8a813f201136e1cad0 Parents: c92d2e3 Author: Nick Couchman <[email protected]> Authored: Fri Oct 27 12:57:15 2017 -0400 Committer: Nick Couchman <[email protected]> Committed: Fri Oct 27 13:06:07 2017 -0400 ---------------------------------------------------------------------- .../auth/cas/conf/CASGuacamoleProperties.java | 1 - .../cas/conf/PrivateKeyGuacamoleProperty.java | 90 ++++++++++++++++++ .../properties/PrivateKeyGuacamoleProperty.java | 96 -------------------- 3 files changed, 90 insertions(+), 97 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/blob/b968e073/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/conf/CASGuacamoleProperties.java ---------------------------------------------------------------------- diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/conf/CASGuacamoleProperties.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/conf/CASGuacamoleProperties.java index aa4a06e..dd741a3 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/conf/CASGuacamoleProperties.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/conf/CASGuacamoleProperties.java @@ -19,7 +19,6 @@ package org.apache.guacamole.auth.cas.conf; -import org.apache.guacamole.properties.PrivateKeyGuacamoleProperty; import org.apache.guacamole.properties.StringGuacamoleProperty; /** http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/blob/b968e073/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/conf/PrivateKeyGuacamoleProperty.java ---------------------------------------------------------------------- diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/conf/PrivateKeyGuacamoleProperty.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/conf/PrivateKeyGuacamoleProperty.java new file mode 100644 index 0000000..caa84cc --- /dev/null +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/conf/PrivateKeyGuacamoleProperty.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.guacamole.auth.cas.conf; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.IOException; +import java.lang.IllegalArgumentException; +import java.security.InvalidKeyException; +import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.KeySpec; +import java.security.spec.PKCS8EncodedKeySpec; +import org.apache.guacamole.properties.GuacamoleProperty; +import org.apache.guacamole.GuacamoleServerException; +import org.apache.guacamole.environment.Environment; +import org.apache.guacamole.environment.LocalEnvironment; + +/** + * A GuacamoleProperty whose value is derived from a private key file. + */ +public abstract class PrivateKeyGuacamoleProperty implements GuacamoleProperty<PrivateKey> { + + @Override + public PrivateKey parseValue(String value) throws GuacamoleServerException { + + if (value == null || value.isEmpty()) + return null; + + try { + + // Open and read the file specified in the configuration. + File keyFile = new File(value); + FileInputStream keyStreamIn = new FileInputStream(keyFile); + ByteArrayOutputStream keyStreamOut = new ByteArrayOutputStream(); + byte[] keyBuffer = new byte[1024]; + try { + for (int readBytes; (readBytes = keyStreamIn.read(keyBuffer)) != -1;) + keyStreamOut.write(keyBuffer, 0, readBytes); + } + catch (IOException e) { + throw new GuacamoleServerException("IOException while trying to read bytes from file.", e); + } + + final byte[] keyBytes = keyStreamOut.toByteArray(); + + // Set up decryption infrastructure + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + KeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); + return keyFactory.generatePrivate(keySpec); + + } + catch (FileNotFoundException e) { + throw new GuacamoleServerException("Could not find the specified key file.", e); + } + catch (IOException e) { + throw new GuacamoleServerException("Could not read in the specified key file.", e); + } + catch (NoSuchAlgorithmException e) { + throw new GuacamoleServerException("RSA algorithm is not available.", e); + } + catch (InvalidKeySpecException e) { + throw new GuacamoleServerException("Key is not in expected PKCS8 encoding.", e); + } + + } + +} http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/blob/b968e073/guacamole-ext/src/main/java/org/apache/guacamole/properties/PrivateKeyGuacamoleProperty.java ---------------------------------------------------------------------- diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/properties/PrivateKeyGuacamoleProperty.java b/guacamole-ext/src/main/java/org/apache/guacamole/properties/PrivateKeyGuacamoleProperty.java deleted file mode 100644 index 68070f5..0000000 --- a/guacamole-ext/src/main/java/org/apache/guacamole/properties/PrivateKeyGuacamoleProperty.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.guacamole.properties; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.IOException; -import java.lang.IllegalArgumentException; -import java.security.InvalidKeyException; -import java.security.KeyFactory; -import java.security.NoSuchAlgorithmException; -import java.security.PrivateKey; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.KeySpec; -import java.security.spec.PKCS8EncodedKeySpec; -import org.apache.guacamole.GuacamoleServerException; -import org.apache.guacamole.environment.Environment; -import org.apache.guacamole.environment.LocalEnvironment; - -/** - * A GuacamoleProperty whose value is derived from a private key file. - */ -public abstract class PrivateKeyGuacamoleProperty implements GuacamoleProperty<PrivateKey> { - - @Override - public PrivateKey parseValue(String value) throws GuacamoleServerException { - - if (value == null || value.isEmpty()) - return null; - - try { - - // Open and read the file specified in the configuration. - File keyFile = new File(value); - InputStream keyInput = new BufferedInputStream(new FileInputStream(keyFile)); - int keyLength = (int) keyFile.length(); - final byte[] keyBytes = new byte[keyLength]; - int totalBytesRead = 0; - for(int keyRead = keyInput.read(keyBytes, 0, keyBytes.length); - keyRead >= 0; - keyRead = keyInput.read(keyBytes, totalBytesRead, (keyBytes.length - totalBytesRead))) { - totalBytesRead += keyRead; - } - - // Zero-sized key - if (totalBytesRead == 0) - throw new GuacamoleServerException("Failed to ready key because key is empty."); - - // Fewer bytes read than contained in the key - else if (totalBytesRead < keyLength) - throw new GuacamoleServerException("Unable to read the full length of the key."); - - keyInput.close(); - - // Set up decryption infrastructure - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - KeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); - return keyFactory.generatePrivate(keySpec); - - } - catch (FileNotFoundException e) { - throw new GuacamoleServerException("Could not find the specified key file.", e); - } - catch (IOException e) { - throw new GuacamoleServerException("Could not read in the specified key file.", e); - } - catch (NoSuchAlgorithmException e) { - throw new GuacamoleServerException("RSA algorithm is not available.", e); - } - catch (InvalidKeySpecException e) { - throw new GuacamoleServerException("Key is not in expected PKCS8 encoding.", e); - } - - } - -}
