public void String2Key_128_0() { var key = new byte[128]; var result = PKInitString2Key.String2Key(key, 32); var expected = new byte[] { 0xac, 0xf7, 0x70, 0x7c, 0x08, 0x97, 0x3d, 0xdf, 0xdb, 0x27, 0xcd, 0x36, 0x14, 0x42, 0xcc, 0xfb, 0xa3, 0x55, 0xc8, 0x88, 0x4c, 0xb4, 0x72, 0xf3, 0x7d, 0xa6, 0x36, 0xd0, 0x7d, 0x56, 0x78, 0x7e }; Assert.IsTrue(result.Span.SequenceEqual(expected)); }
public void String2Key_256_0() { var key = new byte[256]; var result = PKInitString2Key.String2Key(key, 32); var expected = new byte[] { 0x5e, 0xe5, 0x0d, 0x67, 0x5c, 0x80, 0x9f, 0xe5, 0x9e, 0x4a, 0x77, 0x62, 0xc5, 0x4b, 0x65, 0x83, 0x75, 0x47, 0xea, 0xfb, 0x15, 0x9b, 0xd8, 0xcd, 0xc7, 0x5f, 0xfc, 0xa5, 0x91, 0x1e, 0x4c, 0x41 }; Assert.IsTrue(result.Span.SequenceEqual(expected)); }
public void String2Key_128_IncrementingSequence_Len_77() { var key = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; var result = PKInitString2Key.String2Key(key, 32); var expected = new byte[] { 0x00, 0x53, 0x95, 0x3b, 0x84, 0xc8, 0x96, 0xf4, 0xeb, 0x38, 0x5c, 0x3f, 0x2e, 0x75, 0x1c, 0x4a, 0x59, 0x0e, 0xd6, 0xff, 0xad, 0xca, 0x6f, 0xf6, 0x4f, 0x47, 0xeb, 0xeb, 0x8d, 0x78, 0x0f, 0xfc }; Assert.IsTrue(result.Span.SequenceEqual(expected)); }
private ReadOnlyMemory <byte> DeriveDHKeyAgreement(KrbKdcRep kdcRep, KrbPaPkAsRep pkRep) { var dhKeyInfo = ValidateDHReply(pkRep); var kdcPublicKey = DiffieHellmanKey.ParsePublicKey(dhKeyInfo.SubjectPublicKey, agreement.PublicKey.KeyLength); agreement.ImportPartnerKey(kdcPublicKey); var derivedKey = agreement.GenerateAgreement(); ReadOnlySpan <byte> serverDHNonce = default; if (pkRep.DHInfo.ServerDHNonce.HasValue) { serverDHNonce = pkRep.DHInfo.ServerDHNonce.Value.Span; } var transform = CryptoService.CreateTransform(kdcRep.EncPart.EType); return(PKInitString2Key.String2Key(derivedKey.Span, transform.KeySize, clientDHNonce.Span, serverDHNonce)); }
public override async Task <KrbPaData> Validate(KrbKdcReq asReq, PreAuthenticationContext preauth) { var paPk = asReq.PaData.FirstOrDefault(p => p.Type == PaDataType.PA_PK_AS_REQ); if (paPk == null) { return(null); } var pkreq = KrbPaPkAsReq.Decode(paPk.Value); var authPack = await ValidateAuthPack(preauth.Principal, pkreq); ValidateAuthenticator(authPack.PKAuthenticator, asReq.Body); var requestAlg = authPack.ClientPublicValue?.Algorithm?.Algorithm; IKeyAgreement agreement; if (requestAlg?.Value == EllipticCurveDiffieHellman.Value) { agreement = FromEllipticCurveDomainParameters(authPack.ClientPublicValue); } else if (requestAlg?.Value == DiffieHellman.Value) { agreement = await FromDiffieHellmanDomainParametersAsync(authPack.ClientPublicValue); } else { throw OnlyKeyAgreementSupportedException(); } var derivedKey = agreement.GenerateAgreement(); var etype = asReq.Body.EType.First(); var transform = CryptoService.CreateTransform(etype); ReadOnlyMemory <byte> clientDHNonce = authPack.ClientDHNonce.GetValueOrDefault(); ReadOnlyMemory <byte> serverDHNonce = default; if (clientDHNonce.Length > 0) { serverDHNonce = transform.GenerateRandomBytes(agreement.PublicKey.KeyLength); await Service.Principals.CacheKey(agreement.PrivateKey); } var keyInfo = new KrbKdcDHKeyInfo { SubjectPublicKey = agreement.PublicKey.EncodePublicKey() }; if (agreement.PublicKey.CacheExpiry.HasValue) { keyInfo.DHKeyExpiration = agreement.PublicKey.CacheExpiry; keyInfo.Nonce = authPack.PKAuthenticator.Nonce; } var sessionKey = PKInitString2Key.String2Key( derivedKey.Span, transform.KeySize, clientDHNonce.Span, serverDHNonce.Span ); var paPkRep = new KrbPaPkAsRep { DHInfo = new KrbDHReplyInfo { DHSignedData = await SignDHResponseAsync(keyInfo), ServerDHNonce = serverDHNonce } }; preauth.PaData = new[] { new KrbPaData { Type = PaDataType.PA_PK_AS_REP, Value = paPkRep.Encode() } }; preauth.EncryptedPartKey = new KerberosKey(key: sessionKey.ToArray(), etype: etype); return(null); }
public override KrbPaData Validate(KrbKdcReq asReq, PreAuthenticationContext preauth) { if (asReq == null) { throw new ArgumentNullException(nameof(asReq)); } if (preauth == null) { throw new ArgumentNullException(nameof(preauth)); } if (!preauth.PreAuthenticationState.TryGetValue(PaDataType.PA_PK_AS_REQ, out PaDataState paState) || !(paState is PkInitState state)) { return(null); } var authPack = ValidateAuthPack(preauth, state); this.ValidateAuthenticator(authPack.PKAuthenticator, asReq.Body); var requestAlg = authPack.ClientPublicValue?.Algorithm?.Algorithm; IKeyAgreement agreement; if (requestAlg?.Value == EllipticCurveDiffieHellman.Value) { agreement = this.FromEllipticCurveDomainParameters(authPack.ClientPublicValue); } else if (requestAlg?.Value == DiffieHellman.Value) { agreement = this.FromDiffieHellmanDomainParameters(authPack.ClientPublicValue); } else { throw OnlyKeyAgreementSupportedException(); } var derivedKey = agreement.GenerateAgreement(); var preferredEType = GetPreferredEType( asReq.Body.EType, this.Service.Configuration.Defaults.PermittedEncryptionTypes, this.Service.Configuration.Defaults.AllowWeakCrypto ); if (preferredEType is null) { throw new InvalidOperationException("Cannot find a common EType"); } var etype = preferredEType.Value; var transform = CryptoService.CreateTransform(etype); ReadOnlyMemory <byte> clientDHNonce = authPack.ClientDHNonce.GetValueOrDefault(); ReadOnlyMemory <byte> serverDHNonce = default; if (clientDHNonce.Length > 0) { serverDHNonce = transform.GenerateRandomBytes(agreement.PublicKey.KeyLength); this.Service.Principals.CacheKey(agreement.PrivateKey); } var keyInfo = new KrbKdcDHKeyInfo { SubjectPublicKey = agreement.PublicKey.EncodePublicKey() }; if (agreement.PublicKey.CacheExpiry.HasValue) { keyInfo.DHKeyExpiration = agreement.PublicKey.CacheExpiry; keyInfo.Nonce = authPack.PKAuthenticator.Nonce; } var sessionKey = PKInitString2Key.String2Key( derivedKey.Span, transform.KeySize, clientDHNonce.Span, serverDHNonce.Span ); var paPkRep = new KrbPaPkAsRep { DHInfo = new KrbDHReplyInfo { DHSignedData = this.SignDHResponse(keyInfo), ServerDHNonce = serverDHNonce } }; preauth.PaData = new[] { new KrbPaData { Type = PaDataType.PA_PK_AS_REP, Value = paPkRep.Encode() } }; preauth.EncryptedPartKey = new KerberosKey(key: sessionKey.ToArray(), etype: etype); preauth.ClientAuthority = PaDataType.PA_PK_AS_REQ; return(null); }