Skip to content

Instantly share code, notes, and snippets.

@directionless
Created June 29, 2023 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save directionless/db32df0f7472958f365336f3a3483c6f to your computer and use it in GitHub Desktop.
Save directionless/db32df0f7472958f365336f3a3483c6f to your computer and use it in GitHub Desktop.
Certificates
package main
import (
"crypto"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"time"
"github.com/github/smimesign/certstore"
)
func main() {
if err := certList(); err != nil {
fmt.Printf("Error: %s\n", err)
}
}
func certList() error {
store, err := certstore.Open()
if err != nil {
return fmt.Errorf("opening certstore: %w", err)
}
defer store.Close()
idents, err := store.Identities()
if err != nil {
return fmt.Errorf("getting identities: %w", err)
}
for _, ident := range idents {
tryIdent(ident)
}
return nil
}
func tryIdent(ident certstore.Identity) {
defer fmt.Println("\n")
crt, err := ident.Certificate()
if err != nil {
fmt.Printf("Not a cert: %s\n", err)
return
}
fmt.Printf("Subject: %s\nIssuer: %s\n", crt.Subject.String(), crt.Issuer.String())
nonce := time.Now().String()
fmt.Printf("msg: %s\n", nonce)
signer, err := ident.Signer()
if err != nil {
fmt.Printf("error getting signer: %s", err)
return
}
// Digest and sign our message.
digest := sha256.Sum256([]byte(nonce))
signature, err := signer.Sign(rand.Reader, digest[:], crypto.SHA256)
if err != nil {
fmt.Printf("error signing: %s", err)
return
}
fmt.Printf("sig: %s\n", base64.StdEncoding.EncodeToString(signature))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment