New creates a new root certificate from the certificate request.
(req *csr.CertificateRequest)
| 46 | |
| 47 | // New creates a new root certificate from the certificate request. |
| 48 | func New(req *csr.CertificateRequest) (cert, csrPEM, key []byte, err error) { |
| 49 | policy := CAPolicy() |
| 50 | if req.CA != nil { |
| 51 | if req.CA.Expiry != "" { |
| 52 | policy.Default.ExpiryString = req.CA.Expiry |
| 53 | policy.Default.Expiry, err = time.ParseDuration(req.CA.Expiry) |
| 54 | if err != nil { |
| 55 | return |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if req.CA.Backdate != "" { |
| 60 | policy.Default.Backdate, err = time.ParseDuration(req.CA.Backdate) |
| 61 | if err != nil { |
| 62 | return |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | policy.Default.CAConstraint.MaxPathLen = req.CA.PathLength |
| 67 | if req.CA.PathLength != 0 && req.CA.PathLenZero { |
| 68 | log.Infof("ignore invalid 'pathlenzero' value") |
| 69 | } else { |
| 70 | policy.Default.CAConstraint.MaxPathLenZero = req.CA.PathLenZero |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if req.CRL != "" { |
| 75 | policy.Default.CRL = req.CRL |
| 76 | } |
| 77 | |
| 78 | g := &csr.Generator{Validator: validator} |
| 79 | csrPEM, key, err = g.ProcessRequest(req) |
| 80 | if err != nil { |
| 81 | log.Errorf("failed to process request: %v", err) |
| 82 | key = nil |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | priv, err := helpers.ParsePrivateKeyPEM(key) |
| 87 | if err != nil { |
| 88 | log.Errorf("failed to parse private key: %v", err) |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | s, err := local.NewSigner(priv, nil, signer.DefaultSigAlgo(priv), policy) |
| 93 | if err != nil { |
| 94 | log.Errorf("failed to create signer: %v", err) |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | signReq := signer.SignRequest{Hosts: req.Hosts, Request: string(csrPEM)} |
| 99 | cert, err = s.Sign(signReq) |
| 100 | |
| 101 | return |
| 102 | } |
| 103 | |
| 104 | // NewFromPEM creates a new root certificate from the key file passed in. |
| 105 | func NewFromPEM(req *csr.CertificateRequest, keyFile string) (cert, csrPEM []byte, err error) { |