cloudflare/cfssl_trust
Publicmirrored fromhttps://github.com/cloudflare/cfssl_trustAvailable
dump/dump.go
50lines · modecode
| 1 | // Package dump contains functions for extracting single certificates |
| 2 | // from the database. |
| 3 | package dump |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "crypto/x509" |
| 8 | "database/sql" |
| 9 | "encoding/pem" |
| 10 | ) |
| 11 | |
| 12 | // CertPEM returns a slice of certificates for the given SKI. In most |
| 13 | // cases, this will be a single certificate (as SKIs tend to be |
| 14 | // unique); according to the RFC, they only need to be unique for a |
| 15 | // given signer, and therefore there is a chance that there will be |
| 16 | // multiple certificates with the same SKI. |
| 17 | func CertPEM(tx *sql.Tx, ski string) ([]byte, error) { |
| 18 | rows, err := tx.Query("SELECT raw FROM certificates WHERE ski = ?", ski) |
| 19 | if err != nil { |
| 20 | return nil, err |
| 21 | } |
| 22 | |
| 23 | buf := &bytes.Buffer{} |
| 24 | |
| 25 | for rows.Next() { |
| 26 | var raw []byte |
| 27 | err = rows.Scan(&raw) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | |
| 32 | // Make sure it's actually a valid certificate. |
| 33 | cert, err := x509.ParseCertificate(raw) |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | |
| 38 | p := &pem.Block{ |
| 39 | Type: "CERTIFICATE", |
| 40 | Bytes: cert.Raw, |
| 41 | } |
| 42 | |
| 43 | err = pem.Encode(buf, p) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return buf.Bytes(), nil |
| 50 | } |
| 51 | |