-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
111 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> | ||
// SPDX-License-Identifier: MIT | ||
|
||
package server | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"encoding/binary" | ||
"encoding/hex" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
const ( | ||
nonceLifetime = time.Hour // See: https://tools.ietf.org/html/rfc5766#section-4 | ||
nonceLength = 40 | ||
nonceKeyLength = 64 | ||
) | ||
|
||
// NewNonceHash creates a NonceHash | ||
func NewNonceHash() (*NonceHash, error) { | ||
key := make([]byte, nonceKeyLength) | ||
if _, err := rand.Read(key); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &NonceHash{key}, nil | ||
} | ||
|
||
// NonceHash is used to create and verify nonces | ||
type NonceHash struct { | ||
key []byte | ||
} | ||
|
||
// Generate a nonce | ||
func (n *NonceHash) Generate() (string, error) { | ||
nonce := make([]byte, 8, nonceLength) | ||
binary.BigEndian.PutUint64(nonce, uint64(time.Now().UnixMilli())) | ||
|
||
hash := hmac.New(sha256.New, n.key) | ||
if _, err := hash.Write(nonce[:8]); err != nil { | ||
return "", fmt.Errorf("%w: %v", errFailedToGenerateNonce, err) //nolint:errorlint | ||
} | ||
nonce = hash.Sum(nonce) | ||
|
||
return hex.EncodeToString(nonce), nil | ||
} | ||
|
||
// Validate checks that nonce is signed and is not expired | ||
func (n *NonceHash) Validate(nonce string) error { | ||
b, err := hex.DecodeString(nonce) | ||
if err != nil || len(b) != nonceLength { | ||
return fmt.Errorf("%w: %v", errInvalidNonce, err) //nolint:errorlint | ||
} | ||
|
||
if ts := time.UnixMilli(int64(binary.BigEndian.Uint64(b))); time.Since(ts) > nonceLifetime { | ||
return errInvalidNonce | ||
} | ||
|
||
hash := hmac.New(sha256.New, n.key) | ||
if _, err = hash.Write(b[:8]); err != nil { | ||
return fmt.Errorf("%w: %v", errInvalidNonce, err) //nolint:errorlint | ||
} | ||
if !hmac.Equal(b[8:], hash.Sum(nil)) { | ||
return errInvalidNonce | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> | ||
// SPDX-License-Identifier: MIT | ||
|
||
package server | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNonceHash(t *testing.T) { | ||
t.Run("generated hashes validate", func(t *testing.T) { | ||
h, err := NewNonceHash() | ||
assert.NoError(t, err) | ||
nonce, err := h.Generate() | ||
assert.NoError(t, err) | ||
assert.NoError(t, h.Validate(nonce)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters