aboutsummaryrefslogtreecommitdiff
path: root/jessl.f90
blob: bd9d1fe1d02c42cf8947647e3b8fb52c8809719f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
! Copyright (c) 2020 Jeffrey Armstrong <jeff@rainbow-100.com>
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! The Software shall be used for Good, not Evil.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.

! Just Enough SSL...

module jessl
implicit none

    interface 
    
        subroutine library_init() bind(c, name="OPENSSL_init_ssl")
        end subroutine library_init

        subroutine add_ssl_algorithms() bind(c, name="SSLeay_add_ssl_algorithms")
        end subroutine add_ssl_algorithms
        
        subroutine load_error_strings() bind(c, name="SSL_load_error_strings")
        end subroutine load_error_strings
        
        function tls_client_method() bind(c, name="TLS_client_method")
        use iso_c_binding
        type(c_ptr)::tls_client_method
        end function tls_client_method

        function ctx_new(meth) bind(c, name="SSL_CTX_new")
        use iso_c_binding
        type(c_ptr)::ctx_new
        type(c_ptr), value::meth
        end function ctx_new
        
        function ssl_new(ctx) bind(c, name="SSL_new")
        use iso_c_binding
        type(c_ptr)::ssl_new
        type(c_ptr), value::ctx
        end function ssl_new
        
        function get_fd(ssl) bind(c, name="SSL_get_fd")
        use iso_c_binding
        integer(kind=c_int)::get_fd
        type(c_ptr), value::ssl
        end function get_fd
        
        function set_fd(ssl, fd) bind(c, name="SSL_set_fd")
        use iso_c_binding
        integer(kind=c_int)::set_fd
        integer(kind=c_int), value::fd
        type(c_ptr), value::ssl
        end function set_fd
        
        function ssl_connect(ssl) bind(c, name="SSL_connect")
        use iso_c_binding
        integer(kind=c_int)::ssl_connect
        type(c_ptr), value::ssl
        end function ssl_connect
        
        function ssl_shutdown(ssl) bind(c, name="SSL_shutdown")
        use iso_c_binding
        integer(kind=c_int)::ssl_shutdown
        type(c_ptr), value::ssl
        end function ssl_shutdown
        
        function ssl_free(ssl) bind(c, name="SSL_free")
        use iso_c_binding
        integer(kind=c_int)::ssl_free
        type(c_ptr), value::ssl
        end function ssl_free
        
        function ctx_free(ctx) bind(c, name="SSL_CTX_free")
        use iso_c_binding
        integer(kind=c_int)::ctx_free
        type(c_ptr), value::ctx
        end function ctx_free
        
        ! Actually a macro...
        !function get_cipher_c(ssl) bind(c, name="SSL_get_cipher_name")
        !use iso_c_binding
        !type(c_ptr)::get_cipher_c
        !type(c_ptr), value::ssl
        !end function get_cipher_c
        
        function read_c(ssl, buf, length) bind(c, name="SSL_read")
        use iso_c_binding
        type(c_ptr), value::ssl
        character(kind=c_char), dimension(*), intent(inout)::buf
        integer(kind=c_int), value::length
        integer(kind=c_int)::read_c
        end function read_c
        
        function write_c(ssl, buf, length) bind(c, name="SSL_write")
        use iso_c_binding
        type(c_ptr), value::ssl
        character(kind=c_char), dimension(*), intent(inout)::buf
        integer(kind=c_int), value::length
        integer(kind=c_int)::write_c
        end function write_c
        
        function get_error(ssl, retcode) bind(c, name="SSL_get_error")
        use iso_c_binding
        type(c_ptr), value::ssl
        integer(kind=c_int), value::retcode
        integer(kind=c_int)::get_error
        end function get_error
        
        function ssl_pending(ssl) bind(c, name="SSL_pending")
        use iso_c_binding
        type(c_ptr), value::ssl
        integer(kind=c_int)::ssl_pending
        end function ssl_pending
        
    end interface

contains

    function ssl_read(ssl, buf)
    use iso_c_binding
    implicit none
    
        type(c_ptr)::ssl
        character, dimension(:), intent(inout)::buf
        integer::ssl_read
        integer::bufsize
        
        character(kind=c_char), dimension(:), allocatable::cbuf
        bufsize = size(buf)
        allocate(cbuf(bufsize))
        
        ssl_read = read_c(ssl, cbuf, 1)
        buf = cbuf
        
        deallocate(cbuf)
        
    end function ssl_read
    
    function ssl_write(ssl, buf)
    use iso_c_binding
    implicit none
    
        type(c_ptr)::ssl
        character, dimension(:), intent(in)::buf
        integer::ssl_write
        
        character(kind=c_char), dimension(:), allocatable::cbuf
        
        allocate(cbuf(size(buf)))
        
        cbuf = buf
        cbuf(size(buf)) = c_null_char
        ssl_write = write_c(ssl, cbuf, size(buf))

        deallocate(cbuf)
    
    end function ssl_write
    
    subroutine get_cipher(ssl, res)
    use iso_c_binding
    implicit none
    
        character(:), allocatable, intent(out)::res
        type(c_ptr)::ssl
        
        type(c_ptr)::cptr
        
        character(kind=c_char), dimension(:), pointer::cstring
        
        integer::i
        
        cptr = c_null_ptr
        if(.not. c_associated(cptr)) then
        
            allocate(character(len=1)::res)
            res = " "
        
        else
        
            call c_f_pointer(cptr, cstring, [1])
        
            i = 1
            do while(cstring(i) /= c_null_char)
                i = i + 1
            end do
            allocate(character(len=(i-1))::res)
            
            i = 1
            do while(cstring(i) /= c_null_char)
                res(i:i) = cstring(i)
            end do
            
        end if
        
    end subroutine get_cipher

end module jessl