@@ -74,7 +74,7 @@ type AffiliationGroup struct {
74
74
}
75
75
76
76
var (
77
- mutex = & sync.Mutex {}
77
+ mutex = & sync.RWMutex {}
78
78
caOrganization string
79
79
caCountry string
80
80
rootPath string
@@ -367,9 +367,6 @@ func (ca *CA) createCertificate(id string, pub interface{}, usage x509.KeyUsage,
367
367
}
368
368
369
369
func (ca * CA ) createCertificateFromSpec (spec * CertificateSpec , timestamp int64 , kdfKey []byte , persist bool ) ([]byte , error ) {
370
- mutex .Lock ()
371
- defer mutex .Unlock ()
372
-
373
370
Trace .Println ("Creating certificate for " + spec .GetID () + "." )
374
371
375
372
raw , err := ca .newCertificateFromSpec (spec )
@@ -386,6 +383,9 @@ func (ca *CA) createCertificateFromSpec(spec *CertificateSpec, timestamp int64,
386
383
}
387
384
388
385
func (ca * CA ) persistCertificate (id string , timestamp int64 , usage x509.KeyUsage , certRaw []byte , kdfKey []byte ) error {
386
+ mutex .Lock ()
387
+ defer mutex .Unlock ()
388
+
389
389
hash := primitives .NewHash ()
390
390
hash .Write (certRaw )
391
391
var err error
@@ -451,6 +451,9 @@ func (ca *CA) newCertificateFromSpec(spec *CertificateSpec) ([]byte, error) {
451
451
func (ca * CA ) readCertificateByKeyUsage (id string , usage x509.KeyUsage ) ([]byte , error ) {
452
452
Trace .Printf ("Reading certificate for %s and usage %v" , id , usage )
453
453
454
+ mutex .RLock ()
455
+ defer mutex .RUnlock ()
456
+
454
457
var raw []byte
455
458
err := ca .db .QueryRow ("SELECT cert FROM Certificates WHERE id=? AND usage=?" , id , usage ).Scan (& raw )
456
459
@@ -464,6 +467,9 @@ func (ca *CA) readCertificateByKeyUsage(id string, usage x509.KeyUsage) ([]byte,
464
467
func (ca * CA ) readCertificateByTimestamp (id string , ts int64 ) ([]byte , error ) {
465
468
Trace .Println ("Reading certificate for " + id + "." )
466
469
470
+ mutex .RLock ()
471
+ defer mutex .RUnlock ()
472
+
467
473
var raw []byte
468
474
err := ca .db .QueryRow ("SELECT cert FROM Certificates WHERE id=? AND timestamp=?" , id , ts ).Scan (& raw )
469
475
@@ -473,6 +479,9 @@ func (ca *CA) readCertificateByTimestamp(id string, ts int64) ([]byte, error) {
473
479
func (ca * CA ) readCertificates (id string , opt ... int64 ) (* sql.Rows , error ) {
474
480
Trace .Println ("Reading certificatess for " + id + "." )
475
481
482
+ mutex .RLock ()
483
+ defer mutex .RUnlock ()
484
+
476
485
if len (opt ) > 0 && opt [0 ] != 0 {
477
486
return ca .db .Query ("SELECT cert, kdfkey FROM Certificates WHERE id=? AND timestamp=? ORDER BY usage" , id , opt [0 ])
478
487
}
@@ -483,12 +492,18 @@ func (ca *CA) readCertificates(id string, opt ...int64) (*sql.Rows, error) {
483
492
func (ca * CA ) readCertificateSets (id string , start , end int64 ) (* sql.Rows , error ) {
484
493
Trace .Println ("Reading certificate sets for " + id + "." )
485
494
495
+ mutex .RLock ()
496
+ defer mutex .RUnlock ()
497
+
486
498
return ca .db .Query ("SELECT cert, kdfKey, timestamp FROM Certificates WHERE id=? AND timestamp BETWEEN ? AND ? ORDER BY timestamp" , id , start , end )
487
499
}
488
500
489
501
func (ca * CA ) readCertificateByHash (hash []byte ) ([]byte , error ) {
490
502
Trace .Println ("Reading certificate for hash " + string (hash ) + "." )
491
503
504
+ mutex .RLock ()
505
+ defer mutex .RUnlock ()
506
+
492
507
var raw []byte
493
508
row := ca .db .QueryRow ("SELECT cert FROM Certificates WHERE hash=?" , hash )
494
509
err := row .Scan (& raw )
@@ -499,6 +514,9 @@ func (ca *CA) readCertificateByHash(hash []byte) ([]byte, error) {
499
514
func (ca * CA ) isValidAffiliation (affiliation string ) (bool , error ) {
500
515
Trace .Println ("Validating affiliation: " + affiliation )
501
516
517
+ mutex .RLock ()
518
+ defer mutex .RUnlock ()
519
+
502
520
var count int
503
521
var err error
504
522
err = ca .db .QueryRow ("SELECT count(row) FROM AffiliationGroups WHERE name=?" , affiliation ).Scan (& count )
@@ -662,6 +680,9 @@ func (ca *CA) registerAffiliationGroup(name string, parentName string) error {
662
680
func (ca * CA ) deleteUser (id string ) error {
663
681
Trace .Println ("Deleting user " + id + "." )
664
682
683
+ mutex .Lock ()
684
+ defer mutex .Unlock ()
685
+
665
686
var row int
666
687
err := ca .db .QueryRow ("SELECT row FROM Users WHERE id=?" , id ).Scan (& row )
667
688
if err == nil {
@@ -684,6 +705,9 @@ func (ca *CA) deleteUser(id string) error {
684
705
func (ca * CA ) readUser (id string ) * sql.Row {
685
706
Trace .Println ("Reading token for " + id + "." )
686
707
708
+ mutex .RLock ()
709
+ defer mutex .RUnlock ()
710
+
687
711
return ca .db .QueryRow ("SELECT role, token, state, key, enrollmentId FROM Users WHERE id=?" , id )
688
712
}
689
713
@@ -700,6 +724,9 @@ func (ca *CA) readUsers(role int) (*sql.Rows, error) {
700
724
func (ca * CA ) readRole (id string ) int {
701
725
Trace .Println ("Reading role for " + id + "." )
702
726
727
+ mutex .RLock ()
728
+ defer mutex .RUnlock ()
729
+
703
730
var role int
704
731
ca .db .QueryRow ("SELECT role FROM Users WHERE id=?" , id ).Scan (& role )
705
732
@@ -771,6 +798,9 @@ func (ca *CA) parseEnrollID(enrollID string) (id string, role string, affiliatio
771
798
// and with metadata associated with 'newMemberMetadataStr'
772
799
// Return nil if allowed, or an error if not allowed
773
800
func (ca * CA ) canRegister (registrar string , newMemberRole string , newMemberMetadataStr string ) error {
801
+ mutex .RLock ()
802
+ defer mutex .RUnlock ()
803
+
774
804
// Read the user metadata associated with 'registrar'
775
805
var registrarMetadataStr string
776
806
err := ca .db .QueryRow ("SELECT metadata FROM Users WHERE id=?" , registrar ).Scan (& registrarMetadataStr )
0 commit comments