-
Notifications
You must be signed in to change notification settings - Fork 844
GUACAMOLE-2211: Add example JavaScript encryption. #1158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
necouchman
wants to merge
1
commit into
apache:main
Choose a base branch
from
necouchman:jira/2211
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * This file provides a sample/reference implementation for | ||
| * taking plain-text JSON data and signing and encrypting it | ||
| * for use by the guacamole-auth-json authentication extension. | ||
| */ | ||
|
|
||
| // Some connection data | ||
| let json=`{ | ||
| "username" : "test", | ||
| "connections" : { | ||
| "My Connection" : { | ||
| "protocol" : "rdp", | ||
| "parameters" : { | ||
| "hostname" : "10.10.209.63", | ||
| "port":"3389", | ||
| "ignore-cert":"true", | ||
| "recording-path":"/recordings", | ||
| "recording-name":"My-Connection-\${GUAC_USERNAME}-\${GUAC_DATE}-\${GUAC_TIME}" | ||
| } | ||
| }, | ||
| "My OTHER Connection" : { | ||
| "protocol" : "rdp", | ||
| "parameters" : { | ||
| "hostname":"10.10.209.64", | ||
| "port":"3389", | ||
| "ignore-cert":"true", | ||
| "recording-path":"/recordings", | ||
| "recording-name":"My-OTHER-Connection-\${GUAC_USERNAME}-\${GUAC_DATE}-\${GUAC_TIME}" | ||
| } | ||
| } | ||
| } | ||
| }` | ||
|
|
||
| // Our signing/encryption key | ||
| let key = '2ab26b6438c580e5281edd6639f22e65'; | ||
|
|
||
| // This is a null IV | ||
| let zeroiv = new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]); | ||
|
|
||
| /** | ||
| * Encode the provided string of JSON data into an array that can be | ||
| * signed and encrypted, returning the resultant array. | ||
| * | ||
| * @param json | ||
| * The string of JSON data to encode. | ||
| * | ||
| * @return | ||
| * An encoded array of data. | ||
| */ | ||
| function encodeConnections(json) { | ||
| var encoder = new TextEncoder(); | ||
| return encoder.encode(json); | ||
| } | ||
|
|
||
| /** | ||
| * Take the encoded array and generate an HMAC signature for it, | ||
| * combining the HMAC signature with the data and returning the | ||
| * resultant array. | ||
| * | ||
| * @param key | ||
| * The 128-bit key used to sign the data, provided as a | ||
| * 16-character hexadecimal value. | ||
| * | ||
| * @param encoded | ||
| * The encoded data to sign. This should be a Uint8Array. | ||
| * | ||
| * @return | ||
| * A Uint8Array containing the signature of the encoded | ||
| * data and the encoded data. | ||
| */ | ||
| async function signConnections(key, encoded) { | ||
|
|
||
| // Import the key for HMAC signing. | ||
| var keyObject = await window.crypto.subtle.importKey( | ||
| "raw", | ||
| Uint8Array.fromHex(key), | ||
| { | ||
| name: "HMAC", | ||
| hash: "SHA-256" | ||
| }, | ||
| false, | ||
| [ | ||
| "sign" | ||
| ] | ||
| ); | ||
|
|
||
| // Generate the HMAC signature | ||
| var signature = new Uint8Array(await window.crypto.subtle.sign( | ||
| "HMAC", | ||
| keyObject, | ||
| encoded | ||
| )); | ||
|
|
||
| // Take the signature array and encoded data and combine them. | ||
| var signedArray = new Uint8Array(signature.length + encoded.length); | ||
| signedArray.set(signature); | ||
| signedArray.set(encoded, signature.length); | ||
|
|
||
| // Return the combined result. | ||
| return signedArray; | ||
| } | ||
|
|
||
| /** | ||
| * Given a key, IV, and array of signed data, encrypt the data and | ||
| * return a base64 string that can be used to send the data on | ||
| * to the JSON authentication module. | ||
| * | ||
| * @param key | ||
| * The 128-bit key to use to encrypt the data, provided as | ||
| * a 16-character hexadecimal string. | ||
| * | ||
| * @param iv | ||
| * The IV to use to initialize the encryption, in a | ||
| * Uint8Array. This can either be null (all zeroes) or | ||
| * a randomly-generated IV. | ||
| * | ||
| * @param signedData | ||
| * The Uint8Array containing the signed data that will | ||
| * be encrypted and sent on to the JSON authentication | ||
| * module to authenticate the user. | ||
| * | ||
| * @return | ||
| * A base64-encoded string of encrypted data that can be | ||
| * safely transmitted over an HTTPS connection to the | ||
| * JSON authentication module in order to authenticate the | ||
| * user to Guacamole. | ||
| */ | ||
| async function encryptData(key, iv, signedData) { | ||
|
|
||
| // Import the key for AES-CBC encryption. | ||
| var keyObject = await window.crypto.subtle.importKey( | ||
| "raw", | ||
| Uint8Array.fromHex(key), | ||
| { | ||
| name: "AES-CBC" | ||
| }, | ||
| false, | ||
| [ | ||
| "encrypt" | ||
| ] | ||
| ); | ||
|
|
||
| // Encrypt the data. | ||
| var encrypted = await window.crypto.subtle.encrypt( | ||
| { | ||
| name: "AES-CBC", | ||
| iv: iv | ||
| }, | ||
| keyObject, | ||
| signedData | ||
| ); | ||
|
|
||
| // Return the base64-encoded string. | ||
| return btoa(String.fromCharCode.apply(null, new Uint8Array(encrypted))); | ||
|
|
||
| } | ||
|
|
||
| // Run the functions, get the data. | ||
| var encoded = encodeConnections(json); | ||
| var signedData = await signConnections(key, encoded); | ||
| var b64encrypted = await encryptData(key, zeroiv, signedData); | ||
|
|
||
| // Print it out | ||
| console.log(b64encrypted); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably 16 bytes and 32-character hexadecimal value.