enrollmentApkamSymmetricKeyResolver function
Offers each apkamSymmetricKey an approver encapsulated to this
enrollment's key package, once, as it is found.
Pass the result to AtEnrollmentRequest.apkamSymmetricKeyResolver whenever
the request uses EnrollmentKeyExchangeMode.pq. It runs inside
waitForApproval, once PKAM authentication has succeeded — the earliest
moment this enrollment can read anything. An approval that was retried or
raced leaves more than one key, and the handshake keeps the one that
decrypts what the approval encrypted.
Built on AtLookUp rather than an AtClient because a client is
constructed from the keys this call is fetching the last piece of.
Two things authenticate the result, neither optional: the envelope is
pqSealed to this enrollment's key package, whose private half has never
left this device, and it carries an APKAM signature over the whole of
itself, checked against the signing enrollment's published _apsk. Without
the signature, anyone who read the public key package could seal a symmetric
key of their choosing to this enrollment.
Implementation
Stream<String> Function(AtKeys, AtLookUp) enrollmentApkamSymmetricKeyResolver(
String atSign, {
Duration timeout = const Duration(seconds: 30),
Duration pollInterval = const Duration(seconds: 2),
}) {
return (AtKeys keys, AtLookUp atLookUp) async* {
final (kpid, secretKey, keyAlgo) = await _keyPackageHalves(keys);
// NOTE: [timeout] rides out scans and lookups that fail transiently, and
// never waits for the human. The approver writes the envelope before it
// approves, so by the time PKAM succeeds the key is already there to find.
final DateTime deadline = DateTime.now().toUtc().add(timeout);
final offered = <String>{};
while (true) {
for (final envelopeKey in await _envelopeKeys(atLookUp, kpid)) {
if (offered.contains(envelopeKey)) continue;
final String? value = await _openIfSymmetricKey(
atLookUp, envelopeKey, atSign, kpid, secretKey, keyAlgo);
if (value != null) {
offered.add(envelopeKey);
_logger.info('Collected a conveyed apkamSymmetricKey from '
'$envelopeKey');
yield value;
}
}
if (DateTime.now().toUtc().isAfter(deadline)) {
throw StateError(offered.isEmpty
? 'No conveyed apkamSymmetricKey arrived for key package $kpid '
'within $timeout. The enrollment is approved but cannot '
'decrypt anything without it — the approver is running a '
'client that does not convey, or the envelope did not reach '
'this atServer.'
: 'None of the ${offered.length} apkamSymmetricKey(s) conveyed to '
'key package $kpid within $timeout decrypts what this '
'enrollment was approved with.');
}
await Future<void>.delayed(pollInterval);
}
};
}