Introducing a new way to review your PRs.

Read the announcement

capcom6 / android-sms-gateway

The SMS Gateway for Android™ app enables sending and receiving SMS messages through an API that can be accessed directly on the device or via a cloud server when direct device access is not possible.

View on GitHub

20 documents · Updated Nov 20, 2026

docs / security-and-encryption

Security and encryption

The security model protects gateway credentials, message data, local API actions, and MMS files at the points where they enter or leave the app. It combines optional message decryption, password checks, scoped local API access, Android network rules, and narrow file-sharing paths. See Local API authentication for token policy, Local HTTP API for the endpoint surface, and Device identity and key management for device keys and rotation.

Security model

Protection layers

The app uses separate controls for different trust boundaries. Message encryption protects selected outgoing payload fields. Local API authentication controls who can use the phone as a gateway. Android network policy controls which cleartext connections the app may make. File rules limit which local files can leave the app.

These controls are related but do not replace each other:

  • A message passphrase does not protect the local API.
  • Basic authentication or a JWT does not encrypt an HTTP connection.
  • HTTPS protects a webhook or cloud request in transit, but it does not encrypt stored message records.
  • A file-provider rule does not grant access to a protected local API attachment route.

References: app/…/EncryptionService.kt• L5-23 app/…/WebService.kt• L77-109 app/…/network_security_config.xml• L1-13 app/…/AndroidManifest.xml• L24-30 app/…/AndroidManifest.xml• L98-106

Sensitive data paths

Sensitive values move through a small number of paths. The main protection point is before an external system or another client receives the value.

This diagram shows the main paths for outgoing messages, MMS content, cloud credentials, and inbound MMS files.

flowchart LR
    Caller[API or cloud request] --> Record[Local message record]
    Record -->|encrypted flag| Decrypt[Passphrase or device-key decryption]
    Decrypt --> SMS[Android SMS service]
    MMSOut[Outgoing MMS content] --> PDU[App-private PDU file]
    PDU -->|temporary URI grant| Telephony[Android telephony services]
    LocalAPI[Local API client] --> Auth[Basic auth or scoped JWT]
    Auth --> Inbox[Inbox records and MMS attachment route]
    MMS[Phone MMS provider] --> Private[App-private attachment storage]
    Private --> Inbox
    Cloud[Cloud account action] --> Prompt[Password prompt when needed]
    Prompt --> CloudAPI[Cloud API]

The local server can expose message and inbox data after authentication. The cloud path can carry passphrase-encrypted or device-key-encrypted outgoing content to the device. The device decrypts it before sending. Outgoing MMS data uses a temporary file grant for Android telephony services. Inbound MMS bytes first enter app-private storage and leave only through an authorized attachment request or a configured webhook path.

References: app/…/GatewayService.kt• L282-306 app/…/MessagesService.kt• L411-524 app/…/MmsSender.kt• L27-80 app/…/InboxRoutes.kt• L154-178 app/…/IncomingMessagesService.kt• L67-107

Message encryption

Encryption settings

The app supports two message-encryption modes. A passphrase mode uses a configured secret. A device-key mode uses the device's current public key and lets the device decrypt with its matching private key. An empty passphrase remains the UI's disable state for passphrase encryption.

The passphrase is saved in the app settings store. Device-key encryption depends on the device identity and key lifecycle described in Device identity and key management. Neither mode is a general encryption layer for all app settings, logs, inbox records, or files.

The encryption setting is part of the grouped settings API. An API client with settings-write access can change the passphrase setting as well as the app user.

References: app/…/root_preferences.xml• L32-38 app/…/EncryptionSettings.kt• L7-18 app/…/SettingsService.kt• L25-63 app/…/PreferencesStorage.kt• L7-29 app/…/EncryptionProviderFactory.kt• L10-25

Outbound decryption

An outgoing message carries an isEncrypted flag with its content. When the flag is set, the send path selects the provider named by the encrypted value and decrypts the text or data payload. It also decrypts each recipient number before phone-number validation and delivery. Plaintext is therefore needed only at the final handoff to the Android SMS service.

The same flag can arrive from the cloud gateway or the local API. If the required passphrase or device private key is missing, or if the encrypted value has an invalid format, the send operation fails instead of sending ciphertext as normal content.

References: app/…/Message.kt• L6-14 app/…/MessagesRoutes.kt• L176-192 app/…/GatewayService.kt• L282-306 app/…/MessagesService.kt• L422-464 app/…/MessagesService.kt• L514-524 app/…/EncryptionService.kt• L5-23

Encryption formats

The provider boundary lets the app accept older encrypted messages while using the current format for new passphrase encryption. The algorithm marker is part of the message contract, so it must remain with the encrypted value as data moves between the cloud, local API, storage, and send path.

The current passphrase format uses AES with a passphrase-derived key and separate random IV and salt values. The older passphrase format remains available for decryption. The device-key format uses hybrid encryption: a fresh AES key protects the message, while the device's RSA public key protects that AES key. It uses AES-GCM for the message and RSA-OAEP with SHA-256 plus an SHA-1 MGF1 digest for the wrapped key. The same OAEP parameters must be used for encryption and decryption because Android Keystore support depends on them. Its value carries an encryption version and key version so the device can select the private key that matches the message.

FormatMain useKey sourceCompatibility rule
aes-256-cbc/pbkdf2-sha1/v2New passphrase-encrypted valuesConfigured passphraseDefault passphrase format
aes-256-cbc/pbkdf2-sha1Older passphrase-encrypted valuesConfigured passphraseKeep for old data
rsa-oaep-aes-256-gcmDevice-key-encrypted valuesDevice public/private key pairPreserve the embedded device key version and OAEP parameter set

References: app/…/EncryptionProviderFactory.kt• L7-25 app/…/RSAEncryptionProvider.kt• L15-133 app/…/BasePassphraseEncryptionProvider.kt• L12-110 app/…/HardenedPassphraseEncryptionProvider.kt• L10-34 app/…/LegacyPassphraseEncryptionProvider.kt• L10-31

Key version handling

Device-key decryption uses the key version carried by the encrypted value. This permits messages already in flight to use an older private key while a newer device key is active. Rotation and key retention rules are covered in Device identity and key management.

The provider cannot decrypt device-key data when the recorded version is malformed, unsupported, or no longer available. The encrypted value therefore acts as a contract between the sender's public-key view and the device's retained private-key state.

References: app/…/RSAEncryptionProvider.kt• L62-107 app/…/DeviceService.kt• L188-215

Password prompting

Cloud account password

The password prompt protects cloud account actions that need the current password. The app asks for the password when it is not already held in the cloud registration settings. It uses the result for a password change or for a one-time login-code request, then dismisses the dialog.

The prompt accepts a non-empty value and returns no usable password when the user cancels it. A new cloud password must meet the UI minimum before the change starts. The current password is sent with the change request, and the locally held password is updated only after the cloud service accepts the change.

References: app/…/PasswordPromptDialogFragment.kt• L17-69 app/…/CloudServerSettingsFragment.kt• L76-141 app/…/CloudServerSettingsFragment.kt• L153-167 app/…/GatewayService.kt• L70-103

Local server credentials

The local server does not use the cloud password prompt. It uses its own username and password for HTTP Basic authentication. The default username is sms, and the app creates a password when none exists. The local password must have at least eight characters when changed in the settings screen.

Basic authentication is a broad access path. A valid Basic credential can pass the normal route scope checks, so it must be treated as an administrator secret. Clients that need narrower access should use scoped JWTs instead. The detailed token and scope rules are in Local API authentication.

References: app/…/LocalServerSettings.kt• L18-39 app/…/WebService.kt• L83-109 app/…/ScopeAuthorization.kt• L12-45 app/…/LocalServerSettingsFragment.kt• L48-65

Local server network security

Transport choices

The embedded local server serves HTTP on its configured port. It does not set up TLS in the server service, so Basic credentials and bearer tokens are not encrypted by the local listener itself. Use the local server only on a trusted network, or place a separate TLS-capable proxy in front of it.

The Android app has a secure network policy for its client connections. Cleartext traffic is blocked by default, with an exception for 127.0.0.1 so local loopback calls can use HTTP. The policy trusts system and user certificates plus the app certificate authority. The normal debug and release builds use this policy.

References: app/…/WebService.kt• L77-82 app/…/network_security_config.xml• L1-13 app/…/AndroidManifest.xml• L24-30 app/build.gradle• L35-56

Insecure build policy

The debugInsecure and insecure build types allow cleartext traffic for development or controlled compatibility use. This is a build choice, not a per-request setting. It changes the Android client policy for all destinations, so these builds can send credentials and message data over unencrypted links.

The normal build types use the secure policy. Keep insecure builds away from networks where another party can observe or change traffic.

References: app/build.gradle• L35-56 app/…/network_security_config_insecure.xml• L1-10 app/…/BuildHelper.kt• L1-8

Webhook transport

Webhook targets must use HTTPS. The only normal HTTP exception is a 127.0.0.1 target. Insecure builds also allow HTTP targets, which is useful for development but removes transport protection from webhook payloads.

This rule protects message and MMS event data after it leaves the gateway. It does not protect the local API listener, which remains an HTTP service, and it does not stop a valid local API client from reading data before a webhook is sent.

References: app/…/WebHooksService.kt• L79-105 app/…/WebHooksService.kt• L126-181 app/…/BuildHelper.kt• L5-8

File-sharing boundaries

Private MMS storage

Incoming MMS attachment bytes are stored under the app's private files area so they remain available after the phone's MMS provider no longer has the source entry. The app keeps attachment metadata with each file and removes old attachment directories with the inbox retention process.

Outgoing MMS content is also written to the app's private files area before Android telephony services send it. The FileProvider configuration declares both the mms-in/ and mms-out/ roots as shareable paths. The provider is not exported, so another app does not gain standing access. The app must grant temporary URI access to a recipient.

The inbound root supports URI sharing when the app grants it, but it is not a general Android file share. A local API client can download an inbound attachment only through the inbox route after it passes the inbox:read check. This API path is separate from the temporary Android URI-grant path.

References: app/…/MmsAttachmentStorage.kt• L10-26 app/…/MmsAttachmentStorage.kt• L46-66 app/…/MmsSender.kt• L38-50 app/…/IncomingMessagesService.kt• L142-146 app/…/AndroidManifest.xml• L98-106 app/…/file_paths.xml• L1-5

Attachment API access

The attachment route exposes one file by inbox message ID and part ID. It first checks the inbox-read permission, then confirms that the stored file exists. Attachment names are cleaned before they are placed in the download header, which limits header injection through message metadata.

The route is part of the local API security boundary. A client with permission to list inbox records does not automatically receive permission to read attachment bytes. It needs the separate inbox:read scope.

References: app/…/InboxRoutes.kt• L39-61 app/…/InboxRoutes.kt• L154-178 app/…/AuthScopes.kt• L11-14

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep each encrypted-message producer aligned with the algorithm marker and the provider format. For device-key data, preserve the encryption version, key version, wrapped AES key, IV, and ciphertext fields.
  • Keep the isEncrypted flag aligned across cloud requests, local API requests, stored messages, and the final send path. A mismatch can expose ciphertext to a recipient or cause plaintext to be treated as encrypted.
  • Keep the default encryption algorithm aligned with senders while retaining legacy-provider support for old queued data.
  • Keep the RSA-OAEP digest and MGF1 digest aligned in both device-key encryption and decryption. Android Keystore support requires the current OAEP parameter set.
  • Keep device-key versions aligned with public-key publication, private-key retention, and decryption. A new version must reach the server before old key material is removed.
  • Keep local Basic credentials and JWT settings coordinated with service startup. A running server keeps the values it read when it started.
  • Keep the secure and insecure build types mapped to the intended network policy. A new build type can silently permit cleartext traffic if it selects the wrong manifest placeholder.
  • Keep webhook URL validation aligned with the build policy and the event payloads sent to external targets.
  • Keep the inbox attachment scope check aligned with attachment storage and retention. Removing or widening the check changes the boundary around MMS files.
  • Keep the FileProvider roots aligned with the files that Android telephony services must receive. Adding other private directories widens the temporary URI-sharing boundary.

Important failure modes

TriggerConsequenceGuard
Encrypted message arrives without the configured passphrase or required device private keyThe message cannot be decrypted and is not sent.Set the matching passphrase or keep the referenced device key within its retention period.
Encrypted content uses an unsupported algorithm or encryption versionDecryption fails before the SMS service receives the content.Use a supported provider format and preserve its marker.
Device-key content uses different RSA-OAEP parameters on the sender and deviceAndroid Keystore rejects the operation or decryption fails.Keep RSA-OAEP on SHA-256 with SHA-1 MGF1 for both directions.
Device-key content names a retired key after its retention periodThe device cannot recover the AES key, so sending fails.Keep the embedded key version correct and allow in-flight work to finish before key cleanup.
Encrypted content omits or corrupts its key, IV, ciphertext, or passphrase-format fieldsKey recovery or decryption fails.Preserve the complete provider format when producing encrypted values.
Local server is used over an untrusted networkBasic credentials, JWTs, and API data can be observed or changed because the listener uses HTTP.Use a trusted network or add a TLS-capable proxy.
An insecure build sends a cloud or webhook request over HTTPCredentials or message event data can travel without transport encryption.Use a normal secure build and HTTPS endpoints.
The FileProvider roots do not include an MMS handoff fileAndroid telephony services cannot open the URI and MMS sending fails.Keep the configured roots aligned with the outbound MMS handoff.
A client has inbox:list but not inbox:readIt can list inbox records but cannot download MMS attachment bytes.Grant inbox:read only to clients that need file access.
An attachment is removed by retention before a client downloads itThe attachment reference remains possible in a record, but the download returns 404 Not Found.Download needed files before the configured retention period ends.
Cloud password prompt is canceled or left emptyPassword change or login-code retrieval does not proceed.Enter the current password and submit the prompt.

Key files

FileWhy to read it
app/…/EncryptionService.ktDefines the algorithm-marked encrypted value and the provider selection boundary.
app/…/EncryptionProviderFactory.ktMaps encryption markers to supported providers and selects the default format.
app/…/RSAEncryptionProvider.ktShows the device-key message contract, hybrid encryption, and Android-compatible OAEP parameters.
app/…/BasePassphraseEncryptionProvider.ktDefines the shared passphrase-based encryption contract and data protection flow.
app/…/HardenedPassphraseEncryptionProvider.ktDefines the current passphrase format for new encrypted values.
app/…/LegacyPassphraseEncryptionProvider.ktDefines compatibility behavior for older encrypted values.
app/…/MessagesService.ktShows where encrypted text, data, and recipient numbers become plaintext for sending.
app/…/PasswordPromptDialogFragment.ktShows the user confirmation boundary for cloud password actions.
app/…/WebService.ktDefines the local HTTP listener and its Basic and JWT authentication boundary.
app/…/LocalServerSettings.ktDefines local credentials, JWT secret settings, and service defaults.
app/…/network_security_config.xmlDefines the normal Android cleartext and certificate trust policy.
app/…/network_security_config_insecure.xmlDefines the intentionally permissive policy for insecure builds.
app/…/WebHooksService.ktEnforces HTTPS and loopback exceptions for outbound webhook delivery.
app/…/MmsAttachmentStorage.ktDefines the private MMS attachment boundary and retention cleanup.
app/…/MmsSender.ktShows the outbound MMS file handoff through temporary Android URI access.
app/…/InboxRoutes.ktDefines authenticated inbox and attachment access.
app/…/DeviceService.ktDefines device-key availability, version lookup, and retention behavior.
app/…/AndroidManifest.xmlConnects the app network policy and non-exported FileProvider to the Android security boundary.
app/…/file_paths.xmlShows the limited MMS roots available through Android file sharing.