public unsafe void SetPeerKey(ReadableBuffer buffer) { if (buffer.Length != _keyExchangeSize) { Alerts.AlertException.ThrowAlert(Alerts.AlertLevel.Fatal, Alerts.AlertDescription.illegal_parameter, $"The peer key is not the length of the keyexchange size {buffer.Length} - {_keyExchangeSize}"); } GCHandle handle; void * ptr; if (buffer.IsSingleSpan) { ptr = buffer.First.GetPointer(out handle); } else { var tmpBuffer = stackalloc byte[buffer.Length]; var span = new Span <byte>(tmpBuffer, buffer.Length); buffer.CopyTo(span); ptr = tmpBuffer; } _peerKey = EVP_PKEY_new(); ThrowOnError(EVP_PKEY_set_type(_peerKey, _nid)); ThrowOnError(EVP_PKEY_set1_tls_encodedpoint(_peerKey, ptr, (UIntPtr)buffer.Length)); if (!_publicPrivateKey.IsValid()) { GenerateKeyset(); } _hasPeerKey = true; }
private void GenerateECKeySet() { if (_eKey.IsValid()) { return; } var param = CreateParams(); var keyGenCtx = default(EVP_PKEY_CTX); try { keyGenCtx = EVP_PKEY_CTX_new(param, IntPtr.Zero); ThrowOnError(EVP_PKEY_keygen_init(keyGenCtx)); EVP_PKEY keyPair; ThrowOnError(EVP_PKEY_keygen(keyGenCtx, out keyPair)); _eKey = keyPair; } finally { keyGenCtx.Free(); param.Free(); } }