Skip to content
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

Add support for Pre-Shared-Key (PSK-TLS) connections #780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion app/src/actions/ConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,19 @@ export const loadConnectionSettings = () => async (dispatch: Dispatch<any>, getS
}
}

export type CertificateTypes = 'selfSignedCertificate' | 'clientCertificate' | 'clientKey'
// A Pre-shared-key is a symmetric encryption key, such as an AES-128 key, that is shared
// ahead of time (out of band) between the client and server. It might be programmed into
// the firmware at the factory for an IoT device.
//
// The server optionally sends a `psk_hint`, and the client sends a `pskIdentity` (also
// sometimes called an hint) that functions as a username. The server uses the `pskIdentity`
// to figure out which shared key to use, and they both encrypt using that key.
//
// For more information on the `psk` and `pskIdentity` options:
// See https://mosquitto.org/man/mosquitto-conf-5.html#idm854 "Pre-shared-key based SSL/TLS Support", and also
// mosquitto's `psk_file` option.

export type CertificateTypes = 'selfSignedCertificate' | 'clientCertificate' | 'clientKey' | 'psk'
export const selectCertificate =
(type: CertificateTypes, connectionId: string) => async (dispatch: Dispatch<any>, getState: () => AppState) => {
try {
Expand Down
12 changes: 11 additions & 1 deletion app/src/components/ConnectionSetup/Certificates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react'
import CertificateFileSelection from './CertificateFileSelection'
import Undo from '@material-ui/icons/Undo'
import { bindActionCreators } from 'redux'
import { Button, Grid } from '@material-ui/core'
import { Button, Grid, TextField } from '@material-ui/core'
import { connect } from 'react-redux'
import { connectionManagerActions } from '../../actions'
import { ConnectionOptions } from '../../model/ConnectionOptions'
Expand Down Expand Up @@ -68,6 +68,16 @@ class Certificates extends React.PureComponent<Props, State> {
certificateType="clientKey"
/>
</Grid>
<Grid item={true} xs={12} className={classes.gridPadding}>
<TextField
className={`${classes.fullWidth} advanced-connection-settings-topic-input`}
label="Pre Shared Key (PSK)"
placeholder="0123456789ABCDEF0123456789ABCDEF"
margin="normal"
value={this.props.connection.psk}
onChange={this.handleChange('psk')}
/>
</Grid>
<Grid item={true} xs={2} className={classes.gridPadding}>
<br />
<Button
Expand Down
2 changes: 2 additions & 0 deletions app/src/model/ConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ConnectionOptions {
clientCertificate?: CertificateParameters
clientKey?: CertificateParameters
clientId?: string
psk?: string
subscriptions: Array<Subscription>
}

Expand All @@ -45,6 +46,7 @@ export function toMqttConnection(options: ConnectionOptions): MqttOptions | unde
certificateAuthority: options.selfSignedCertificate ? options.selfSignedCertificate.data : undefined,
clientCertificate: options.clientCertificate ? options.clientCertificate.data : undefined,
clientKey: options.clientKey ? options.clientKey.data : undefined,
psk: options.psk ? options.psk : undefined,
}
}

Expand Down
16 changes: 15 additions & 1 deletion backend/src/DataSource/MqttSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface MqttOptions {
certificateAuthority?: string
clientCertificate?: string
clientKey?: string
psk?: string
}

export interface Subscription {
Expand Down Expand Up @@ -47,6 +48,18 @@ export class MqttSource implements DataSource<MqttOptions> {
throw error
}

// See psk under https://nodejs.org/api/tls.html#tls_tls_connect_options_callback
const pskOptions = {
pskCallback: () => {
if (options.psk) {
return {
psk: Buffer.from(options.psk, 'hex'),
identity: options.username,
}
}
},
}

const client = mqttConnect(url.toString(), {
resubscribe: false,
rejectUnauthorized: options.certValidation,
Expand All @@ -57,12 +70,13 @@ export class MqttSource implements DataSource<MqttOptions> {
ca: options.certificateAuthority ? Buffer.from(options.certificateAuthority, 'base64') : undefined,
cert: options.clientCertificate ? Buffer.from(options.clientCertificate, 'base64') : undefined,
key: options.clientKey ? Buffer.from(options.clientKey, 'base64') : undefined,
...(options.psk ? pskOptions : {}),
} as any)

this.client = client

client.on('error', (error: Error) => {
console.log(error)
console.log('client error', error)
this.stateMachine.setError(error)
})

Expand Down