openapi: 3.1.0
info:
  title: OHN-45 Cryptographic Trust Services
  version: 1.0.0
  description: Synthetic-ready trust service contracts. Production use requires accepted OHN-01/OHN-38/OHN-16 integrations and security authorization.
jsonSchemaDialect: https://json-schema.org/draft/2020-12/schema
servers:
  - url: /
security:
  - bearerAuth: []
tags:
  - name: Keys
  - name: Verification
  - name: Reports
paths:
  /api/ohn/v1/crypto/health:
    get:
      operationId: ohn45_health
      security: []
      responses:
        '200': { description: Service health, content: { application/json: { schema: { type: object } } } }
  /api/ohn/v1/crypto/auth/token:
    post:
      operationId: ohn45_issueSyntheticToken
      security: []
      description: Available only outside production when OHN45_SYNTHETIC_AUTH=true.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/SyntheticTokenCommand' }
      responses:
        '200': { description: Synthetic token, content: { application/json: { schema: { type: object, required: [access_token], properties: { access_token: { type: string } } } } } }
        '404': { $ref: '#/components/responses/NotFound' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/crypto/keys/register:
    post:
      operationId: ohn45_registerKey
      x-inventory-id: OHN-45-F001-SUBMIT
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { $ref: '#/components/requestBodies/KeyRegistration' }
      responses:
        '201': { description: Key registered in requested state, content: { application/json: { schema: { $ref: '#/components/schemas/KeyView' } } } }
        '400': { $ref: '#/components/responses/BadRequest' }
        '401': { $ref: '#/components/responses/Unauthorized' }
        '403': { $ref: '#/components/responses/Denied' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/crypto/certificates:
    get:
      operationId: ohn45_listCertificates
      x-inventory-id: OHN-45-API-CERT-LIST
      description: >-
        Read-only, jurisdiction-wide list of certificates/keys other modules may reference (OHN-30 insurer endpoints).
        Active keys only; label, purpose, owner organisation, expiry (certificate expiry, else activation + rotation interval).
        Never the KMS/HSM reference or the key owner. Roles key_custodian, security_officer, and certificate_reader
        (gateway service tokens only, token_use=service, purpose certificate_reference; it can read nothing else). Audited.
      parameters:
        - { name: purpose, in: query, schema: { type: string, pattern: '^[a-z_]{1,64}$' } }
        - { name: include_inactive, in: query, description: staff roles only, schema: { type: boolean } }
      responses:
        '200':
          description: Certificates
          content:
            application/json:
              schema:
                type: object
                required: [data, total, generated_at]
                properties:
                  data:
                    type: array
                    items:
                      type: object
                      required: [id, label, key_purpose, service_id, organization_id, status, expires_at, expired, rotation_due]
                      properties:
                        id: { type: string, format: uuid }
                        label: { type: string }
                        key_purpose: { type: string }
                        service_id: { type: string, format: uuid }
                        organization_id: { type: string, format: uuid }
                        status: { type: string }
                        expires_at: { type: [string, 'null'], format: date-time }
                        expired: { type: boolean }
                        rotation_due: { type: boolean }
                  total: { type: integer }
                  generated_at: { type: string, format: date-time }
        '401': { $ref: '#/components/responses/Unauthorized' }
        '403': { $ref: '#/components/responses/Denied' }
        '422': { description: Malformed purpose }
  /api/ohn/v1/crypto/key-status:
    get:
      operationId: ohn45_getKeyStatus
      x-inventory-id: OHN-45-API-003
      parameters:
        - { name: cursor, in: query, schema: { type: string, format: uuid } }
        - { name: page_size, in: query, schema: { type: integer, minimum: 1, maximum: 100, default: 25 } }
        - { name: service_id, in: query, schema: { type: string, format: uuid } }
      responses:
        '200': { description: Scoped key status and operational evidence, content: { application/json: { schema: { $ref: '#/components/schemas/KeyStatusResponse' } } } }
        '401': { $ref: '#/components/responses/Unauthorized' }
        '403': { $ref: '#/components/responses/Denied' }
        '429': { $ref: '#/components/responses/RateLimited' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/crypto/artifacts:
    post:
      operationId: ohn45_registerArtifact
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content: { application/json: { schema: { $ref: '#/components/schemas/ArtifactCommand' } } }
      responses:
        '201': { description: Artifact reference created, content: { application/json: { schema: { $ref: '#/components/schemas/ArtifactView' } } } }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/crypto/signatures:
    get:
      operationId: ohn45_listSignedItems
      summary: Signed items of the organisation with their last check (F003 chooses from it). Never the content, the signature value or the public key.
      responses:
        '200': { description: Signed items, content: { application/json: { schema: { $ref: '#/components/schemas/SignedItemList' } } } }
        '401': { $ref: '#/components/responses/Unauthorized' }
        '403': { $ref: '#/components/responses/Denied' }
    post:
      operationId: ohn45_registerSignature
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content: { application/json: { schema: { $ref: '#/components/schemas/SignatureCommand' } } }
      responses:
        '201': { description: Public signature evidence registered, content: { application/json: { schema: { $ref: '#/components/schemas/SignatureView' } } } }
        '404': { $ref: '#/components/responses/NotFound' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/crypto/signatures/verify:
    post:
      operationId: ohn45_verifySignature
      x-inventory-id: OHN-45-API-001
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content: { application/json: { schema: { $ref: '#/components/schemas/SignatureVerificationCommand' } } }
      responses:
        '201': { description: Real verification result; no artifact or public key content returned, content: { application/json: { schema: { $ref: '#/components/schemas/VerificationView' } } } }
        '404': { $ref: '#/components/responses/NotFound' }
        '422': { $ref: '#/components/responses/Validation' }
        '409': { $ref: '#/components/responses/Conflict' }
  /api/ohn/v1/crypto/keys/rotate:
    post:
      operationId: ohn45_rotateKey
      x-inventory-id: OHN-45-API-002
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content: { application/json: { schema: { $ref: '#/components/schemas/KeyRotationCommand' } } }
      responses:
        '201': { description: Dual-approved rollover created, content: { application/json: { schema: { $ref: '#/components/schemas/WorkflowView' } } } }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/crypto/keys/revoke:
    post:
      operationId: ohn45_revokeKey
      x-inventory-id: OHN-45-F004-SUBMIT
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content: { application/json: { schema: { $ref: '#/components/schemas/KeyRevocationCommand' } } }
      responses:
        '201': { description: Key revoked and incident workflow started, content: { application/json: { schema: { $ref: '#/components/schemas/KeyView' } } } }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/crypto/workflows/{id}/transitions:
    post:
      operationId: ohn45_transitionWorkflow
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
        - { $ref: '#/components/parameters/IdempotencyKey' }
      requestBody:
        required: true
        content: { application/json: { schema: { $ref: '#/components/schemas/WorkflowTransitionCommand' } } }
      responses:
        '200': { description: Transition committed, content: { application/json: { schema: { $ref: '#/components/schemas/WorkflowView' } } } }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/crypto/rotations:
    get:
      operationId: ohn45_listRotations
      summary: Planned key rotations with who asked, both named approvers, their decisions and whether the caller must decide (P004).
      responses:
        '200': { description: Rotations, content: { application/json: { schema: { $ref: '#/components/schemas/RotationList' } } } }
        '401': { $ref: '#/components/responses/Unauthorized' }
        '403': { $ref: '#/components/responses/Denied' }
  /api/ohn/v1/crypto/rotations/{id}/approvals:
    post:
      operationId: ohn45_decideRotation
      summary: A named approver records their own decision once (approved or rejected, reason 10-4096). The requester and anyone not named are refused (403). One refusal closes the rotation as rejected and the old key is active again. The rollover cannot leave rotation_due before both approved (transition answers 422 approvals_pending).
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, format: uuid } }
        - { $ref: '#/components/parameters/IdempotencyKey' }
      requestBody:
        required: true
        content: { application/json: { schema: { $ref: '#/components/schemas/RotationApprovalCommand' } } }
      responses:
        '201': { description: Decision recorded, content: { application/json: { schema: { type: object, required: [rotation, decision, state, version, receipt], properties: { rotation: { $ref: '#/components/schemas/RotationDetail' }, decision: { enum: [approved, rejected] }, state: { type: string }, version: { type: integer }, receipt: { type: object } } } } } }
        '403': { $ref: '#/components/responses/Denied' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/crypto/reports/{id}:
    get:
      operationId: ohn45_getReport
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, enum: [OHN-45-R001, OHN-45-R002, OHN-45-R003] } }
        - { name: service_id, in: query, schema: { type: string, format: uuid } }
        - { name: period_from, in: query, schema: { type: string, format: date-time } }
        - { name: period_to, in: query, schema: { type: string, format: date-time } }
      responses:
        '200': { description: Authorized report, content: { application/json: { schema: { $ref: '#/components/schemas/Report' } } } }
        '404': { $ref: '#/components/responses/NotFound' }
  /api/ohn/v1/crypto/reports/{id}/export:
    post:
      operationId: ohn45_createExport
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, enum: [OHN-45-R001, OHN-45-R002, OHN-45-R003] } }
        - { $ref: '#/components/parameters/IdempotencyKey' }
      requestBody:
        required: true
        content: { application/json: { schema: { $ref: '#/components/schemas/ExportCommand' } } }
      responses:
        '201': { description: Signed one-time five-minute retrieval created, content: { application/json: { schema: { $ref: '#/components/schemas/ExportReceipt' } } } }
        '403': { $ref: '#/components/responses/Denied' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/crypto/exports/{token}:
    get:
      operationId: ohn45_downloadExport
      parameters:
        - { name: token, in: path, required: true, schema: { type: string, minLength: 32 } }
      responses:
        '200':
          description: Signed private export; single successful download only
          headers:
            X-OHN-Export-Signature: { required: true, schema: { type: string } }
            X-OHN-Receipt-Id: { required: true, schema: { type: string, format: uuid } }
          content:
            text/csv: { schema: { type: string } }
            application/pdf: { schema: { type: string, contentEncoding: base64 } }
        '404': { $ref: '#/components/responses/NotFound' }
components:
  securitySchemes:
    bearerAuth: { type: http, scheme: bearer, bearerFormat: JWT }
  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: true
      schema: { type: string, minLength: 1, maxLength: 255 }
  requestBodies:
    KeyRegistration:
      required: true
      content: { application/json: { schema: { $ref: '#/components/schemas/KeyRegistrationCommand' } } }
  responses:
    BadRequest: { description: Malformed request, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Unauthorized: { description: Unauthenticated, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Denied: { description: Default-deny policy refusal, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    NotFound: { description: Non-disclosing not-found response, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Conflict: { description: Stale version or idempotency conflict, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Validation: { description: Field or domain validation failed, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    RateLimited: { description: Retry after the stated window, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Unavailable: { description: Dependency unavailable; no fabricated success, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
  schemas:
    SignedItemList:
      type: object
      required: [data, total]
      properties:
        total: { type: integer }
        data:
          type: array
          items:
            type: object
            required: [id, artifact_ref, artifact_type, trusted_signer_ref, algorithm, signed_at, checks, last_check]
            properties:
              id: { type: string, format: uuid }
              artifact_ref: { type: string, format: uuid }
              artifact_type: { enum: [signed_export, clinical_document, certificate] }
              artifact_created_at: { type: string, format: date-time }
              trusted_signer_ref: { type: string, format: uuid }
              algorithm: { const: Ed25519 }
              signed_at: { type: string, format: date-time }
              checks: { type: integer }
              last_check: { oneOf: [{ type: 'null' }, { type: object, properties: { result: { enum: [valid, invalid] }, failure_cause: { type: [string, 'null'] }, purpose: { type: string }, verified_at: { type: string, format: date-time } } }] }
    RotationApprovalCommand:
      type: object
      additionalProperties: false
      required: [decision, reason]
      properties:
        decision: { enum: [approved, rejected] }
        reason: { type: string, minLength: 10, maxLength: 4096 }
    RotationDetail:
      type: object
      required: [id, key_ref, new_key_ref, planned_cutover_at, state, version, approvals, approval_status, awaiting_my_decision]
      properties:
        id: { type: string, format: uuid }
        key_ref: { type: string, format: uuid }
        new_key_ref: { type: string, format: uuid }
        planned_cutover_at: { type: string, format: date-time }
        rollback_plan: { type: string }
        state: { enum: [rotation_due, new_key_generated, consumers_migrated, old_deactivated, verified, rejected] }
        version: { type: integer }
        created_at: { type: string, format: date-time }
        requested_by: { type: [string, 'null'], format: uuid }
        approval_status: { enum: [pending, approved, rejected] }
        awaiting_my_decision: { type: boolean }
        approvals: { type: array, minItems: 2, maxItems: 2, items: { type: object, properties: { approver_id: { type: string, format: uuid }, decision: { enum: [approved, rejected, null] }, reason: { type: [string, 'null'] }, decided_at: { type: [string, 'null'], format: date-time } } } }
    RotationList:
      type: object
      required: [data, total]
      properties:
        total: { type: integer }
        data: { type: array, items: { $ref: '#/components/schemas/RotationDetail' } }
    Uuid: { type: string, format: uuid }
    Error:
      type: object
      required: [error]
      properties: { error: { type: string }, message: { type: string }, details: { type: object } }
      additionalProperties: false
    Receipt:
      type: object
      required: [receipt_id, committed_at, aggregate_id, aggregate_type, version, status]
      properties:
        receipt_id: { $ref: '#/components/schemas/Uuid' }
        committed_at: { type: string, format: date-time }
        aggregate_id: { $ref: '#/components/schemas/Uuid' }
        aggregate_type: { type: string }
        version: { type: integer, minimum: 1 }
        status: { type: string, enum: [committed, pending] }
      additionalProperties: false
    KeyRegistrationCommand:
      type: object
      additionalProperties: false
      required: [jurisdiction_id, service_id, key_purpose, hsm_or_kms_ref, rotation_interval_days, key_owner]
      properties:
        jurisdiction_id: { $ref: '#/components/schemas/Uuid' }
        service_id: { $ref: '#/components/schemas/Uuid' }
        key_purpose: { type: string, enum: [encryption, signing, key_wrapping, certificate_renewal, authentication] }
        hsm_or_kms_ref: { $ref: '#/components/schemas/Uuid' }
        rotation_interval_days: { type: integer, minimum: 1, maximum: 3650 }
        key_owner: { $ref: '#/components/schemas/Uuid' }
    KeyRotationCommand:
      type: object
      additionalProperties: false
      required: [key_ref, new_key_ref, planned_cutover_at, rollback_plan, approver_a, approver_b]
      properties:
        key_ref: { $ref: '#/components/schemas/Uuid' }
        new_key_ref: { $ref: '#/components/schemas/Uuid' }
        planned_cutover_at: { type: string, format: date-time }
        rollback_plan: { type: string, minLength: 10, maxLength: 8192 }
        approver_a: { $ref: '#/components/schemas/Uuid' }
        approver_b: { $ref: '#/components/schemas/Uuid' }
      description: key_ref differs from new_key_ref; approvers are distinct and exclude the requesting operator.
    SignatureVerificationCommand:
      type: object
      additionalProperties: false
      required: [artifact_ref, signature_ref, trusted_signer_ref, verification_purpose]
      properties:
        artifact_ref: { $ref: '#/components/schemas/Uuid' }
        signature_ref: { $ref: '#/components/schemas/Uuid' }
        trusted_signer_ref: { $ref: '#/components/schemas/Uuid' }
        verification_purpose: { type: string, enum: [document_integrity, export_integrity, certificate_validation] }
    KeyRevocationCommand:
      type: object
      additionalProperties: false
      required: [key_ref, compromise_reason, revocation_priority, security_owner]
      properties:
        key_ref: { $ref: '#/components/schemas/Uuid' }
        compromise_reason: { type: string, minLength: 10, maxLength: 4096 }
        revocation_priority: { type: string, enum: [emergency, high, routine] }
        security_owner: { $ref: '#/components/schemas/Uuid' }
    ArtifactCommand:
      type: object
      additionalProperties: false
      required: [artifact_type, content_base64]
      properties:
        artifact_type: { type: string, enum: [signed_export, clinical_document, certificate] }
        content_base64: { type: string, contentEncoding: base64, maxLength: 2000000 }
    SignatureCommand:
      type: object
      additionalProperties: false
      required: [artifact_ref, trusted_signer_ref, algorithm, public_key_base64, signature_base64]
      properties:
        artifact_ref: { $ref: '#/components/schemas/Uuid' }
        trusted_signer_ref: { $ref: '#/components/schemas/Uuid' }
        algorithm: { const: Ed25519 }
        public_key_base64: { type: string, contentEncoding: base64 }
        signature_base64: { type: string, contentEncoding: base64 }
    WorkflowTransitionCommand:
      type: object
      additionalProperties: false
      required: [expected_version, to_state, evidence_ref, reason]
      properties:
        expected_version: { type: integer, minimum: 1 }
        to_state: { type: string }
        evidence_ref: { $ref: '#/components/schemas/Uuid' }
        reason: { type: string, minLength: 10, maxLength: 4096 }
    ExportCommand:
      type: object
      additionalProperties: false
      required: [format]
      properties:
        format: { type: string, enum: [csv, pdf] }
        service_id: { $ref: '#/components/schemas/Uuid' }
        period_from: { type: string, format: date-time }
        period_to: { type: string, format: date-time }
    SyntheticTokenCommand:
      type: object
      additionalProperties: false
      required: [principal_id, role, jurisdiction, organization_id, purpose]
      properties:
        principal_id: { $ref: '#/components/schemas/Uuid' }
        role: { type: string, enum: [key_custodian, security_officer, unauthorized_viewer] }
        jurisdiction: { $ref: '#/components/schemas/Uuid' }
        organization_id: { $ref: '#/components/schemas/Uuid' }
        purpose: { type: string, enum: [key_management, signature_verification, security_incident, compliance_reporting] }
    KeyView:
      type: object
      required: [id, key_ref_masked, service_id, key_purpose, status, version]
      properties:
        id: { $ref: '#/components/schemas/Uuid' }
        key_ref_masked: { type: string }
        service_id: { $ref: '#/components/schemas/Uuid' }
        key_purpose: { type: string }
        status: { type: string, enum: [requested, policy_checked, kms_created, attested, active, rotating, retired, revoked] }
        version: { type: integer, minimum: 1 }
        workflow_id: { $ref: '#/components/schemas/Uuid' }
        receipt: { $ref: '#/components/schemas/Receipt' }
    KeyStatusResponse:
      type: object
      required: [data, next_cursor, page_size, summary, rotations, verifications, leases, workflows, provenance]
      properties:
        data: { type: array, items: { $ref: '#/components/schemas/KeyView' } }
        next_cursor: { type: [string, 'null'], format: uuid }
        page_size: { type: integer, maximum: 100 }
        summary: { type: object }
        rotations: { type: array, items: { type: object } }
        verifications: { type: array, items: { type: object } }
        leases: { type: array, items: { type: object } }
        workflows: { type: array, items: { type: object } }
        provenance: { type: object }
    ArtifactView:
      type: object
      properties: { id: { $ref: '#/components/schemas/Uuid' }, artifact_type: { type: string }, created_at: { type: string, format: date-time } }
    SignatureView:
      type: object
      properties: { id: { $ref: '#/components/schemas/Uuid' }, artifact_ref: { $ref: '#/components/schemas/Uuid' }, trusted_signer_ref: { $ref: '#/components/schemas/Uuid' }, algorithm: { const: Ed25519 } }
    VerificationView:
      type: object
      required: [id, verification_result, algorithm, verified_at, receipt]
      properties:
        id: { $ref: '#/components/schemas/Uuid' }
        verification_result: { type: string, enum: [valid, invalid] }
        failure_cause: { type: [string, 'null'], enum: [untrusted_signer, invalid_signature, unsupported_algorithm, null] }
        algorithm: { const: Ed25519 }
        verified_at: { type: string, format: date-time }
        receipt: { $ref: '#/components/schemas/Receipt' }
    WorkflowView:
      type: object
      properties: { workflow_id: { $ref: '#/components/schemas/Uuid' }, state: { type: string }, version: { type: integer }, receipt: { $ref: '#/components/schemas/Receipt' } }
    Report:
      type: object
      required: [report_id, data, metrics, provenance]
      properties:
        report_id: { type: string, enum: [OHN-45-R001, OHN-45-R002, OHN-45-R003] }
        data: { type: array, items: { type: object } }
        metrics: { type: object }
        provenance: { type: object }
    ExportReceipt:
      type: object
      required: [receipt, retrieval, signature]
      properties:
        receipt: { $ref: '#/components/schemas/Receipt' }
        retrieval:
          type: object
          required: [path, expires_at]
          properties: { path: { type: string }, expires_at: { type: string, format: date-time } }
        signature:
          type: object
          required: [algorithm, value, key_ref]
          properties: { algorithm: { const: HMAC-SHA256 }, value: { type: string }, key_ref: { type: string } }
