/*
 * Copyright (C) 2015 Glyptodon LLC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.glyptodon.guacamole.auth.json;

import java.io.UnsupportedEncodingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.GuacamoleServerException;
import org.glyptodon.guacamole.properties.GuacamoleProperty;

/**
 * A GuacamoleProperty whose value is a SecretKey. The key will be generated
 * using the AES key generation algorithm from the UTF-8 bytes of the value of
 * the property.
 *
 * @author Michael Jumper
 */
public abstract class SecretKeyProperty implements GuacamoleProperty<SecretKey> {

    /**
     * The name of the key generation algorithm used by this property.
     */
    public static final String KEY_ALGORITHM = "AES";

    @Override
    public SecretKey parseValue(String value) throws GuacamoleException {

        // If no property provided, return null.
        if (value == null)
            return null;

        try {

            // Read value as UTF-8
            byte[] keyBytes = value.getBytes("UTF-8");

            // Return parsed key
            return new SecretKeySpec(keyBytes, KEY_ALGORITHM);

        }

        // Handle impossible lack of support for UTF-8
        catch (UnsupportedEncodingException e) {
            throw new GuacamoleServerException(e);
        }

    }

}

@@ -1160,53 +1160,8 @@
 n;%0A%0A
-import java.io.UnsupportedEncodingException;%0A
 impo
@@ -1227,16 +1227,57 @@
 eySpec;%0A
+import javax.xml.bind.DatatypeConverter;%0A
 import o
@@ -1570,21 +1570,23 @@
 rom the 
-UTF-8
+decoded
  bytes o
@@ -1595,16 +1595,32 @@
 the 
+hex-%0A * encoded 
 value of
 %0A * 
@@ -1615,19 +1615,16 @@
 value of
-%0A *
  the pro
@@ -2123,21 +2123,19 @@
 alue as 
-UTF-8
+hex
 %0A       
@@ -2161,30 +2161,46 @@
 s = 
-value.getBytes(%22UTF-8%22
+DatatypeConverter.parseHexBinary(value
 );%0A%0A
@@ -2322,86 +2322,64 @@
  // 
-Handle impossible lack of support for UTF-8%0A        catch (UnsupportedEncoding
+Fail parse if hex invalid%0A        catch (IllegalArgument
 Exce
@@ -2436,16 +2436,54 @@
 ception(
+%22Invalid hexadecimal value for key.%22, 
 e);%0A    

Read encryption key as hex.