dvDevClientCertificate function

DVDevClientCertificate dvDevClientCertificate({
  1. required Uint8List publicKey,
  2. required Uint8List privateKey,
  3. DateTime? now,
  4. Random? random,
})

A self-signed P-256 certificate for publicKey, signed with privateKey, valid from a day before now for thirty days.

Implementation

DVDevClientCertificate dvDevClientCertificate({
  required Uint8List publicKey,
  required Uint8List privateKey,
  DateTime? now,
  Random? random,
}) {
  final DateTime at = now ?? DateTime.now();
  final Random source = random ?? Random.secure();
  final List<int> serial = <int>[
    for (int i = 0; i < 16; i++) source.nextInt(256),
  ]..[0] &= 0x7f;
  serial[0] |= 0x01;
  final List<int> name = _sequence(<List<int>>[
    _tlv(
      0x31,
      _sequence(<List<int>>[
        _oid(_commonName),
        _tlv(0x0c, utf8.encode('dartvel dev')),
      ]),
    ),
  ]);
  final List<int> algorithm = _sequence(<List<int>>[_oid(_ecdsaWithSha256)]);
  final List<int> spki = _sequence(<List<int>>[
    _sequence(<List<int>>[_oid(_ecPublicKey), _oid(_prime256v1)]),
    _tlv(0x03, <int>[0, ...publicKey]),
  ]);
  final List<int> tbs = _sequence(<List<int>>[
    _tlv(0xa0, _tlv(0x02, <int>[2])),
    _tlv(0x02, serial),
    algorithm,
    name,
    _sequence(<List<int>>[
      _utcTime(at.subtract(const Duration(days: 1))),
      _utcTime(at.add(const Duration(days: 30))),
    ]),
    name,
    spki,
  ]);
  final Uint8List raw = dvWebPushSignEs256(tbs, privateKey);
  final List<int> signature = _sequence(<List<int>>[
    _unsigned(raw.sublist(0, 32)),
    _unsigned(raw.sublist(32, 64)),
  ]);
  final Uint8List der = Uint8List.fromList(
    _sequence(<List<int>>[
      tbs,
      algorithm,
      _tlv(0x03, <int>[0, ...signature]),
    ]),
  );
  final List<int> pkcs8 = _sequence(<List<int>>[
    _tlv(0x02, <int>[0]),
    _sequence(<List<int>>[_oid(_ecPublicKey), _oid(_prime256v1)]),
    _tlv(
      0x04,
      _sequence(<List<int>>[
        _tlv(0x02, <int>[1]),
        _tlv(0x04, privateKey),
        _tlv(0xa1, _tlv(0x03, <int>[0, ...publicKey])),
      ]),
    ),
  ]);
  return DVDevClientCertificate(
    der: der,
    certificatePem: _pem('CERTIFICATE', der),
    privateKeyPem: _pem('PRIVATE KEY', pkcs8),
  );
}