Skip to content

Commit

Permalink
get x509 name entry
Browse files Browse the repository at this point in the history
Change-Id: I75b097b206888cfe066d5470b9cdaec9da950244
  • Loading branch information
Andrew Harding committed Jun 1, 2015
1 parent c598d1a commit 0c8dfef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ func (n *Name) AddTextEntries(entries map[string]string) error {
return nil
}

// GetEntry returns a name entry based on NID. If no entry, then ("", false) is
// returned.
func (n *Name) GetEntry(nid NID) (entry string, ok bool) {
entrylen := C.X509_NAME_get_text_by_NID(n.name, C.int(nid), nil, 0)
if entrylen == -1 {
return "", false
}
buf := (*C.char)(C.malloc(C.size_t(entrylen + 1)))
defer C.free(unsafe.Pointer(buf))
C.X509_NAME_get_text_by_NID(n.name, C.int(nid), buf, entrylen+1)
return C.GoStringN(buf, entrylen), true
}

// NewCertificate generates a basic certificate based
// on the provided CertificateInfo struct
func NewCertificate(info *CertificateInfo, key PublicKey) (*Certificate, error) {
Expand Down
37 changes: 37 additions & 0 deletions cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,40 @@ func TestCAGenerate(t *testing.T) {
t.Fatal(err)
}
}

func TestCertGetNameEntry(t *testing.T) {
key, err := GenerateRSAKey(2048)
if err != nil {
t.Fatal(err)
}
info := &CertificateInfo{
Serial: 1,
Issued: 0,
Expires: 24 * time.Hour,
Country: "US",
Organization: "Test",
CommonName: "localhost",
}
cert, err := NewCertificate(info, key)
if err != nil {
t.Fatal(err)
}
name, err := cert.GetSubjectName()
if err != nil {
t.Fatal(err)
}
entry, ok := name.GetEntry(NID_commonName)
if !ok {
t.Fatal("no common name")
}
if entry != "localhost" {
t.Fatalf("expected localhost; got %q", entry)
}
entry, ok = name.GetEntry(NID_localityName)
if ok {
t.Fatal("did not expect a locality name")
}
if entry != "" {
t.Fatalf("entry should be empty; got %q", entry)
}
}

0 comments on commit 0c8dfef

Please sign in to comment.