func LoadCertsKeys(uname string, upass []byte) (ca []byte, ee []byte, pkey []byte, err error) { user_dir := common.GetConfDir(uname) cafs, err := ioutil.ReadFile(filepath.Join(user_dir, "ca.cert")) if err != nil { return nil, nil, nil, err } ca, err = passcrypto.DecryptMeBase32(string(cafs), upass) if err != nil { return nil, nil, nil, err } ee_pem, err := ioutil.ReadFile(filepath.Join(user_dir, "ee.cert")) if err != nil { return nil, nil, nil, err } ee_blk, _ := pem.Decode(ee_pem) ee = ee_blk.Bytes pkeyfs, err := ioutil.ReadFile(filepath.Join(user_dir, "ee.key")) if err != nil { return nil, nil, nil, err } pkey_pem, _ := pem.Decode(pkeyfs) pkey, err = x509.DecryptPEMBlock(pkey_pem, upass) if err != nil { return nil, nil, nil, err } return }
func LoadManpassCA(uname string, passwd []byte) (*CertKey, error) { //generate a new EE cert/key with specified uname's CA //return encrypted CA cert, EE cert and encrypted EE key in a string confdir := common.GetConfDir(uname) fi, err := os.Stat(confdir) if err != nil { return nil, err } if !fi.IsDir() { return nil, fmt.Errorf("%s is not a directory", confdir) } encacs, err := ioutil.ReadFile(filepath.Join(confdir, "ca.cert")) if err != nil { return nil, err } cacert_der, err := passcrypto.DecryptMeBase32(string(encacs), passwd) if err != nil { return nil, err } encaks, err := ioutil.ReadFile(filepath.Join(confdir, "ca.key")) if err != nil { return nil, err } blk, _ := pem.Decode(encaks) cakey_der, err := x509.DecryptPEMBlock(blk, passwd) var ck CertKey switch blk.Type { case "RSA PRIVATE KEY": ca := new(RSACertKey) cakey, err := x509.ParsePKCS1PrivateKey(cakey_der) if err != nil { return nil, err } ca.key = cakey cert, err := x509.ParseCertificates(cacert_der) if err != nil { return nil, err } ca.cert = *cert[0] ck = ca return &ck, nil case "EC PRIVATE KEY": ecck := new(ECCertKey) priv_key, err := x509.ParseECPrivateKey(cakey_der) if err != nil { return nil, err } ecck.key = priv_key cert, err := x509.ParseCertificates(cacert_der) if err != nil { return nil, err } ecck.cert = *cert[0] ck = ecck return &ck, nil } return nil, errors.New("Unussal error, you shouldn't see this") }