資安

Coap協議接入物聯網平臺(java實現)

作者:三烽

概述

物聯網平臺支持CoAP協議連接通信。CoAP協議適用在資源受限的低功耗設備上,尤其是NB-IoT的設備使用。本文介紹基於開源的CoAP協議進行對稱加密自主接入的流程,並提供java示例代碼。
官方鏈接

流程

一、連接CoAP服務器
endpoint地址為:{port}
:您的產品的。{port}:端口。使用對稱加密時端口為5682。
———————————————————————————————————————
二、進行設備認證
具體請求參數詳見官方文檔
———————————————————————————————————————
三、上報數據
具體請求參數詳見官方文檔

示例代碼

一、pom.xml依賴

<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>

二、代碼

/*
 * Copyright © 2019 Alibaba. All rights reserved.
 */

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;

/**
 * CoAP客戶端連接阿里雲物聯網平臺,基於eclipse californium開發。
 * 自主接入開發流程及參數填寫,請參見:
 * https://help.aliyun.com/document_detail/57697.html [使用對稱加密自主接入]
 */
public class IotCoapClientWithAes {

    // ===================需要用戶填寫的參數,開始===========================
    // 地域ID,當前僅支持華東2
    private static String regionId = "cn-shanghai";
    // 產品productKey
    private static String productKey = "a1fBBLKhpwv";
    // 設備名成deviceName
    private static String deviceName = "AirC";
    // 設備密鑰deviceSecret
    private static String deviceSecret = "LL6VoF5L49EbMdoGvdwRppOgHWuwE9bL";
    //發送的消息內容payload
    private static String payload = "hello coap!!";
    // ===================需要用戶填寫的參數,結束===========================

    // 定義加密方式 MAC算法可選以下多種算法 HmacMD5 HmacSHA1,需與signmethod一致。
    private static final String HMAC_ALGORITHM = "hmacsha1";

    // CoAP接入地址,對稱加密端口號是5682。
    private static String serverURI = "coap://" + productKey + ".coap." + regionId + ".link.aliyuncs.com:5682";

    // 發送消息用的Topic。需要在控制檯自定義Topic,設備操作權限需選擇為“發佈”。
    private static String updateTopic = "/" + productKey + "/" + deviceName + "/user/update";

    // token option
    private static final int COAP2_OPTION_TOKEN = 2088;
    // seq option
    private static final int COAP2_OPTION_SEQ = 2089;

    // 加密算法sha256
    private static final String SHA_256 = "SHA-256";

    private static final int DIGITAL_16 = 16;
    private static final int DIGITAL_48 = 48;

    // CoAP客戶端
    private CoapClient coapClient = new CoapClient();

    // token 7天有效,失效後需要重新獲取。
    private String token = null;
    private String random = null;
    @SuppressWarnings("unused")
    private long seqOffset = 0;

    /**
     * 初始化CoAP客戶端
     *
     * @param productKey 產品key
     * @param deviceName 設備名稱
     * @param deviceSecret 設備密鑰
     */
    public void conenct(String productKey, String deviceName, String deviceSecret) {
        try {
            // 認證uri,/auth
            String uri = serverURI + "/auth";

            // 只支持POST方法
            Request request = new Request(Code.POST, Type.CON);

            // 設置option
            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);

            // 設置認證uri
            request.setURI(uri);

            // 設置認證請求payload
            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();
        }
    }

    /**
     * 發送消息
     *
     * @param topic 發送消息的Topic
     * @param payload 消息內容
     */
    public void publish(String topic, byte[] payload) {
        try {
            // 消息發佈uri,/topic/${topic}
            String uri = serverURI + "/topic" + topic;

            // AES加密seq,seq=RandomUtils.nextInt()
            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);

            // 只支持POST方法
            Request request = new Request(CoAP.Code.POST, CoAP.Type.CON);

            // 設置option
            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);

            // 設置消息發佈uri
            request.setURI(uri);

            // 設置消息payload
            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();
        }
    }

    /**
     * 生成認證請求內容
     *
     * @param productKey 產品key
     * @param deviceName 設備名字
     * @param deviceSecret 設備密鑰
     * @return 認證請求
     */
    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();
    }

    /**
     * 設備端簽名
     *
     * @param params 簽名參數
     * @param deviceSecret 設備密鑰
     * @return 簽名十六進制字符串
     */
    private String sign(JSONObject params, String deviceSecret) {

        // 請求參數按字典順序排序
        Set<String> keys = getSortedKeys(params);

        // sign、signmethod、version、resources除外
        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;
    }

    /**
     * 獲取JSON對象排序後的key集合
     *
     * @param json 需要排序的JSON對象
     * @return 排序後的key集合
     */
    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();
    }

    /**
     * 使用 HMAC_ALGORITHM 加密
     *
     * @param content 明文
     * @param secret 密鑰
     * @return 密文
     */
    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;
        }
    }

    /**
     * SHA-256
     *
     * @param str 待加密的報文
     */
    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;
    }

    // AES 加解密算法
    private static final String IV = "543yhjy97ae7fyfg";
    private static final String TRANSFORM = "AES/CBC/PKCS5Padding";
    private static final String ALGORITHM = "AES";

    /**
     * key length = 16 bits
     */
    private byte[] encrypt(byte[] content, byte[] key) {
        return encrypt(content, key, IV);
    }

    /**
     * key length = 16 bits
     */
    private byte[] decrypt(byte[] content, byte[] key) {
        return decrypt(content, key, IV);
    }

    /**
     * aes 128 cbc key length = 16 bits
     */
    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;
        }
    }

    /**
     * aes 128 cbc key length = 16 bits
     */
    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));
    }
}

注意事項

1、coap協議是短連接,和mqtt長連接不同,所以在控制檯的設備行為分析日誌裡看不到記錄,需要注意。
2、coap協議發送的payload有大小限制,不能超過1KB,如果超過的話這個請求會被拒絕,消息發不到平臺。代碼中System.out.println(request.getPayload().length);就是用來打印payload長度的。
3、目前只有華東2地域的設備支持使用coap協議接入。

Leave a Reply

Your email address will not be published. Required fields are marked *