Skip to content

Security: Crayonic/WearAuthn

Security

docs/SECURITY.md

WearAuthn Security Documentation

Security Overview

WearAuthn implements a comprehensive security model designed to protect user credentials and maintain the integrity of the FIDO2/WebAuthn authentication process. This document outlines the security architecture, threat model, and security controls implemented in the system.

Threat Model

Assets to Protect

  1. Private Cryptographic Keys

    • EC P-256 private keys for WebAuthn credentials
    • HMAC secret keys for extensions
    • Master signing keys for credential integrity
  2. User Credentials and Metadata

    • User identifiers and display names
    • Relying Party information
    • Credential creation timestamps
  3. Authentication Decisions

    • User presence verification
    • User verification status
    • Authentication assertions
  4. User Privacy Information

    • Credential usage patterns
    • Relying Party associations
    • Biometric templates (handled by system)

Threat Actors

  1. Malicious Applications

    • Other apps on the same device
    • Privilege escalation attempts
    • Data extraction via shared storage
  2. Network Attackers

    • Man-in-the-middle attacks
    • Replay attacks
    • Protocol downgrade attempts
  3. Physical Device Access

    • Unauthorized device access
    • Hardware tampering
    • Side-channel attacks
  4. Compromised Transport Channels

    • Bluetooth interception
    • NFC eavesdropping
    • Protocol manipulation

Attack Vectors

  1. Key Extraction

    • Memory dumps
    • Debug interfaces
    • Hardware attacks
  2. Credential Enumeration

    • Storage analysis
    • Timing attacks
    • Metadata leakage
  3. Authentication Bypass

    • Biometric spoofing
    • PIN/password attacks
    • Social engineering
  4. Protocol Attacks

    • Replay attacks
    • Downgrade attacks
    • Extension manipulation

Security Architecture

1. Hardware Security Foundation

Android Keystore Integration

// Secure key generation with hardware backing
val keyGenParameterSpec = KeyGenParameterSpec.Builder(
    keyAlias,
    KeyProperties.PURPOSE_SIGN
)
.setAlgorithmParameterSpec(ECGenParameterSpec("secp256r1"))
.setDigests(KeyProperties.DIGEST_SHA256)
.setUserAuthenticationRequired(true)
.setUserAuthenticationValidityDurationSeconds(validityDuration)
.setAttestationChallenge(attestationChallenge)
.build()

Security Properties:

  • Hardware-backed key storage (when available)
  • Keys never leave secure hardware
  • User authentication enforcement
  • Hardware attestation support

Trusted Execution Environment (TEE)

TrustZone Integration:

  • Secure key operations in TEE
  • Isolated execution environment
  • Hardware-enforced security boundaries
  • Secure boot verification

Secure Element (SE) Support:

  • NFC-based secure element access
  • Hardware security module integration
  • Tamper-resistant key storage
  • Certified security evaluation

2. Authentication Security

Multi-Factor Authentication

// User verification implementation
suspend fun verifyUser(): Boolean {
    return when {
        biometricManager.canAuthenticate(BIOMETRIC_WEAK) == BIOMETRIC_SUCCESS -> {
            performBiometricAuthentication()
        }
        keyguardManager.isDeviceSecure -> {
            performDeviceCredentialAuthentication()
        }
        else -> false
    }
}

Authentication Methods:

  1. Biometric Authentication

    • Fingerprint recognition
    • Face recognition
    • Voice recognition (where supported)
  2. Device Credentials

    • PIN verification
    • Password authentication
    • Pattern unlock
  3. Hardware Tokens

    • Secure element authentication
    • Hardware security keys
    • Smart card integration

User Presence Verification

Implementation:

  • Physical user interaction required
  • Timeout-based presence checking
  • Anti-automation measures
  • Gesture-based confirmation

3. Cryptographic Security

Key Management

Key Generation:

fun generateWebAuthnCredential(
    createResidentKey: Boolean,
    createHmacSecret: Boolean,
    attestationChallenge: ByteArray?
): String? {
    val keyAlias = SecureRandom.getInstanceStrong().nextBytes(32).base64()
    
    // Generate EC P-256 key pair
    val keyPairGenerator = KeyPairGenerator.getInstance(
        KeyProperties.KEY_ALGORITHM_EC,
        "AndroidKeyStore"
    )
    
    keyPairGenerator.initialize(keyGenParameterSpec)
    val keyPair = keyPairGenerator.generateKeyPair()
    
    return keyAlias
}

Key Properties:

  • Algorithm: ECDSA with P-256 curve
  • Key Size: 256 bits
  • Storage: Android Keystore (hardware-backed)
  • Usage: Digital signatures only
  • Lifecycle: Tied to user authentication

Signature Generation

WebAuthn Assertion:

suspend fun assertWebAuthn(
    clientDataHash: ByteArray,
    userVerified: Boolean
): ByteArray {
    val authenticatorData = buildAuthenticatorData(userVerified)
    val signatureData = authenticatorData + clientDataHash
    
    val signature = signWithKey(keyAlias, signatureData)
    return buildAssertionResponse(authenticatorData, signature)
}

Security Properties:

  • Non-repudiation through digital signatures
  • Integrity protection of authentication data
  • Replay protection via challenge-response
  • Cryptographic binding to relying party

Encryption and Data Protection

Credential Serialization:

fun serialize(userVerified: Boolean): String {
    val userMap = if (userVerified) {
        encryptUserInfo(userDisplayName, userName, userIcon)
    } else {
        null
    }
    
    val credentialData = CborMap(mapOf(
        "keyAlias" to CborTextString(keyAlias),
        "userId" to CborByteString(userId),
        "userMap" to userMap
    ))
    
    return credentialData.toCbor().base64()
}

Encryption Details:

  • Algorithm: AES-256-GCM
  • Key Derivation: PBKDF2 with hardware-backed key
  • Authentication: AEAD for integrity protection
  • IV Generation: Cryptographically secure random

4. Protocol Security

FIDO2/WebAuthn Compliance

CTAP 2.1 Implementation:

  • Full protocol compliance
  • Extension support validation
  • Error handling standardization
  • Backward compatibility

Security Features:

  • Challenge-response authentication
  • Origin validation
  • Credential isolation per RP
  • User verification enforcement

Transport Security

Bluetooth HID Security:

class SecureHidTransport : HidDeviceApp() {
    override fun sendData(data: ByteArray) {
        // Validate data integrity
        require(data.size <= MAX_HID_PACKET_SIZE)
        
        // Send via encrypted Bluetooth channel
        bluetoothDevice.sendEncryptedData(data)
    }
}

NFC Security:

  • ISO 14443-4 secure communication
  • Secure channel establishment
  • Anti-collision protocols
  • Proximity verification

5. Data Protection

Storage Security

Credential Storage:

private fun storeCredential(credential: WebAuthnCredential) {
    val encryptedData = encryptWithUserInfoKey(credential.serialize(true))
    val rpIdHash = credential.rpIdHash.base64()
    val userId = credential.userId.base64()
    
    getSecurePreferences(rpIdHash).edit {
        putString("cred_$userId", encryptedData)
    }
}

Storage Properties:

  • Encrypted SharedPreferences
  • Per-RP isolation
  • User-specific encryption keys
  • Secure key derivation

Privacy Protection

Data Minimization:

  • Only necessary data stored
  • Automatic data expiration
  • User-controlled data deletion
  • Minimal metadata collection

Anonymization:

  • RP ID hashing for storage keys
  • User ID encryption
  • No cross-RP correlation
  • Privacy-preserving design

6. Runtime Security

Application Security

Code Protection:

// Obfuscation and anti-tampering
@Keep
class SecurityManager {
    @JvmStatic
    external fun validateIntegrity(): Boolean
    
    companion object {
        init {
            System.loadLibrary("security")
        }
    }
}

Runtime Checks:

  • Application signature verification
  • Debug detection
  • Root detection
  • Emulator detection

Memory Protection

Secure Memory Handling:

class SecureByteArray(size: Int) : AutoCloseable {
    private val data = ByteArray(size)
    
    fun get(): ByteArray = data.clone()
    
    override fun close() {
        data.fill(0) // Clear sensitive data
    }
}

Memory Security:

  • Sensitive data clearing
  • Stack protection
  • Heap randomization
  • Memory encryption (where available)

Security Controls

1. Access Controls

Permission Model

<!-- Minimal permissions required -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.USE_BIOMETRIC" />

Component Protection

<!-- Exported components with proper protection -->
<activity
    android:name=".ui.MainActivity"
    android:exported="true"
    android:permission="android.permission.BIND_DEVICE_ADMIN" />

2. Input Validation

CTAP Request Validation

fun validateCtapRequest(request: ByteArray): Boolean {
    // Size validation
    if (request.size > MAX_CTAP_REQUEST_SIZE) return false
    
    // Format validation
    if (!isValidCborFormat(request)) return false
    
    // Command validation
    val command = request.firstOrNull()
    if (!VALID_COMMANDS.contains(command)) return false
    
    return true
}

Parameter Sanitization

fun sanitizeRpId(rpId: String): String {
    require(rpId.isNotBlank()) { "RP ID cannot be blank" }
    require(rpId.length <= MAX_RP_ID_LENGTH) { "RP ID too long" }
    require(rpId.matches(RP_ID_REGEX)) { "Invalid RP ID format" }
    
    return rpId.lowercase().trim()
}

3. Error Handling

Secure Error Responses

fun handleError(error: Exception): ByteArray {
    val ctapError = when (error) {
        is SecurityException -> CtapError.OperationDenied
        is IllegalArgumentException -> CtapError.InvalidParameter
        is TimeoutException -> CtapError.UserActionTimeout
        else -> CtapError.Other
    }
    
    // Log error without sensitive information
    Timber.w("CTAP error: ${ctapError.name}")
    
    return ctapError.toResponse()
}

Information Disclosure Prevention

  • Generic error messages
  • No stack traces in production
  • Minimal error information
  • Consistent error timing

4. Audit and Monitoring

Security Event Logging

object SecurityAudit {
    fun logAuthenticationAttempt(rpId: String, success: Boolean) {
        val event = SecurityEvent(
            type = "authentication",
            rpId = rpId.sha256().take(8), // Anonymized
            success = success,
            timestamp = System.currentTimeMillis()
        )
        
        secureLog(event)
    }
}

Anomaly Detection

  • Unusual authentication patterns
  • Rapid credential creation
  • Suspicious transport usage
  • Error rate monitoring

Security Testing

1. Penetration Testing

Test Scenarios

  • Key extraction attempts
  • Protocol manipulation
  • Transport interception
  • Authentication bypass

Tools and Techniques

  • Static code analysis
  • Dynamic analysis
  • Fuzzing testing
  • Side-channel analysis

2. Compliance Testing

FIDO Certification

  • FIDO2 conformance tests
  • Security evaluation criteria
  • Interoperability testing
  • Vulnerability assessment

Standards Compliance

  • Common Criteria evaluation
  • FIPS 140-2 validation
  • WebAuthn specification compliance
  • CTAP 2.1 certification

3. Continuous Security

Security Pipeline

# Security checks in CI/CD
security_scan:
  - static_analysis
  - dependency_check
  - secret_detection
  - vulnerability_scan

Regular Assessments

  • Quarterly security reviews
  • Annual penetration testing
  • Continuous monitoring
  • Incident response procedures

Security Best Practices

1. Development Guidelines

Secure Coding

  • Input validation everywhere
  • Fail-secure defaults
  • Principle of least privilege
  • Defense in depth

Code Review

  • Security-focused reviews
  • Cryptographic implementation review
  • Protocol compliance verification
  • Threat model validation

2. Deployment Security

Release Process

  • Code signing verification
  • Integrity checking
  • Secure distribution
  • Update mechanisms

Runtime Protection

  • Application hardening
  • Anti-tampering measures
  • Runtime application self-protection
  • Secure configuration

3. Incident Response

Security Incident Handling

  1. Detection: Automated monitoring and alerting
  2. Analysis: Threat assessment and impact evaluation
  3. Containment: Immediate threat mitigation
  4. Recovery: System restoration and validation
  5. Lessons Learned: Process improvement and prevention

Communication Plan

  • User notification procedures
  • Vendor coordination
  • Regulatory reporting
  • Public disclosure guidelines

This security documentation provides a comprehensive overview of the security measures implemented in WearAuthn. Regular updates and reviews ensure that the security posture remains strong against evolving threats.

There aren’t any published security advisories