Skip to content

Commit

Permalink
Add --psk-index, --payload-file and --payload-format.
Browse files Browse the repository at this point in the history
The PskCredentialStore keeps the credential's order of the psk-file.

Signed-off-by: Achim Kraus <achim.kraus@bosch.io>
  • Loading branch information
Achim Kraus committed Aug 9, 2020
1 parent daf8688 commit bada767
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 20 deletions.
Expand Up @@ -15,12 +15,17 @@
******************************************************************************/
package org.eclipse.californium.cli;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.PrivateKey;
import java.security.PublicKey;

import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.coap.CoAP.Type;
import org.eclipse.californium.core.network.config.NetworkConfig.Keys;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.elements.util.StringUtil;

Expand Down Expand Up @@ -130,13 +135,30 @@ public static class Payload {
*/
@Option(names = "--payload64", description = "payload, base64")
public String base64;

/**
* Payload from file.
*
* @since 2.4
*/
@Option(names = "--payload-file", description = "payload from file")
public String file;
}

/**
* Payload in bytes.
*/
public byte[] payloadBytes;

/**
* Apply {@link String#format(String, Object...)} to payload. The used
* parameter depends on the client implementation.
*
* @since 2.4
*/
@Option(names = "--payload-format", description = "apply format to payload.")
public boolean payloadFormat;

/**
* Request type. {@code true} for {@link Type#CON}, {@code false} for
* {@link Type#NON}, and {@code null}, if not defined.
Expand Down Expand Up @@ -169,6 +191,39 @@ public void defaults() {
payloadBytes = StringUtil.hex2ByteArray(payload.hex);
} else if (payload.base64 != null) {
payloadBytes = StringUtil.base64ToByteArray(payload.base64);
} else if (payload.file != null) {
int max = networkConfig.getInt(Keys.MAX_RESOURCE_BODY_SIZE);
File file = new File(payload.file);
if (file.canRead()) {
long length = file.length();
if (length <= max) {
payloadBytes = new byte[(int)length];
InputStream in = null;
try {
in = new FileInputStream(file);
int len = in.read(payloadBytes);
if (len != length) {
LOGGER.error("file {} with {} bytes, read {} bytes!", payload.file, length, len);
}
} catch (FileNotFoundException e) {
LOGGER.error("Missing file {}", payload.file, e);
} catch (IOException e) {
LOGGER.error("Error reading file {}", payload.file, e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
LOGGER.error("Error closing file {}", payload.file, e);
}
}
}
} else {
LOGGER.error("file {} with {} bytes is too large! (Maximum {} bytes.)", payload.file, length, max);
}
} else {
LOGGER.error("Can not read file {} ({})", payload.file, file.getAbsolutePath());
}
}
}
}
Expand Down
Expand Up @@ -15,17 +15,14 @@
******************************************************************************/
package org.eclipse.californium.cli;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.crypto.SecretKey;

Expand Down Expand Up @@ -162,6 +159,7 @@ public static class Authentication {
public Trust trust;

public static class Trust {

/**
* X509 trusts loaded from store.
*/
Expand All @@ -176,6 +174,14 @@ public static class Trust {
public boolean trustall;
}

/**
* PSK store index.
*
* @since 2.4
*/
@Option(names = "--psk-index", description = "Index of identity in PSK store. Starts at 0.")
public Integer pskIndex;

/**
* PSK store file. Lines in format:
*
Expand Down Expand Up @@ -274,6 +280,11 @@ public void defaults() {
System.err.println("Use either '--psk-store' or single psk credentials!");
helpRequested = true;
}
if (pskIndex != null) {
secret = new ConnectorConfig.Secret();
secret.hex = StringUtil.byteArray2Hex(pskStore.getSecrets(pskIndex));
identity = pskStore.getIdentity(pskIndex);
}
}
if (secret != null && secretKey == null) {
if (secret.text != null) {
Expand Down Expand Up @@ -424,30 +435,43 @@ public PskCredentialStore convert(String value) throws Exception {
* identity = secret - key(base64)
* </pre>
*
* The identity must not contain a {@code =}! The created psk credentials
* store keeps the order of the credentials in the file. Index {@code 0}
* will contain the credential of the first line.
*
* @param file filename of credentials store.
* @return psk credentials store
*/
public static PskCredentialStore loadPskCredentials(String file) {
Properties credentials = new Properties();
boolean error = false;
BufferedReader lineReader = null;
try (FileReader reader = new FileReader(file)) {
credentials.load(reader);
Set<Object> keys = credentials.keySet();
SortedSet<String> sortedKeys = new TreeSet<>();
for (Object key : keys) {
if (key instanceof String) {
sortedKeys.add((String) key);
PskCredentialStore pskCredentials = new PskCredentialStore();
int lineNumber = 0;
String line;
lineReader = new BufferedReader(reader);
while ((line = lineReader.readLine()) != null) {
++lineNumber;
String[] entry = line.split("=", 2);
if (entry.length == 2) {
byte[] secretBytes = StringUtil.base64ToByteArray(entry[1]);
pskCredentials.add(entry[0], secretBytes);
} else {
error = true;
LOGGER.error("{}: '{}' invalid psk-line!", lineNumber, line);
}
}
if (!sortedKeys.isEmpty()) {
PskCredentialStore pskCredentials = new PskCredentialStore();
for (String key : sortedKeys) {
String secret = credentials.getProperty(key);
byte[] secretBytes = StringUtil.base64ToByteArray(secret);
pskCredentials.add(key, secretBytes);
}
if (!error) {
return pskCredentials;
}
} catch (IOException e) {
} finally {
if (lineReader != null) {
try {
lineReader.close();
} catch (IOException e) {
}
}
}
return null;
}
Expand All @@ -456,6 +480,7 @@ public static PskCredentialStore loadPskCredentials(String file) {
* PSK credentials store.
*/
public static class PskCredentialStore {

/**
* Identities.
*/
Expand All @@ -469,7 +494,7 @@ public static class PskCredentialStore {
* Add entry.
*
* @param identity identity
* @param secret secret key
* @param secret secret key
*/
private void add(String identity, byte[] secret) {
identities.add(identity);
Expand Down

0 comments on commit bada767

Please sign in to comment.