-
Notifications
You must be signed in to change notification settings - Fork 498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring crypto code for future reuse. #25148
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
666bea7
Refactoring crypto code for future reuse.
getvictor 3a146c2
Fixing circular import.
getvictor ef2ea77
Removed unused code.
getvictor 4e7ce73
Removed unused imports.
getvictor a40237d
Moved cryptoutil directory up to mdm level.
getvictor ae8fe6e
Fix typo.
getvictor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cryptoutil | ||
|
||
import ( | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/ed25519" | ||
"crypto/elliptic" | ||
"crypto/rsa" | ||
"crypto/sha256" | ||
"crypto/x509" | ||
"encoding/asn1" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// GenerateSubjectKeyID generates Subject Key Identifier (SKI) using SHA-256 | ||
// hash of the public key bytes according to RFC 7093 section 2. | ||
func GenerateSubjectKeyID(pub crypto.PublicKey) ([]byte, error) { | ||
var pubBytes []byte | ||
var err error | ||
switch pub := pub.(type) { | ||
case *rsa.PublicKey: | ||
pubBytes, err = asn1.Marshal(*pub) | ||
if err != nil { | ||
return nil, err | ||
} | ||
case *ecdsa.PublicKey: | ||
pubBytes = elliptic.Marshal(pub.Curve, pub.X, pub.Y) | ||
default: | ||
return nil, errors.New("only ECDSA and RSA public keys are supported") | ||
} | ||
|
||
hash := sha256.Sum256(pubBytes) | ||
|
||
// According to RFC 7093, The keyIdentifier is composed of the leftmost | ||
// 160-bits of the SHA-256 hash of the value of the BIT STRING | ||
// subjectPublicKey (excluding the tag, length, and number of unused bits). | ||
return hash[:20], nil | ||
} | ||
|
||
// ParsePrivateKey parses a PEM encoded private key and returns a crypto.PrivateKey. | ||
// It can be used for private keys passed in from environment variables or command line or files. | ||
func ParsePrivateKey(privKeyPEM []byte, keyName string) (crypto.PrivateKey, error) { | ||
block, _ := pem.Decode(privKeyPEM) | ||
if block == nil { | ||
return nil, fmt.Errorf("failed to decode %s", keyName) | ||
} | ||
|
||
// The code below is based on tls.parsePrivateKey | ||
// https://cs.opensource.google/go/go/+/release-branch.go1.23:src/crypto/tls/tls.go;l=355-372 | ||
if key, err := x509.ParsePKCS1PrivateKey(block.Bytes); err == nil { | ||
return key, nil | ||
} | ||
if key, err := x509.ParsePKCS8PrivateKey(block.Bytes); err == nil { | ||
switch key := key.(type) { | ||
case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey: | ||
return key, nil | ||
default: | ||
return nil, fmt.Errorf("unmarshaled PKCS8 %s is not an RSA, ECDSA, or Ed25519 private key", keyName) | ||
} | ||
} | ||
if key, err := x509.ParseECPrivateKey(block.Bytes); err == nil { | ||
return key, nil | ||
} | ||
|
||
return nil, fmt.Errorf("failed to parse %s of type %s", keyName, block.Type) | ||
} |
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,94 @@ | ||
package cryptoutil | ||
|
||
import ( | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"math/big" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGenerateSubjectKeyID(t *testing.T) { | ||
ecKey, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
for _, test := range []struct { | ||
testName string | ||
pub crypto.PublicKey | ||
}{ | ||
{"RSA", &rsa.PublicKey{N: big.NewInt(123), E: 65537}}, | ||
{"ECDSA", ecKey.Public()}, | ||
} { | ||
test := test | ||
t.Run(test.testName, func(t *testing.T) { | ||
t.Parallel() | ||
ski, err := GenerateSubjectKeyID(test.pub) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(ski) != 20 { | ||
t.Fatalf("unexpected subject public key identifier length: %d", len(ski)) | ||
} | ||
ski2, err := GenerateSubjectKeyID(test.pub) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !testSKIEq(ski, ski2) { | ||
t.Fatal("subject key identifier generation is not deterministic") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func testSKIEq(a, b []byte) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
|
||
for i := range a { | ||
if a[i] != b[i] { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func TestParsePrivateKey(t *testing.T) { | ||
t.Parallel() | ||
// nil block not allowed | ||
_, err := ParsePrivateKey(nil, "APNS private key") | ||
assert.ErrorContains(t, err, "failed to decode") | ||
|
||
// encrypted pkcs8 not supported | ||
pkcs8Encrypted, err := os.ReadFile("testdata/pkcs8-encrypted.key") | ||
require.NoError(t, err) | ||
_, err = ParsePrivateKey(pkcs8Encrypted, "APNS private key") | ||
assert.ErrorContains(t, err, "failed to parse APNS private key of type ENCRYPTED PRIVATE KEY") | ||
|
||
// X25519 pkcs8 not supported | ||
pkcs8Encrypted, err = os.ReadFile("testdata/pkcs8-x25519.key") | ||
require.NoError(t, err) | ||
_, err = ParsePrivateKey(pkcs8Encrypted, "APNS private key") | ||
assert.ErrorContains(t, err, "unmarshaled PKCS8 APNS private key is not") | ||
|
||
// In this test, the pkcs1 key and pkcs8 keys are the same key, just different formats | ||
pkcs1, err := os.ReadFile("testdata/pkcs1.key") | ||
require.NoError(t, err) | ||
pkcs1Key, err := ParsePrivateKey(pkcs1, "APNS private key") | ||
require.NoError(t, err) | ||
|
||
pkcs8, err := os.ReadFile("testdata/pkcs8-rsa.key") | ||
require.NoError(t, err) | ||
pkcs8Key, err := ParsePrivateKey(pkcs8, "APNS private key") | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, pkcs1Key, pkcs8Key) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't fully reviewed, I assume this (and related tests) were just code copied from the old places?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes