mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/openssl/lib/openssl/*.rb: [DOC] Document the following:
- Integer#to_bn - OpenSSL::Buffering module - Document deprecated OpenSSL::Digest::Digest compatibility class - OpenSSL::Config These changes were based on a patch by @vbatts via GH-436 https://github.com/ruby/ruby/pull/436 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43663 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2019f34bc9
commit
33b63fcf93
5 changed files with 190 additions and 5 deletions
12
ChangeLog
12
ChangeLog
|
@ -1,3 +1,15 @@
|
||||||
|
Wed Nov 13 17:09:45 2013 Zachary Scott <e@zzak.io>
|
||||||
|
|
||||||
|
* ext/openssl/lib/openssl/*.rb: [DOC] Document the following:
|
||||||
|
|
||||||
|
- Integer#to_bn
|
||||||
|
- OpenSSL::Buffering module
|
||||||
|
- Document deprecated OpenSSL::Digest::Digest compatibility class
|
||||||
|
- OpenSSL::Config
|
||||||
|
|
||||||
|
These changes were based on a patch by @vbatts via GH-436
|
||||||
|
https://github.com/ruby/ruby/pull/436
|
||||||
|
|
||||||
Wed Nov 13 10:55:43 2013 Zachary Scott <e@zzak.io>
|
Wed Nov 13 10:55:43 2013 Zachary Scott <e@zzak.io>
|
||||||
|
|
||||||
* doc/regexp.rdoc: [DOC] Fix typo in Special global variables section.
|
* doc/regexp.rdoc: [DOC] Fix typo in Special global variables section.
|
||||||
|
|
|
@ -28,6 +28,9 @@ end # OpenSSL
|
||||||
# Add double dispatch to Integer
|
# Add double dispatch to Integer
|
||||||
#
|
#
|
||||||
class Integer
|
class Integer
|
||||||
|
# Casts an Integer as an OpenSSL::BN
|
||||||
|
#
|
||||||
|
# See `man bn` for more info.
|
||||||
def to_bn
|
def to_bn
|
||||||
OpenSSL::BN::new(self)
|
OpenSSL::BN::new(self)
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,6 +18,9 @@
|
||||||
# OpenSSL IO buffering mix-in module.
|
# OpenSSL IO buffering mix-in module.
|
||||||
#
|
#
|
||||||
# This module allows an OpenSSL::SSL::SSLSocket to behave like an IO.
|
# This module allows an OpenSSL::SSL::SSLSocket to behave like an IO.
|
||||||
|
#
|
||||||
|
# You typically won't use this module directly, you can see it implemented in
|
||||||
|
# OpenSSL::SSL::SSLSocket.
|
||||||
|
|
||||||
module OpenSSL::Buffering
|
module OpenSSL::Buffering
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
@ -34,6 +37,9 @@ module OpenSSL::Buffering
|
||||||
|
|
||||||
BLOCK_SIZE = 1024*16
|
BLOCK_SIZE = 1024*16
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates an instance of OpenSSL's buffering IO module.
|
||||||
|
|
||||||
def initialize(*args)
|
def initialize(*args)
|
||||||
@eof = false
|
@eof = false
|
||||||
@rbuffer = ""
|
@rbuffer = ""
|
||||||
|
|
|
@ -13,10 +13,25 @@
|
||||||
require 'stringio'
|
require 'stringio'
|
||||||
|
|
||||||
module OpenSSL
|
module OpenSSL
|
||||||
|
##
|
||||||
|
# = OpenSSL::Config
|
||||||
|
#
|
||||||
|
# Configuration for the openssl library.
|
||||||
|
#
|
||||||
|
# Many system's installation of openssl library will depend on your system
|
||||||
|
# configuration. See the value of OpenSSL::Config::DEFAULT_CONFIG_FILE for
|
||||||
|
# the location of the file for your host.
|
||||||
|
#
|
||||||
|
# See also http://www.openssl.org/docs/apps/config.html
|
||||||
class Config
|
class Config
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
|
##
|
||||||
|
# Parses a given +str+ as a blob that contains configuration for openssl.
|
||||||
|
#
|
||||||
|
# If the source of the IO is a file, then consider using #parse_config.
|
||||||
def parse(str)
|
def parse(str)
|
||||||
c = new()
|
c = new()
|
||||||
parse_config(StringIO.new(str)).each do |section, hash|
|
parse_config(StringIO.new(str)).each do |section, hash|
|
||||||
|
@ -25,8 +40,14 @@ module OpenSSL
|
||||||
c
|
c
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# load is an alias to ::new
|
||||||
alias load new
|
alias load new
|
||||||
|
|
||||||
|
##
|
||||||
|
# Parses the configuration data read from +io+, see also #parse.
|
||||||
|
#
|
||||||
|
# Raises a ConfigError on invalid configuration data.
|
||||||
def parse_config(io)
|
def parse_config(io)
|
||||||
begin
|
begin
|
||||||
parse_config_lines(io)
|
parse_config_lines(io)
|
||||||
|
@ -209,6 +230,18 @@ module OpenSSL
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates an instance of OpenSSL's configuration class.
|
||||||
|
#
|
||||||
|
# This can be used in contexts like OpenSSL::X509::ExtensionFactory.config=
|
||||||
|
#
|
||||||
|
# If the optional +filename+ parameter is provided, then it is read in and
|
||||||
|
# parsed via #parse_config.
|
||||||
|
#
|
||||||
|
# This can raise IO exceptions based on the access, or availability of the
|
||||||
|
# file. A ConfigError exception may be raised depending on the validity of
|
||||||
|
# the data being configured.
|
||||||
|
#
|
||||||
def initialize(filename = nil)
|
def initialize(filename = nil)
|
||||||
@data = {}
|
@data = {}
|
||||||
if filename
|
if filename
|
||||||
|
@ -220,6 +253,23 @@ module OpenSSL
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Gets the value of +key+ from the given +section+
|
||||||
|
#
|
||||||
|
# Given the following configurating file being loaded:
|
||||||
|
#
|
||||||
|
# config = OpenSSL::Config.load('foo.cnf')
|
||||||
|
# #=> #<OpenSSL::Config sections=["default"]>
|
||||||
|
# puts config.to_s
|
||||||
|
# #=> [ default ]
|
||||||
|
# # foo=bar
|
||||||
|
#
|
||||||
|
# You can get a specific value from the config if you know the +section+
|
||||||
|
# and +key+ like so:
|
||||||
|
#
|
||||||
|
# config.get_value('default','foo')
|
||||||
|
# #=> "bar"
|
||||||
|
#
|
||||||
def get_value(section, key)
|
def get_value(section, key)
|
||||||
if section.nil?
|
if section.nil?
|
||||||
raise TypeError.new('nil not allowed')
|
raise TypeError.new('nil not allowed')
|
||||||
|
@ -228,7 +278,12 @@ module OpenSSL
|
||||||
get_key_string(section, key)
|
get_key_string(section, key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def value(arg1, arg2 = nil)
|
##
|
||||||
|
#
|
||||||
|
# *Deprecated*
|
||||||
|
#
|
||||||
|
# Use #get_value instead
|
||||||
|
def value(arg1, arg2 = nil) # :nodoc:
|
||||||
warn('Config#value is deprecated; use Config#get_value')
|
warn('Config#value is deprecated; use Config#get_value')
|
||||||
if arg2.nil?
|
if arg2.nil?
|
||||||
section, key = 'default', arg1
|
section, key = 'default', arg1
|
||||||
|
@ -240,20 +295,84 @@ module OpenSSL
|
||||||
get_key_string(section, key)
|
get_key_string(section, key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Set the target +key+ with a given +value+ under a specific +section+.
|
||||||
|
#
|
||||||
|
# Given the following configurating file being loaded:
|
||||||
|
#
|
||||||
|
# config = OpenSSL::Config.load('foo.cnf')
|
||||||
|
# #=> #<OpenSSL::Config sections=["default"]>
|
||||||
|
# puts config.to_s
|
||||||
|
# #=> [ default ]
|
||||||
|
# # foo=bar
|
||||||
|
#
|
||||||
|
# You can set the value of +foo+ under the +default+ section to a new
|
||||||
|
# value:
|
||||||
|
#
|
||||||
|
# config.add_value('default', 'foo', 'buzz')
|
||||||
|
# #=> "buzz"
|
||||||
|
# puts config.to_s
|
||||||
|
# #=> [ default ]
|
||||||
|
# # foo=buzz
|
||||||
|
#
|
||||||
def add_value(section, key, value)
|
def add_value(section, key, value)
|
||||||
check_modify
|
check_modify
|
||||||
(@data[section] ||= {})[key] = value
|
(@data[section] ||= {})[key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Get a specific +section+ from the current configuration
|
||||||
|
#
|
||||||
|
# Given the following configurating file being loaded:
|
||||||
|
#
|
||||||
|
# config = OpenSSL::Config.load('foo.cnf')
|
||||||
|
# #=> #<OpenSSL::Config sections=["default"]>
|
||||||
|
# puts config.to_s
|
||||||
|
# #=> [ default ]
|
||||||
|
# # foo=bar
|
||||||
|
#
|
||||||
|
# You can get a hash of the specific section like so:
|
||||||
|
#
|
||||||
|
# config['default']
|
||||||
|
# #=> {"foo"=>"bar"}
|
||||||
|
#
|
||||||
def [](section)
|
def [](section)
|
||||||
@data[section] || {}
|
@data[section] || {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def section(name)
|
##
|
||||||
|
# Deprecated
|
||||||
|
#
|
||||||
|
# Use #[] instead
|
||||||
|
def section(name) # :nodoc:
|
||||||
warn('Config#section is deprecated; use Config#[]')
|
warn('Config#section is deprecated; use Config#[]')
|
||||||
@data[name] || {}
|
@data[name] || {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Sets a specific +section+ name with a Hash +pairs+
|
||||||
|
#
|
||||||
|
# Given the following configuration being created:
|
||||||
|
#
|
||||||
|
# config = OpenSSL::Config.new
|
||||||
|
# #=> #<OpenSSL::Config sections=[]>
|
||||||
|
# config['default'] = {"foo"=>"bar","baz"=>"buz"}
|
||||||
|
# #=> {"foo"=>"bar", "baz"=>"buz"}
|
||||||
|
# puts config.to_s
|
||||||
|
# #=> [ default ]
|
||||||
|
# # foo=bar
|
||||||
|
# # baz=buz
|
||||||
|
#
|
||||||
|
# It's important to note that this will essentially merge any of the keys
|
||||||
|
# in +pairs+ with the existing +section+. For example:
|
||||||
|
#
|
||||||
|
# config['default']
|
||||||
|
# #=> {"foo"=>"bar", "baz"=>"buz"}
|
||||||
|
# config['default'] = {"foo" => "changed"}
|
||||||
|
# #=> {"foo"=>"changed"}
|
||||||
|
# config['default']
|
||||||
|
# #=> {"foo"=>"changed", "baz"=>"buz"}
|
||||||
|
#
|
||||||
def []=(section, pairs)
|
def []=(section, pairs)
|
||||||
check_modify
|
check_modify
|
||||||
@data[section] ||= {}
|
@data[section] ||= {}
|
||||||
|
@ -262,10 +381,38 @@ module OpenSSL
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Get the names of all sections in the current configuration
|
||||||
def sections
|
def sections
|
||||||
@data.keys
|
@data.keys
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Get the parsable form of the current configuration
|
||||||
|
#
|
||||||
|
# Given the following configuration being created:
|
||||||
|
#
|
||||||
|
# config = OpenSSL::Config.new
|
||||||
|
# #=> #<OpenSSL::Config sections=[]>
|
||||||
|
# config['default'] = {"foo"=>"bar","baz"=>"buz"}
|
||||||
|
# #=> {"foo"=>"bar", "baz"=>"buz"}
|
||||||
|
# puts config.to_s
|
||||||
|
# #=> [ default ]
|
||||||
|
# # foo=bar
|
||||||
|
# # baz=buz
|
||||||
|
#
|
||||||
|
# You can parse get the serialized configuration using #to_s and then parse
|
||||||
|
# it later:
|
||||||
|
#
|
||||||
|
# serialized_config = config.to_s
|
||||||
|
# # much later...
|
||||||
|
# new_config = OpenSSL::Config.parse(serialized_config)
|
||||||
|
# #=> #<OpenSSL::Config sections=["default"]>
|
||||||
|
# puts new_config
|
||||||
|
# #=> [ default ]
|
||||||
|
# foo=bar
|
||||||
|
# baz=buz
|
||||||
|
#
|
||||||
def to_s
|
def to_s
|
||||||
ary = []
|
ary = []
|
||||||
@data.keys.sort.each do |section|
|
@data.keys.sort.each do |section|
|
||||||
|
@ -278,6 +425,15 @@ module OpenSSL
|
||||||
ary.join
|
ary.join
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# For a block.
|
||||||
|
#
|
||||||
|
# Receive the section and its pairs for the current configuration.
|
||||||
|
#
|
||||||
|
# config.each do |section, key, value|
|
||||||
|
# # ...
|
||||||
|
# end
|
||||||
|
#
|
||||||
def each
|
def each
|
||||||
@data.each do |section, hash|
|
@data.each do |section, hash|
|
||||||
hash.each do |key, value|
|
hash.each do |key, value|
|
||||||
|
@ -286,13 +442,16 @@ module OpenSSL
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# String representation of this configuration object, including the class
|
||||||
|
# name and its sections.
|
||||||
def inspect
|
def inspect
|
||||||
"#<#{self.class.name} sections=#{sections.inspect}>"
|
"#<#{self.class.name} sections=#{sections.inspect}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def data
|
def data # :nodoc:
|
||||||
@data
|
@data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -59,8 +59,13 @@ module OpenSSL
|
||||||
const_set(name, klass)
|
const_set(name, klass)
|
||||||
}
|
}
|
||||||
|
|
||||||
# This class is only provided for backwards compatibility. Use OpenSSL::Digest in the future.
|
# This class is only provided for backwards compatibility.
|
||||||
class Digest < Digest
|
#
|
||||||
|
# Use OpenSSL::Digest in the future.
|
||||||
|
class Digest < Digest # :nodoc:
|
||||||
|
# Deprecated.
|
||||||
|
#
|
||||||
|
# See OpenSSL::Digest.new
|
||||||
def initialize(*args)
|
def initialize(*args)
|
||||||
# add warning
|
# add warning
|
||||||
super(*args)
|
super(*args)
|
||||||
|
|
Loading…
Add table
Reference in a new issue