-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: This adds example code on how to fetch a fulcio certificate (…
…#324) * examples: This adds example code on how to fetch a fulcio certificate Signed-off-by: Morten Linderud <morten@linderud.pw> * examples: Added readme Signed-off-by: Morten Linderud <morten@linderud.pw> * examples/request-certificate: Updated authflow call Signed-off-by: Morten Linderud <morten@linderud.pw> * go.sum/mod: Updated dependencies Signed-off-by: Morten Linderud <morten@linderud.pw>
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fulcio examples | ||
=============== | ||
|
||
This directory contains example code that shows how one utilize the fulcio | ||
library for certificate generation. | ||
|
||
# request-certificate | ||
This code is a minimal example how one would request a short-lived certificate | ||
(20 minutes) for code signing. It uses the fulcio oauth portal and generates a | ||
RSA 4096 key. |
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,99 @@ | ||
// Copyright 2022 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"crypto" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
"log" | ||
"net/url" | ||
|
||
"github.com/sigstore/fulcio/pkg/api" | ||
"github.com/sigstore/sigstore/pkg/oauthflow" | ||
"github.com/sigstore/sigstore/pkg/signature" | ||
"github.com/sigstore/sigstore/pkg/signature/options" | ||
) | ||
|
||
var ( | ||
fulcioUrl = "https://fulcio.sigstore.dev" | ||
oidcIssuer = "https://oauth2.sigstore.dev/auth" | ||
oidcClientID = "sigstore" | ||
) | ||
|
||
// Some of this is just ripped from cosign | ||
func GetCert(signer *signature.RSAPKCS1v15SignerVerifier, fc api.Client, oidcIssuer string, oidcClientID string) (*api.CertificateResponse, error) { | ||
|
||
tok, err := oauthflow.OIDConnect(oidcIssuer, oidcClientID, "", "", oauthflow.DefaultIDTokenGetter) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Sign the email address as part of the request | ||
b := bytes.NewBuffer([]byte(tok.Subject)) | ||
proof, err := signer.SignMessage(b, options.WithCryptoSignerOpts(crypto.SHA256)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
pubBytes, err := x509.MarshalPKIXPublicKey(signer.Public()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cr := api.CertificateRequest{ | ||
PublicKey: api.Key{ | ||
Algorithm: "rsa4096", | ||
Content: pubBytes, | ||
}, | ||
SignedEmailAddress: proof, | ||
} | ||
return fc.SigningCert(cr, tok.RawString) | ||
} | ||
|
||
func NewClient(fulcioURL string) (api.Client, error) { | ||
fulcioServer, err := url.Parse(fulcioURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fClient := api.NewClient(fulcioServer, api.WithUserAgent("Fulcio Example Code")) | ||
return fClient, nil | ||
} | ||
|
||
func main() { | ||
signer, _, err := signature.NewDefaultRSAPKCS1v15SignerVerifier() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fClient, err := NewClient(fulcioUrl) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
certResp, err := GetCert(signer, fClient, oidcIssuer, oidcClientID) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
clientPEM, _ := pem.Decode([]byte(certResp.CertPEM)) | ||
cert, err := x509.ParseCertificate(clientPEM.Bytes) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Received signing cerificate with serial number: ", cert.SerialNumber) | ||
} |
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