|
| 1 | +/* |
| 2 | + * Copyright (c) 2024-2025 Mikhail Knyazhev <markus621@yandex.ru>. All rights reserved. |
| 3 | + * Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file. |
| 4 | + */ |
| 5 | + |
| 6 | +package pki |
| 7 | + |
| 8 | +import ( |
| 9 | + "crypto/x509" |
| 10 | + "crypto/x509/pkix" |
| 11 | + "encoding/asn1" |
| 12 | +) |
| 13 | + |
| 14 | +type Config struct { |
| 15 | + SignatureAlgorithm x509.SignatureAlgorithm `yaml:"signature_algorithm" json:"signature_algorithm"` |
| 16 | + |
| 17 | + Organization string `yaml:"organization,omitempty" json:"organization,omitempty"` |
| 18 | + OrganizationalUnit string `yaml:"organizational_unit,omitempty" json:"organizational_unit,omitempty"` |
| 19 | + Country string `yaml:"country,omitempty" json:"country,omitempty"` |
| 20 | + Province string `yaml:"province,omitempty" json:"province,omitempty"` |
| 21 | + Locality string `yaml:"locality,omitempty" json:"locality,omitempty"` |
| 22 | + StreetAddress string `yaml:"street_address,omitempty" json:"street_address,omitempty"` |
| 23 | + PostalCode string `yaml:"postal_code,omitempty" json:"postal_code,omitempty"` |
| 24 | + CommonName string `yaml:"common_name,omitempty" json:"common_name,omitempty"` |
| 25 | + |
| 26 | + EmailAddress []string `yaml:"email_address,omitempty" json:"email_address,omitempty"` |
| 27 | + OCSPServerURLs []string `yaml:"ocsp_server_ur_ls,omitempty" json:"ocsp_server_ur_ls,omitempty"` |
| 28 | + IssuingCertificateURLs []string `yaml:"issuing_certificate_urls,omitempty" json:"issuing_certificate_urls,omitempty"` |
| 29 | + CRLDistributionPointURLs []string `yaml:"crl_distribution_point_ur_ls,omitempty" json:"crl_distribution_point_ur_ls,omitempty"` |
| 30 | + CertificatePoliciesURLs []string `yaml:"certificate_policies_urls,omitempty" json:"certificate_policies_urls,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +func (v Config) Subject() pkix.Name { |
| 34 | + result := pkix.Name{} |
| 35 | + |
| 36 | + if len(v.Country) > 0 { |
| 37 | + result.Country = []string{v.Country} |
| 38 | + } |
| 39 | + if len(v.Organization) > 0 { |
| 40 | + result.Organization = []string{v.Organization} |
| 41 | + } |
| 42 | + if len(v.OrganizationalUnit) > 0 { |
| 43 | + result.OrganizationalUnit = []string{v.OrganizationalUnit} |
| 44 | + } |
| 45 | + if len(v.Locality) > 0 { |
| 46 | + result.Locality = []string{v.Locality} |
| 47 | + } |
| 48 | + if len(v.Province) > 0 { |
| 49 | + result.Province = []string{v.Province} |
| 50 | + } |
| 51 | + if len(v.StreetAddress) > 0 { |
| 52 | + result.StreetAddress = []string{v.StreetAddress} |
| 53 | + } |
| 54 | + if len(v.PostalCode) > 0 { |
| 55 | + result.PostalCode = []string{v.PostalCode} |
| 56 | + } |
| 57 | + if len(v.CommonName) > 0 { |
| 58 | + result.CommonName = v.CommonName |
| 59 | + } |
| 60 | + |
| 61 | + return result |
| 62 | +} |
| 63 | + |
| 64 | +func (v Config) ExtraExtensions() []pkix.Extension { |
| 65 | + var result []pkix.Extension |
| 66 | + |
| 67 | + if len(v.IssuingCertificateURLs) > 0 { |
| 68 | + for _, value := range stringsPrepare(v.CertificatePoliciesURLs) { |
| 69 | + result = append(result, pkix.Extension{ |
| 70 | + Id: asn1.ObjectIdentifier{2, 23, 140, 1, 1}, |
| 71 | + Critical: true, |
| 72 | + Value: []byte(value), |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + return result |
| 79 | +} |
0 commit comments