Top Level Namespace

Defined Under Namespace

Modules: Vcert

Constant Summary collapse

TIMEOUT =
420
CLIENT_ID =
'vcert-sdk'.freeze
SCOPE =
'certificate:manage,revoke'.freeze
LOG =
Logger.new(STDOUT)

Instance Method Summary collapse

Instance Method Details

#getApiClientInformationObject



149
150
151
152
153
154
155
156
157
# File 'lib/utils/utils.rb', line 149

def getApiClientInformation()
  ip = Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
  ip_addres = ip.ip_address
  data = {
    type: CLIENT_ID,
    identifier: ip_addres
  }
  return data
end

#parse_csr_fields(csr) ⇒ Object



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
# File 'lib/utils/utils.rb', line 23

def parse_csr_fields(csr)
  LOG.info("#{Vcert::VCERT_PREFIX} Trying to parse CSR:\n#{csr}")
  csr_obj = OpenSSL::X509::Request.new(csr)
  result = Hash.new

  subject_array = csr_obj.subject.to_a
  subject_array.map do |x|
    if x[1] != ""
      result[x[0].to_sym] = x[1]
    end
  end

  attributes = csr_obj.attributes

  seq = nil
  values = nil

  if attributes
    attributes.each do |a|
      if a.oid == 'extReq'
        seq = a.value
        break
      end
    end
    # return nil if not seq
  end

  if seq
    seq.value.each do |v|
      v.each do |v|
        if v.value[0].value == 'subjectAltName'
          values = v.value[1].value
          break
        end
        break if values
      end
    end
    # return nil if not values
  end


  if values
    values = OpenSSL::ASN1.decode(values).value

    values.each do |v|
      case v.tag
      when 2
        result[:DNS] = v.value
      when 7
        case v.value.size
        when 4
          ip = v.value.unpack('C*').join('.')
        when 16
          ip = v.value.unpack('n*').map { |o| sprintf("%X", o) }.join(':')
        else
          STDERR.print "The encountered IP-address is neither IPv4 nor IPv6\n"
          next
        end
        result[:IP] = ip
      else
        STDERR.print "Uknown tag #{v.tag} -- I only know 2 (DNS) and 7 (IP)\n"
      end
    end
  end

  if csr_obj.public_key.instance_of? OpenSSL::PKey::RSA
    result[:key_type] = Vcert::KeyType.new "rsa", csr_obj.public_key.n.num_bits
  elsif csr_obj.public_key.instance_of? OpenSSL::PKey::EC
    # todo: implement
    raise "not implemented"
  else
    raise Vcert::VcertError
  end


  LOG.info("#{Vcert::VCERT_PREFIX} Parsed CSR fields:\n #{result.inspect}")
  return result
end

#parse_csr_fields_tpp(csr) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/utils/utils.rb', line 102

def parse_csr_fields_tpp(csr)
  LOG.info("#{Vcert::VCERT_PREFIX} Trying to parse CSR:\n#{csr}")
  csr_obj = OpenSSL::X509::Certificate.new(csr)
  result = Hash.new

  subject_array = csr_obj.subject.to_a
  subject_array.map do |x|
    result[x[0].to_sym] = x[1] unless x[1] == ''
  end

  LOG.info("#{Vcert::VCERT_PREFIX} Parsed CSR fields:\n #{result.inspect}")
  result
end

#parse_pem_list(multiline) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/utils/utils.rb', line 3

def parse_pem_list(multiline)
  pems = []
  buf = ""
  current_string_is_pem = false
  multiline.each_line do |line|
    if line.match(/-----BEGIN [A-Z\ ]+-----/)
      current_string_is_pem = true
    end
    if current_string_is_pem
      buf = buf + line
    end
    if line.match(/-----END [A-Z\ ]+-----/)
      current_string_is_pem = false
      pems.push(buf)
      buf = ""
    end
  end
  pems
end