Skip to content

Commit 5f43bb6

Browse files
committed
opensource
0 parents  commit 5f43bb6

File tree

10 files changed

+519
-0
lines changed

10 files changed

+519
-0
lines changed

LICENSE

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) 2015-present Cloud <cloud@txthinking.com> https://www.txthinking.com
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.

ca.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package mad
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/rsa"
6+
"crypto/sha1"
7+
"crypto/x509"
8+
"crypto/x509/pkix"
9+
"encoding/asn1"
10+
"encoding/pem"
11+
"math/big"
12+
"os"
13+
"time"
14+
)
15+
16+
type Ca struct {
17+
C *x509.Certificate
18+
CaPEM []byte
19+
KeyPEM []byte
20+
}
21+
22+
func NewCa(Organization, OrganizationalUnit, CommonName string) *Ca {
23+
c := &x509.Certificate{
24+
Subject: pkix.Name{
25+
Organization: []string{Organization},
26+
OrganizationalUnit: []string{OrganizationalUnit},
27+
CommonName: CommonName,
28+
},
29+
NotBefore: time.Now(),
30+
NotAfter: time.Now().AddDate(10, 0, 0),
31+
IsCA: true,
32+
KeyUsage: x509.KeyUsageCertSign,
33+
BasicConstraintsValid: true,
34+
MaxPathLenZero: true,
35+
}
36+
return &Ca{
37+
C: c,
38+
}
39+
}
40+
41+
func (c *Ca) Create() error {
42+
p, err := rsa.GenerateKey(rand.Reader, 4096)
43+
if err != nil {
44+
return err
45+
}
46+
pub := p.Public()
47+
48+
b, err := x509.MarshalPKIXPublicKey(pub)
49+
if err != nil {
50+
return err
51+
}
52+
var spki struct {
53+
Algorithm pkix.AlgorithmIdentifier
54+
SubjectPublicKey asn1.BitString
55+
}
56+
if _, err := asn1.Unmarshal(b, &spki); err != nil {
57+
return err
58+
}
59+
skid := sha1.Sum(spki.SubjectPublicKey.Bytes)
60+
c.C.SubjectKeyId = skid[:]
61+
62+
sn, err := rand.Int(rand.Reader, big.NewInt(0).Lsh(big.NewInt(1), 128))
63+
if err != nil {
64+
return err
65+
}
66+
c.C.SerialNumber = sn
67+
68+
b, err = x509.CreateCertificate(rand.Reader, c.C, c.C, pub, p)
69+
if err != nil {
70+
return err
71+
}
72+
c.CaPEM = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: b})
73+
// c.KeyPEM = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(p)})
74+
b, err = x509.MarshalPKCS8PrivateKey(p)
75+
if err != nil {
76+
return err
77+
}
78+
c.KeyPEM = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: b})
79+
return nil
80+
}
81+
82+
func (c *Ca) Ca() []byte {
83+
return c.CaPEM
84+
}
85+
86+
func (c *Ca) Key() []byte {
87+
return c.KeyPEM
88+
}
89+
90+
func (c *Ca) SaveToFile(ca, key string) error {
91+
f, err := os.Create(ca)
92+
if err != nil {
93+
return err
94+
}
95+
if _, err := f.Write(c.CaPEM); err != nil {
96+
return err
97+
}
98+
if err := f.Close(); err != nil {
99+
return err
100+
}
101+
f, err = os.Create(key)
102+
if err != nil {
103+
return err
104+
}
105+
if _, err := f.Write(c.KeyPEM); err != nil {
106+
return err
107+
}
108+
if err := f.Close(); err != nil {
109+
return err
110+
}
111+
return nil
112+
}

cert.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package mad
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/rsa"
6+
"crypto/sha1"
7+
"crypto/tls"
8+
"crypto/x509"
9+
"crypto/x509/pkix"
10+
"encoding/asn1"
11+
"encoding/pem"
12+
"math/big"
13+
"net"
14+
"os"
15+
"time"
16+
)
17+
18+
type Cert struct {
19+
CaPEM []byte
20+
CaKeyPEM []byte
21+
C *x509.Certificate
22+
CertPEM []byte
23+
KeyPEM []byte
24+
}
25+
26+
func NewCert(caPEM, caKeyPEM []byte, Organization, OrganizationalUnit string) *Cert {
27+
c := &x509.Certificate{
28+
Subject: pkix.Name{
29+
Organization: []string{Organization},
30+
OrganizationalUnit: []string{OrganizationalUnit},
31+
},
32+
NotBefore: time.Date(2019, time.June, 1, 0, 0, 0, 0, time.UTC),
33+
NotAfter: time.Now().AddDate(10, 0, 0),
34+
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
35+
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
36+
BasicConstraintsValid: true,
37+
}
38+
return &Cert{
39+
CaPEM: caPEM,
40+
CaKeyPEM: caKeyPEM,
41+
C: c,
42+
}
43+
}
44+
45+
func (c *Cert) SetIPAddresses(ips []net.IP) {
46+
c.C.IPAddresses = ips
47+
if len(ips) > 0 {
48+
c.C.Subject.CommonName = ips[0].String()
49+
}
50+
}
51+
52+
func (c *Cert) SetDNSNames(domains []string) {
53+
c.C.DNSNames = domains
54+
if len(domains) > 0 {
55+
c.C.Subject.CommonName = domains[0]
56+
}
57+
}
58+
59+
func (c *Cert) Create() error {
60+
tc, err := tls.X509KeyPair(c.CaPEM, c.CaKeyPEM)
61+
if err != nil {
62+
return err
63+
}
64+
ca, err := x509.ParseCertificate(tc.Certificate[0])
65+
if err != nil {
66+
return err
67+
}
68+
69+
p, err := rsa.GenerateKey(rand.Reader, 4096)
70+
if err != nil {
71+
return err
72+
}
73+
pub := p.Public()
74+
75+
b, err := x509.MarshalPKIXPublicKey(pub)
76+
if err != nil {
77+
return err
78+
}
79+
var spki struct {
80+
Algorithm pkix.AlgorithmIdentifier
81+
SubjectPublicKey asn1.BitString
82+
}
83+
if _, err := asn1.Unmarshal(b, &spki); err != nil {
84+
return err
85+
}
86+
skid := sha1.Sum(spki.SubjectPublicKey.Bytes)
87+
c.C.SubjectKeyId = skid[:]
88+
89+
sn, err := rand.Int(rand.Reader, big.NewInt(0).Lsh(big.NewInt(1), 128))
90+
if err != nil {
91+
return err
92+
}
93+
c.C.SerialNumber = sn
94+
95+
b, err = x509.CreateCertificate(rand.Reader, c.C, ca, pub, tc.PrivateKey)
96+
if err != nil {
97+
return err
98+
}
99+
c.CertPEM = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: b})
100+
// c.KeyPEM = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(p)})
101+
b, err = x509.MarshalPKCS8PrivateKey(p)
102+
if err != nil {
103+
return err
104+
}
105+
c.KeyPEM = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: b})
106+
return nil
107+
}
108+
109+
func (c *Cert) Cert() []byte {
110+
return c.CertPEM
111+
}
112+
113+
func (c *Cert) Key() []byte {
114+
return c.KeyPEM
115+
}
116+
117+
func (c *Cert) SaveToFile(cert, key string) error {
118+
f, err := os.Create(cert)
119+
if err != nil {
120+
return err
121+
}
122+
if _, err := f.Write(c.CertPEM); err != nil {
123+
return err
124+
}
125+
if err := f.Close(); err != nil {
126+
return err
127+
}
128+
f, err = os.Create(key)
129+
if err != nil {
130+
return err
131+
}
132+
if _, err := f.Write(c.KeyPEM); err != nil {
133+
return err
134+
}
135+
if err := f.Close(); err != nil {
136+
return err
137+
}
138+
return nil
139+
}

cli/mad/build.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
if [ $# -ne 1 ]; then
4+
echo "./build.sh version"
5+
exit
6+
fi
7+
8+
mkdir _
9+
10+
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-w -s" -o _/mad_darwin_amd64
11+
CGO_ENABLED=0 GOOS=freebsd GOARCH=386 go build -ldflags="-w -s" -o _/mad_freebsd_386
12+
CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 go build -ldflags="-w -s" -o _/mad_freebsd_amd64
13+
CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags="-w -s" -o _/mad_linux_386
14+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o _/mad_linux_amd64
15+
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-w -s" -o _/mad_linux_arm64
16+
CGO_ENABLED=0 GOOS=netbsd GOARCH=386 go build -ldflags="-w -s" -o _/mad_netbsd_386
17+
CGO_ENABLED=0 GOOS=netbsd GOARCH=amd64 go build -ldflags="-w -s" -o _/mad_netbsd_amd64
18+
CGO_ENABLED=0 GOOS=openbsd GOARCH=386 go build -ldflags="-w -s" -o _/mad_openbsd_386
19+
CGO_ENABLED=0 GOOS=openbsd GOARCH=amd64 go build -ldflags="-w -s" -o _/mad_openbsd_amd64
20+
CGO_ENABLED=0 GOOS=openbsd GOARCH=arm64 go build -ldflags="-w -s" -o _/mad_openbsd_arm64
21+
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-w -s" -o _/mad_windows_amd64.exe
22+
CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags="-w -s" -o _/mad_windows_386.exe
23+
24+
mad release github.com/txthinking/mad $1 _
25+
26+
rm -rf _

0 commit comments

Comments
 (0)