msg.h (3828B)
1 #ifndef LIBQAEDA_MSG_H_ 2 #define LIBQAEDA_MSG_H_ 3 4 #include <stddef.h> 5 #include <time.h> 6 7 #include "lq/crypto.h" 8 #include "lq/store.h" 9 10 /** 11 * \struct LQMsg 12 * 13 * \brief Represents a message that is used as part of certificate as request or response. 14 * 15 * \see lq_msg_t 16 */ 17 struct lq_msg_t { 18 char *data; ///< Arbitrary data constituting the message. 19 size_t len; ///< Length of arbitrary data. 20 struct timespec time; ///< Nanosecond timestamp of when the message was created. 21 LQPubKey *pubkey; ///< Public key authoring the message. Must be checked against any private key calculating a signature over it. 22 }; 23 typedef struct lq_msg_t LQMsg; 24 25 /** 26 * @brief Instantiate a new message object. 27 * @param[in] Message data. Data will be copied. 28 * @param[in] Length of message data. 29 * @return Instantiated message object. It is the caller's responsibility to free to object. 30 * @see lq_msg_free 31 */ 32 LQMsg* lq_msg_new(const char *msg_data, size_t msg_len); 33 34 /** 35 * @brief Calculate a signature over the message. Uses default salt value. 36 * @param[in] Message to sign. 37 * @param[in] Private key to sign with. 38 * @param[in] Salt data to secure signature with. Set to NULL if salt is not to be used. 39 * @return Signature object. Object will be NULL if signature calculation failed. It is the caller's responsibility to free the signature object. 40 * @see lq_signature_free 41 */ 42 LQSig* lq_msg_sign(LQMsg *msg, LQPrivKey *pk, const char *salt); 43 44 /** 45 * @brief Calculate a signature over the message with a specified salt value. The salt value length must be LQ_SALT_LEN. 46 * @param[in] Message to sign. 47 * @param[in] Private key to sign with. 48 * @param[in] Salt data to secure signature with. Set to NULL if salt is not to be used. 49 * @param[in] Extra data to prefix message data with when calculating digest. If set to NULL, only message data will be used in digest. 50 * @param[in] Length of extra data. Ignored if extra data is NULL. 51 * @return Signature object. Object will be NULL if signature calculation failed. It is the caller's responsibility to free the signature object. 52 * @see lq_signature_free 53 */ 54 LQSig* lq_msg_sign_extra(LQMsg *msg, LQPrivKey *pk, const char *salt, const char *extra, size_t extra_len); 55 56 /** 57 * @brief Serialize message data payload for inclusion in certificate. 58 * @param[in] Message to serialize. 59 * @param[out] Output buffer. 60 * @param[out] Value behind pointer must contain the capacity of output buffer. Will be overwritten with the actual number of bytes written. 61 * @param[in] Store implementations to use for storing serialized message data. 62 * @return ERR_OK if serialization is successful, or: 63 * * ERR_INIT if the serialization object couldn't be instantiated. 64 * * ERR_OVERFLOW if output exceeded the available space in output buffer. 65 * * ERR_WRITE if serialization of an element failed. 66 * * ERR_ENCODING if generating the final serialization string failed. 67 */ 68 int lq_msg_serialize(LQMsg *msg, char *out, size_t *out_len, LQResolve *resolve); 69 70 /** 71 * @brief Deserialize message data payload from certificate. 72 * @param[out] Pointer to instantiated message. It is the caller's responsibility to free the message object. 73 * @param[in] Serialized data. 74 * @param[in] Length of serialized data. 75 * @param[in] Store implementations to use for resolving content key from deserialized message data. 76 * @return ERR_OK if deserialization is successful, or: 77 * * ERR_INIT if the serialization object couldn't be instantiated. 78 * * ERR_READ if deserialization of an element failed. 79 * * ERR_ENCODING if interpretation of the serialized data failed. 80 * @see lq_msg_free 81 */ 82 int lq_msg_deserialize(LQMsg **msg, const char *in, size_t in_len, LQResolve *resolve); 83 84 /** 85 * @brief Free an instantiated message. 86 * @param[in] Message to free. 87 */ 88 void lq_msg_free(LQMsg *msg); 89 90 #endif // LIBQAEDA_MSG_H_