Skip to content

Commit a63c2e8

Browse files
committed
Consolidate smimesign-specific dependencies into a monorepo.
github.com/github/fakeca → github.com/github/smimesign/fakeca github.com/github/certstore → github.com/github/smimesign/certstore github.com/github/ietf-cms → github.com/github/smimesign/ietf-cms
1 parent ed54d09 commit a63c2e8

45 files changed

Lines changed: 8196 additions & 24 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

certstore/LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Ben Toews.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

certstore/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# certstore [![PkgGoDev](https://pkg.go.dev/badge/github.com/github/certstore?tab=doc)](https://pkg.go.dev/github.com/github/certstore?tab=doc) [![Report card](https://goreportcard.com/badge/github.com/github/certstore)](https://goreportcard.com/report/github.com/github/certstore)
2+
3+
[![Test macOS (recent Go versions)](<https://github.com/github/certstore/workflows/Test%20macOS%20(recent%20Go%20versions)/badge.svg>)](https://github.com/github/certstore/actions?query=workflow%3A%22Test+macOS+%28recent+Go+versions%29%22)
4+
[![Test Windows (recent Go versions)](<https://github.com/github/certstore/workflows/Test%20Windows%20(recent%20Go%20versions)/badge.svg>)](https://github.com/github/certstore/actions?query=workflow%3A%22Test+Windows+%28recent+Go+versions%29%22)
5+
6+
Certstore is a Go library for accessing user identities stored in platform certificate stores. On Windows and macOS, certstore can enumerate user identities and sign messages with their private keys.
7+
8+
## Example
9+
10+
```go
11+
package main
12+
13+
import (
14+
"crypto"
15+
"encoding/hex"
16+
"errors"
17+
"fmt"
18+
19+
"crypto/rand"
20+
"crypto/sha256"
21+
22+
"github.com/github/certstore"
23+
)
24+
25+
func main() {
26+
sig, err := signWithMyIdentity("Ben Toews", "hello, world!")
27+
if err != nil {
28+
panic(err)
29+
}
30+
31+
fmt.Println(hex.EncodeToString(sig))
32+
}
33+
34+
func signWithMyIdentity(cn, msg string) ([]byte, error) {
35+
// Open the certificate store for use. This must be Close()'ed once you're
36+
// finished with the store and any identities it contains.
37+
store, err := certstore.Open()
38+
if err != nil {
39+
return nil, err
40+
}
41+
defer store.Close()
42+
43+
// Get an Identity slice, containing every identity in the store. Each of
44+
// these must be Close()'ed when you're done with them.
45+
idents, err := store.Identities()
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
// Iterate through the identities, looking for the one we want.
51+
var me certstore.Identity
52+
for _, ident := range idents {
53+
defer ident.Close()
54+
55+
crt, errr := ident.Certificate()
56+
if errr != nil {
57+
return nil, errr
58+
}
59+
60+
if crt.Subject.CommonName == "Ben Toews" {
61+
me = ident
62+
}
63+
}
64+
65+
if me == nil {
66+
return nil, errors.New("Couldn't find my identity")
67+
}
68+
69+
// Get a crypto.Signer for the identity.
70+
signer, err := me.Signer()
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
// Digest and sign our message.
76+
digest := sha256.Sum256([]byte(msg))
77+
signature, err := signer.Sign(rand.Reader, digest[:], crypto.SHA256)
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
return signature, nil
83+
}
84+
85+
```

certstore/certstore.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package certstore
2+
3+
import (
4+
"crypto"
5+
"crypto/x509"
6+
"errors"
7+
)
8+
9+
var (
10+
// ErrUnsupportedHash is returned by Signer.Sign() when the provided hash
11+
// algorithm isn't supported.
12+
ErrUnsupportedHash = errors.New("unsupported hash algorithm")
13+
)
14+
15+
// Open opens the system's certificate store.
16+
func Open() (Store, error) {
17+
return openStore()
18+
}
19+
20+
// Store represents the system's certificate store.
21+
type Store interface {
22+
// Identities gets a list of identities from the store.
23+
Identities() ([]Identity, error)
24+
25+
// Import imports a PKCS#12 (PFX) blob containing a certificate and private
26+
// key.
27+
Import(data []byte, password string) error
28+
29+
// Close closes the store.
30+
Close()
31+
}
32+
33+
// Identity is a X.509 certificate and its corresponding private key.
34+
type Identity interface {
35+
// Certificate gets the identity's certificate.
36+
Certificate() (*x509.Certificate, error)
37+
38+
// CertificateChain attempts to get the identity's full certificate chain.
39+
CertificateChain() ([]*x509.Certificate, error)
40+
41+
// Signer gets a crypto.Signer that uses the identity's private key.
42+
Signer() (crypto.Signer, error)
43+
44+
// Delete deletes this identity from the system.
45+
Delete() error
46+
47+
// Close any manually managed memory held by the Identity.
48+
Close()
49+
}

0 commit comments

Comments
 (0)