-
Notifications
You must be signed in to change notification settings - Fork 91
/
serverrevoke.go
206 lines (176 loc) · 6.22 KB
/
serverrevoke.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lib
import (
"encoding/hex"
"strings"
"github.com/cloudflare/cfssl/log"
"github.com/kfsoftware/hlf-operator/internal/github.com/hyperledger/fabric-ca/api"
"github.com/kfsoftware/hlf-operator/internal/github.com/hyperledger/fabric-ca/lib/caerrors"
"github.com/kfsoftware/hlf-operator/internal/github.com/hyperledger/fabric-ca/lib/server/db"
"github.com/kfsoftware/hlf-operator/internal/github.com/hyperledger/fabric-ca/util"
"golang.org/x/crypto/ocsp"
)
type revocationResponseNet struct {
RevokedCerts []api.RevokedCert
CRL string
}
// CertificateStatus represents status of an enrollment certificate
type CertificateStatus string
const (
// Revoked is the status of a revoked certificate
Revoked CertificateStatus = "revoked"
// Good is the status of a active certificate
Good = "good"
)
func newRevokeEndpoint(s *Server) *serverEndpoint {
return &serverEndpoint{
Path: "revoke",
Methods: []string{"POST"},
Handler: revokeHandler,
Server: s,
}
}
// revocationReasonCodes is a map between string reason codes to integers as defined in RFC 5280
var revocationReasonCodes = map[string]int{
"unspecified": ocsp.Unspecified,
"keycompromise": ocsp.KeyCompromise,
"cacompromise": ocsp.CACompromise,
"affiliationchanged": ocsp.AffiliationChanged,
"superseded": ocsp.Superseded,
"cessationofoperation": ocsp.CessationOfOperation,
"certificatehold": ocsp.CertificateHold,
"removefromcrl": ocsp.RemoveFromCRL,
"privilegewithdrawn": ocsp.PrivilegeWithdrawn,
"aacompromise": ocsp.AACompromise,
}
// Handle an revoke request
func revokeHandler(ctx *serverRequestContextImpl) (interface{}, error) {
// Parse revoke request body
var req api.RevocationRequestNet
err := ctx.ReadBody(&req)
if err != nil {
return nil, err
}
// Authentication
caller, err := ctx.TokenAuthentication()
if err != nil {
return nil, err
}
// Get targeted CA
ca, err := ctx.GetCA()
if err != nil {
return nil, err
}
req.AKI = parseInput(req.AKI)
req.Serial = parseInput(req.Serial)
certDBAccessor := ca.certDBAccessor
registry := ca.registry
reason := revocationReasonCodes[req.Reason]
result := &revocationResponseNet{}
if req.Serial != "" && req.AKI != "" {
calleraki := strings.ToLower(strings.TrimLeft(hex.EncodeToString(ctx.enrollmentCert.AuthorityKeyId), "0"))
callerserial := strings.ToLower(strings.TrimLeft(util.GetSerialAsHex(ctx.enrollmentCert.SerialNumber), "0"))
certificate, err := certDBAccessor.GetCertificateWithID(req.Serial, req.AKI)
if err != nil {
return nil, caerrors.NewHTTPErr(404, caerrors.ErrRevCertNotFound, "Certificate with serial %s and AKI %s was not found: %s",
req.Serial, req.AKI, err)
}
// Authorization
err = checkAuth(caller, certificate.ID, ca)
if err != nil {
return nil, err
}
if certificate.Status == string(Revoked) {
return nil, caerrors.NewHTTPErr(404, caerrors.ErrCertAlreadyRevoked, "Certificate with serial %s and AKI %s was already revoked",
req.Serial, req.AKI)
}
if req.Name != "" && req.Name != certificate.ID {
return nil, caerrors.NewHTTPErr(400, caerrors.ErrCertWrongOwner, "Certificate with serial %s and AKI %s is not owned by %s",
req.Serial, req.AKI, req.Name)
}
userInfo, err := registry.GetUser(certificate.ID, nil)
if err != nil {
return nil, caerrors.NewHTTPErr(404, caerrors.ErrRevokeIDNotFound, "Identity %s was not found: %s", certificate.ID, err)
}
if !((req.AKI == calleraki) && (req.Serial == callerserial)) {
err = ctx.CanManageUser(userInfo)
if err != nil {
return nil, err
}
}
err = certDBAccessor.RevokeCertificate(req.Serial, req.AKI, reason)
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrRevokeFailure, "Revoke of certificate <%s,%s> failed: %s", req.Serial, req.AKI, err)
}
result.RevokedCerts = append(result.RevokedCerts, api.RevokedCert{Serial: req.Serial, AKI: req.AKI})
} else if req.Name != "" {
// Authorization
err = checkAuth(caller, req.Name, ca)
if err != nil {
return nil, err
}
user, err := registry.GetUser(req.Name, nil)
if err != nil {
return nil, caerrors.NewHTTPErr(404, caerrors.ErrRevokeIDNotFound, "Identity %s was not found: %s", req.Name, err)
}
// Set user state to -1 for revoked user
if user != nil {
caller, err := ctx.GetCaller()
if err != nil {
return nil, err
}
if caller.GetName() != user.GetName() {
err = ctx.CanManageUser(user)
if err != nil {
return nil, err
}
}
err = user.Revoke()
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrRevokeUpdateUser, "Failed to revoke user: %s", err)
}
}
var recs []db.CertRecord
recs, err = certDBAccessor.RevokeCertificatesByID(req.Name, reason)
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrNoCertsRevoked, "Failed to revoke certificates for '%s': %s",
req.Name, err)
}
if len(recs) == 0 {
log.Warningf("No certificates were revoked for '%s' but the ID was disabled", req.Name)
} else {
log.Debugf("Revoked the following certificates owned by '%s': %+v", req.Name, recs)
for _, certRec := range recs {
result.RevokedCerts = append(result.RevokedCerts, api.RevokedCert{AKI: certRec.AKI, Serial: certRec.Serial})
}
}
} else {
return nil, caerrors.NewHTTPErr(400, caerrors.ErrMissingRevokeArgs, "Either Name or Serial and AKI are required for a revoke request")
}
log.Debugf("Revoke was successful: %+v", req)
if req.GenCRL && len(result.RevokedCerts) > 0 {
log.Debugf("Generating CRL")
crl, err := genCRL(ca, api.GenCRLRequest{CAName: ca.Config.CA.Name})
if err != nil {
return nil, err
}
result.CRL = util.B64Encode(crl)
}
return result, nil
}
func parseInput(input string) string {
return strings.Replace(strings.TrimLeft(strings.ToLower(input), "0"), ":", "", -1)
}
func checkAuth(callerName, revokeUserName string, ca *CA) error {
if callerName != revokeUserName {
// Make sure that the caller has the "hf.Revoker" attribute.
err := ca.attributeIsTrue(callerName, "hf.Revoker")
if err != nil {
return caerrors.NewAuthorizationErr(caerrors.ErrNotRevoker, "Caller does not have authority to revoke")
}
}
return nil
}