Class: UDAPSecurityTestKit::UDAPJWTValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/udap_security_test_kit/udap_jwt_validator.rb

Class Method Summary collapse

Class Method Details

.get_crl_from_uri(crl_uri) ⇒ Object



65
66
67
68
69
# File 'lib/udap_security_test_kit/udap_jwt_validator.rb', line 65

def self.get_crl_from_uri(crl_uri)
  uri = URI(crl_uri)
  crl = Net::HTTP.get(uri)
  OpenSSL::X509::CRL.new(crl)
end

.validate_signature(signed_metadata_jwt, algorithm, cert) ⇒ Object



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

def self.validate_signature(, algorithm, cert)
  JWT.decode(
    ,
    cert.public_key,
    true,
    algorithm:
  )
  {
    success: true,
    error_message: nil
  }
rescue JWT::DecodeError => e
  {
    success: false,
    error_message: e.full_message
  }
end

.validate_trust_chain(x5c_header_encoded, trust_anchor_certs) ⇒ 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
# File 'lib/udap_security_test_kit/udap_jwt_validator.rb', line 23

def self.validate_trust_chain(x5c_header_encoded, trust_anchor_certs)
  cert_chain = x5c_header_encoded.map do |cert|
    cert_der = Base64.decode64(cert)
    OpenSSL::X509::Certificate.new(cert_der)
  end
  crl_uris = cert_chain.map(&:crl_uris).compact.flatten
  crl_uris_anchors = trust_anchor_certs.map(&:crl_uris).compact.flatten
  crl_uris.concat(crl_uris_anchors)
  begin
    crls = crl_uris.map do |uri|
      get_crl_from_uri(uri)
    end
  rescue OpenSSL::X509::CRLError => e
    return {
      success: false,
      error_message: e.message
    }
  end

  begin
    # JWT library can validate the trust chain while decoding the provided
    # JWT/verifying its signature, but we don't have currently have access
    # to certs that satisfy both prerequisites to doing this. We have:
    # A) client certs that can establish a legitimate trust chain (but don't
    # have access to private key needed to create a valid, signed JWT with them)
    # B) client certs that have a valid private key (but which cannot
    # establish a legitimate trust chain)
    # As a result, these capabilities are decoupled for testing purposes
    JWT::X5cKeyFinder.new(trust_anchor_certs,
                          crls).from(x5c_header_encoded)
    {
      success: true,
      error_message: nil
    }
  rescue JWT::VerificationError => e
    {
      success: false,
      error_message: e.full_message
    }
  end
end