Skip to content

Commit

Permalink
x509{,util}: support X25519 public key parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddrysdale committed Apr 30, 2022
1 parent 9bc27bb commit 05b4aa3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions x509/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
// - Support for parsing RSASES-OAEP public keys from certificates
// - Ed25519 support:
// - Support for parsing and marshaling Ed25519 keys
// - X25519 support:
// - Support for parsing X25519 keys
// - General improvements:
// - Export and use OID values throughout.
// - Export OIDFromNamedCurve().
Expand Down Expand Up @@ -321,6 +323,7 @@ const (
ECDSA
Ed25519
RSAESOAEP
X25519
)

var publicKeyAlgoName = [...]string{
Expand All @@ -329,6 +332,7 @@ var publicKeyAlgoName = [...]string{
ECDSA: "ECDSA",
Ed25519: "Ed25519",
RSAESOAEP: "RSAESOAEP",
X25519: "X25519",
}

func (algo PublicKeyAlgorithm) String() string {
Expand Down Expand Up @@ -584,6 +588,7 @@ var (
OIDPublicKeyECDSA = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1}
OIDPublicKeyRSAObsolete = asn1.ObjectIdentifier{2, 5, 8, 1, 1}
OIDPublicKeyEd25519 = oidSignatureEd25519
OIDPublicKeyX25519 = asn1.ObjectIdentifier{1, 3, 101, 110}
)

func getPublicKeyAlgorithmFromOID(oid asn1.ObjectIdentifier) PublicKeyAlgorithm {
Expand All @@ -598,6 +603,8 @@ func getPublicKeyAlgorithmFromOID(oid asn1.ObjectIdentifier) PublicKeyAlgorithm
return RSAESOAEP
case oid.Equal(OIDPublicKeyEd25519):
return Ed25519
case oid.Equal(OIDPublicKeyX25519):
return X25519
}
return UnknownPublicKeyAlgorithm
}
Expand Down Expand Up @@ -1451,6 +1458,8 @@ func parsePublicKey(algo PublicKeyAlgorithm, keyData *publicKeyInfo, nfe *NonFat
return pub, nil
case Ed25519:
return ed25519.PublicKey(asn1Data), nil
case X25519:
return asn1Data, nil
default:
return nil, nil
}
Expand Down
5 changes: 5 additions & 0 deletions x509util/x509util.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ func publicKeyAlgorithmToString(algo x509.PublicKeyAlgorithm) string {
return "id-ecPublicKey"
case x509.Ed25519:
return "Ed25519"
case x509.X25519:
return "X25519"
default:
return strconv.Itoa(int(algo))
}
Expand Down Expand Up @@ -180,6 +182,9 @@ func publicKeyToString(_ x509.PublicKeyAlgorithm, pub interface{}) string {
case ed25519.PublicKey:
buf.WriteString(" pub:\n")
appendHexData(&buf, []byte(pub), 15, " ")
case []byte:
buf.WriteString(" pub:\n")
appendHexData(&buf, pub, 15, " ")
default:
buf.WriteString(fmt.Sprintf(" %T: %v", pub, pub))
}
Expand Down

0 comments on commit 05b4aa3

Please sign in to comment.