-
Notifications
You must be signed in to change notification settings - Fork 1
/
x509_rsa_store.go
175 lines (154 loc) · 4.05 KB
/
x509_rsa_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package signer
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"sync"
"time"
)
type x509RSAOneToManyStore struct {
rootCert RSACert
subject *X509Subject
priKey *rsa.PrivateKey
expire time.Time
mu sync.Mutex
source *RSADescriptorCollection
tag string
bucket string
}
func (s *x509RSAOneToManyStore) SetTag(tag string) {
s.mu.Lock()
defer s.mu.Unlock()
s.tag = tag
}
func (s *x509RSAOneToManyStore) Tag() string {
return s.tag
}
func (s *x509RSAOneToManyStore) Certificate(clientID string) (RSADescriptor, error) {
if s.expire.Unix() > time.Now().Unix() && s.source.AnyClientID(clientID) {
return s.source.FirstClientID(clientID), nil
}
if s.expire.Unix() <= time.Now().Unix() {
err := func() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.expire.Unix() <= time.Now().Unix() {
priKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
s.priKey = priKey
s.expire = time.Now().Add(time.Hour * 24 * 7)
accessor, err := ParseURI(fmt.Sprintf("files://~/.feiniubus/signer/%s_%d_%s.pem", s.Tag(), time.Now().Unix(), clientID))
if err != nil {
return err
}
buf := x509.MarshalPKCS1PrivateKey(s.priKey)
keyPem := &pem.Block{
Type: "PRIVATE KEY",
Bytes: buf,
}
key := pem.EncodeToMemory(keyPem)
_, _ = accessor.Upload(key)
}
return nil
}()
if err != nil {
return nil, err
}
}
s.mu.Lock()
defer s.mu.Unlock()
if s.source.AnyClientID(clientID) {
return s.source.FirstClientID(clientID), nil
}
issuor := Newx509RSACertIssuor(s.rootCert, s.priKey)
certificate, err := issuor.Issue(s.subject)
if err != nil {
return nil, err
}
fileName := fmt.Sprintf("%s_%d_%s.crt", s.Tag(), time.Now().Unix(), clientID)
accessor, err := ParseURI(fmt.Sprintf("s3://%s/%s?key=%s", GetS3Options().GetRegion(), s.bucket, fileName))
if err != nil {
return nil, err
}
url, err := accessor.Upload(certificate.GetCertificateBytes())
if err != nil {
return nil, err
}
descriptor := Newx509RSADescriptor(clientID, url, s.priKey)
s.source.AddOrReplace(descriptor)
file, err := ParseURI(fmt.Sprintf("files://~/.feiniubus/signer/%s_%d_%s.crt", s.Tag(), time.Now().Unix(), clientID))
if err != nil {
return nil, err
}
_, _ = file.Upload(certificate.GetCertificateBytes())
return descriptor, nil
}
type X509RSAStoreMode int
const (
_ X509RSAStoreMode = iota
X509RSAStore_OneToMany
X509RSAStore_Test
)
type RSAStoreFactory struct {
rootCert RSACert
subject *X509Subject
tag string
bucket string
}
func NewRSAStoreFactory(tag string, bucket string, rootCert RSACert, subject *X509Subject) *RSAStoreFactory {
return &RSAStoreFactory{
rootCert: rootCert,
subject: subject,
tag: tag,
bucket: bucket,
}
}
func NewRSAStoreFactoryFrom(tag string, bucket string, rootPriKeyUrl string, rootCertUrl string, subject *X509Subject) (*RSAStoreFactory, error) {
rootPriKeyAccessor, err := ParseURI(rootPriKeyUrl)
if err != nil {
return nil, err
}
rootCertAccessor, err := ParseURI(rootCertUrl)
if err != nil {
return nil, err
}
rootPK, err := rootPriKeyAccessor.Download()
if err != nil {
return nil, err
}
rootCert, err := rootCertAccessor.Download()
if err != nil {
return nil, err
}
cert, err := Parsex509RSACert(rootCert, rootPK)
if err != nil {
return nil, err
}
return NewRSAStoreFactory(tag, bucket, cert, subject), nil
}
func (factory *RSAStoreFactory) Create(mode X509RSAStoreMode) (RSAStore, error) {
if mode == X509RSAStore_OneToMany {
return &x509RSAOneToManyStore{
rootCert: factory.rootCert,
subject: factory.subject,
tag: factory.tag,
bucket: factory.bucket,
expire: time.Now().Add(time.Hour * -1),
source: NewRSADescriptorCollection(),
}, nil
} else if mode == X509RSAStore_Test {
return &x509RSATestStore{
rootCert: factory.rootCert,
subject: factory.subject,
tag: factory.tag,
expire: time.Now().Add(time.Hour * -1),
source: NewRSADescriptorCollection(),
}, nil
}
return nil, errors.New("unknown mode x509RSAStoreMode")
}