Debian Codegraph @

ruby/lib/core/ssl_domain.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18
19 module Qpid::Proton
20
21 # The top-level object that stores the configuration used by one or more
22 # SSL sessions.
23 #
24 # @see SSL
25 #
26 class SSLDomain
27
28 # The local connection endpoint is an SSL client.
29 # @private
30 MODE_CLIENT = Cproton::PN_SSL_MODE_CLIENT
31 # The local connection endpoint is an SSL server.
32 # @private
33 MODE_SERVER = Cproton::PN_SSL_MODE_SERVER
34
35 # Require the peer to provide a valid identifying certificate.
36 VERIFY_PEER = Cproton::PN_SSL_VERIFY_PEER
37 # Do no require a certificate nor a cipher authorization.
38 ANONYMOUS_PEER = Cproton::PN_SSL_ANONYMOUS_PEER
39 # Require a valid certficate and matching name.
40 VERIFY_PEER_NAME = Cproton::PN_SSL_VERIFY_PEER_NAME
41
42 # @private
43 include Util::ErrorHandler
44
45 # @private
46 attr_reader :impl
47
48 # @private
49 def initialize(mode)
50 @impl = Cproton.pn_ssl_domain(mode)
51 raise Qpid::Proton::SSLError, "SSL Unavailable" if @impl.nil?
52 end
53
54 # Set the certificate that identifies the local node to the remote.
55 #
56 # This certificate establishes the identity for thelocal node for all SSL
57 # sessions created from this domain. It will be sent to the remote if the
58 # remote needs to verify the dientify of this node. This may be used for
59 # both SSL servers and SSL clients (if client authentication is required by
60 # the server).
61 #
62 # *NOTE:* This setting affects only those instances of SSL created *after*
63 # this call returns. SSL objects created before invoking this method will
64 # use the domain's previous settings.
65 #
66 # @param cert_file [String] The filename containing the identify
67 # certificate. For OpenSSL users, this is a PEM file. For Windows SChannel
68 # users, this is the PKCS\#12 file or system store.
69 # @param key_file [String] An option key to access the identifying
70 # certificate. For OpenSSL users, this is an optional PEM file containing
71 # the private key used to sign the certificate. For Windows SChannel users,
72 # this is the friendly name of the self-identifying certficate if there are
73 # multiple certfificates in the store.
74 # @param password [String] The password used to sign the key, or *nil* if
75 # the key is not protected.
76 #
77 # @raise [SSLError] If an error occurs.
78 #
79 def credentials(cert_file, key_file, password)
80 Cproton.pn_ssl_domain_set_credentials(@impl,
81 cert_file, key_file, password)
82 end
83
84 # Configures the set of trusted CA certificates used by this domain to
85 # verify peers.
86 #
87 # If the local SSL client/server needs to verify the identify of the remote,
88 # it must validate the signature of the remote's certificate. This function
89 # sets the database of trusted CAs that will be used to verify the signature
90 # of the remote's certificate.
91 #
92 # *NOTE:# This setting affects only those SSL instances created *after* this
93 # call returns. SSL objects created before invoking this method will use the
94 # domain's previous setting.
95 #
96 # @param certificate_db [String] The filename for the databse of trusted
97 # CAs, used to authenticate the peer.
98 #
99 # @raise [SSLError] If an error occurs.
100 #
101 def trusted_ca_db(certificate_db)
102 Cproton.pn_ssl_domain_set_trusted_ca_db(@impl, certificate_db)
103 end
104
105 # Configures the level of verification used on the peer certificate.
106 #
107 # This method congtrols how the peer's certificate is validated, if at all.
108 # By default, servers do not attempt to verify their peers
109 # (*ANONYMOUS_PEER*) but clients attempt to verify both the certificate and
110 # peer name (*VERIFY_PEER_NAME*). Once certficates and trusted CAs are
111 # configured, peer verification can be enabled.
112 #
113 # *NOTE:* In order to verify a peer, a trusted CA must be configured.
114 #
115 # *NOTE:* Servers must provide their own certficate when verifying a peer.
116 #
117 # *NOTE:* This setting affects only those SSL instances created after this
118 # call returns. SSL instances created before invoking this method will use
119 # the domain's previous setting.
120 #
121 # @param verify_mode [Integer] The level of validation to apply to the peer.
122 # @param trusted_CAs [String] The path to a database of trusted CAs that
123 # the server will advertise to the peer client if the server has been
124 # configured to verify its peer.
125 #
126 # @see VERIFY_PEER
127 # @see ANONYMOUS_PEER
128 # @see VERIFY_PEER_NAME
129 #
130 # @raise [SSLError] If an error occurs.
131 #
132 def peer_authentication(verify_mode, trusted_CAs = nil)
133 Cproton.pn_ssl_domain_set_peer_authentication(@impl,
134 verify_mode, trusted_CAs)
135 end
136
137 # Permit a server to accept connection requests from non-SSL clients.
138 #
139 # This configures the server to "sniff" the incomfing client data stream and
140 # dynamically determine whether SSL/TLS is being used. This option is
141 # disabled by default: only clients using SSL/TLS are accepted by default.
142 #
143 # @raise [SSLError] If an error occurs.
144 #
145 def allow_unsecured_client
146 Cproton.pn_ssl_domain_allow_unsecured_client(@impl);
147 end
148
149 can_raise_error :credentials, :error_class => Qpid::Proton::SSLError
150 can_raise_error :trusted_ca_db, :error_class => Qpid::Proton::SSLError
151 can_raise_error :peer_authentication, :error_class => Qpid::Proton::SSLError
152 can_raise_error :allow_unsecured_client, :error_class => Qpid::Proton::SSLError
153 end
154 end