Debian Codegraph @

ruby/lib/core/ssl.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 SSL support for Transport.
22 #
23 # A Transport may be configured ot use SLL for encryption and/or
24 # authentication. A Transport can be configured as either the SSL
25 # client or the server. An SSL client is the party that proctively
26 # establishes a connection to an SSL server. An SSL server is the
27 # party that accepts a connection request from the remote SSL client.
28 #
29 # If either the client or the server needs to identify itself with the
30 # remote node, it must have its SSL certificate configured.
31 #
32 # @see SSLDomain#credentials For setting the SSL certificate.
33 #
34 # If either the client or the server needs to verify the identify of the
35 # remote node, it must have its database of trusted CAs configured.
36 #
37 # @see SSLDomain#trusted_ca_db Setting the CA database.
38 #
39 # An SSL server connection may allow the remote client to connect without
40 # SS (i.e., "in the clear").
41 #
42 # @see SSLDomain#allow_unsecured_client Allowing unsecured clients.
43 #
44 # The level of verification required of the remote may be configured.
45 #
46 # @see SSLDomain#peer_authentication Setting peer authentication.
47 #
48 # Support for SSL client session resume is provided as well.
49 #
50 # @see SSLDomain
51 # @see #resume_status
52 #
53 class SSL
54
55 # Session resume state is unkonnwn or not supported.
56 RESUME_UNKNOWN = Cproton::PN_SSL_RESUME_UNKNOWN
57 # Session renegotiated and not resumed.
58 RESUME_NEW = Cproton::PN_SSL_RESUME_NEW
59 # Session resumed from the previous session.
60 RESUME_REUSED = Cproton::PN_SSL_RESUME_REUSED
61
62 # @private
63 PROTON_METHOD_PREFIX = "pn_ssl"
64 # @private
65 include Util::Wrapper
66 # @private
67 include Util::ErrorHandler
68
69 # Returns whether SSL is supported.
70 #
71 # @return [Boolean] True if SSL support is available.
72 #
73 def self.present?
74 Cproton.pn_ssl_present
75 end
76
77 # @private
78 def self.create(transport, domain, session_details = nil)
79 result = nil
80 # like python, make sure we're not creating a different SSL
81 # object for a transport with an existing SSL object
82 if transport.ssl?
83 transport.instance_eval { result = @ssl }
84 if ((!domain.nil? && (result.domain != domain)) ||
85 (!session_details.nil? && (result.session_details != session_details)))
86 raise SSLException.new("cannot re-configure existing SSL object")
87 end
88 else
89 impl = Cproton.pn_ssl(transport.impl)
90 session_id = nil
91 session_id = session_details.session_id unless session_details.nil?
92 result = SSL.new(impl, domain, session_details, session_id)
93 end
94 return result
95 end
96
97 private
98
99 def initialize(impl, domain, session_details, session_id)
100 @impl = impl
101 @domain = domain.impl unless domain.nil?
102 @session_details = session_details
103 @session_id = session_id
104 Cproton.pn_ssl_init(@impl, @domain, @session_id)
105 end
106
107 public
108
109 # Returns the cipher name that is currently in used.
110 #
111 # Gets the text description of the cipher that is currently active, or
112 # returns nil if SSL is not active. Note that the cipher in use my change
113 # over time due to renegotiation or other changes to the SSL layer.
114 #
115 # @return [String, nil] The cipher name.
116 #
117 def cipher_name
118 rc, name = Cproton.pn_ssl_get_cipher_name(@impl, 128)
119 return name if rc
120 nil
121 end
122
123 # Returns the name of the SSL protocol that is currently active, or
124 # returns nil if SSL is nota ctive. Not that the protocol may change over
125 # time due to renegotation.
126 #
127 # @return [String, nil] The protocol name.
128 #
129 def protocol_name
130 rc, name = Cproton.pn_ssl_get_protocol_name(@impl, 128)
131 name if rc
132 end
133
134 # Checks whether or not the state has resumed.
135 #
136 # Used for client session resume. When called on an active session, it
137 # indicates wehther the state has been resumed from a previous session.
138 #
139 # *NOTE:* This is a best-effort service - there is no guarantee that the
140 # remote server will accept the resumed parameters. The remote server may
141 # choose to ignore these parameters, and request a renegotation instead.
142 #
143 def resume_status
144 Cproton.pn_ssl_resume_status(@impl)
145 end
146
147 # Gets the peer hostname.
148 #
149 # @return [String] The peer hostname.
150 def peer_hostname
151 (error, name) = Cproton.pn_ssl_get_peer_hostname(@impl, 1024)
152 raise SSLError.new if error < 0
153 return name
154 end
155 end
156 end