Files
zeekay 85f1a56714 Issue certificates under our own name
The defaults behind every certificate subject named upstream's
organization, a development unit and an invented city and state, so the
CAs, servers, clients and routers the bootstrap mints all carried them.
They now read O=Hanzo AI, OU=ZT, C=US and leave ST and L unset.

A router put an unset attribute into its CSR as an empty value, which
is not a DirectoryString (RFC 5280 4.1.2.4); it now leaves it out. And
`pki create` read the province from a key its flag was never bound to,
so --pki-province did nothing.

Claude-Session: https://claude.ai/code/session_01HuMDfgFssPUptYUzgZ6Cp8
2026-09-23 14:46:06 -07:00

76 lines
2.0 KiB
Go

/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package enroll
import (
"crypto"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"github.com/hanzozt/zt/v2/router/env"
)
type Csr struct {
Pem string
Name string
SanDns []string
SanEmail []string
SanIp []string
SanUri []string
}
func CreateCsr(key crypto.PrivateKey, algo x509.SignatureAlgorithm, subj *pkix.Name, sans *env.Sans) (string, error) {
template := x509.CertificateRequest{
Subject: *subj,
SignatureAlgorithm: algo,
DNSNames: sans.DnsAddresses,
EmailAddresses: sans.EmailAddresses,
IPAddresses: sans.IpAddressesParsed,
URIs: sans.UriAddressesParsed,
}
csrBytes, csrErr := x509.CreateCertificateRequest(rand.Reader, &template, key)
if csrErr != nil {
return "", csrErr
}
outBuff := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes})
return string(outBuff), nil
}
// csrSubject names cn with the CSR attributes c sets. An unset attribute is
// left out, since an empty value is not a DirectoryString (RFC 5280 ยง4.1.2.4).
func csrSubject(cn string, c *env.Csr) *pkix.Name {
rdn := func(s string) []string {
if s == "" {
return nil
}
return []string{s}
}
return &pkix.Name{
CommonName: cn,
Country: rdn(c.Country),
Province: rdn(c.Province),
Locality: rdn(c.Locality),
Organization: rdn(c.Organization),
OrganizationalUnit: rdn(c.OrganizationalUnit),
}
}