Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
golang-github-golang-jwt-jwt-v5
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Debian Go Packaging Team
packages
golang-github-golang-jwt-jwt-v5
Commits
5e00fbc8
Unverified
Commit
5e00fbc8
authored
2 years ago
by
Tom Anderson
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
enable jwt.ParsePublicKeyFromPEM to parse PKCS1 Public Key (#120)
parent
6c9126f9
No related branches found
Branches containing commit
Tags
v5.0.0
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
rsa_test.go
+19
-0
19 additions, 0 deletions
rsa_test.go
rsa_utils.go
+4
-2
4 additions, 2 deletions
rsa_utils.go
with
23 additions
and
2 deletions
rsa_test.go
+
19
−
0
View file @
5e00fbc8
package
jwt_test
import
(
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"os"
"reflect"
"strings"
...
...
@@ -115,6 +120,17 @@ func TestRSAKeyParsing(t *testing.T) {
pubKey
,
_
:=
os
.
ReadFile
(
"test/sample_key.pub"
)
badKey
:=
[]
byte
(
"All your base are belong to key"
)
randomKey
,
err
:=
rsa
.
GenerateKey
(
rand
.
Reader
,
2048
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to generate RSA private key: %v"
,
err
)
}
publicKeyBytes
:=
x509
.
MarshalPKCS1PublicKey
(
&
randomKey
.
PublicKey
)
pkcs1Buffer
:=
new
(
bytes
.
Buffer
)
if
err
=
pem
.
Encode
(
pkcs1Buffer
,
&
pem
.
Block
{
Type
:
"RSA PUBLIC KEY"
,
Bytes
:
publicKeyBytes
});
err
!=
nil
{
t
.
Errorf
(
"Failed to encode public pem: %v"
,
err
)
}
// Test parsePrivateKey
if
_
,
e
:=
jwt
.
ParseRSAPrivateKeyFromPEM
(
key
);
e
!=
nil
{
t
.
Errorf
(
"Failed to parse valid private key: %v"
,
e
)
...
...
@@ -149,6 +165,9 @@ func TestRSAKeyParsing(t *testing.T) {
t
.
Errorf
(
"Parsed invalid key as valid private key: %v"
,
k
)
}
if
_
,
err
:=
jwt
.
ParseRSAPublicKeyFromPEM
(
pkcs1Buffer
.
Bytes
());
err
!=
nil
{
t
.
Errorf
(
"failed to parse RSA public key: %v"
,
err
)
}
}
func
BenchmarkRSAParsing
(
b
*
testing
.
B
)
{
...
...
This diff is collapsed.
Click to expand it.
rsa_utils.go
+
4
−
2
View file @
5e00fbc8
...
...
@@ -75,7 +75,7 @@ func ParseRSAPrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.Pr
return
pkey
,
nil
}
// ParseRSAPublicKeyFromPEM parses a PEM encoded PKCS1 or PK
CS8
public key
// ParseRSAPublicKeyFromPEM parses a
certificate or a
PEM encoded PKCS1 or PK
IX
public key
func
ParseRSAPublicKeyFromPEM
(
key
[]
byte
)
(
*
rsa
.
PublicKey
,
error
)
{
var
err
error
...
...
@@ -91,7 +91,9 @@ func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
if
cert
,
err
:=
x509
.
ParseCertificate
(
block
.
Bytes
);
err
==
nil
{
parsedKey
=
cert
.
PublicKey
}
else
{
return
nil
,
err
if
parsedKey
,
err
=
x509
.
ParsePKCS1PublicKey
(
block
.
Bytes
);
err
!=
nil
{
return
nil
,
err
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment