MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / createCAWithValidity

Function createCAWithValidity

pkg/certs/certs.go:434–494  ·  view source on GitHub ↗

createCAWithValidity create a CA with a certain validity, with a parent certificate and signed by a certain private key. If the latest two parameters are nil, the CA will be a root one (self-signed)

(
	notBefore,
	notAfter time.Time,
	parentCertificate *x509.Certificate,
	parentPrivateKey interface{},
	commonName string,
	organizationalUnit string,
)

Source from the content-addressed store, hash-verified

432// createCAWithValidity create a CA with a certain validity, with a parent certificate and signed by a certain
433// private key. If the latest two parameters are nil, the CA will be a root one (self-signed)
434func createCAWithValidity(
435 notBefore,
436 notAfter time.Time,
437 parentCertificate *x509.Certificate,
438 parentPrivateKey interface{},
439 commonName string,
440 organizationalUnit string,
441) (*KeyPair, error) {
442 serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
443 serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
444 if err != nil {
445 return nil, err
446 }
447 rootKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
448 if err != nil {
449 return nil, err
450 }
451
452 rootTemplate := x509.Certificate{
453 SerialNumber: serialNumber,
454 NotBefore: notBefore,
455 NotAfter: notAfter,
456 KeyUsage: x509.KeyUsageCertSign,
457 BasicConstraintsValid: true,
458 IsCA: true,
459 Subject: pkix.Name{
460 CommonName: commonName,
461 OrganizationalUnit: []string{
462 organizationalUnit,
463 },
464 },
465 }
466
467 if parentCertificate == nil {
468 parentCertificate = &rootTemplate
469 }
470
471 if parentPrivateKey == nil {
472 parentPrivateKey = rootKey
473 }
474
475 certificateBytes, err := x509.CreateCertificate(
476 rand.Reader,
477 &rootTemplate,
478 parentCertificate,
479 &rootKey.PublicKey,
480 parentPrivateKey)
481 if err != nil {
482 return nil, err
483 }
484
485 privateKey, err := x509.MarshalECPrivateKey(rootKey)
486 if err != nil {
487 return nil, err
488 }
489
490 return &KeyPair{
491 Private: encodePrivateKey(privateKey),

Callers 4

CreateDerivedCAMethod · 0.85
CreateRootCAFunction · 0.85
certs_test.goFile · 0.85
k8s_test.goFile · 0.85

Calls 2

encodePrivateKeyFunction · 0.85
encodeCertificateFunction · 0.85

Tested by

no test coverage detected