Skip to content

Commit

Permalink
Adding Marshal and Unmarshal for pkix.Name types
Browse files Browse the repository at this point in the history
  • Loading branch information
brimstone committed Sep 23, 2015
1 parent f0a2e56 commit 819f845
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkix/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"io/ioutil"
"math/big"
"strings"
)

type Certificate struct {
Expand Down Expand Up @@ -85,3 +86,65 @@ func (certificate *Certificate) ToPEMFile(filename string) error {
func (certificate *Certificate) GetSerialNumber() *big.Int {
return certificate.Crt.SerialNumber
}

func Marshal(name pkix.Name) (string, error) {
var output []string
if name.CommonName != "" {
output = append(output, "CN="+name.CommonName)
}
if len(name.Country) > 0 {
for i := range name.Country {
output = append(output, "C="+name.Country[i])
}
}
if len(name.Locality) > 0 {
for i := range name.Locality {
output = append(output, "L="+name.Locality[i])
}
}
if len(name.Province) > 0 {
for i := range name.Province {
output = append(output, "ST="+name.Province[i])
}
}
if len(name.StreetAddress) > 0 {
for i := range name.StreetAddress {
output = append(output, "SA="+name.StreetAddress[i])
}
}
if len(name.Organization) > 0 {
for i := range name.Organization {
output = append(output, "O="+name.Organization[i])
}
}
if len(name.OrganizationalUnit) > 0 {
for i := range name.OrganizationalUnit {
output = append(output, "OU="+name.OrganizationalUnit[i])
}
}
return strings.Join(output, ","), nil
}

func Unmarshal(dn string) (pkix.Name, error) {
var output pkix.Name
segments := strings.Split(dn, ",")
for segment := range segments {
identifier := strings.SplitN(segments[segment], "=", 2)
if identifier[0] == "CN" {
output.CommonName = identifier[1]
} else if identifier[0] == "C" {
output.Country = append(output.Country, identifier[1])
} else if identifier[0] == "L" {
output.Locality = append(output.Locality, identifier[1])
} else if identifier[0] == "ST" {
output.Province = append(output.Province, identifier[1])
} else if identifier[0] == "SA" {
output.StreetAddress = append(output.StreetAddress, identifier[1])
} else if identifier[0] == "O" {
output.Organization = append(output.Organization, identifier[1])
} else if identifier[0] == "OU" {
output.OrganizationalUnit = append(output.OrganizationalUnit, identifier[1])
}
}
return output, nil
}
71 changes: 71 additions & 0 deletions pkix/cert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package pkix

import (
"crypto/x509/pkix"
"reflect"
"testing"
)

func Test_Marshal(t *testing.T) {
testdata := make(map[string]pkix.Name)

testdata["O=null"] = pkix.Name{
Organization: []string{"null"},
}

testdata["CN=myserver,O=myorg"] = pkix.Name{
Organization: []string{"myorg"},
CommonName: "myserver",
}
testdata["CN=myserver,C=merica,L=City,ST=State,SA=StreetAddress,O=myorg,OU=myorgunit"] = pkix.Name{
Organization: []string{"myorg"},
OrganizationalUnit: []string{"myorgunit"},
Country: []string{"merica"},
Locality: []string{"City"},
Province: []string{"State"},
StreetAddress: []string{"StreetAddress"},
CommonName: "myserver",
}

for expected, input := range testdata {
output, err := Marshal(input)
if err != nil {
t.Error(err)
}
if output != expected {
t.Error("Failed got " + output + " expected " + expected)
}
}
}

func Test_Unmarshal(t *testing.T) {
testdata := make(map[string]pkix.Name)

testdata["O=null"] = pkix.Name{
Organization: []string{"null"},
}

testdata["CN=myserver,O=myorg"] = pkix.Name{
Organization: []string{"myorg"},
CommonName: "myserver",
}
testdata["CN=myserver,C=merica,L=City,ST=State,SA=StreetAddress,O=myorg,OU=myorgunit"] = pkix.Name{
Organization: []string{"myorg"},
OrganizationalUnit: []string{"myorgunit"},
Country: []string{"merica"},
Locality: []string{"City"},
Province: []string{"State"},
StreetAddress: []string{"StreetAddress"},
CommonName: "myserver",
}

for dnstring, expected := range testdata {
pkixName, err := Unmarshal(dnstring)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(pkixName, expected) {
t.Error("Failed got ", pkixName, " expected ", expected)
}
}
}

0 comments on commit 819f845

Please sign in to comment.