openapi: 3.0.3
info:
  title: Secure Keystore API
  version: 1.0.0
  license:
    name: GNU Affero General Public License v3.0
    url: https://www.gnu.org/licenses/agpl-3.0.html
  description: |
    User-facing endpoints that the Wallet Integration Server forwards, plus the unit registration and revocation
    endpoints it calls itself. The description is hand-written from the API documents in this directory
    (API-General-Principles.md, API-PowerAuth-Units.md, API-EUDI.md); the API is a target design and is not implemented yet.

    The user endpoints take an end-to-end encrypted body. The plaintext, which is a signed request whose `request`
    claim carries the members listed in each operation, is visible only to the keystore and the wallet app.
    Backend key endpoints (`/v1/backend`) are not part of this description.
servers:
  - url: https://keystore.wultra.com
    description: Placeholder host, replace with your keystore URL
tags:
  - name: Units
    description: Unit registration and revocation (backend caller)
  - name: User endpoints
    description: Encrypted, user-authorized calls forwarded by the Wallet Integration Server
paths:
  /v1/pa/units:
    post:
      tags: [Units]
      operationId: registerUnit
      summary: Register a unit
      description: |
        Registers a PowerAuth activation as a unit; the `unitId` is the activation id. Called by the Wallet Integration
        Server from `CustomActivationProvider`.

        Called while the activation is `PENDING_COMMIT` or `ACTIVE`. By default it creates the user-signing keys, and the
        wrapped keys reach the phone in the encrypted activation response (register in `processCustomActivationAttributes`).
        With `includeSigningKeys` false no key is created or returned, the unit cannot be used for user signing, and the
        call may be made at any point, for example from `activationWasCommitted`.
      security:
        - backendBearer: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [unitId]
              additionalProperties: false
              properties:
                unitId:
                  $ref: '#/components/schemas/UnitId'
                includeSigningKeys:
                  type: boolean
                  default: true
                  description: '`false` registers the unit without user-signing keys; `keys` is then empty.'
      responses:
        '201':
          description: Unit registered
          headers:
            Cache-Control: {$ref: '#/components/headers/CacheControl'}
            X-Request-Id: {$ref: '#/components/headers/RequestId'}
          content:
            application/json:
              schema:
                type: object
                required: [keys]
                properties:
                  keys:
                    type: array
                    description: Empty when `includeSigningKeys` is `false`.
                    items:
                      type: object
                      required: [keyType, publicKey, wrappedKey]
                      properties:
                        keyType: {$ref: '#/components/schemas/UserSigningKeyType'}
                        publicKey:
                          type: string
                          description: |
                            PowerAuth serialization, standard Base64: uncompressed point `0x04||X||Y` for `ECDSA_P384`,
                            X.509 SubjectPublicKeyInfo for ML-DSA.
                        wrappedKey: {$ref: '#/components/schemas/UserSigningWrappedKey'}
        '400':
          description: '`invalid_request`'
          content:
            application/problem+json:
              schema: {$ref: '#/components/schemas/Problem'}
        '401': {$ref: '#/components/responses/BackendUnauthenticated'}
        '403': {$ref: '#/components/responses/BackendForbidden'}
        '409':
          description: '`unit_already_exists`, `unit_revoked`, or `activation_state_invalid` when the activation is neither `PENDING_COMMIT` nor `ACTIVE`'
          content:
            application/problem+json:
              schema: {$ref: '#/components/schemas/Problem'}
        '500': {$ref: '#/components/responses/InternalError'}
        '503': {$ref: '#/components/responses/Unavailable'}
  /v1/pa/units/{unitId}/revocation:
    post:
      tags: [Units]
      operationId: revokeUnit
      summary: Revoke a unit
      description: Revokes the unit permanently. A repeated revocation is `409 unit_revoked`, which the caller treats as revoked. The request body must be exactly `{}`.
      security:
        - backendBearer: []
      parameters:
        - $ref: '#/components/parameters/UnitId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              maxProperties: 0
              example: {}
      responses:
        '204':
          description: Unit revoked
          headers:
            Cache-Control: {$ref: '#/components/headers/CacheControl'}
            X-Request-Id: {$ref: '#/components/headers/RequestId'}
        '400': {$ref: '#/components/responses/InvalidRequest'}
        '401': {$ref: '#/components/responses/BackendUnauthenticated'}
        '403': {$ref: '#/components/responses/BackendForbidden'}
        '409': {$ref: '#/components/responses/UnitRevoked'}
        '500': {$ref: '#/components/responses/InternalError'}
  /v1/pa/units/{unitId}/signatures:
    post:
      tags: [User endpoints]
      operationId: signWithUserSigningKeys
      security: []
      summary: Sign with the user-signing keys
      description: |
        Plaintext `request` claim: `items` (1-2 entries, distinct key types), each with `wrappedKey`
        (UserSigningWrappedKey) and `signingInput` (base64url without padding: for `ECDSA_P384` the 48-byte SHA-384 digest of the data, for ML-DSA the data itself, 1 to 16,384 bytes).
        Plaintext response: `signatures`, a list of `{keyType, signature}`.
      parameters:
        - $ref: '#/components/parameters/UnitId'
        - $ref: '#/components/parameters/Encryption'
      requestBody: {$ref: '#/components/requestBodies/Encrypted'}
      responses:
        '200': {$ref: '#/components/responses/EncryptedOk'}
        '400': {$ref: '#/components/responses/InvalidRequest'}
        '403': {$ref: '#/components/responses/InvalidUserAuthorization'}
        '409':
          description: '`unit_revoked` or `wrapped_key_invalid`'
          content:
            application/problem+json:
              schema: {$ref: '#/components/schemas/Problem'}
        '500': {$ref: '#/components/responses/InternalError'}
        '503': {$ref: '#/components/responses/Unavailable'}
  /v1/eudi/units/{unitId}/wallet-instance-keys:
    post:
      tags: [User endpoints]
      operationId: createWalletInstanceKey
      security: []
      summary: Create the wallet instance key
      description: |
        Plaintext `request` claim: `{}`.
        Plaintext response: `publicJwk` (EC P-256 JWK) and `wrappedKey`.
      parameters:
        - $ref: '#/components/parameters/UnitId'
        - $ref: '#/components/parameters/Encryption'
      requestBody: {$ref: '#/components/requestBodies/Encrypted'}
      responses:
        '201': {$ref: '#/components/responses/EncryptedOk'}
        '400': {$ref: '#/components/responses/InvalidRequest'}
        '403': {$ref: '#/components/responses/InvalidUserAuthorization'}
        '409': {$ref: '#/components/responses/UnitRevoked'}
        '500': {$ref: '#/components/responses/InternalError'}
        '503': {$ref: '#/components/responses/Unavailable'}
  /v1/eudi/units/{unitId}/wallet-instance-attestations:
    post:
      tags: [User endpoints]
      operationId: createWalletInstanceAttestation
      security: []
      summary: Create the wallet instance attestation and signed PoP
      description: |
        Plaintext `request` claim: `publicJwk`, `wrappedKey`, `walletVersion` (`MAJOR.MINOR.PATCH`, 1-4 digits each)
        and `unsignedPop` (3-2048 characters).
        Plaintext response: `wia` and `signedPop`.
      parameters:
        - $ref: '#/components/parameters/UnitId'
        - $ref: '#/components/parameters/Encryption'
      requestBody: {$ref: '#/components/requestBodies/Encrypted'}
      responses:
        '200': {$ref: '#/components/responses/EncryptedOk'}
        '400': {$ref: '#/components/responses/InvalidRequest'}
        '403': {$ref: '#/components/responses/InvalidUserAuthorization'}
        '409':
          description: '`unit_revoked` or `wrapped_key_invalid`'
          content:
            application/problem+json:
              schema: {$ref: '#/components/schemas/Problem'}
        '500': {$ref: '#/components/responses/InternalError'}
        '503': {$ref: '#/components/responses/Unavailable'}
  /v1/eudi/units/{unitId}/credential-keys:
    post:
      tags: [User endpoints]
      operationId: createCredentialKeys
      security: []
      summary: Create credential keys and the key attestation
      description: |
        Plaintext `request` claim: `keyCount` (1-16), `cNonce` (1-512 characters), and optional `includeProofJwt`,
        `credentialIssuer` (https URL, required when `includeProofJwt` is true) and `clientId`.
        Plaintext response: `keys` (list of `publicJwk` and `wrappedKey`), `keyAttestation` and optional `proofJwt`.
      parameters:
        - $ref: '#/components/parameters/UnitId'
        - $ref: '#/components/parameters/Encryption'
      requestBody: {$ref: '#/components/requestBodies/Encrypted'}
      responses:
        '201': {$ref: '#/components/responses/EncryptedOk'}
        '400': {$ref: '#/components/responses/InvalidRequest'}
        '403': {$ref: '#/components/responses/InvalidUserAuthorization'}
        '409': {$ref: '#/components/responses/UnitRevoked'}
        '500': {$ref: '#/components/responses/InternalError'}
        '503': {$ref: '#/components/responses/Unavailable'}
  /v1/eudi/units/{unitId}/key-binding-signatures:
    post:
      tags: [User endpoints]
      operationId: signKeyBinding
      security: []
      summary: Sign key-binding JWT inputs
      description: |
        Plaintext `request` claim: `items` (1-16 entries), each with `wrappedKey` and `signingInput` (SHA-256 digest, 43 characters, base64url without padding).
        Plaintext response: `signatures`, a list of signatures (86 characters, base64url without padding).
      parameters:
        - $ref: '#/components/parameters/UnitId'
        - $ref: '#/components/parameters/Encryption'
      requestBody: {$ref: '#/components/requestBodies/Encrypted'}
      responses:
        '200': {$ref: '#/components/responses/EncryptedOk'}
        '400': {$ref: '#/components/responses/InvalidRequest'}
        '403': {$ref: '#/components/responses/InvalidUserAuthorization'}
        '409':
          description: '`unit_revoked` or `wrapped_key_invalid`'
          content:
            application/problem+json:
              schema: {$ref: '#/components/schemas/Problem'}
        '500': {$ref: '#/components/responses/InternalError'}
        '503': {$ref: '#/components/responses/Unavailable'}
components:
  securitySchemes:
    backendBearer:
      type: http
      scheme: bearer
      description: Backend caller token of the Wallet Integration Server (mutual TLS is planned later).
  headers:
    CacheControl:
      description: Always `no-store`.
      schema: {type: string, enum: [no-store]}
    RequestId:
      description: Request identifier for log correlation.
      schema: {type: string}
  parameters:
    UnitId:
      name: unitId
      in: path
      required: true
      schema: {$ref: '#/components/schemas/UnitId'}
    Encryption:
      name: X-PowerAuth-Encryption
      in: header
      required: true
      description: |
        PowerAuth encryption header produced by the SDK encryptor, for example
        `PowerAuth version="4.0", application_key="...", activation_id="<unitId>"`.
      schema: {type: string, maxLength: 1024}
  requestBodies:
    Encrypted:
      required: true
      content:
        application/json:
          schema: {$ref: '#/components/schemas/EncryptedRequest'}
  responses:
    EncryptedOk:
      description: Encrypted response; the plaintext is described in the operation.
      headers:
        Cache-Control: {$ref: '#/components/headers/CacheControl'}
        X-Request-Id: {$ref: '#/components/headers/RequestId'}
      content:
        application/json:
          schema: {$ref: '#/components/schemas/EncryptedResponse'}
    InvalidRequest:
      description: '`invalid_request`, or `unit_not_found`'
      content:
        application/problem+json:
          schema: {$ref: '#/components/schemas/Problem'}
    BackendUnauthenticated:
      description: '`backend_unauthenticated`'
      headers:
        WWW-Authenticate:
          schema: {type: string, example: Bearer}
      content:
        application/problem+json:
          schema: {$ref: '#/components/schemas/Problem'}
    BackendForbidden:
      description: '`backend_forbidden`'
      content:
        application/problem+json:
          schema: {$ref: '#/components/schemas/Problem'}
    InvalidUserAuthorization:
      description: '`invalid_user_authorization`'
      content:
        application/problem+json:
          schema: {$ref: '#/components/schemas/Problem'}
    UnitRevoked:
      description: '`unit_revoked`'
      content:
        application/problem+json:
          schema: {$ref: '#/components/schemas/Problem'}
    InternalError:
      description: '`internal_error`'
      content:
        application/problem+json:
          schema: {$ref: '#/components/schemas/Problem'}
    Unavailable:
      description: '`hsm_unavailable` or `authorization_unavailable`'
      headers:
        Retry-After:
          schema: {type: integer}
      content:
        application/problem+json:
          schema: {$ref: '#/components/schemas/Problem'}
  schemas:
    Problem:
      type: object
      required: [type, title, status, code]
      properties:
        type: {type: string}
        title: {type: string}
        status: {type: integer}
        detail: {type: string}
        code:
          type: string
          enum:
            - invalid_request
            - unit_not_found
            - backend_unauthenticated
            - invalid_user_authorization
            - backend_forbidden
            - unit_already_exists
            - unit_revoked
            - activation_state_invalid
            - wrapped_key_invalid
            - internal_error
            - hsm_unavailable
            - authorization_unavailable
    EncryptedRequest:
      type: object
      required: [temporaryKeyId, encryptedData, nonce, timestamp]
      additionalProperties: false
      properties:
        temporaryKeyId:
          type: string
          pattern: '^[0-9A-Za-z-]{1,64}$'
        encryptedData:
          type: string
          description: Standard Base64.
        nonce:
          type: string
          maxLength: 64
          description: Base64.
        timestamp:
          type: integer
          format: int64
          minimum: 1
    EncryptedResponse:
      type: object
      required: [encryptedData, timestamp]
      properties:
        encryptedData:
          type: string
          description: Standard Base64.
        timestamp:
          type: integer
          format: int64
    UnitId:
      type: string
      description: Canonical lowercase UUID, the PowerAuth activation id.
      pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
    UserSigningKeyType:
      type: string
      enum: [ECDSA_P384, MLDSA_65, MLDSA_87]
    UserSigningWrappedKey:
      type: string
      minLength: 1
      maxLength: 9605
