hostname.go 4.05 KB
Newer Older
1
// Copyright (C) 2017. See AUTHORS.
JT Olds's avatar
JT Olds committed
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// 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.

JT Olds's avatar
JT Olds committed
15 16 17 18 19 20 21 22 23 24 25 26
package openssl

/*
#include <openssl/ssl.h>
#include <openssl/conf.h>
#include <openssl/x509.h>

#ifndef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
#define X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT	0x1
#define X509_CHECK_FLAG_NO_WILDCARDS	0x2

extern int X509_check_host(X509 *x, const unsigned char *chk, size_t chklen,
27
    unsigned int flags, char **peername);
JT Olds's avatar
JT Olds committed
28 29 30 31 32 33 34 35 36
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);
#endif
*/
import "C"

import (
37 38 39
	"errors"
	"net"
	"unsafe"
JT Olds's avatar
JT Olds committed
40 41 42
)

var (
43
	ValidationError = errors.New("Host validation error")
JT Olds's avatar
JT Olds committed
44 45 46 47 48
)

type CheckFlags int

const (
49 50
	AlwaysCheckSubject CheckFlags = C.X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
	NoWildcards        CheckFlags = C.X509_CHECK_FLAG_NO_WILDCARDS
JT Olds's avatar
JT Olds committed
51 52
)

JT Olds's avatar
JT Olds committed
53 54 55 56 57
// 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.
// Specifically returns ValidationError if the Certificate didn't match but
// there was no internal error.
JT Olds's avatar
JT Olds committed
58
func (c *Certificate) CheckHost(host string, flags CheckFlags) error {
59 60
	chost := unsafe.Pointer(C.CString(host))
	defer C.free(chost)
61

62
	rv := C.X509_check_host(c.x, (*C.uchar)(chost), C.size_t(len(host)),
63
		C.uint(flags), nil)
64 65 66 67 68 69 70
	if rv > 0 {
		return nil
	}
	if rv == 0 {
		return ValidationError
	}
	return errors.New("hostname validation had an internal failure")
JT Olds's avatar
JT Olds committed
71 72
}

JT Olds's avatar
JT Olds committed
73 74 75 76 77
// CheckEmail checks that the X509 certificate is signed for the provided
// email address. See http://www.openssl.org/docs/crypto/X509_check_host.html
// for more.
// Specifically returns ValidationError if the Certificate didn't match but
// there was no internal error.
JT Olds's avatar
JT Olds committed
78
func (c *Certificate) CheckEmail(email string, flags CheckFlags) error {
79 80 81 82 83 84 85 86 87 88 89
	cemail := unsafe.Pointer(C.CString(email))
	defer C.free(cemail)
	rv := C.X509_check_email(c.x, (*C.uchar)(cemail), C.size_t(len(email)),
		C.uint(flags))
	if rv > 0 {
		return nil
	}
	if rv == 0 {
		return ValidationError
	}
	return errors.New("email validation had an internal failure")
JT Olds's avatar
JT Olds committed
90 91
}

JT Olds's avatar
JT Olds committed
92 93 94 95 96
// CheckIP checks that the X509 certificate is signed for the provided
// IP address. See http://www.openssl.org/docs/crypto/X509_check_host.html
// for more.
// Specifically returns ValidationError if the Certificate didn't match but
// there was no internal error.
JT Olds's avatar
JT Olds committed
97
func (c *Certificate) CheckIP(ip net.IP, flags CheckFlags) error {
98 99 100 101 102 103
	// X509_check_ip will fail to validate the 16-byte representation of an IPv4
	// address, so convert to the 4-byte representation.
	if ip4 := ip.To4(); ip4 != nil {
		ip = ip4
	}

104 105 106 107 108 109 110 111 112 113
	cip := unsafe.Pointer(&ip[0])
	rv := C.X509_check_ip(c.x, (*C.uchar)(cip), C.size_t(len(ip)),
		C.uint(flags))
	if rv > 0 {
		return nil
	}
	if rv == 0 {
		return ValidationError
	}
	return errors.New("ip validation had an internal failure")
JT Olds's avatar
JT Olds committed
114 115
}

JT Olds's avatar
JT Olds committed
116 117 118 119 120
// VerifyHostname is a combination of CheckHost and CheckIP. If the provided
// hostname looks like an IP address, it will be checked as an IP address,
// otherwise it will be checked as a hostname.
// Specifically returns ValidationError if the Certificate didn't match but
// there was no internal error.
JT Olds's avatar
JT Olds committed
121
func (c *Certificate) VerifyHostname(host string) error {
122 123 124 125 126 127 128 129 130 131
	var ip net.IP
	if len(host) >= 3 && host[0] == '[' && host[len(host)-1] == ']' {
		ip = net.ParseIP(host[1 : len(host)-1])
	} else {
		ip = net.ParseIP(host)
	}
	if ip != nil {
		return c.CheckIP(ip, 0)
	}
	return c.CheckHost(host, 0)
JT Olds's avatar
JT Olds committed
132
}