Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list subject alternative name for dns #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions hostname.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include <openssl/x509.h>
#include "hostname.h"

#ifndef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT

Expand Down Expand Up @@ -271,6 +272,30 @@ static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
}
}

void list_x509_san(X509 *x, SanList *list)
{
STACK_OF(GENERAL_NAME) *gens = NULL;
gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
if (gens)
{
int i;
int gens_length=sk_GENERAL_NAME_num(gens);
list->length = gens_length;
list->sans = (char**)realloc(list->sans, gens_length*sizeof(char*));
for (i = 0; i < gens_length; i++)
{
unsigned char *astr;
GENERAL_NAME *gen;
ASN1_STRING *cstr;

gen = sk_GENERAL_NAME_value(gens, i);
ASN1_STRING_to_UTF8(&astr, gen->d.dNSName);
list->sans[i] = astr;
}
GENERAL_NAMES_free(gens);
}
}

static int do_x509_check(X509 *x, const unsigned char *chk, size_t chklen,
unsigned int flags, int check_type)
{
Expand Down
22 changes: 22 additions & 0 deletions hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package openssl
#include <openssl/ssl.h>
#include <openssl/conf.h>
#include <openssl/x509.h>
#include "hostname.h"

#ifndef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
#define X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT 0x1
Expand All @@ -29,13 +30,15 @@ extern int X509_check_email(X509 *x, const unsigned char *chk, size_t chklen,
unsigned int flags);
extern int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
unsigned int flags);
extern void list_x509_san(X509 *x, SanList *sans);
#endif
*/
import "C"

import (
"errors"
"net"
"reflect"
"unsafe"
)

Expand All @@ -50,6 +53,25 @@ const (
NoWildcards CheckFlags = C.X509_CHECK_FLAG_NO_WILDCARDS
)

func (c *Certificate) ListSanDns() []string {
list := C.SanList{}
list.sans = (**C.char)(C.malloc(100))
defer C.free(unsafe.Pointer(list.sans))
C.list_x509_san(c.x, &list)

var str []string
var pbuf []*C.char
header := (*reflect.SliceHeader)(unsafe.Pointer(&pbuf))
header.Cap = int(list.length)
header.Len = int(list.length)
header.Data = uintptr(unsafe.Pointer(list.sans))
for _, i := range pbuf {
str = append(str, C.GoString(i))
}

return str
}

// CheckHost checks that the X509 certificate is signed for the provided
// host name. See http://www.openssl.org/docs/crypto/X509_check_host.html for
// more. Note that CheckHost does not check the IP field. See VerifyHostname.
Expand Down
6 changes: 6 additions & 0 deletions hostname.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

typedef struct _SanList {
int length;
char** sans;
} SanList;