forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ca.go
990 lines (815 loc) · 28 KB
/
ca.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ca
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"database/sql"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
gp "google/protobuf"
"github.com/hyperledger/fabric/core/crypto/primitives"
"github.com/hyperledger/fabric/flogging"
pb "github.com/hyperledger/fabric/membersrvc/protos"
_ "github.com/mattn/go-sqlite3" // This blank import is required to load sqlite3 driver
"github.com/op/go-logging"
"github.com/spf13/viper"
)
var caLogger = logging.MustGetLogger("ca")
// CA is the base certificate authority.
type CA struct {
db *sql.DB
path string
priv *ecdsa.PrivateKey
cert *x509.Certificate
raw []byte
}
// CertificateSpec defines the parameter used to create a new certificate.
type CertificateSpec struct {
id string
commonName string
serialNumber *big.Int
pub interface{}
usage x509.KeyUsage
NotBefore *time.Time
NotAfter *time.Time
ext *[]pkix.Extension
}
// AffiliationGroup struct
type AffiliationGroup struct {
name string
parentID int64
parent *AffiliationGroup
preKey []byte
}
var (
mutex = &sync.RWMutex{}
caOrganization string
caCountry string
rootPath string
caDir string
)
// NewCertificateSpec creates a new certificate spec
func NewCertificateSpec(id string, commonName string, serialNumber *big.Int, pub interface{}, usage x509.KeyUsage, notBefore *time.Time, notAfter *time.Time, opt ...pkix.Extension) *CertificateSpec {
spec := new(CertificateSpec)
spec.id = id
spec.commonName = commonName
spec.serialNumber = serialNumber
spec.pub = pub
spec.usage = usage
spec.NotBefore = notBefore
spec.NotAfter = notAfter
spec.ext = &opt
return spec
}
// NewDefaultPeriodCertificateSpec creates a new certificate spec with notBefore a minute ago and not after 90 days from notBefore.
//
func NewDefaultPeriodCertificateSpec(id string, serialNumber *big.Int, pub interface{}, usage x509.KeyUsage, opt ...pkix.Extension) *CertificateSpec {
return NewDefaultPeriodCertificateSpecWithCommonName(id, id, serialNumber, pub, usage, opt...)
}
// NewDefaultPeriodCertificateSpecWithCommonName creates a new certificate spec with notBefore a minute ago and not after 90 days from notBefore and a specifc commonName.
//
func NewDefaultPeriodCertificateSpecWithCommonName(id string, commonName string, serialNumber *big.Int, pub interface{}, usage x509.KeyUsage, opt ...pkix.Extension) *CertificateSpec {
notBefore := time.Now().Add(-1 * time.Minute)
notAfter := notBefore.Add(time.Hour * 24 * 90)
return NewCertificateSpec(id, commonName, serialNumber, pub, usage, ¬Before, ¬After, opt...)
}
// NewDefaultCertificateSpec creates a new certificate spec with serialNumber = 1, notBefore a minute ago and not after 90 days from notBefore.
//
func NewDefaultCertificateSpec(id string, pub interface{}, usage x509.KeyUsage, opt ...pkix.Extension) *CertificateSpec {
serialNumber := big.NewInt(1)
return NewDefaultPeriodCertificateSpec(id, serialNumber, pub, usage, opt...)
}
// NewDefaultCertificateSpecWithCommonName creates a new certificate spec with serialNumber = 1, notBefore a minute ago and not after 90 days from notBefore and a specific commonName.
//
func NewDefaultCertificateSpecWithCommonName(id string, commonName string, pub interface{}, usage x509.KeyUsage, opt ...pkix.Extension) *CertificateSpec {
serialNumber := big.NewInt(1)
return NewDefaultPeriodCertificateSpecWithCommonName(id, commonName, serialNumber, pub, usage, opt...)
}
// CacheConfiguration caches the viper configuration
func CacheConfiguration() {
caOrganization = viper.GetString("pki.ca.subject.organization")
caCountry = viper.GetString("pki.ca.subject.country")
rootPath = viper.GetString("server.rootpath")
caDir = viper.GetString("server.cadir")
}
// GetID returns the spec's ID field/value
//
func (spec *CertificateSpec) GetID() string {
return spec.id
}
// GetCommonName returns the spec's Common Name field/value
//
func (spec *CertificateSpec) GetCommonName() string {
return spec.commonName
}
// GetSerialNumber returns the spec's Serial Number field/value
//
func (spec *CertificateSpec) GetSerialNumber() *big.Int {
return spec.serialNumber
}
// GetPublicKey returns the spec's Public Key field/value
//
func (spec *CertificateSpec) GetPublicKey() interface{} {
return spec.pub
}
// GetUsage returns the spec's usage (which is the x509.KeyUsage) field/value
//
func (spec *CertificateSpec) GetUsage() x509.KeyUsage {
return spec.usage
}
// GetNotBefore returns the spec NotBefore (time.Time) field/value
//
func (spec *CertificateSpec) GetNotBefore() *time.Time {
return spec.NotBefore
}
// GetNotAfter returns the spec NotAfter (time.Time) field/value
//
func (spec *CertificateSpec) GetNotAfter() *time.Time {
return spec.NotAfter
}
// GetOrganization returns the spec's Organization field/value
//
func (spec *CertificateSpec) GetOrganization() string {
return caOrganization
}
// GetCountry returns the spec's Country field/value
//
func (spec *CertificateSpec) GetCountry() string {
return caCountry
}
// GetSubjectKeyID returns the spec's subject KeyID
//
func (spec *CertificateSpec) GetSubjectKeyID() *[]byte {
return &[]byte{1, 2, 3, 4}
}
// GetSignatureAlgorithm returns the X509.SignatureAlgorithm field/value
//
func (spec *CertificateSpec) GetSignatureAlgorithm() x509.SignatureAlgorithm {
return x509.ECDSAWithSHA384
}
// GetExtensions returns the sepc's extensions
//
func (spec *CertificateSpec) GetExtensions() *[]pkix.Extension {
return spec.ext
}
// TableInitializer is a function type for table initialization
type TableInitializer func(*sql.DB) error
func initializeCommonTables(db *sql.DB) error {
if _, err := db.Exec("CREATE TABLE IF NOT EXISTS Certificates (row INTEGER PRIMARY KEY, id VARCHAR(64), timestamp INTEGER, usage INTEGER, cert BLOB, hash BLOB, kdfkey BLOB)"); err != nil {
return err
}
if _, err := db.Exec("CREATE TABLE IF NOT EXISTS Users (row INTEGER PRIMARY KEY, id VARCHAR(64), enrollmentId VARCHAR(100), role INTEGER, metadata VARCHAR(256), token BLOB, state INTEGER, key BLOB)"); err != nil {
return err
}
if _, err := db.Exec("CREATE TABLE IF NOT EXISTS AffiliationGroups (row INTEGER PRIMARY KEY, name VARCHAR(64), parent INTEGER, FOREIGN KEY(parent) REFERENCES AffiliationGroups(row))"); err != nil {
return err
}
return nil
}
// NewCA sets up a new CA.
func NewCA(name string, initTables TableInitializer) *CA {
ca := new(CA)
flogging.LoggingInit("ca")
ca.path = filepath.Join(rootPath, caDir)
if _, err := os.Stat(ca.path); err != nil {
caLogger.Info("Fresh start; creating databases, key pairs, and certificates.")
if err := os.MkdirAll(ca.path, 0755); err != nil {
caLogger.Panic(err)
}
}
// open or create certificate database
db, err := sql.Open("sqlite3", ca.path+"/"+name+".db")
if err != nil {
caLogger.Panic(err)
}
if err = db.Ping(); err != nil {
caLogger.Panic(err)
}
if err = initTables(db); err != nil {
caLogger.Panic(err)
}
ca.db = db
// read or create signing key pair
priv, err := ca.readCAPrivateKey(name)
if err != nil {
priv = ca.createCAKeyPair(name)
}
ca.priv = priv
// read CA certificate, or create a self-signed CA certificate
raw, err := ca.readCACertificate(name)
if err != nil {
raw = ca.createCACertificate(name, &ca.priv.PublicKey)
}
cert, err := x509.ParseCertificate(raw)
if err != nil {
caLogger.Panic(err)
}
ca.raw = raw
ca.cert = cert
return ca
}
// Stop Close closes down the CA.
func (ca *CA) Stop() error {
err := ca.db.Close()
if err == nil {
caLogger.Debug("Shutting down CA - Successfully")
} else {
caLogger.Debug(fmt.Sprintf("Shutting down CA - Error closing DB [%s]", err))
}
return err
}
func (ca *CA) createCAKeyPair(name string) *ecdsa.PrivateKey {
caLogger.Debug("Creating CA key pair.")
curve := primitives.GetDefaultCurve()
priv, err := ecdsa.GenerateKey(curve, rand.Reader)
if err == nil {
raw, _ := x509.MarshalECPrivateKey(priv)
cooked := pem.EncodeToMemory(
&pem.Block{
Type: "ECDSA PRIVATE KEY",
Bytes: raw,
})
err = ioutil.WriteFile(ca.path+"/"+name+".priv", cooked, 0644)
if err != nil {
caLogger.Panic(err)
}
raw, _ = x509.MarshalPKIXPublicKey(&priv.PublicKey)
cooked = pem.EncodeToMemory(
&pem.Block{
Type: "ECDSA PUBLIC KEY",
Bytes: raw,
})
err = ioutil.WriteFile(ca.path+"/"+name+".pub", cooked, 0644)
if err != nil {
caLogger.Panic(err)
}
}
if err != nil {
caLogger.Panic(err)
}
return priv
}
func (ca *CA) readCAPrivateKey(name string) (*ecdsa.PrivateKey, error) {
caLogger.Debug("Reading CA private key.")
cooked, err := ioutil.ReadFile(ca.path + "/" + name + ".priv")
if err != nil {
return nil, err
}
block, _ := pem.Decode(cooked)
return x509.ParseECPrivateKey(block.Bytes)
}
func (ca *CA) createCACertificate(name string, pub *ecdsa.PublicKey) []byte {
caLogger.Debug("Creating CA certificate.")
raw, err := ca.newCertificate(name, pub, x509.KeyUsageDigitalSignature|x509.KeyUsageCertSign, nil)
if err != nil {
caLogger.Panic(err)
}
cooked := pem.EncodeToMemory(
&pem.Block{
Type: "CERTIFICATE",
Bytes: raw,
})
err = ioutil.WriteFile(ca.path+"/"+name+".cert", cooked, 0644)
if err != nil {
caLogger.Panic(err)
}
return raw
}
func (ca *CA) readCACertificate(name string) ([]byte, error) {
caLogger.Debug("Reading CA certificate.")
cooked, err := ioutil.ReadFile(ca.path + "/" + name + ".cert")
if err != nil {
return nil, err
}
block, _ := pem.Decode(cooked)
return block.Bytes, nil
}
func (ca *CA) createCertificate(id string, pub interface{}, usage x509.KeyUsage, timestamp int64, kdfKey []byte, opt ...pkix.Extension) ([]byte, error) {
spec := NewDefaultCertificateSpec(id, pub, usage, opt...)
return ca.createCertificateFromSpec(spec, timestamp, kdfKey, true)
}
func (ca *CA) createCertificateFromSpec(spec *CertificateSpec, timestamp int64, kdfKey []byte, persist bool) ([]byte, error) {
caLogger.Debug("Creating certificate for " + spec.GetID() + ".")
raw, err := ca.newCertificateFromSpec(spec)
if err != nil {
caLogger.Error(err)
return nil, err
}
if persist {
err = ca.persistCertificate(spec.GetID(), timestamp, spec.GetUsage(), raw, kdfKey)
}
return raw, err
}
func (ca *CA) persistCertificate(id string, timestamp int64, usage x509.KeyUsage, certRaw []byte, kdfKey []byte) error {
mutex.Lock()
defer mutex.Unlock()
hash := primitives.NewHash()
hash.Write(certRaw)
var err error
if _, err = ca.db.Exec("INSERT INTO Certificates (id, timestamp, usage, cert, hash, kdfkey) VALUES (?, ?, ?, ?, ?, ?)", id, timestamp, usage, certRaw, hash.Sum(nil), kdfKey); err != nil {
caLogger.Error(err)
}
return err
}
func (ca *CA) newCertificate(id string, pub interface{}, usage x509.KeyUsage, ext []pkix.Extension) ([]byte, error) {
spec := NewDefaultCertificateSpec(id, pub, usage, ext...)
return ca.newCertificateFromSpec(spec)
}
func (ca *CA) newCertificateFromSpec(spec *CertificateSpec) ([]byte, error) {
notBefore := spec.GetNotBefore()
notAfter := spec.GetNotAfter()
parent := ca.cert
isCA := parent == nil
tmpl := x509.Certificate{
SerialNumber: spec.GetSerialNumber(),
Subject: pkix.Name{
CommonName: spec.GetCommonName(),
Organization: []string{spec.GetOrganization()},
Country: []string{spec.GetCountry()},
},
NotBefore: *notBefore,
NotAfter: *notAfter,
SubjectKeyId: *spec.GetSubjectKeyID(),
SignatureAlgorithm: spec.GetSignatureAlgorithm(),
KeyUsage: spec.GetUsage(),
BasicConstraintsValid: true,
IsCA: isCA,
}
if len(*spec.GetExtensions()) > 0 {
tmpl.Extensions = *spec.GetExtensions()
tmpl.ExtraExtensions = *spec.GetExtensions()
}
if isCA {
parent = &tmpl
}
raw, err := x509.CreateCertificate(
rand.Reader,
&tmpl,
parent,
spec.GetPublicKey(),
ca.priv,
)
if isCA && err != nil {
caLogger.Panic(err)
}
return raw, err
}
func (ca *CA) readCertificateByKeyUsage(id string, usage x509.KeyUsage) ([]byte, error) {
caLogger.Debugf("Reading certificate for %s and usage %v", id, usage)
mutex.RLock()
defer mutex.RUnlock()
var raw []byte
err := ca.db.QueryRow("SELECT cert FROM Certificates WHERE id=? AND usage=?", id, usage).Scan(&raw)
if err != nil {
caLogger.Debugf("readCertificateByKeyUsage() Error: %v", err)
}
return raw, err
}
func (ca *CA) readCertificateByTimestamp(id string, ts int64) ([]byte, error) {
caLogger.Debug("Reading certificate for " + id + ".")
mutex.RLock()
defer mutex.RUnlock()
var raw []byte
err := ca.db.QueryRow("SELECT cert FROM Certificates WHERE id=? AND timestamp=?", id, ts).Scan(&raw)
return raw, err
}
func (ca *CA) readCertificates(id string, opt ...int64) (*sql.Rows, error) {
caLogger.Debug("Reading certificatess for " + id + ".")
mutex.RLock()
defer mutex.RUnlock()
if len(opt) > 0 && opt[0] != 0 {
return ca.db.Query("SELECT cert, kdfkey FROM Certificates WHERE id=? AND timestamp=? ORDER BY usage", id, opt[0])
}
return ca.db.Query("SELECT cert, kdfkey FROM Certificates WHERE id=?", id)
}
func (ca *CA) readCertificateSets(id string, start, end int64) (*sql.Rows, error) {
caLogger.Debug("Reading certificate sets for " + id + ".")
mutex.RLock()
defer mutex.RUnlock()
return ca.db.Query("SELECT cert, kdfKey, timestamp FROM Certificates WHERE id=? AND timestamp BETWEEN ? AND ? ORDER BY timestamp", id, start, end)
}
func (ca *CA) readCertificateByHash(hash []byte) ([]byte, error) {
caLogger.Debug("Reading certificate for hash " + string(hash) + ".")
mutex.RLock()
defer mutex.RUnlock()
var raw []byte
row := ca.db.QueryRow("SELECT cert FROM Certificates WHERE hash=?", hash)
err := row.Scan(&raw)
return raw, err
}
func (ca *CA) isValidAffiliation(affiliation string) (bool, error) {
caLogger.Debug("Validating affiliation: " + affiliation)
mutex.RLock()
defer mutex.RUnlock()
var count int
var err error
err = ca.db.QueryRow("SELECT count(row) FROM AffiliationGroups WHERE name=?", affiliation).Scan(&count)
if err != nil {
caLogger.Debug("Affiliation <" + affiliation + "> is INVALID.")
return false, err
}
caLogger.Debug("Affiliation <" + affiliation + "> is VALID.")
return count == 1, nil
}
//
// Determine if affiliation is required for a given registration request.
//
// Affiliation is required if the role is client or peer.
// Affiliation is not required if the role is validator or auditor.
// 1: client, 2: peer, 4: validator, 8: auditor
//
func (ca *CA) requireAffiliation(role pb.Role) bool {
roleStr, _ := MemberRoleToString(role)
caLogger.Debug("Assigned role is: " + roleStr + ".")
return role != pb.Role_VALIDATOR && role != pb.Role_AUDITOR
}
// validateAndGenerateEnrollID validates the affiliation subject
func (ca *CA) validateAndGenerateEnrollID(id, affiliation string, role pb.Role) (string, error) {
roleStr, _ := MemberRoleToString(role)
caLogger.Debug("Validating and generating enrollID for user id: " + id + ", affiliation: " + affiliation + ", role: " + roleStr + ".")
// Check whether the affiliation is required for the current user.
//
// Affiliation is required if the role is client or peer.
// Affiliation is not required if the role is validator or auditor.
if ca.requireAffiliation(role) {
valid, err := ca.isValidAffiliation(affiliation)
if err != nil {
return "", err
}
if !valid {
caLogger.Debug("Invalid affiliation group: ")
return "", errors.New("Invalid affiliation group " + affiliation)
}
return ca.generateEnrollID(id, affiliation)
}
return "", nil
}
// registerUser registers a new member with the CA
//
func (ca *CA) registerUser(id, affiliation string, role pb.Role, attrs []*pb.Attribute, aca *ACA, registrar, memberMetadata string, opt ...string) (string, error) {
memberMetadata = removeQuotes(memberMetadata)
roleStr, _ := MemberRoleToString(role)
caLogger.Debugf("Received request to register user with id: %s, affiliation: %s, role: %s, attrs: %+v, registrar: %s, memberMetadata: %s\n",
id, affiliation, roleStr, attrs, registrar, memberMetadata)
var enrollID, tok string
var err error
// There are two ways that registerUser can be called:
// 1) At initialization time from eca.users in the YAML file
// In this case, 'registrar' may be nil but we still register the users from the YAML file
// 2) At runtime via the GRPC ECA.RegisterUser handler (see RegisterUser in eca.go)
// In this case, 'registrar' must never be nil and furthermore the caller must have been authenticated
// to actually be the 'registrar' identity
// This means we trust what is in the YAML file but not what comes over the network
if registrar != "" {
// Check the permission of member named 'registrar' to perform this registration
err = ca.canRegister(registrar, role2String(int(role)), memberMetadata)
if err != nil {
return "", err
}
}
enrollID, err = ca.validateAndGenerateEnrollID(id, affiliation, role)
if err != nil {
return "", err
}
tok, err = ca.registerUserWithEnrollID(id, enrollID, role, memberMetadata, opt...)
if err != nil {
return "", err
}
if attrs != nil && aca != nil {
var pairs []*AttributePair
pairs, err = toAttributePairs(id, affiliation, attrs)
if err == nil {
err = aca.PopulateAttributes(pairs)
}
}
return tok, err
}
// registerUserWithEnrollID registers a new user and its enrollmentID, role and state
//
func (ca *CA) registerUserWithEnrollID(id string, enrollID string, role pb.Role, memberMetadata string, opt ...string) (string, error) {
mutex.Lock()
defer mutex.Unlock()
roleStr, _ := MemberRoleToString(role)
caLogger.Debugf("Registering user %s as %s with memberMetadata %s\n", id, roleStr, memberMetadata)
var tok string
if len(opt) > 0 && len(opt[0]) > 0 {
tok = opt[0]
} else {
tok = randomString(12)
}
var row int
err := ca.db.QueryRow("SELECT row FROM Users WHERE id=?", id).Scan(&row)
if err == nil {
return "", errors.New("User is already registered")
}
_, err = ca.db.Exec("INSERT INTO Users (id, enrollmentId, token, role, metadata, state) VALUES (?, ?, ?, ?, ?, ?)", id, enrollID, tok, role, memberMetadata, 0)
if err != nil {
caLogger.Error(err)
}
return tok, err
}
// registerAffiliationGroup registers a new affiliation group
//
func (ca *CA) registerAffiliationGroup(name string, parentName string) error {
mutex.Lock()
defer mutex.Unlock()
caLogger.Debug("Registering affiliation group " + name + " parent " + parentName + ".")
var parentID int
var err error
var count int
err = ca.db.QueryRow("SELECT count(row) FROM AffiliationGroups WHERE name=?", name).Scan(&count)
if err != nil {
return err
}
if count > 0 {
return errors.New("Affiliation group is already registered")
}
if strings.Compare(parentName, "") != 0 {
err = ca.db.QueryRow("SELECT row FROM AffiliationGroups WHERE name=?", parentName).Scan(&parentID)
if err != nil {
return err
}
}
_, err = ca.db.Exec("INSERT INTO AffiliationGroups (name, parent) VALUES (?, ?)", name, parentID)
if err != nil {
caLogger.Error(err)
}
return err
}
// deleteUser deletes a user given a name
//
func (ca *CA) deleteUser(id string) error {
caLogger.Debug("Deleting user " + id + ".")
mutex.Lock()
defer mutex.Unlock()
var row int
err := ca.db.QueryRow("SELECT row FROM Users WHERE id=?", id).Scan(&row)
if err == nil {
_, err = ca.db.Exec("DELETE FROM Certificates Where id=?", id)
if err != nil {
caLogger.Error(err)
}
_, err = ca.db.Exec("DELETE FROM Users WHERE row=?", row)
if err != nil {
caLogger.Error(err)
}
}
return err
}
// readUser reads a token given an id
//
func (ca *CA) readUser(id string) *sql.Row {
caLogger.Debug("Reading token for " + id + ".")
mutex.RLock()
defer mutex.RUnlock()
return ca.db.QueryRow("SELECT role, token, state, key, enrollmentId FROM Users WHERE id=?", id)
}
// readUsers reads users of a given Role
//
func (ca *CA) readUsers(role int) (*sql.Rows, error) {
caLogger.Debug("Reading users matching role " + strconv.FormatInt(int64(role), 2) + ".")
return ca.db.Query("SELECT id, role FROM Users WHERE role&?!=0", role)
}
// readRole returns the user Role given a user id
//
func (ca *CA) readRole(id string) int {
caLogger.Debug("Reading role for " + id + ".")
mutex.RLock()
defer mutex.RUnlock()
var role int
ca.db.QueryRow("SELECT role FROM Users WHERE id=?", id).Scan(&role)
return role
}
func (ca *CA) readAffiliationGroups() ([]*AffiliationGroup, error) {
caLogger.Debug("Reading affilition groups.")
rows, err := ca.db.Query("SELECT row, name, parent FROM AffiliationGroups")
if err != nil {
return nil, err
}
defer rows.Close()
groups := make(map[int64]*AffiliationGroup)
for rows.Next() {
group := new(AffiliationGroup)
var id int64
if e := rows.Scan(&id, &group.name, &group.parentID); e != nil {
return nil, err
}
groups[id] = group
}
groupList := make([]*AffiliationGroup, len(groups))
idx := 0
for _, eachGroup := range groups {
eachGroup.parent = groups[eachGroup.parentID]
groupList[idx] = eachGroup
idx++
}
return groupList, nil
}
func (ca *CA) generateEnrollID(id string, affiliation string) (string, error) {
if id == "" || affiliation == "" {
return "", errors.New("Please provide all the input parameters, id and role")
}
if strings.Contains(id, "\\") || strings.Contains(affiliation, "\\") {
return "", errors.New("Do not include the escape character \\ as part of the values")
}
return id + "\\" + affiliation, nil
}
func (ca *CA) parseEnrollID(enrollID string) (id string, affiliation string, err error) {
if enrollID == "" {
return "", "", errors.New("Input parameter missing")
}
enrollIDSections := strings.Split(enrollID, "\\")
if len(enrollIDSections) != 2 {
return "", "", errors.New("Either the userId or affiliation is missing from the enrollmentID. EnrollID was " + enrollID)
}
id = enrollIDSections[0]
affiliation = enrollIDSections[1]
err = nil
return
}
// Check to see if member 'registrar' can register a new member of type 'newMemberRole'
// and with metadata associated with 'newMemberMetadataStr'
// Return nil if allowed, or an error if not allowed
func (ca *CA) canRegister(registrar string, newMemberRole string, newMemberMetadataStr string) error {
mutex.RLock()
defer mutex.RUnlock()
// Read the user metadata associated with 'registrar'
var registrarMetadataStr string
err := ca.db.QueryRow("SELECT metadata FROM Users WHERE id=?", registrar).Scan(®istrarMetadataStr)
if err != nil {
caLogger.Debugf("CA.canRegister: db error: %s\n", err.Error())
return err
}
caLogger.Debugf("CA.canRegister: registrar=%s, registrarMD=%s, newMemberRole=%s, newMemberMD=%s",
registrar, registrarMetadataStr, newMemberRole, newMemberMetadataStr)
// If isn't a registrar at all, then error
if registrarMetadataStr == "" {
caLogger.Debug("canRegister: member " + registrar + " is not a registrar")
return errors.New("member " + registrar + " is not a registrar")
}
// Get the registrar's metadata
caLogger.Debug("CA.canRegister: parsing registrar's metadata")
registrarMetadata, err := newMemberMetadata(registrarMetadataStr)
if err != nil {
return err
}
// Convert the user's meta to an object
caLogger.Debug("CA.canRegister: parsing new member's metadata")
newMemberMetadata, err := newMemberMetadata(newMemberMetadataStr)
if err != nil {
return err
}
// See if the metadata to be registered is acceptable for the registrar
return registrarMetadata.canRegister(registrar, newMemberRole, newMemberMetadata)
}
// Convert a string to a MemberMetadata
func newMemberMetadata(metadata string) (*MemberMetadata, error) {
if metadata == "" {
caLogger.Debug("newMemberMetadata: nil")
return nil, nil
}
var mm MemberMetadata
err := json.Unmarshal([]byte(metadata), &mm)
if err != nil {
caLogger.Debugf("newMemberMetadata: error: %s, metadata: %s\n", err.Error(), metadata)
}
caLogger.Debugf("newMemberMetadata: metadata=%s, object=%+v\n", metadata, mm)
return &mm, err
}
// MemberMetadata Additional member metadata
type MemberMetadata struct {
Registrar Registrar `json:"registrar"`
}
// Registrar metadata
type Registrar struct {
Roles []string `json:"roles"`
DelegateRoles []string `json:"delegateRoles"`
}
// See if member 'registrar' can register a member of type 'newRole'
// with MemberMetadata of 'newMemberMetadata'
func (mm *MemberMetadata) canRegister(registrar string, newRole string, newMemberMetadata *MemberMetadata) error {
// Can register a member of this type?
caLogger.Debugf("MM.canRegister registrar=%s, newRole=%s\n", registrar, newRole)
if !strContained(newRole, mm.Registrar.Roles) {
caLogger.Debugf("MM.canRegister: role %s can't be registered by %s\n", newRole, registrar)
return errors.New("member " + registrar + " may not register member of type " + newRole)
}
// The registrar privileges that are being registered must not be larger than the registrar's
if newMemberMetadata == nil {
// Not requesting registrar privileges for this member, so we are OK
caLogger.Debug("MM.canRegister: not requesting registrar privileges")
return nil
}
// Make sure this registrar is not delegating an invalid role
err := checkDelegateRoles(newMemberMetadata.Registrar.Roles, mm.Registrar.DelegateRoles, registrar)
if err != nil {
caLogger.Debug("MM.canRegister: checkDelegateRoles failure")
return err
}
// Can register OK
caLogger.Debug("MM.canRegister: OK")
return nil
}
// Return an error if all strings in 'strs1' are not contained in 'strs2'
func checkDelegateRoles(strs1 []string, strs2 []string, registrar string) error {
caLogger.Debugf("CA.checkDelegateRoles: registrar=%s, strs1=%+v, strs2=%+v\n", registrar, strs1, strs2)
for _, s := range strs1 {
if !strContained(s, strs2) {
caLogger.Debugf("CA.checkDelegateRoles: no: %s not in %+v\n", s, strs2)
return errors.New("user " + registrar + " may not register delegateRoles " + s)
}
}
caLogger.Debug("CA.checkDelegateRoles: ok")
return nil
}
// Return true if 'str' is in 'strs'; otherwise return false
func strContained(str string, strs []string) bool {
for _, s := range strs {
if s == str {
return true
}
}
return false
}
// Return true if 'str' is prefixed by any string in 'strs'; otherwise return false
func isPrefixed(str string, strs []string) bool {
for _, s := range strs {
if strings.HasPrefix(str, s) {
return true
}
}
return false
}
// convert a role to a string
func role2String(role int) string {
if role == int(pb.Role_CLIENT) {
return "client"
} else if role == int(pb.Role_PEER) {
return "peer"
} else if role == int(pb.Role_VALIDATOR) {
return "validator"
} else if role == int(pb.Role_AUDITOR) {
return "auditor"
}
return ""
}
// Remove outer quotes from a string if necessary
func removeQuotes(str string) string {
if str == "" {
return str
}
if (strings.HasPrefix(str, "'") && strings.HasSuffix(str, "'")) ||
(strings.HasPrefix(str, "\"") && strings.HasSuffix(str, "\"")) {
str = str[1 : len(str)-1]
}
caLogger.Debugf("removeQuotes: %s\n", str)
return str
}
// Convert the protobuf array of attributes to the AttributePair array format
// as required by the ACA code to populate the table
func toAttributePairs(id, affiliation string, attrs []*pb.Attribute) ([]*AttributePair, error) {
var pairs = make([]*AttributePair, 0)
for _, attr := range attrs {
vals := []string{id, affiliation, attr.Name, attr.Value, attr.NotBefore, attr.NotAfter}
pair, err := NewAttributePair(vals, nil)
if err != nil {
return nil, err
}
pairs = append(pairs, pair)
}
caLogger.Debugf("toAttributePairs: id=%s, affiliation=%s, attrs=%v, pairs=%v\n",
id, affiliation, attrs, pairs)
return pairs, nil
}
func convertTime(ts *gp.Timestamp) time.Time {
var t time.Time
if ts == nil {
t = time.Unix(0, 0).UTC()
} else {
t = time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
}
return t
}