forked from canonical/iot-identity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organization.go
76 lines (64 loc) · 2.11 KB
/
organization.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* This file is part of the IoT Identity Service
* Copyright 2019 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License version 3, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cert
import (
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"time"
)
const (
organizationCertificateYearsLength = 10
)
var organizationCertificateDuration = time.Now().AddDate(organizationCertificateYearsLength, 0, 0)
// CreateOrganizationCert creates a signed organization certificate
func CreateOrganizationCert(certsPath, orgName string) ([]byte, []byte, error) {
// Get the parsed CA from the filesystem
caKeyPair, caTemplate, err := getCertificateAuthority(certsPath)
if err != nil {
return nil, nil, err
}
template := orgTemplate(orgName)
privateKey, cert, err := createCertificate(template, caTemplate, caKeyPair)
if err != nil {
return nil, nil, fmt.Errorf("cannot create certificate: %v", err)
}
// Create plain text PEM for certificate
certPEM := certToPEM(cert)
// Create plain text PEM for key
keyPEM := keyToPEM(privateKey)
return keyPEM, certPEM, err
}
func orgTemplate(name string) *x509.Certificate {
serial, err := randomNumber()
if err != nil {
return nil
}
// Prepare certificate
return &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
Organization: []string{name},
},
NotBefore: time.Now(),
NotAfter: organizationCertificateDuration,
SubjectKeyId: []byte{1, 2, 3},
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
}
}