Skip to content

Commit 5c87101

Browse files
authored
Merge pull request #160 from smlx/fix-invalid-pubkey
fix: correctly convert to openpgp ecdsa key representation
2 parents bd6437f + cbad1f0 commit 5c87101

File tree

5 files changed

+75
-47
lines changed

5 files changed

+75
-47
lines changed

internal/keyservice/gpg/keyservice.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"bytes"
77
"crypto"
88
"crypto/ecdsa"
9-
"crypto/elliptic"
109
"crypto/rsa"
1110
"fmt"
1211

@@ -137,48 +136,6 @@ func (g *KeyService) getRSAKey(keygrip []byte) (*rsa.PrivateKey, error) {
137136
return nil, nil
138137
}
139138

140-
func nameToCurve(name string) (elliptic.Curve, error) {
141-
switch name {
142-
case elliptic.P224().Params().Name:
143-
return elliptic.P224(), nil
144-
case elliptic.P256().Params().Name:
145-
return elliptic.P256(), nil
146-
case elliptic.P384().Params().Name:
147-
return elliptic.P384(), nil
148-
case elliptic.P521().Params().Name:
149-
return elliptic.P521(), nil
150-
default:
151-
return nil, fmt.Errorf("unknown curve name: %s", name)
152-
}
153-
}
154-
155-
func ecdsaPublicKey(k *openpgpecdsa.PublicKey) (*ecdsa.PublicKey, error) {
156-
curve, err := nameToCurve(k.GetCurve().GetCurveName())
157-
if err != nil {
158-
return nil, err
159-
}
160-
return &ecdsa.PublicKey{
161-
Curve: curve,
162-
X: k.X,
163-
Y: k.Y,
164-
}, nil
165-
}
166-
167-
func ecdsaPrivateKey(k *openpgpecdsa.PrivateKey) (*ecdsa.PrivateKey, error) {
168-
curve, err := nameToCurve(k.GetCurve().GetCurveName())
169-
if err != nil {
170-
return nil, err
171-
}
172-
return &ecdsa.PrivateKey{
173-
D: k.D,
174-
PublicKey: ecdsa.PublicKey{
175-
Curve: curve,
176-
X: k.X,
177-
Y: k.Y,
178-
},
179-
}, nil
180-
}
181-
182139
// getECDSAKey returns a matching private ECDSA key if the keygrip matches. If
183140
// a key is returned err will be nil. If no key is found, both values will be
184141
// nil.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package gpg
2+
3+
import (
4+
"crypto/ecdsa"
5+
"crypto/elliptic"
6+
"fmt"
7+
8+
openpgpecdsa "github.com/ProtonMail/go-crypto/openpgp/ecdsa"
9+
)
10+
11+
// nameToCurve takes a given curve name and returns the associated
12+
// elliptic.Curve.
13+
func nameToCurve(name string) (elliptic.Curve, error) {
14+
switch name {
15+
case elliptic.P224().Params().Name:
16+
return elliptic.P224(), nil
17+
case elliptic.P256().Params().Name:
18+
return elliptic.P256(), nil
19+
case elliptic.P384().Params().Name:
20+
return elliptic.P384(), nil
21+
case elliptic.P521().Params().Name:
22+
return elliptic.P521(), nil
23+
default:
24+
return nil, fmt.Errorf("unknown curve name: %s", name)
25+
}
26+
}
27+
28+
// ecdsaPublicKey converts the given ECDSA Key in go-crypto/openpgp
29+
// representation, to standard library crypto/ecdsa representation.
30+
func ecdsaPublicKey(k *openpgpecdsa.PublicKey) (*ecdsa.PublicKey, error) {
31+
curve, err := nameToCurve(k.GetCurve().GetCurveName())
32+
if err != nil {
33+
return nil, err
34+
}
35+
return &ecdsa.PublicKey{
36+
Curve: curve,
37+
X: k.X,
38+
Y: k.Y,
39+
}, nil
40+
}
41+
42+
// ecdsaPrivateKey converts the given ECDSA Key in go-crypto/openpgp
43+
// representation, to standard library crypto/ecdsa representation.
44+
func ecdsaPrivateKey(k *openpgpecdsa.PrivateKey) (*ecdsa.PrivateKey, error) {
45+
curve, err := nameToCurve(k.GetCurve().GetCurveName())
46+
if err != nil {
47+
return nil, err
48+
}
49+
return &ecdsa.PrivateKey{
50+
D: k.D,
51+
PublicKey: ecdsa.PublicKey{
52+
Curve: curve,
53+
X: k.X,
54+
Y: k.Y,
55+
},
56+
}, nil
57+
}

internal/securitykey/decryptingkey.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77

8-
openpgpecdsa "github.com/ProtonMail/go-crypto/openpgp/ecdsa"
98
"github.com/ProtonMail/go-crypto/openpgp/packet"
109
"github.com/go-piv/piv-go/piv"
1110
)
@@ -39,7 +38,7 @@ func decryptingKeys(yk *piv.YubiKey) ([]DecryptingKey, error) {
3938
SlotSpec: s,
4039
},
4140
PubPGP: packet.NewECDSAPublicKey(cert.NotBefore,
42-
openpgpecdsa.NewPublicKeyFromCurve(pubKey.Curve)),
41+
openpgpECDSAPublicKey(pubKey)),
4342
})
4443
}
4544
return decryptingKeys, nil

internal/securitykey/openpgpecdsa.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package securitykey
2+
3+
import (
4+
"crypto/ecdsa"
5+
6+
openpgpecdsa "github.com/ProtonMail/go-crypto/openpgp/ecdsa"
7+
)
8+
9+
// openpgpECDSAPublicKey converts the given ECDSA Key in crypto/ecdsa
10+
// representation, to go-crypto/openpgp representation.
11+
func openpgpECDSAPublicKey(k *ecdsa.PublicKey) *openpgpecdsa.PublicKey {
12+
openpgpPubKey := openpgpecdsa.NewPublicKeyFromCurve(k.Curve)
13+
openpgpPubKey.X = k.X
14+
openpgpPubKey.Y = k.Y
15+
return openpgpPubKey
16+
}

internal/securitykey/signingkey.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77

8-
openpgpecdsa "github.com/ProtonMail/go-crypto/openpgp/ecdsa"
98
"github.com/ProtonMail/go-crypto/openpgp/packet"
109
"github.com/go-piv/piv-go/piv"
1110
"golang.org/x/crypto/ssh"
@@ -45,7 +44,7 @@ func signingKeys(yk *piv.YubiKey) ([]SigningKey, error) {
4544
},
4645
PubSSH: pubSSH,
4746
PubPGP: packet.NewECDSAPublicKey(cert.NotBefore,
48-
openpgpecdsa.NewPublicKeyFromCurve(pubKey.Curve)),
47+
openpgpECDSAPublicKey(pubKey)),
4948
})
5049
}
5150
return signingKeys, nil

0 commit comments

Comments
 (0)