Example #1
0
        public virtual void TestEncodeRegisterResponse()
        {
            var registerResponse = new KeyRegisterResponse(USER_PUBLIC_KEY_ENROLL_HEX, KEY_HANDLE, VENDOR_CERTIFICATE,
                                                           SIGNATURE_ENROLL);
            var encodedBytes = RawMessageCodec.EncodeKeyRegisterResponse(registerResponse);

            CollectionAssert.AreEqual(REGISTRATION_RESPONSE_DATA, encodedBytes);
        }
Example #2
0
        public virtual void TestDecodeRegisterResponse()
        {
            KeyRegisterResponse keyRegisterResponse = RawMessageCodec
                                                      .DecodeKeyRegisterResponse(REGISTRATION_RESPONSE_DATA.Segment());

            Assert.AreEqual(new KeyRegisterResponse(
                                USER_PUBLIC_KEY_ENROLL_HEX, KEY_HANDLE, VENDOR_CERTIFICATE, SIGNATURE_ENROLL), keyRegisterResponse
                            );
        }
Example #3
0
        public Task <KeyResponse <KeyRegisterResponse> > RegisterAsync(KeyRegisterRequest request, CancellationToken cancellationToken = new CancellationToken(),
                                                                       bool invididualAttestation = false)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            log.Info(">> register");

            var applicationSha256 = request.ApplicationSha256;
            var challengeSha256   = request.ChallengeSha256;

            log.Info(" -- Inputs --");
            log.Info("  applicationSha256: " + applicationSha256.ToHexString());
            log.Info("  challengeSha256: " + challengeSha256.ToHexString());

            var userPresent = userPresenceVerifier.VerifyUserPresence();

            if ((userPresent & UserPresenceVerifierConstants.UserPresentFlag) == 0)
            {
                return(TestOfUserPresenceRequired <KeyRegisterResponse>());
            }

            var keyPair   = keyPairGenerator.GenerateKeyPair(applicationSha256, challengeSha256);
            var keyHandle = keyHandleGenerator.GenerateKeyHandle(applicationSha256, keyPair);

            dataStore.StoreKeyPair(keyHandle, keyPair);

            var userPublicKey = keyPairGenerator.EncodePublicKey(keyPair.PublicKey);
            var signedData    = RawMessageCodec.EncodeKeyRegisterSignedBytes(applicationSha256, challengeSha256, keyHandle,
                                                                             userPublicKey);

            log.Info("Signing bytes " + signedData.ToHexString());
            var signature = crypto.Sign(signedData, certificatePrivateKey);

            log.Info(" -- Outputs --");
            log.Info("  userPublicKey: " + userPublicKey.ToHexString());
            log.Info("  keyHandle: " + keyHandle.ToHexString());
            log.Info("  vendorCertificate: " + vendorCertificate);
            log.Info("  signature: " + signature.ToHexString());
            log.Info("<< register");

            var response     = new KeyRegisterResponse(userPublicKey, keyHandle, vendorCertificate, signature);
            var responseData = RawMessageCodec.EncodeKeyRegisterResponse(response).Segment();
            var apdu         = new ApduResponse(ApduResponseStatus.NoError, responseData);
            var keyResponse  = new KeyResponse <KeyRegisterResponse>(apdu, response, KeyResponseStatus.Success);

            return(TaskEx.FromResult(keyResponse));
        }
Example #4
0
        /// <exception cref="U2FException"/>
        public static byte[] EncodeKeyRegisterResponse([NotNull] KeyRegisterResponse keyRegisterResponse)
        {
            if (keyRegisterResponse == null)
            {
                throw new ArgumentNullException(nameof(keyRegisterResponse));
            }

            var userPublicKey          = keyRegisterResponse.UserPublicKey;
            var keyHandle              = keyRegisterResponse.KeyHandle;
            var attestationCertificate = keyRegisterResponse.AttestationCertificate;
            var signature              = keyRegisterResponse.Signature;

            byte[] attestationCertificateBytes;
            try
            {
                attestationCertificateBytes = attestationCertificate.GetEncoded();
            }
            catch (CertificateEncodingException e)
            {
                throw new U2FException("Error when encoding attestation certificate.", e);
            }
            if (keyHandle.Length > 255)
            {
                throw new U2FException("keyHandle length cannot be longer than 255 bytes!"
                                       );
            }
            var result = new byte[1 + userPublicKey.Length + 1 + keyHandle.Length + attestationCertificateBytes
                                  .Length + signature.Length];

            using (var writer = new EndianWriter(new MemoryStream(result), Endianness.BigEndian))
            {
                writer.Write(RegistrationReservedByteValue);
                writer.Write(userPublicKey);
                writer.Write((byte)keyHandle.Length);
                writer.Write(keyHandle);
                writer.Write(attestationCertificateBytes);
                writer.Write(signature);
            }
            return(result);
        }
Example #5
0
 /// <exception cref="System.IO.IOException"/>
 /// <exception cref="U2FException"/>
 public static void SendRegisterResponse(Stream outputStream, KeyRegisterResponse keyRegisterResponse)
 {
     SendResponse(outputStream, RawMessageCodec.EncodeKeyRegisterResponse(keyRegisterResponse));
 }
 public static RegisterOperationResult Success(KeyRegisterRequest request, KeyRegisterResponse response)
 {
     return(new RegisterOperationResult(KeyResponseStatus.Success, request, response));
 }
 private RegisterOperationResult(KeyResponseStatus status, KeyRegisterRequest request, KeyRegisterResponse response)
 {
     Status   = status;
     Request  = request;
     Response = response;
 }