概述
阿里云物联网平台支持CoAP协议连接通信。CoAP协议适用在资源受限的低功耗设备上,尤其是NB-IoT的设备使用。本文介绍基于开源的CoAP协议进行对称加密自主接入的流程,并提供java示例代码。
官方文档:CoAP连接通信
说明与限制
- Topic规范和MQTT Topic一致,CoAP协议内 coap://host:port/topic/topic接口对于所有{topic}和MQTT Topic可以复用。
- 客户端缓存认证返回的token是请求的令牌。
- 传输的数据大小依赖于MTU的大小,建议在1 KB以内。
- 仅华东2(上海)地域支持CoAP通信。
接入流程
连接CoAP服务器
- endpoint地址为:YourProductKey.coap.cn−shanghai.link.aliyuncs.com:{port}
- ${YourProductKey}:产品的ProductKey
- ${port}:端口。使用对称加密时端口为5682
设备认证
POST /auth
Host: ${YourProductKey}.coap.cn-shanghai.link.aliyuncs.com
Port: 5682
Accept: application/json or application/cbor
Content-Format: application/json or application/cbor
payload: {"productKey":"a1NUjcV****","deviceName":"ff1a1****","clientId":"a1NUjcV****&ff1a1****","sign":"F9FD53EE0CD010FCA40D14A9FE******", "seq":"10"}
参数 |
说明 |
Method |
请求方法。只支持POST方法 |
URL |
URL地址,取值:/auth |
Host |
Endpoint地址。取值格式:YourProductKey.coap.cn−shanghai.link.aliyuncs.com。其中,变量{YourProductKey}需替换为您的产品Key。 |
Port |
端口,取值:5682 |
Accept |
设备接收的数据编码方式。目前,支持两种方式:application/json和application/cbor。 |
Content-Format |
设备发送给物联网平台的上行数据的编码格式,目前支持两种方式: application/json和application/cbor。 |
payload |
设备认证信息内容,JSON数据格式。具体参数,请参见payload说明。 |
上报数据
POST /topic/${topic}
Host: ${YourProductKey}.coap.cn-shanghai.link.aliyuncs.com
Port: 5682
Accept: application/json or application/cbor
Content-Format: application/json or application/cbor
payload: ${your_data}
CustomOptions: number:2088(标识token), 2089(seq)
参数 |
是否必需 |
说明 |
Method |
是 |
请求方法。支持POST方法。 |
URL |
是 |
传入格式:/topic/topic。其中,变量{topic}需替换为设备数据上行Topic。 |
Host |
是 |
endpoint地址。传入格式:YourProductKey.coap.cn−shanghai.link.aliyuncs.com。其中,{YourProductKey}需替换为设备所属产品的Key。 |
Port |
是 |
端口。取值:5682。 |
Accept |
是 |
设备接收的数据编码方式。目前,支持两种方式:application/json和application/cbor。 |
Content-Format |
是 |
上行数据的编码格式,服务端对此不做校验。目前,支持两种方式:application/json和application/cbor。 |
payload |
是 |
待上传的数据经高级加密标准(AES)加密后的数据。加密规则 |
CustomOptions |
是 |
option值有2088和2089两种类型。类型解释 |
java示例代码
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>californium-core</artifactId>
<version>2.0.0-M17</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
</dependency>
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.RandomUtils;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.Utils;
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.coap.CoAP.Code;
import org.eclipse.californium.core.coap.CoAP.Type;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.coap.Option;
import org.eclipse.californium.core.coap.OptionNumberRegistry;
import org.eclipse.californium.core.coap.OptionSet;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.elements.exception.ConnectorException;
import com.alibaba.fastjson.JSONObject;
public class IotCoapClientWithAes {
private static String regionId = "cn-shanghai";
private static String productKey = "****";
private static String deviceName = "****";
private static String deviceSecret = "****";
private static String payload = "hello coap!!";
private static final String HMAC_ALGORITHM = "hmacsha1";
private static String serverURI = "coap://" + productKey + ".coap." + regionId + ".link.aliyuncs.com:5682";
private static String updateTopic = "/" + productKey + "/" + deviceName + "/user/update";
private static final int COAP2_OPTION_TOKEN = 2088;
private static final int COAP2_OPTION_SEQ = 2089;
private static final String SHA_256 = "SHA-256";
private static final int DIGITAL_16 = 16;
private static final int DIGITAL_48 = 48;
private CoapClient coapClient = new CoapClient();
private String token = null;
private String random = null;
@SuppressWarnings("unused")
private long seqOffset = 0;
public void conenct(String productKey, String deviceName, String deviceSecret) {
try {
String uri = serverURI + "/auth";
Request request = new Request(Code.POST, Type.CON);
OptionSet optionSet = new OptionSet();
optionSet.addOption(new Option(OptionNumberRegistry.CONTENT_FORMAT, MediaTypeRegistry.APPLICATION_JSON));
optionSet.addOption(new Option(OptionNumberRegistry.ACCEPT, MediaTypeRegistry.APPLICATION_JSON));
request.setOptions(optionSet);
request.setURI(uri);
request.setPayload(authBody(productKey, deviceName, deviceSecret));
CoapResponse response = coapClient.advanced(request);
System.out.println(Utils.prettyPrint(response));
System.out.println();
JSONObject json = JSONObject.parseObject(response.getResponseText());
token = json.getString("token");
random = json.getString("random");
seqOffset = json.getLongValue("seqOffset");
} catch (ConnectorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void publish(String topic, byte[] payload) {
try {
String uri = serverURI + "/topic" + topic;
String shaKey = encod(deviceSecret + "," + random);
byte[] keys = Hex.decodeHex(shaKey.substring(DIGITAL_16, DIGITAL_48));
byte[] seqBytes = encrypt(String.valueOf(RandomUtils.nextInt()).getBytes(StandardCharsets.UTF_8), keys);
Request request = new Request(CoAP.Code.POST, CoAP.Type.CON);
OptionSet optionSet = new OptionSet();
optionSet.addOption(new Option(OptionNumberRegistry.CONTENT_FORMAT, MediaTypeRegistry.APPLICATION_JSON));
optionSet.addOption(new Option(OptionNumberRegistry.ACCEPT, MediaTypeRegistry.APPLICATION_JSON));
optionSet.addOption(new Option(COAP2_OPTION_TOKEN, token));
optionSet.addOption(new Option(COAP2_OPTION_SEQ, seqBytes));
request.setOptions(optionSet);
request.setURI(uri);
request.setPayload(encrypt(payload, keys));
CoapResponse response = coapClient.advanced(request);
System.out.println("----------------");
System.out.println(request.getPayload().length);
System.out.println("----------------");
System.out.println(Utils.prettyPrint(response));
String result = null;
if (response.getPayload() != null) {
result = new String(decrypt(response.getPayload(), keys));
}
System.out.println("payload: " + result);
System.out.println();
} catch (ConnectorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DecoderException e) {
e.printStackTrace();
}
}
private String authBody(String productKey, String deviceName, String deviceSecret) {
JSONObject body = new JSONObject();
body.put("productKey", productKey);
body.put("deviceName", deviceName);
body.put("clientId", productKey + "." + deviceName);
body.put("timestamp", String.valueOf(System.currentTimeMillis()));
body.put("signmethod", HMAC_ALGORITHM);
body.put("seq", DIGITAL_16);
body.put("sign", sign(body, deviceSecret));
System.out.println("----- auth body -----");
System.out.println(body.toJSONString());
return body.toJSONString();
}
private String sign(JSONObject params, String deviceSecret) {
Set<String> keys = getSortedKeys(params);
keys.remove("sign");
keys.remove("signmethod");
keys.remove("version");
keys.remove("resources");
StringBuffer content = new StringBuffer();
for (String key : keys) {
content.append(key);
content.append(params.getString(key));
}
String sign = encrypt(content.toString(), deviceSecret);
System.out.println("sign content=" + content);
System.out.println("sign result=" + sign);
return sign;
}
private Set<String> getSortedKeys(JSONObject json) {
SortedMap<String, String> map = new TreeMap<String, String>();
for (String key : json.keySet()) {
String vlaue = json.getString(key);
map.put(key, vlaue);
}
return map.keySet();
}
private String encrypt(String content, String secret) {
try {
byte[] text = content.getBytes(StandardCharsets.UTF_8);
byte[] key = secret.getBytes(StandardCharsets.UTF_8);
SecretKeySpec secretKey = new SecretKeySpec(key, HMAC_ALGORITHM);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return Hex.encodeHexString(mac.doFinal(text));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private String encod(String str) {
MessageDigest messageDigest;
String encdeStr = "";
try {
messageDigest = MessageDigest.getInstance(SHA_256);
byte[] hash = messageDigest.digest(str.getBytes(StandardCharsets.UTF_8));
encdeStr = Hex.encodeHexString(hash);
} catch (NoSuchAlgorithmException e) {
System.out.println(String.format("Exception@encod: str=%s;", str));
e.printStackTrace();
return null;
}
return encdeStr;
}
private static final String IV = "543yhjy97ae7fyfg";
private static final String TRANSFORM = "AES/CBC/PKCS5Padding";
private static final String ALGORITHM = "AES";
private byte[] encrypt(byte[] content, byte[] key) {
return encrypt(content, key, IV);
}
private byte[] decrypt(byte[] content, byte[] key) {
return decrypt(content, key, IV);
}
private byte[] encrypt(byte[] content, byte[] key, String ivContent) {
try {
SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORM);
IvParameterSpec iv = new IvParameterSpec(ivContent.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
return cipher.doFinal(content);
} catch (Exception ex) {
System.out.println(
String.format("AES encrypt error, %s, %s, %s", content, Hex.encodeHex(key), ex.getMessage()));
return null;
}
}
private byte[] decrypt(byte[] content, byte[] key, String ivContent) {
try {
SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORM);
IvParameterSpec iv = new IvParameterSpec(ivContent.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
return cipher.doFinal(content);
} catch (Exception ex) {
System.out.println(String.format("AES decrypt error, %s, %s, %s", Hex.encodeHex(content),
Hex.encodeHex(key), ex.getMessage()));
return null;
}
}
public static void main(String[] args) throws InterruptedException {
IotCoapClientWithAes client = new IotCoapClientWithAes();
client.conenct(productKey, deviceName, deviceSecret);
client.publish(updateTopic, payload.getBytes(StandardCharsets.UTF_8));
}
}
注意事项
- coap协议是短连接,和mqtt长连接不同,所以在控制台的设备行为日志里看不到记录,需要注意。
- coap协议发送的payload有大小限制,不能超过1KB,如果超过的话这个请求会被拒绝,消息发不到平台。代码中System.out.println(request.getPayload().length);就是用来打印payload长度的。