-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathcertificate.go
949 lines (835 loc) · 28.5 KB
/
certificate.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
package database
import (
"crypto/x509"
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/lib/pq"
"github.com/mozilla/tls-observatory/certificate"
)
// InsertCertificate inserts a x509 certificate to the database.
// It takes as input a Certificate pointer.
// It returns the database ID of the inserted certificate ( -1 if an error occurs ) and an error, if it occurs.
func (db *DB) InsertCertificate(cert *certificate.Certificate) (int64, error) {
var id int64
crl_dist_points, err := json.Marshal(cert.X509v3Extensions.CRLDistributionPoints)
if err != nil {
return -1, err
}
extkeyusage, err := json.Marshal(cert.X509v3Extensions.ExtendedKeyUsage)
if err != nil {
return -1, err
}
extKeyUsageOID, err := json.Marshal(cert.X509v3Extensions.ExtendedKeyUsageOID)
if err != nil {
return -1, err
}
keyusage, err := json.Marshal(cert.X509v3Extensions.KeyUsage)
if err != nil {
return -1, err
}
subaltname, err := json.Marshal(cert.X509v3Extensions.SubjectAlternativeName)
if err != nil {
return -1, err
}
policies, err := json.Marshal(cert.X509v3Extensions.PolicyIdentifiers)
if err != nil {
return -1, err
}
issuer, err := json.Marshal(cert.Issuer)
if err != nil {
return -1, err
}
subject, err := json.Marshal(cert.Subject)
if err != nil {
return -1, err
}
key, err := json.Marshal(cert.Key)
if err != nil {
return -1, err
}
mozPolicy, err := json.Marshal(cert.MozillaPolicyV2_5)
if err != nil {
return -1, err
}
domainstr := ""
if !cert.CA {
domainfound := false
for _, d := range cert.X509v3Extensions.SubjectAlternativeName {
if d == cert.Subject.CommonName {
domainfound = true
}
}
var domains []string
if !domainfound {
domains = append(cert.X509v3Extensions.SubjectAlternativeName, cert.Subject.CommonName)
} else {
domains = cert.X509v3Extensions.SubjectAlternativeName
}
domainstr = strings.Join(domains, ",")
}
// We want to store an empty array, not NULL
if cert.X509v3Extensions.PermittedDNSDomains == nil {
cert.X509v3Extensions.PermittedDNSDomains = make([]string, 0)
}
if cert.X509v3Extensions.ExcludedDNSDomains == nil {
cert.X509v3Extensions.ExcludedDNSDomains = make([]string, 0)
}
err = db.QueryRow(`INSERT INTO certificates(
serial_number,
sha1_fingerprint,
sha256_fingerprint,
sha256_spki,
sha256_subject_spki,
pkp_sha256,
issuer,
subject,
version,
is_ca,
not_valid_before,
not_valid_after,
first_seen,
last_seen,
key_alg,
key,
x509_basicConstraints,
x509_crlDistributionPoints,
x509_extendedKeyUsage,
x509_extendedKeyUsageOID,
x509_authorityKeyIdentifier,
x509_subjectKeyIdentifier,
x509_keyUsage,
x509_subjectAltName,
x509_certificatePolicies,
signature_algo,
domains,
raw_cert,
permitted_dns_domains,
permitted_ip_addresses,
excluded_dns_domains,
excluded_ip_addresses,
is_technically_constrained,
cisco_umbrella_rank,
mozillaPolicyV2_5
) VALUES ($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)
RETURNING id`,
cert.Serial,
cert.Hashes.SHA1,
cert.Hashes.SHA256,
cert.Hashes.SPKISHA256,
cert.Hashes.SubjectSPKISHA256,
cert.Hashes.PKPSHA256,
issuer,
subject,
cert.Version,
cert.CA,
cert.Validity.NotBefore,
cert.Validity.NotAfter,
cert.FirstSeenTimestamp,
cert.LastSeenTimestamp,
cert.Key.Alg,
key,
cert.X509v3BasicConstraints,
crl_dist_points,
extkeyusage,
extKeyUsageOID,
cert.X509v3Extensions.AuthorityKeyId,
cert.X509v3Extensions.SubjectKeyId,
keyusage,
subaltname,
policies,
cert.SignatureAlgorithm,
domainstr,
cert.Raw,
pq.Array(cert.X509v3Extensions.PermittedDNSDomains),
pq.Array(cert.X509v3Extensions.PermittedIPAddresses),
pq.Array(cert.X509v3Extensions.ExcludedDNSDomains),
pq.Array(cert.X509v3Extensions.ExcludedIPAddresses),
cert.X509v3Extensions.IsTechnicallyConstrained,
cert.CiscoUmbrellaRank,
mozPolicy,
).Scan(&id)
if err != nil {
return -1, err
}
if db.metricsSender != nil {
db.metricsSender.NewCertificate()
}
return id, nil
}
// UpdateCertificate updates a x509 certificate in the database.
// It takes as input a Certificate pointer, and returns an error
func (db *DB) UpdateCertificate(cert *certificate.Certificate) error {
crl_dist_points, err := json.Marshal(cert.X509v3Extensions.CRLDistributionPoints)
if err != nil {
return err
}
extkeyusage, err := json.Marshal(cert.X509v3Extensions.ExtendedKeyUsage)
if err != nil {
return err
}
keyusage, err := json.Marshal(cert.X509v3Extensions.KeyUsage)
if err != nil {
return err
}
extKeyUsageOID, err := json.Marshal(cert.X509v3Extensions.ExtendedKeyUsageOID)
if err != nil {
return err
}
subaltname, err := json.Marshal(cert.X509v3Extensions.SubjectAlternativeName)
if err != nil {
return err
}
policies, err := json.Marshal(cert.X509v3Extensions.PolicyIdentifiers)
if err != nil {
return err
}
issuer, err := json.Marshal(cert.Issuer)
if err != nil {
return err
}
subject, err := json.Marshal(cert.Subject)
if err != nil {
return err
}
key, err := json.Marshal(cert.Key)
if err != nil {
return err
}
mozPolicy, err := json.Marshal(cert.MozillaPolicyV2_5)
if err != nil {
return err
}
domainstr := ""
if !cert.CA {
domainfound := false
for _, d := range cert.X509v3Extensions.SubjectAlternativeName {
if d == cert.Subject.CommonName {
domainfound = true
}
}
var domains []string
if !domainfound {
domains = append(cert.X509v3Extensions.SubjectAlternativeName, cert.Subject.CommonName)
} else {
domains = cert.X509v3Extensions.SubjectAlternativeName
}
domainstr = strings.Join(domains, ",")
}
// We want to store an empty array, not NULL
if cert.X509v3Extensions.PermittedDNSDomains == nil {
cert.X509v3Extensions.PermittedDNSDomains = make([]string, 0)
}
if cert.X509v3Extensions.ExcludedDNSDomains == nil {
cert.X509v3Extensions.ExcludedDNSDomains = make([]string, 0)
}
_, err = db.Exec(`UPDATE certificates SET (
serial_number,
sha1_fingerprint,
sha256_fingerprint,
sha256_spki,
sha256_subject_spki,
pkp_sha256,
issuer,
subject,
version,
is_ca,
not_valid_before,
not_valid_after,
first_seen,
last_seen,
key_alg,
key,
x509_basicConstraints,
x509_crlDistributionPoints,
x509_extendedKeyUsage,
x509_extendedKeyUsageOID,
x509_authorityKeyIdentifier,
x509_subjectKeyIdentifier,
x509_keyUsage,
x509_subjectAltName,
x509_certificatePolicies,
signature_algo,
domains,
raw_cert,
permitted_dns_domains,
permitted_ip_addresses,
excluded_dns_domains,
excluded_ip_addresses,
is_technically_constrained,
cisco_umbrella_rank,
mozillaPolicyV2_5
) VALUES ($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)
WHERE id=$35
`,
cert.Serial,
cert.Hashes.SHA1,
cert.Hashes.SHA256,
cert.Hashes.SPKISHA256,
cert.Hashes.SubjectSPKISHA256,
cert.Hashes.PKPSHA256,
issuer,
subject,
cert.Version,
cert.CA,
cert.Validity.NotBefore,
cert.Validity.NotAfter,
cert.FirstSeenTimestamp,
cert.LastSeenTimestamp,
cert.Key.Alg,
key,
cert.X509v3BasicConstraints,
crl_dist_points,
extkeyusage,
extKeyUsageOID,
cert.X509v3Extensions.AuthorityKeyId,
cert.X509v3Extensions.SubjectKeyId,
keyusage,
subaltname,
policies,
cert.SignatureAlgorithm,
domainstr,
cert.Raw,
pq.Array(cert.X509v3Extensions.PermittedDNSDomains),
pq.Array(cert.X509v3Extensions.PermittedIPAddresses),
pq.Array(cert.X509v3Extensions.ExcludedDNSDomains),
pq.Array(cert.X509v3Extensions.ExcludedIPAddresses),
cert.X509v3Extensions.IsTechnicallyConstrained,
cert.CiscoUmbrellaRank,
mozPolicy,
cert.ID,
)
return err
}
// UpdateCertificateRank updates the rank integer of the input certificate.
func (db *DB) UpdateCertificateRank(id, rank int64) error {
_, err := db.Exec("UPDATE certificates SET cisco_umbrella_rank=$1 WHERE id=$2", rank, id)
return err
}
// UpdateCertLastSeen updates the last_seen timestamp of the input certificate.
// Outputs an error if it occurs.
func (db *DB) UpdateCertLastSeen(cert *certificate.Certificate) error {
_, err := db.Exec("UPDATE certificates SET last_seen=$1 WHERE sha1_fingerprint=$2", cert.LastSeenTimestamp, cert.Hashes.SHA1)
return err
}
// UpdateCertLastSeenByID updates the last_seen timestamp of the certificate with the given id.
// Outputs an error if it occurs.
func (db *DB) UpdateCertLastSeenByID(id int64) error {
_, err := db.Exec("UPDATE certificates SET last_seen=$1 WHERE id=$2", time.Now(), id)
return err
}
// UpdateCertsLastSeenByID updates the last_seen timestamp for certificates with the given id.
// Outputs an error if it occurs.
func (db *DB) UpdateCertsLastSeenByID(ids []int64) error {
_, err := db.Exec("UPDATE certificates SET last_seen=$1 WHERE id = ANY($2)", time.Now(), pq.Array(ids))
return err
}
func (db *DB) UpdateCertMarkAsRevoked(id int64, when time.Time) error {
_, err := db.Exec("UPDATE certificates SET is_revoked=true, revoked_at=$2 WHERE id=$1", id, when)
return err
}
func (db *DB) AddCertToUbuntuTruststore(id int64) error {
_, err := db.Exec(`UPDATE certificates SET in_ubuntu_root_store='true',last_seen=NOW() WHERE id=$1`, id)
return err
}
func (db *DB) AddCertToMozillaTruststore(id int64) error {
_, err := db.Exec(`UPDATE certificates SET in_mozilla_root_store='true',last_seen=NOW() WHERE id=$1`, id)
return err
}
func (db *DB) AddCertToMicrosoftTruststore(id int64) error {
_, err := db.Exec(`UPDATE certificates SET in_microsoft_root_store='true',last_seen=NOW() WHERE id=$1`, id)
return err
}
func (db *DB) AddCertToAppleTruststore(id int64) error {
_, err := db.Exec(`UPDATE certificates SET in_apple_root_store='true',last_seen=NOW() WHERE id=$1`, id)
return err
}
func (db *DB) AddCertToAndroidTruststore(id int64) error {
_, err := db.Exec(`UPDATE certificates SET in_android_root_store='true',last_seen=NOW() WHERE id=$1`, id)
return err
}
// RemoveCACertFromTruststore takes a list of hashes from certs trusted by a given truststore and disables
// the trust of all certs not listed but trusted in DB
func (db *DB) RemoveCACertFromTruststore(trustedCerts []string, tsName string) error {
if len(trustedCerts) == 0 {
return errors.New("Cannot work with empty list of trusted certs")
}
tsVariable := ""
switch tsName {
case certificate.Ubuntu_TS_name:
tsVariable = "in_ubuntu_root_store"
case certificate.Mozilla_TS_name:
tsVariable = "in_mozilla_root_store"
case certificate.Microsoft_TS_name:
tsVariable = "in_microsoft_root_store"
case certificate.Apple_TS_name:
tsVariable = "in_apple_root_store"
case certificate.Android_TS_name:
tsVariable = "in_android_root_store"
default:
return errors.New(fmt.Sprintf("Cannot update DB, %s does not represent a valid truststore name.", tsName))
}
var fps string
for _, fp := range trustedCerts {
if len(fps) > 1 {
fps += ","
}
fps += "'" + fp + "'"
}
q := fmt.Sprintf(`UPDATE certificates SET %s='false', last_seen=NOW() WHERE %s='true' AND sha256_fingerprint NOT IN (%s)`,
tsVariable, tsVariable, fps)
_, err := db.Exec(q)
return err
}
// GetCertIDWithSHA1Fingerprint fetches the database id of the certificate with the given SHA1 fingerprint.
// Returns the mentioned id and any errors that happen.
// It wraps the sql.ErrNoRows error in order to avoid passing not existing row errors to upper levels.
// In that case it returns -1 with no error.
func (db *DB) GetCertIDBySHA1Fingerprint(sha1 string) (id int64, err error) {
id = -1
err = db.QueryRow(`SELECT id
FROM certificates
WHERE sha1_fingerprint=upper($1)
ORDER BY id ASC LIMIT 1`,
sha1).Scan(&id)
if err == sql.ErrNoRows {
return -1, nil
}
return
}
// GetCertIDWithSHA256Fingerprint fetches the database id of the certificate with the given SHA256 fingerprint.
// Returns the mentioned id and any errors that happen.
// It wraps the sql.ErrNoRows error in order to avoid passing not existing row errors to upper levels.
// In that case it returns -1 with no error.
func (db *DB) GetCertIDBySHA256Fingerprint(sha256 string) (id int64, err error) {
id = -1
err = db.QueryRow(`SELECT id
FROM certificates
WHERE sha256_fingerprint=upper($1)
ORDER BY id ASC LIMIT 1`,
strings.ToUpper(sha256)).Scan(&id)
if err == sql.ErrNoRows {
return -1, nil
}
return
}
// GetCertIDFromTrust fetches the database id of the certificate in the trust relation with the given id.
// Returns the mentioned id and any errors that happen.
// It wraps the sql.ErrNoRows error in order to avoid passing not existing row errors to upper levels.
// In that case it returns -1 with no error.
func (db *DB) GetCertIDFromTrust(trustID int64) (id int64, err error) {
id = -1
err = db.QueryRow("SELECT cert_id FROM trust WHERE id=$1", trustID).Scan(&id)
if err == sql.ErrNoRows {
return -1, nil
}
return
}
type Scannable interface {
Scan(dest ...interface{}) error
}
func (db *DB) scanCert(row Scannable) (certificate.Certificate, error) {
cert := certificate.Certificate{}
var crl_dist_points, extkeyusage, extKeyUsageOID, keyusage, subaltname, policies, issuer, subject, key, mozPolicy []byte
// smooth rollout: store in an interface and convert to string if not nil
var stubSubjectSPKI interface{}
err := row.Scan(&cert.ID, &cert.Serial, &cert.Hashes.SHA1, &cert.Hashes.SHA256, &cert.Hashes.SPKISHA256, &stubSubjectSPKI, &cert.Hashes.PKPSHA256,
&issuer, &subject,
&cert.Version, &cert.CA, &cert.Validity.NotBefore, &cert.Validity.NotAfter, &key, &cert.FirstSeenTimestamp,
&cert.LastSeenTimestamp, &cert.X509v3BasicConstraints, &crl_dist_points, &extkeyusage, &extKeyUsageOID, &cert.X509v3Extensions.AuthorityKeyId,
&cert.X509v3Extensions.SubjectKeyId, &keyusage, &subaltname, &policies,
&cert.SignatureAlgorithm, &cert.Raw,
pq.Array(&cert.X509v3Extensions.PermittedDNSDomains),
pq.Array(&cert.X509v3Extensions.PermittedIPAddresses),
pq.Array(&cert.X509v3Extensions.ExcludedDNSDomains),
pq.Array(&cert.X509v3Extensions.ExcludedIPAddresses),
&cert.X509v3Extensions.IsTechnicallyConstrained,
&cert.CiscoUmbrellaRank, &mozPolicy,
)
if err != nil {
return cert, err
}
// smooth rollout: this can be removed once the entire certificate table has been updated
if stubSubjectSPKI != nil {
cert.Hashes.SubjectSPKISHA256 = stubSubjectSPKI.(string)
}
err = json.Unmarshal(crl_dist_points, &cert.X509v3Extensions.CRLDistributionPoints)
if err != nil {
return cert, err
}
err = json.Unmarshal(extkeyusage, &cert.X509v3Extensions.ExtendedKeyUsage)
if err != nil {
return cert, err
}
// this construct handles columns that do not yet have a value (NULL)
// it is used to add new columns without breaking existing certs in db
if extKeyUsageOID == nil {
cert.X509v3Extensions.ExtendedKeyUsageOID = make([]string, 0)
} else {
err = json.Unmarshal(extKeyUsageOID, &cert.X509v3Extensions.ExtendedKeyUsageOID)
if err != nil {
return cert, err
}
}
err = json.Unmarshal(keyusage, &cert.X509v3Extensions.KeyUsage)
if err != nil {
return cert, err
}
err = json.Unmarshal(subaltname, &cert.X509v3Extensions.SubjectAlternativeName)
if err != nil {
return cert, err
}
err = json.Unmarshal(policies, &cert.X509v3Extensions.PolicyIdentifiers)
if err != nil {
return cert, err
}
err = json.Unmarshal(issuer, &cert.Issuer)
if err != nil {
return cert, err
}
err = json.Unmarshal(subject, &cert.Subject)
if err != nil {
return cert, err
}
err = json.Unmarshal(key, &cert.Key)
if err != nil {
return cert, err
}
// added on release 1.3.3 and not yet present everywhere
if mozPolicy != nil {
err = json.Unmarshal(mozPolicy, &cert.MozillaPolicyV2_5)
if err != nil {
return cert, err
}
}
cert.ValidationInfo, cert.Issuer.ID, err = db.GetValidationMapForCert(cert.ID)
return cert, err
}
// GetCertByID fetches a certain certificate from the database.
// It returns a pointer to a Certificate struct and any errors that occur.
func (db *DB) GetCertByID(certID int64) (*certificate.Certificate, error) {
row := db.QueryRow(`SELECT `+strings.Join(allCertificateColumns, ", ")+`
FROM certificates WHERE id=$1`, certID)
cert, err := db.scanCert(row)
return &cert, err
}
var ErrInvalidCertStore = fmt.Errorf("Invalid certificate store provided")
func (db *DB) GetAllCertsInStore(store string) (out []certificate.Certificate, err error) {
switch store {
case
"mozilla",
"android",
"apple",
"microsoft",
"ubuntu":
query := fmt.Sprintf(`SELECT `+strings.Join(allCertificateColumns, ", ")+`
FROM certificates WHERE in_%s_root_store=true`, store)
rows, err := db.Query(query)
if err != nil {
return nil, err
}
for rows.Next() {
cert, err := db.scanCert(rows)
if err != nil {
return nil, err
}
out = append(out, cert)
}
return out, err
default:
return nil, ErrInvalidCertStore
}
}
// GetEECountForIssuerByID gets the count of valid end entity certificates in the
// database that chain to the certificate with the specified ID
func (db *DB) GetEECountForIssuerByID(certID int64) (count int64, err error) {
count = -1
err = db.QueryRow(`
WITH RECURSIVE issued_by(cert_id, issuer_id) AS (
SELECT DISTINCT cert_id, issuer_id
FROM trust
WHERE issuer_id = $1
AND cert_id != issuer_id
UNION ALL
SELECT trust.cert_id, trust.issuer_id
FROM trust
JOIN issued_by
ON (trust.issuer_id = issued_by.cert_id)
)
SELECT count(id) FROM certificates WHERE id IN (
SELECT DISTINCT cert_id FROM issued_by
) AND is_ca = false
AND not_valid_after > NOW()
AND not_valid_before < NOW()`, certID).Scan(&count)
return
}
// GetCertBySHA1Fingerprint fetches a certain certificate from the database.
// It returns a pointer to a Certificate struct and any errors that occur.
func (db *DB) GetCertBySHA1Fingerprint(sha1 string) (*certificate.Certificate, error) {
var id int64 = -1
cert := &certificate.Certificate{}
err := db.QueryRow(`SELECT id FROM certificates WHERE sha1_fingerprint=$1`, sha1).Scan(&id)
if err == sql.ErrNoRows {
return cert, err
}
return db.GetCertByID(id)
}
// GetCACertsBySubject returns a list of CA certificates that match a given subject
func (db *DB) GetCACertsBySubject(subject certificate.Subject) (certs []*certificate.Certificate, err error) {
// we must remove the ID before looking for the cert in database
subject.ID = 0
subjectJson, err := json.Marshal(subject)
if err != nil {
return
}
rows, err := db.Query(`SELECT `+strings.Join(allCertificateColumns, ", ")+`
FROM certificates WHERE is_ca='true' AND subject=$1`, subjectJson)
if rows != nil {
defer rows.Close()
}
if err == sql.ErrNoRows {
return
}
if err != nil && err != sql.ErrNoRows {
err = fmt.Errorf("Error while getting certificates by subject: '%v'", err)
return
}
for rows.Next() {
var (
cert certificate.Certificate
)
cert, err = db.scanCert(rows)
if err != nil {
return
}
certs = append(certs, &cert)
}
if err := rows.Err(); err != nil {
err = fmt.Errorf("Failed to complete database query: '%v'", err)
}
return
}
func (db *DB) InsertTrustToDB(cert certificate.Certificate, certID, parID int64) (int64, error) {
var trustID int64
trusted_ubuntu, trusted_mozilla, trusted_microsoft, trusted_apple, trusted_android := cert.GetBooleanValidity()
err := db.QueryRow(`INSERT INTO trust(cert_id,issuer_id,timestamp,trusted_ubuntu,trusted_mozilla,trusted_microsoft,trusted_apple,trusted_android,is_current)
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING id`, certID, parID, time.Now(), trusted_ubuntu, trusted_mozilla, trusted_microsoft, trusted_apple, trusted_android, true).Scan(&trustID)
if err != nil {
return -1, err
}
if db.metricsSender != nil {
db.metricsSender.NewTrustRelation()
}
return trustID, nil
}
func (db *DB) UpdateTrust(trustID int64, cert certificate.Certificate) (int64, error) {
var trusted_ubuntu, trusted_mozilla, trusted_microsoft, trusted_apple, trusted_android bool
var certID, parID int64
err := db.QueryRow(`SELECT cert_id, issuer_id, trusted_ubuntu, trusted_mozilla, trusted_microsoft, trusted_apple, trusted_android FROM trust WHERE id=$1 AND is_current=TRUE`,
trustID).Scan(&certID, &parID, &trusted_ubuntu, &trusted_mozilla, &trusted_microsoft, &trusted_apple, &trusted_android)
if err != nil {
return -1, err
}
new_ubuntu, new_mozilla, new_microsoft, new_apple, new_android := cert.GetBooleanValidity()
isTrustCurrent := true
if trusted_ubuntu != new_ubuntu || trusted_mozilla != new_mozilla || trusted_microsoft != new_microsoft || trusted_apple != new_apple || trusted_android != new_android {
isTrustCurrent = false
}
if !isTrustCurrent { // create new trust and obsolete old one
newID, err := db.InsertTrustToDB(cert, certID, parID)
if err != nil {
return -1, err
}
_, err = db.Exec("UPDATE trust SET is_current=$1 WHERE id=$2", false, trustID)
if err != nil {
return -1, err
}
return newID, nil
} else { //update current timestamp
_, err = db.Exec("UPDATE trust SET timestamp=$1 WHERE id=$2", time.Now(), trustID)
return trustID, err
}
}
func (db *DB) GetCurrentTrustID(certID, issuerID int64) (int64, error) {
var trustID int64
row := db.QueryRow("SELECT id FROM trust WHERE cert_id=$1 AND issuer_id=$2 AND is_current=TRUE", certID, issuerID)
err := row.Scan(&trustID)
if err != nil {
if err == sql.ErrNoRows {
return -1, nil
} else {
return -1, err
}
}
return trustID, nil
}
func (db *DB) GetCurrentTrustIDForCert(certID int64) (int64, error) {
var trustID int64
row := db.QueryRow("SELECT id FROM trust WHERE cert_id=$1 AND is_current=TRUE", certID)
err := row.Scan(&trustID)
if err != nil {
if err == sql.ErrNoRows {
return -1, nil
} else {
return -1, err
}
}
return trustID, nil
}
func (db *DB) GetValidationMapForCert(certID int64) (map[string]certificate.ValidationInfo, int64, error) {
var (
ubuntu, mozilla, microsoft, apple, android bool
issuerId int64
)
m := make(map[string]certificate.ValidationInfo)
row := db.QueryRow(`SELECT
trusted_ubuntu,
trusted_mozilla,
trusted_microsoft,
trusted_apple,
trusted_android,
issuer_id
FROM trust
WHERE cert_id=$1 AND is_current=TRUE`,
certID)
err := row.Scan(&ubuntu, &mozilla, µsoft, &apple, &android, &issuerId)
if err != nil {
if err == sql.ErrNoRows {
return m, 0, nil
} else {
return m, 0, err
}
}
return certificate.GetValidityMap(ubuntu, mozilla, microsoft, apple, android), issuerId, nil
}
// GetCertPaths returns the various certificates paths from the current cert to roots.
// It takes a certificate as argument that will be used as the start of the path.
func (db *DB) GetCertPaths(cert *certificate.Certificate) (paths certificate.Paths, err error) {
// check if we have a path in the cache
if paths, ok := db.paths.Get(cert.Issuer.String()); ok {
// the end-entity in the cache is likely another cert, so replace
// it with the current one
newpaths := paths.(certificate.Paths)
newpaths.Cert = cert
return newpaths, nil
}
// nothing in the cache, go to the database, recursively
var ancestors []string
paths, err = db.getCertPaths(cert, ancestors)
if err != nil {
return
}
// add the path into the cache
db.paths.Add(cert.Issuer.String(), paths)
return
}
func (db *DB) getCertPaths(cert *certificate.Certificate, ancestors []string) (paths certificate.Paths, err error) {
xcert, err := cert.ToX509()
if err != nil {
return
}
paths.Cert = cert
ancestors = append(ancestors, cert.Hashes.SPKISHA256)
parents, err := db.GetCACertsBySubject(cert.Issuer)
if err != nil {
return
}
for _, parent := range parents {
var (
curPath certificate.Paths
xparent *x509.Certificate
)
curPath.Cert = parent
if parent.Validity.NotAfter.Before(time.Now()) || parent.Validity.NotBefore.After(time.Now()) {
// certificate is not valid, skip it
continue
}
xparent, err = parent.ToX509()
if err != nil {
return
}
// verify the parent signed the cert, or skip it
if xcert.CheckSignatureFrom(xparent) != nil {
continue
}
// if the parent is self-signed, we have a root, no need to go deeper
if parent.IsSelfSigned() {
paths.Parents = append(paths.Parents, curPath)
continue
}
isLooping := false
for _, ancestor := range ancestors {
if ancestor == parent.Hashes.SPKISHA256 {
isLooping = true
}
}
if isLooping {
paths.Parents = append(paths.Parents, curPath)
continue
}
// if the parent is not self signed, we grab its own parents
curPath, err := db.getCertPaths(parent, ancestors)
if err != nil {
continue
}
paths.Parents = append(paths.Parents, curPath)
}
return
}
// IsTrustValid returns the validity of the trust relationship for the given id.
// It returns a "valid" if any of the per truststore valitities is valid
// It returns a boolean that represent if trust is valid or not.
func (db *DB) IsTrustValid(id int64) (bool, error) {
row := db.QueryRow(`SELECT trusted_ubuntu OR
trusted_mozilla OR
trusted_microsoft OR
trusted_apple OR
trusted_android
FROM trust WHERE id=$1`, id)
isValid := false
err := row.Scan(&isValid)
return isValid, err
}
var allCertificateColumns = []string{
"id",
"serial_number",
"sha1_fingerprint",
"sha256_fingerprint",
"sha256_spki",
"sha256_subject_spki",
"pkp_sha256",
"issuer",
"subject",
"version",
"is_ca",
"not_valid_before",
"not_valid_after",
"key",
"first_seen",
"last_seen",
"x509_basicConstraints",
"x509_crlDistributionPoints",
"x509_extendedKeyUsage",
"x509_extendedKeyUsageOID",
"x509_authorityKeyIdentifier",
"x509_subjectKeyIdentifier",
"x509_keyUsage",
"x509_subjectAltName",
"x509_certificatePolicies",
"signature_algo",
"raw_cert",
"permitted_dns_domains",
"permitted_ip_addresses",
"excluded_dns_domains",
"excluded_ip_addresses",
"is_technically_constrained",
"cisco_umbrella_rank",
"mozillaPolicyV2_5",
}