forked from sigstore/fulcio
-
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.
Signed-off-by: Alex Cameron <[email protected]>
- Loading branch information
1 parent
bda9c01
commit 05a012b
Showing
1 changed file
with
171 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1188,8 +1188,96 @@ func TestAPIWithIssuerClaimConfig(t *testing.T) { | |
} | ||
} | ||
|
||
// Tests API with challenge sent as CSR with an ECDSA key | ||
func TestAPIWithCSRChallengeECDSA(t *testing.T) { | ||
// Tests API with an RSA key | ||
func TestAPIWithRSA(t *testing.T) { | ||
emailSigner, emailIssuer := newOIDCIssuer(t) | ||
|
||
// Create a FulcioConfig that supports these issuers. | ||
cfg, err := config.Read([]byte(fmt.Sprintf(`{ | ||
"OIDCIssuers": { | ||
%q: { | ||
"IssuerURL": %q, | ||
"ClientID": "sigstore", | ||
"Type": "email" | ||
} | ||
} | ||
}`, emailIssuer, emailIssuer))) | ||
if err != nil { | ||
t.Fatalf("config.Read() = %v", err) | ||
} | ||
|
||
emailSubject := "[email protected]" | ||
|
||
// Create an OIDC token using this issuer's signer. | ||
tok, err := jwt.Signed(emailSigner).Claims(jwt.Claims{ | ||
Issuer: emailIssuer, | ||
IssuedAt: jwt.NewNumericDate(time.Now()), | ||
Expiry: jwt.NewNumericDate(time.Now().Add(30 * time.Minute)), | ||
Subject: emailSubject, | ||
Audience: jwt.Audience{"sigstore"}, | ||
}).Claims(customClaims{Email: emailSubject, EmailVerified: true}).CompactSerialize() | ||
if err != nil { | ||
t.Fatalf("CompactSerialize() = %v", err) | ||
} | ||
|
||
ctClient, eca := createCA(cfg, t) | ||
ctx := context.Background() | ||
server, conn := setupGRPCForTest(ctx, t, cfg, ctClient, eca) | ||
defer func() { | ||
server.Stop() | ||
conn.Close() | ||
}() | ||
|
||
client := protobuf.NewCAClient(conn) | ||
|
||
priv, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
t.Fatalf("GenerateKey() = %v", err) | ||
} | ||
pubBytes, err := x509.MarshalPKIXPublicKey(&priv.PublicKey) | ||
if err != nil { | ||
t.Fatalf("x509.MarshalPKIXPublicKey() = %v", err) | ||
} | ||
hash := sha256.Sum256([]byte(emailSubject)) | ||
proof, err := rsa.SignPKCS1v15(rand.Reader, priv, crypto.SHA256, hash[:]) | ||
if err != nil { | ||
t.Fatalf("SignPKCS1v15() = %v", err) | ||
} | ||
pemBytes := string(cryptoutils.PEMEncode(cryptoutils.PublicKeyPEMType, pubBytes)) | ||
|
||
// Hit the API to have it sign our certificate. | ||
resp, err := client.CreateSigningCertificate(ctx, &protobuf.CreateSigningCertificateRequest{ | ||
Credentials: &protobuf.Credentials{ | ||
Credentials: &protobuf.Credentials_OidcIdentityToken{ | ||
OidcIdentityToken: tok, | ||
}, | ||
}, | ||
Key: &protobuf.CreateSigningCertificateRequest_PublicKeyRequest{ | ||
PublicKeyRequest: &protobuf.PublicKeyRequest{ | ||
PublicKey: &protobuf.PublicKey{ | ||
Content: pemBytes, | ||
}, | ||
ProofOfPossession: proof, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatalf("SigningCert() = %v", err) | ||
} | ||
|
||
leafCert := verifyResponse(resp, eca, emailIssuer, t) | ||
|
||
// Expect email subject | ||
if len(leafCert.EmailAddresses) != 1 { | ||
t.Fatalf("unexpected length of leaf certificate URIs, expected 1, got %d", len(leafCert.URIs)) | ||
} | ||
if leafCert.EmailAddresses[0] != emailSubject { | ||
t.Fatalf("subjects do not match: Expected %v, got %v", emailSubject, leafCert.EmailAddresses[0]) | ||
} | ||
} | ||
|
||
// Tests API with challenge sent as CSR | ||
func TestAPIWithCSRChallenge(t *testing.T) { | ||
emailSigner, emailIssuer := newOIDCIssuer(t) | ||
|
||
// Create a FulcioConfig that supports this issuer. | ||
|
@@ -1572,6 +1660,87 @@ func TestAPIWithInvalidChallenge(t *testing.T) { | |
} | ||
} | ||
|
||
// Tests API with an ECDSA key with an unpermitted curve | ||
func TestAPIWithInvalidPublicKey(t *testing.T) { | ||
emailSigner, emailIssuer := newOIDCIssuer(t) | ||
|
||
// Create a FulcioConfig that supports these issuers. | ||
cfg, err := config.Read([]byte(fmt.Sprintf(`{ | ||
"OIDCIssuers": { | ||
%q: { | ||
"IssuerURL": %q, | ||
"ClientID": "sigstore", | ||
"Type": "email" | ||
} | ||
} | ||
}`, emailIssuer, emailIssuer))) | ||
if err != nil { | ||
t.Fatalf("config.Read() = %v", err) | ||
} | ||
|
||
emailSubject := "[email protected]" | ||
|
||
// Create an OIDC token using this issuer's signer. | ||
tok, err := jwt.Signed(emailSigner).Claims(jwt.Claims{ | ||
Issuer: emailIssuer, | ||
IssuedAt: jwt.NewNumericDate(time.Now()), | ||
Expiry: jwt.NewNumericDate(time.Now().Add(30 * time.Minute)), | ||
Subject: emailSubject, | ||
Audience: jwt.Audience{"sigstore"}, | ||
}).Claims(customClaims{Email: emailSubject, EmailVerified: true}).CompactSerialize() | ||
if err != nil { | ||
t.Fatalf("CompactSerialize() = %v", err) | ||
} | ||
|
||
ctClient, eca := createCA(cfg, t) | ||
ctx := context.Background() | ||
server, conn := setupGRPCForTest(ctx, t, cfg, ctClient, eca) | ||
defer func() { | ||
server.Stop() | ||
conn.Close() | ||
}() | ||
|
||
client := protobuf.NewCAClient(conn) | ||
|
||
// Generate an ECDSA key with an unpermitted curve | ||
priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) | ||
if err != nil { | ||
t.Fatalf("GenerateKey() = %v", err) | ||
} | ||
pubBytes, err := x509.MarshalPKIXPublicKey(&priv.PublicKey) | ||
if err != nil { | ||
t.Fatalf("x509.MarshalPKIXPublicKey() = %v", err) | ||
} | ||
hash := sha256.Sum256([]byte(emailSubject)) | ||
proof, err := ecdsa.SignASN1(rand.Reader, priv, hash[:]) | ||
if err != nil { | ||
t.Fatalf("SignASN1() = %v", err) | ||
} | ||
pemBytes := string(cryptoutils.PEMEncode(cryptoutils.PublicKeyPEMType, pubBytes)) | ||
|
||
_, err = client.CreateSigningCertificate(ctx, &protobuf.CreateSigningCertificateRequest{ | ||
Credentials: &protobuf.Credentials{ | ||
Credentials: &protobuf.Credentials_OidcIdentityToken{ | ||
OidcIdentityToken: tok, | ||
}, | ||
}, | ||
Key: &protobuf.CreateSigningCertificateRequest_PublicKeyRequest{ | ||
PublicKeyRequest: &protobuf.PublicKeyRequest{ | ||
PublicKey: &protobuf.PublicKey{ | ||
Content: pemBytes, | ||
}, | ||
ProofOfPossession: proof, | ||
}, | ||
}, | ||
}) | ||
if err == nil || !strings.Contains(err.Error(), "Signing algorithm not permitted") { | ||
t.Fatalf("expected signing algorithm not permitted, got %v", err) | ||
} | ||
if status.Code(err) != codes.InvalidArgument { | ||
t.Fatalf("expected invalid argument, got %v", status.Code(err)) | ||
} | ||
} | ||
|
||
// Tests API with an invalid CSR. | ||
func TestAPIWithInvalidCSR(t *testing.T) { | ||
emailSigner, emailIssuer := newOIDCIssuer(t) | ||
|