| 1 | # | ||
| 2 | # Licensed to the Apache Software Foundation (ASF) under one | ||
| 3 | # or more contributor license agreements. See the NOTICE file | ||
| 4 | # distributed with this work for additional information | ||
| 5 | # regarding copyright ownership. The ASF licenses this file | ||
| 6 | # to you under the Apache License, Version 2.0 (the | ||
| 7 | # "License"); you may not use this file except in compliance | ||
| 8 | # with the License. You may obtain a copy of the License at | ||
| 9 | # | ||
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | # | ||
| 12 | # Unless required by applicable law or agreed to in writing, | ||
| 13 | # software distributed under the License is distributed on an | ||
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| 15 | # KIND, either express or implied. See the License for the | ||
| 16 | # specific language governing permissions and limitations | ||
| 17 | # under the License. | ||
| 18 | # | ||
| 19 | |||
| 20 | use 5.10.0; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | use Thrift; | ||
| 25 | use Thrift::SSLSocket; | ||
| 26 | use Thrift::ServerSocket; | ||
| 27 | |||
| 28 | use IO::Socket::SSL; | ||
| 29 | |||
| 30 | package Thrift::SSLServerSocket; | ||
| 31 | use base qw( Thrift::ServerSocket ); | ||
| 32 | use version 0.77; our $VERSION = version->declare("$Thrift::VERSION"); | ||
| 33 | |||
| 34 | # | ||
| 35 | # Constructor. | ||
| 36 | # Takes a hash: | ||
| 37 | # See Thrift::Socket for base class parameters. | ||
| 38 | # @param[in] ca certificate authority filename - not required | ||
| 39 | # @param[in] cert certificate filename; may contain key in which case key is not required | ||
| 40 | # @param[in] key private key filename for the certificate if it is not inside the cert file | ||
| 41 | # | ||
| 42 | sub new | ||
| 43 | { | ||
| 44 | my $classname = shift; | ||
| 45 | my $self = $classname->SUPER::new(@_); | ||
| 46 | return bless($self, $classname); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub __client | ||
| 50 | { | ||
| 51 | return Thrift::SSLSocket->new(); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub __listen | ||
| 55 | { | ||
| 56 | my $self = shift; | ||
| 57 | my $opts = {Listen => $self->{queue}, | ||
| 58 | LocalAddr => $self->{host}, | ||
| 59 | LocalPort => $self->{port}, | ||
| 60 | Proto => 'tcp', | ||
| 61 | ReuseAddr => 1}; | ||
| 62 | |||
| 63 | my $verify = IO::Socket::SSL::SSL_VERIFY_PEER | IO::Socket::SSL::SSL_VERIFY_FAIL_IF_NO_PEER_CERT | IO::Socket::SSL::SSL_VERIFY_CLIENT_ONCE; | ||
| 64 | |||
| 65 | $opts->{SSL_ca_file} = $self->{ca} if defined $self->{ca}; | ||
| 66 | $opts->{SSL_cert_file} = $self->{cert} if defined $self->{cert}; | ||
| 67 | $opts->{SSL_cipher_list} = $self->{ciphers} if defined $self->{ciphers}; | ||
| 68 | $opts->{SSL_key_file} = $self->{key} if defined $self->{key}; | ||
| 69 | $opts->{SSL_use_cert} = (defined $self->{cert}) ? 1 : 0; | ||
| 70 | $opts->{SSL_verify_mode} = (defined $self->{ca}) ? $verify : IO::Socket::SSL::SSL_VERIFY_NONE; | ||
| 71 | $opts->{SSL_version} = (defined $self->{version}) ? $self->{version} : 'SSLv23:!SSLv3:!SSLv2'; | ||
| 72 | |||
| 73 | return IO::Socket::SSL->new(%$opts); | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; |