2017-06-06 03:58:58 +08:00
|
|
|
// Copyright (C) 2017. See AUTHORS.
|
2014-05-10 05:38:13 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2014-01-02 05:42:25 +08:00
|
|
|
package openssl
|
|
|
|
|
2017-06-02 22:47:50 +08:00
|
|
|
// #include "shim.h"
|
2014-01-02 05:42:25 +08:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2014-02-26 02:45:14 +08:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"unsafe"
|
2014-01-02 05:42:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-02-26 02:45:14 +08:00
|
|
|
SSLRecordSize = 16 * 1024
|
2014-01-02 05:42:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func nonCopyGoBytes(ptr uintptr, length int) []byte {
|
2014-02-26 02:45:14 +08:00
|
|
|
var slice []byte
|
|
|
|
header := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
|
|
|
|
header.Cap = length
|
|
|
|
header.Len = length
|
|
|
|
header.Data = ptr
|
|
|
|
return slice
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func nonCopyCString(data *C.char, size C.int) []byte {
|
2014-02-26 02:45:14 +08:00
|
|
|
return nonCopyGoBytes(uintptr(unsafe.Pointer(data)), int(size))
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 04:41:57 +08:00
|
|
|
var writeBioMapping = newMapping()
|
|
|
|
|
2014-01-02 05:42:25 +08:00
|
|
|
type writeBio struct {
|
2014-05-01 04:47:50 +08:00
|
|
|
data_mtx sync.Mutex
|
|
|
|
op_mtx sync.Mutex
|
|
|
|
buf []byte
|
|
|
|
release_buffers bool
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadWritePtr(b *C.BIO) *writeBio {
|
2017-06-02 22:47:50 +08:00
|
|
|
t := token(C.X_BIO_get_data(b))
|
|
|
|
return (*writeBio)(writeBioMapping.Get(t))
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2014-04-09 04:38:28 +08:00
|
|
|
func bioClearRetryFlags(b *C.BIO) {
|
2017-06-02 22:47:50 +08:00
|
|
|
C.X_BIO_clear_flags(b, C.BIO_FLAGS_RWS|C.BIO_FLAGS_SHOULD_RETRY)
|
2014-04-09 04:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func bioSetRetryRead(b *C.BIO) {
|
2017-06-02 22:47:50 +08:00
|
|
|
C.X_BIO_set_flags(b, C.BIO_FLAGS_READ|C.BIO_FLAGS_SHOULD_RETRY)
|
2014-04-09 04:38:28 +08:00
|
|
|
}
|
|
|
|
|
2017-06-02 22:47:50 +08:00
|
|
|
//export go_write_bio_write
|
|
|
|
func go_write_bio_write(b *C.BIO, data *C.char, size C.int) (rc C.int) {
|
2014-04-08 05:12:55 +08:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
logger.Critf("openssl: writeBioWrite panic'd: %v", err)
|
|
|
|
rc = -1
|
|
|
|
}
|
|
|
|
}()
|
2014-02-26 02:45:14 +08:00
|
|
|
ptr := loadWritePtr(b)
|
|
|
|
if ptr == nil || data == nil || size < 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
ptr.data_mtx.Lock()
|
|
|
|
defer ptr.data_mtx.Unlock()
|
2014-04-09 04:38:28 +08:00
|
|
|
bioClearRetryFlags(b)
|
2014-02-26 02:45:14 +08:00
|
|
|
ptr.buf = append(ptr.buf, nonCopyCString(data, size)...)
|
|
|
|
return size
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2017-06-02 22:47:50 +08:00
|
|
|
//export go_write_bio_ctrl
|
|
|
|
func go_write_bio_ctrl(b *C.BIO, cmd C.int, arg1 C.long, arg2 unsafe.Pointer) (
|
2014-04-08 05:12:55 +08:00
|
|
|
rc C.long) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
logger.Critf("openssl: writeBioCtrl panic'd: %v", err)
|
|
|
|
rc = -1
|
|
|
|
}
|
|
|
|
}()
|
2014-02-26 02:45:14 +08:00
|
|
|
switch cmd {
|
|
|
|
case C.BIO_CTRL_WPENDING:
|
|
|
|
return writeBioPending(b)
|
|
|
|
case C.BIO_CTRL_DUP, C.BIO_CTRL_FLUSH:
|
|
|
|
return 1
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeBioPending(b *C.BIO) C.long {
|
2014-02-26 02:45:14 +08:00
|
|
|
ptr := loadWritePtr(b)
|
|
|
|
if ptr == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
ptr.data_mtx.Lock()
|
|
|
|
defer ptr.data_mtx.Unlock()
|
|
|
|
return C.long(len(ptr.buf))
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:47:21 +08:00
|
|
|
func (wb *writeBio) WriteTo(w io.Writer) (rv int64, err error) {
|
|
|
|
wb.op_mtx.Lock()
|
|
|
|
defer wb.op_mtx.Unlock()
|
2014-01-02 05:42:25 +08:00
|
|
|
|
2014-02-26 02:45:14 +08:00
|
|
|
// write whatever data we currently have
|
2021-07-19 23:47:21 +08:00
|
|
|
wb.data_mtx.Lock()
|
|
|
|
data := wb.buf
|
|
|
|
wb.data_mtx.Unlock()
|
2014-01-20 04:43:40 +08:00
|
|
|
|
2014-02-26 02:45:14 +08:00
|
|
|
if len(data) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
n, err := w.Write(data)
|
2014-01-20 04:43:40 +08:00
|
|
|
|
2014-02-26 02:45:14 +08:00
|
|
|
// subtract however much data we wrote from the buffer
|
2021-07-19 23:47:21 +08:00
|
|
|
wb.data_mtx.Lock()
|
|
|
|
wb.buf = wb.buf[:copy(wb.buf, wb.buf[n:])]
|
|
|
|
if wb.release_buffers && len(wb.buf) == 0 {
|
|
|
|
wb.buf = nil
|
2014-05-01 04:47:50 +08:00
|
|
|
}
|
2021-07-19 23:47:21 +08:00
|
|
|
wb.data_mtx.Unlock()
|
2014-01-20 04:43:40 +08:00
|
|
|
|
2014-02-26 02:45:14 +08:00
|
|
|
return int64(n), err
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:47:21 +08:00
|
|
|
func (wb *writeBio) Disconnect(b *C.BIO) {
|
|
|
|
if loadWritePtr(b) == wb {
|
2017-06-02 22:47:50 +08:00
|
|
|
writeBioMapping.Del(token(C.X_BIO_get_data(b)))
|
|
|
|
C.X_BIO_set_data(b, nil)
|
2014-02-26 02:45:14 +08:00
|
|
|
}
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:47:21 +08:00
|
|
|
func (wb *writeBio) MakeCBIO() *C.BIO {
|
2017-06-02 22:47:50 +08:00
|
|
|
rv := C.X_BIO_new_write_bio()
|
2021-07-19 23:47:21 +08:00
|
|
|
token := writeBioMapping.Add(unsafe.Pointer(wb))
|
2017-06-02 22:47:50 +08:00
|
|
|
C.X_BIO_set_data(rv, unsafe.Pointer(token))
|
2014-02-26 02:45:14 +08:00
|
|
|
return rv
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 04:41:57 +08:00
|
|
|
var readBioMapping = newMapping()
|
|
|
|
|
2014-01-02 05:42:25 +08:00
|
|
|
type readBio struct {
|
2014-05-01 04:47:50 +08:00
|
|
|
data_mtx sync.Mutex
|
|
|
|
op_mtx sync.Mutex
|
|
|
|
buf []byte
|
|
|
|
eof bool
|
|
|
|
release_buffers bool
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadReadPtr(b *C.BIO) *readBio {
|
2017-06-02 22:47:50 +08:00
|
|
|
return (*readBio)(readBioMapping.Get(token(C.X_BIO_get_data(b))))
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2017-06-02 22:47:50 +08:00
|
|
|
//export go_read_bio_read
|
|
|
|
func go_read_bio_read(b *C.BIO, data *C.char, size C.int) (rc C.int) {
|
2014-04-08 05:12:55 +08:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2017-06-02 22:47:50 +08:00
|
|
|
logger.Critf("openssl: go_read_bio_read panic'd: %v", err)
|
2014-04-08 05:12:55 +08:00
|
|
|
rc = -1
|
|
|
|
}
|
|
|
|
}()
|
2014-02-26 02:45:14 +08:00
|
|
|
ptr := loadReadPtr(b)
|
|
|
|
if ptr == nil || size < 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
ptr.data_mtx.Lock()
|
|
|
|
defer ptr.data_mtx.Unlock()
|
2014-04-09 04:38:28 +08:00
|
|
|
bioClearRetryFlags(b)
|
2014-02-26 02:45:14 +08:00
|
|
|
if len(ptr.buf) == 0 {
|
|
|
|
if ptr.eof {
|
|
|
|
return 0
|
|
|
|
}
|
2014-04-09 04:38:28 +08:00
|
|
|
bioSetRetryRead(b)
|
2014-02-26 02:45:14 +08:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if size == 0 || data == nil {
|
|
|
|
return C.int(len(ptr.buf))
|
|
|
|
}
|
|
|
|
n := copy(nonCopyCString(data, size), ptr.buf)
|
|
|
|
ptr.buf = ptr.buf[:copy(ptr.buf, ptr.buf[n:])]
|
2014-05-01 04:47:50 +08:00
|
|
|
if ptr.release_buffers && len(ptr.buf) == 0 {
|
|
|
|
ptr.buf = nil
|
|
|
|
}
|
2014-02-26 02:45:14 +08:00
|
|
|
return C.int(n)
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2017-06-02 22:47:50 +08:00
|
|
|
//export go_read_bio_ctrl
|
|
|
|
func go_read_bio_ctrl(b *C.BIO, cmd C.int, arg1 C.long, arg2 unsafe.Pointer) (
|
2014-04-08 05:12:55 +08:00
|
|
|
rc C.long) {
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
logger.Critf("openssl: readBioCtrl panic'd: %v", err)
|
|
|
|
rc = -1
|
|
|
|
}
|
|
|
|
}()
|
2014-02-26 02:45:14 +08:00
|
|
|
switch cmd {
|
|
|
|
case C.BIO_CTRL_PENDING:
|
|
|
|
return readBioPending(b)
|
|
|
|
case C.BIO_CTRL_DUP, C.BIO_CTRL_FLUSH:
|
|
|
|
return 1
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func readBioPending(b *C.BIO) C.long {
|
2014-02-26 02:45:14 +08:00
|
|
|
ptr := loadReadPtr(b)
|
|
|
|
if ptr == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
ptr.data_mtx.Lock()
|
|
|
|
defer ptr.data_mtx.Unlock()
|
|
|
|
return C.long(len(ptr.buf))
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:47:21 +08:00
|
|
|
func (rb *readBio) ReadFromOnce(r io.Reader) (n int, err error) {
|
|
|
|
rb.op_mtx.Lock()
|
|
|
|
defer rb.op_mtx.Unlock()
|
2014-02-26 02:45:14 +08:00
|
|
|
|
|
|
|
// make sure we have a destination that fits at least one SSL record
|
2021-07-19 23:47:21 +08:00
|
|
|
rb.data_mtx.Lock()
|
|
|
|
if cap(rb.buf) < len(rb.buf)+SSLRecordSize {
|
|
|
|
new_buf := make([]byte, len(rb.buf), len(rb.buf)+SSLRecordSize)
|
|
|
|
copy(new_buf, rb.buf)
|
|
|
|
rb.buf = new_buf
|
2014-02-26 02:45:14 +08:00
|
|
|
}
|
2021-07-19 23:47:21 +08:00
|
|
|
dst := rb.buf[len(rb.buf):cap(rb.buf)]
|
|
|
|
dst_slice := rb.buf
|
|
|
|
rb.data_mtx.Unlock()
|
2014-02-26 02:45:14 +08:00
|
|
|
|
|
|
|
n, err = r.Read(dst)
|
2021-07-19 23:47:21 +08:00
|
|
|
rb.data_mtx.Lock()
|
|
|
|
defer rb.data_mtx.Unlock()
|
2014-02-26 02:45:14 +08:00
|
|
|
if n > 0 {
|
2021-07-19 23:47:21 +08:00
|
|
|
if len(dst_slice) != len(rb.buf) {
|
2014-02-26 02:45:14 +08:00
|
|
|
// someone shrunk the buffer, so we read in too far ahead and we
|
|
|
|
// need to slide backwards
|
2021-07-19 23:47:21 +08:00
|
|
|
copy(rb.buf[len(rb.buf):len(rb.buf)+n], dst)
|
2014-02-26 02:45:14 +08:00
|
|
|
}
|
2021-07-19 23:47:21 +08:00
|
|
|
rb.buf = rb.buf[:len(rb.buf)+n]
|
2014-02-26 02:45:14 +08:00
|
|
|
}
|
|
|
|
return n, err
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:47:21 +08:00
|
|
|
func (rb *readBio) MakeCBIO() *C.BIO {
|
2017-06-02 22:47:50 +08:00
|
|
|
rv := C.X_BIO_new_read_bio()
|
2021-07-19 23:47:21 +08:00
|
|
|
token := readBioMapping.Add(unsafe.Pointer(rb))
|
2017-06-02 22:47:50 +08:00
|
|
|
C.X_BIO_set_data(rv, unsafe.Pointer(token))
|
2014-02-26 02:45:14 +08:00
|
|
|
return rv
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:47:21 +08:00
|
|
|
func (rb *readBio) Disconnect(b *C.BIO) {
|
|
|
|
if loadReadPtr(b) == rb {
|
2017-06-02 22:47:50 +08:00
|
|
|
readBioMapping.Del(token(C.X_BIO_get_data(b)))
|
|
|
|
C.X_BIO_set_data(b, nil)
|
2014-02-26 02:45:14 +08:00
|
|
|
}
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:47:21 +08:00
|
|
|
func (rb *readBio) MarkEOF() {
|
|
|
|
rb.data_mtx.Lock()
|
|
|
|
defer rb.data_mtx.Unlock()
|
|
|
|
rb.eof = true
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type anyBio C.BIO
|
|
|
|
|
|
|
|
func asAnyBio(b *C.BIO) *anyBio { return (*anyBio)(b) }
|
|
|
|
|
|
|
|
func (b *anyBio) Read(buf []byte) (n int, err error) {
|
2014-03-01 05:24:02 +08:00
|
|
|
if len(buf) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2017-06-02 22:47:50 +08:00
|
|
|
n = int(C.X_BIO_read((*C.BIO)(b), unsafe.Pointer(&buf[0]), C.int(len(buf))))
|
2014-02-26 02:45:14 +08:00
|
|
|
if n <= 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
return n, nil
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *anyBio) Write(buf []byte) (written int, err error) {
|
2014-03-01 05:24:02 +08:00
|
|
|
if len(buf) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2017-06-02 22:47:50 +08:00
|
|
|
n := int(C.X_BIO_write((*C.BIO)(b), unsafe.Pointer(&buf[0]),
|
2014-02-26 02:45:14 +08:00
|
|
|
C.int(len(buf))))
|
|
|
|
if n != len(buf) {
|
|
|
|
return n, errors.New("BIO write failed")
|
|
|
|
}
|
|
|
|
return n, nil
|
2014-01-02 05:42:25 +08:00
|
|
|
}
|