diff options
| author | Jeremy Clerc <jclerc@google.com> | 2017-02-11 00:13:54 +0100 |
|---|---|---|
| committer | Jeremy Clerc <jclerc@google.com> | 2017-02-12 23:54:16 +0100 |
| commit | 06ee1171dee17245e71bb0ddd742c7f95f9bd2cb (patch) | |
| tree | 64767087217188af49e4c3788188ce6568198fa7 /pkg/certificate/certificate.go | |
| parent | c42a84ae556034b9fe2f9710603b1c10e8c5588f (diff) | |
| download | easypki-06ee1171dee17245e71bb0ddd742c7f95f9bd2cb.tar.xz | |
Refactor the all API for cleanup and extensibility.v1.0.0
API now has a store interface so one could choose to store the different
files in a database for example.
Diffstat (limited to 'pkg/certificate/certificate.go')
| -rw-r--r-- | pkg/certificate/certificate.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/pkg/certificate/certificate.go b/pkg/certificate/certificate.go new file mode 100644 index 0000000..187fb1c --- /dev/null +++ b/pkg/certificate/certificate.go @@ -0,0 +1,58 @@ +// Copyright 2015 Google 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 +// +// http://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 certificate provide helpers to manipulate certificates. +package certificate + +import ( + "crypto/rsa" + "crypto/x509" + "fmt" +) + +// Bundle represents a pair of private key and certificate. +type Bundle struct { + Name string + Key *rsa.PrivateKey + Cert *x509.Certificate +} + +// Raw returns the raw bytes for the private key and certificate. +func (b *Bundle) Raw() ([]byte, []byte) { + return x509.MarshalPKCS1PrivateKey(b.Key), b.Cert.Raw +} + +// RawToBundle creates a bundle from the name and bytes given for a private key +// and a certificate. +func RawToBundle(name string, key []byte, cert []byte) (*Bundle, error) { + k, err := x509.ParsePKCS1PrivateKey(key) + if err != nil { + return nil, fmt.Errorf("failed parsing private key from PEM bytes: %v", err) + } + c, err := x509.ParseCertificate(cert) + if err != nil { + return nil, fmt.Errorf("failed parsing certificate from PEM bytes: %v", err) + } + return &Bundle{Name: name, Key: k, Cert: c}, nil +} + +// State represents a certificate state (Valid, Expired, Revoked). +type State int + +// Certificate states. +const ( + Valid State = iota + Revoked + Expired +) |
