github.com/brimstone/sbuca@v0.0.0-20151202175429-8691d9eba5c5/ca/cert_store.go (about) 1 package ca 2 3 import ( 4 "io/ioutil" 5 "strconv" 6 7 "github.com/brimstone/sbuca/pkix" 8 ) 9 10 type CertStore struct { 11 RootDir string 12 } 13 14 func NewCertStore(rootDir string) *CertStore { 15 16 store := &CertStore{ 17 RootDir: rootDir, 18 } 19 20 return store 21 } 22 23 func (store *CertStore) Get(id int64) (*pkix.Certificate, error) { 24 // FIXME 25 // currently using serialnumber as id, should change to something which can be 26 // mapped to (host, sn) pair 27 filename := strconv.Itoa(int(id)) + ".crt" 28 29 cert, err := pkix.NewCertificateFromPEMFile(store.RootDir + "/" + filename) 30 if err != nil { 31 return nil, err 32 } 33 34 return cert, nil 35 } 36 37 func (store *CertStore) Put(id int64, cert *pkix.Certificate) error { 38 39 pemBytes, err := cert.ToPEM() 40 if err != nil { 41 return err 42 } 43 filename := strconv.Itoa(int(id)) + ".crt" 44 err = ioutil.WriteFile(store.RootDir+"/"+filename, pemBytes, 0400) 45 if err != nil { 46 return err 47 } 48 49 return nil 50 } 51 52 func (store *CertStore) GetAllNames() ([]string, error) { 53 54 files, err := ioutil.ReadDir(store.RootDir + "/") 55 if err != nil { 56 return nil, err 57 } 58 names := make([]string, len(files)) 59 for _, f := range files { 60 names = append(names, f.Name()) 61 } 62 63 return names, nil 64 65 } 66 67 // should limit to 100 FIXME