tap_agent/
message.rs

1//! Message types and utilities for the TAP Agent.
2//!
3//! This module provides constants and types for working with TAP messages,
4//! including security modes and message type identifiers.
5
6use serde::{Deserialize, Serialize};
7// Value is not used in this file
8
9/// Security mode for message packing and unpacking.
10///
11/// Defines the level of protection applied to messages:
12/// - `Plain`: No encryption or signing (insecure, only for testing)
13/// - `Signed`: Message is signed but not encrypted (integrity protected)
14/// - `AuthCrypt`: Message is authenticated and encrypted (confidentiality + integrity)
15/// - `Any`: Accept any security mode when unpacking (only used for receiving)
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum SecurityMode {
18    /// Plaintext - no encryption or signatures
19    Plain,
20    /// Signed - message is signed but not encrypted
21    Signed,
22    /// Authenticated and Encrypted - message is both signed and encrypted
23    AuthCrypt,
24    /// Any security mode - used for unpacking when any mode is acceptable
25    Any,
26}
27
28/// Message type identifiers used by the TAP Protocol
29/// These constant strings are used to identify different message types
30/// in the TAP protocol communications.
31/// Type identifier for Presentation messages
32pub const PRESENTATION_MESSAGE_TYPE: &str = "https://tap.rsvp/schema/1.0#Presentation";
33
34pub const DIDCOMM_SIGNED: &str = "application/didcomm-signed+json";
35pub const DIDCOMM_ENCRYPTED: &str = "application/didcomm-encrypted+json";
36
37// JWS-related types
38
39#[derive(Serialize, Deserialize, Debug)]
40pub struct Jws {
41    pub payload: String,
42    pub signatures: Vec<JwsSignature>,
43}
44
45#[derive(Serialize, Deserialize, Debug)]
46pub struct JwsSignature {
47    pub protected: String,
48    pub signature: String,
49    pub header: JwsHeader,
50}
51
52#[derive(Serialize, Deserialize, Debug)]
53pub struct JwsHeader {
54    pub kid: String,
55}
56
57// Structure for decoded JWS protected field
58#[derive(Serialize, Deserialize, Debug, Clone)]
59pub struct JwsProtected {
60    #[serde(default = "default_didcomm_signed")]
61    pub typ: String,
62    pub alg: String,
63}
64
65// Helper function for JwsProtected typ default
66fn default_didcomm_signed() -> String {
67    DIDCOMM_SIGNED.to_string()
68}
69// JWE-related types
70
71#[derive(Serialize, Deserialize, Debug)]
72pub struct Jwe {
73    pub ciphertext: String,
74    pub protected: String,
75    pub recipients: Vec<JweRecipient>,
76    pub tag: String,
77    pub iv: String,
78}
79
80#[derive(Serialize, Deserialize, Debug)]
81pub struct JweRecipient {
82    pub encrypted_key: String,
83    pub header: JweHeader,
84}
85
86#[derive(Serialize, Deserialize, Debug)]
87pub struct JweHeader {
88    pub kid: String,
89    #[serde(skip_serializing_if = "Option::is_none")]
90    pub sender_kid: Option<String>,
91}
92
93// Structure for decoded JWE protected field
94#[derive(Serialize, Deserialize, Debug)]
95pub struct JweProtected {
96    pub epk: EphemeralPublicKey,
97    pub apv: String,
98    #[serde(default = "default_didcomm_encrypted")]
99    pub typ: String,
100    pub enc: String,
101    pub alg: String,
102}
103
104// Helper function for JweProtected typ default
105fn default_didcomm_encrypted() -> String {
106    DIDCOMM_ENCRYPTED.to_string()
107}
108
109// Enum to handle different ephemeral public key types
110#[derive(Serialize, Deserialize, Debug)]
111#[serde(tag = "kty")]
112pub enum EphemeralPublicKey {
113    #[serde(rename = "EC")]
114    Ec { crv: String, x: String, y: String },
115    #[serde(rename = "OKP")]
116    Okp { crv: String, x: String },
117}