|
| 1 | +package certificates |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/rsa" |
| 6 | + "crypto/sha256" |
| 7 | + "crypto/x509" |
| 8 | + "crypto/x509/pkix" |
| 9 | + "encoding/pem" |
| 10 | + "fmt" |
| 11 | + "math/big" |
| 12 | + "net/url" |
| 13 | + "os" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +func GenerateRootCertificate(addThumbPrintToName bool, commonName, country, organization string, strong bool) (*x509.Certificate, *rsa.PrivateKey, error) { |
| 18 | + keyLength := 2048 |
| 19 | + if strong { |
| 20 | + keyLength = 3072 |
| 21 | + } |
| 22 | + |
| 23 | + // Generate RSA keys |
| 24 | + privateKey, err := rsa.GenerateKey(rand.Reader, keyLength) |
| 25 | + if err != nil { |
| 26 | + return nil, nil, err |
| 27 | + } |
| 28 | + |
| 29 | + // Preparing the certificate |
| 30 | + var maxValue uint = 128 |
| 31 | + |
| 32 | + serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), maxValue)) |
| 33 | + if err != nil { |
| 34 | + return nil, nil, err |
| 35 | + } |
| 36 | + |
| 37 | + thirtyYears := 30 |
| 38 | + |
| 39 | + if addThumbPrintToName { |
| 40 | + hash := sha256.New() |
| 41 | + hash.Write(privateKey.PublicKey.N.Bytes()) // Simplified approach to get a thumbprint-like result |
| 42 | + commonName += "-" + fmt.Sprintf("%x", hash.Sum(nil)[:3]) |
| 43 | + } |
| 44 | + |
| 45 | + if country == "" { |
| 46 | + country = "unknown country" |
| 47 | + } |
| 48 | + |
| 49 | + if organization == "" { |
| 50 | + organization = "unknown organization" |
| 51 | + } |
| 52 | + |
| 53 | + template := x509.Certificate{ |
| 54 | + SerialNumber: serialNumber, |
| 55 | + Subject: pkix.Name{ |
| 56 | + CommonName: commonName, |
| 57 | + Organization: []string{organization}, |
| 58 | + Country: []string{country}, |
| 59 | + }, |
| 60 | + NotBefore: time.Now().AddDate(-1, 0, 0), |
| 61 | + NotAfter: time.Now().AddDate(thirtyYears, 0, 0), |
| 62 | + |
| 63 | + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, |
| 64 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, |
| 65 | + BasicConstraintsValid: true, |
| 66 | + IsCA: true, |
| 67 | + } |
| 68 | + |
| 69 | + // Create a self-signed certificate |
| 70 | + certBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) |
| 71 | + if err != nil { |
| 72 | + return nil, nil, err |
| 73 | + } |
| 74 | + |
| 75 | + // Encoding certificate to PEM format |
| 76 | + certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certBytes}) |
| 77 | + |
| 78 | + // Save to files (optional) |
| 79 | + certOut, err := os.Create("config/root_cert.pem") |
| 80 | + if err != nil { |
| 81 | + return nil, nil, err |
| 82 | + } |
| 83 | + |
| 84 | + _, err = certOut.Write(certPEM) |
| 85 | + if err != nil { |
| 86 | + return nil, nil, err |
| 87 | + } |
| 88 | + |
| 89 | + certOut.Close() |
| 90 | + |
| 91 | + keyOut, err := os.Create("config/root_key.pem") |
| 92 | + if err != nil { |
| 93 | + return nil, nil, err |
| 94 | + } |
| 95 | + |
| 96 | + err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}) |
| 97 | + if err != nil { |
| 98 | + return nil, nil, err |
| 99 | + } |
| 100 | + |
| 101 | + keyOut.Close() |
| 102 | + |
| 103 | + return &template, privateKey, nil |
| 104 | +} |
| 105 | + |
| 106 | +type CertAndKeyType struct { |
| 107 | + Cert *x509.Certificate |
| 108 | + Key *rsa.PrivateKey |
| 109 | +} |
| 110 | + |
| 111 | +func IssueWebServerCertificate(rootCert CertAndKeyType, addThumbPrintToName bool, commonName, country, organization string, strong bool) (*x509.Certificate, *rsa.PrivateKey, error) { |
| 112 | + keyLength := 2048 |
| 113 | + if strong { |
| 114 | + keyLength = 3072 |
| 115 | + } |
| 116 | + |
| 117 | + // Generate RSA keys |
| 118 | + keys, err := rsa.GenerateKey(rand.Reader, keyLength) |
| 119 | + if err != nil { |
| 120 | + return nil, nil, err |
| 121 | + } |
| 122 | + |
| 123 | + var maxValue uint = 128 |
| 124 | + |
| 125 | + serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), maxValue)) |
| 126 | + if err != nil { |
| 127 | + return nil, nil, err |
| 128 | + } |
| 129 | + |
| 130 | + thirtyYears := 30 |
| 131 | + notBefore := time.Now().AddDate(-1, 0, 0) |
| 132 | + notAfter := time.Now().AddDate(thirtyYears, 0, 0) |
| 133 | + |
| 134 | + subject := pkix.Name{ |
| 135 | + CommonName: commonName, |
| 136 | + } |
| 137 | + |
| 138 | + if country != "" { |
| 139 | + subject.Country = []string{country} |
| 140 | + } |
| 141 | + |
| 142 | + if organization != "" { |
| 143 | + subject.Organization = []string{organization} |
| 144 | + } |
| 145 | + |
| 146 | + if addThumbPrintToName { |
| 147 | + hash := sha256.New() |
| 148 | + hash.Write(keys.PublicKey.N.Bytes()) // Simplified approach to get a thumbprint-like result |
| 149 | + subject.CommonName += "-" + string(hash.Sum(nil)[:3]) |
| 150 | + } |
| 151 | + |
| 152 | + hash := sha256.Sum256(keys.PublicKey.N.Bytes()) |
| 153 | + |
| 154 | + template := x509.Certificate{ |
| 155 | + SerialNumber: serialNumber, |
| 156 | + Subject: subject, |
| 157 | + NotBefore: notBefore, |
| 158 | + NotAfter: notAfter, |
| 159 | + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageCertSign | x509.KeyUsageDataEncipherment, |
| 160 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 161 | + BasicConstraintsValid: true, |
| 162 | + IsCA: false, |
| 163 | + SubjectKeyId: hash[:], |
| 164 | + } |
| 165 | + |
| 166 | + // Subject Alternative Name |
| 167 | + uri, _ := url.Parse("http://" + commonName + "/") |
| 168 | + template.DNSNames = []string{commonName, "localhost"} |
| 169 | + template.URIs = []*url.URL{uri} |
| 170 | + |
| 171 | + // Sign the certificate with root certificate private key |
| 172 | + certBytes, err := x509.CreateCertificate(rand.Reader, &template, rootCert.Cert, &keys.PublicKey, rootCert.Key) |
| 173 | + if err != nil { |
| 174 | + return nil, nil, err |
| 175 | + } |
| 176 | + |
| 177 | + // Encoding certificate to PEM format |
| 178 | + certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certBytes}) |
| 179 | + |
| 180 | + // Save to files (optional) |
| 181 | + certOut, err := os.Create("config/" + commonName + "_cert.pem") |
| 182 | + if err != nil { |
| 183 | + return nil, nil, err |
| 184 | + } |
| 185 | + |
| 186 | + _, err = certOut.Write(certPEM) |
| 187 | + if err != nil { |
| 188 | + return nil, nil, err |
| 189 | + } |
| 190 | + |
| 191 | + certOut.Close() |
| 192 | + |
| 193 | + keyOut, err := os.Create("config/" + commonName + "_key.pem") |
| 194 | + if err != nil { |
| 195 | + return nil, nil, err |
| 196 | + } |
| 197 | + |
| 198 | + err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(keys)}) |
| 199 | + if err != nil { |
| 200 | + return nil, nil, err |
| 201 | + } |
| 202 | + |
| 203 | + keyOut.Close() |
| 204 | + |
| 205 | + return &template, keys, nil |
| 206 | +} |
0 commit comments