@@ -592,30 +592,8 @@ def test_v_flag_partial_chain
592592 assert_equal OpenSSL ::X509 ::V_OK , store2 . error
593593 end
594594
595- # Helper: build a proper ASN.1 nameConstraints extension since
596- # JRuby's create_extension doesn't encode nameConstraints correctly yet.
597595 private
598596
599- def build_name_constraints_ext ( permitted_dns : nil , excluded_dns : nil )
600- subtrees = [ ]
601- if permitted_dns
602- dns_names = Array ( permitted_dns ) . map do |name |
603- dns = OpenSSL ::ASN1 ::IA5String . new ( name , 2 , :IMPLICIT , :CONTEXT_SPECIFIC )
604- OpenSSL ::ASN1 ::Sequence . new ( [ dns ] )
605- end
606- subtrees << OpenSSL ::ASN1 ::Sequence . new ( dns_names , 0 , :IMPLICIT , :CONTEXT_SPECIFIC )
607- end
608- if excluded_dns
609- dns_names = Array ( excluded_dns ) . map do |name |
610- dns = OpenSSL ::ASN1 ::IA5String . new ( name , 2 , :IMPLICIT , :CONTEXT_SPECIFIC )
611- OpenSSL ::ASN1 ::Sequence . new ( [ dns ] )
612- end
613- subtrees << OpenSSL ::ASN1 ::Sequence . new ( dns_names , 1 , :IMPLICIT , :CONTEXT_SPECIFIC )
614- end
615- nc = OpenSSL ::ASN1 ::Sequence . new ( subtrees )
616- OpenSSL ::X509 ::Extension . new ( "nameConstraints" , nc . to_der , true )
617- end
618-
619597 def build_cert_with_san ( name , serial , san_dns , issuer_cert , issuer_key )
620598 key = OpenSSL ::PKey ::RSA . new ( 2048 )
621599 cert = OpenSSL ::X509 ::Certificate . new
@@ -638,10 +616,9 @@ def test_name_constraints_permitted_dns
638616 now = Time . now
639617 ca_key = OpenSSL ::PKey ::RSA . new ( 2048 )
640618 ca_cert = issue_cert ( OpenSSL ::X509 ::Name . parse ( "/CN=CA" ) , ca_key , 1 ,
641- [ [ "basicConstraints" , "CA:TRUE" , true ] , [ "keyUsage" , "cRLSign,keyCertSign" , true ] ] ,
619+ [ [ "basicConstraints" , "CA:TRUE" , true ] , [ "keyUsage" , "cRLSign,keyCertSign" , true ] ,
620+ [ "nameConstraints" , "permitted;DNS:.example.com" , true ] ] ,
642621 nil , nil , not_before : now , not_after : now + 3600 )
643- ca_cert . add_extension ( build_name_constraints_ext ( permitted_dns : [ ".example.com" ] ) )
644- ca_cert . sign ( ca_key , "SHA256" ) # re-sign after adding extension
645622
646623 good = build_cert_with_san ( "good" , 10 , "good.example.com" , ca_cert , ca_key )
647624 bad = build_cert_with_san ( "bad" , 11 , "evil.attacker.com" , ca_cert , ca_key )
@@ -658,10 +635,9 @@ def test_name_constraints_excluded_dns
658635 now = Time . now
659636 ca_key = OpenSSL ::PKey ::RSA . new ( 2048 )
660637 ca_cert = issue_cert ( OpenSSL ::X509 ::Name . parse ( "/CN=CA" ) , ca_key , 1 ,
661- [ [ "basicConstraints" , "CA:TRUE" , true ] , [ "keyUsage" , "cRLSign,keyCertSign" , true ] ] ,
638+ [ [ "basicConstraints" , "CA:TRUE" , true ] , [ "keyUsage" , "cRLSign,keyCertSign" , true ] ,
639+ [ "nameConstraints" , "excluded;DNS:.evil.com" , true ] ] ,
662640 nil , nil , not_before : now , not_after : now + 3600 )
663- ca_cert . add_extension ( build_name_constraints_ext ( excluded_dns : [ ".evil.com" ] ) )
664- ca_cert . sign ( ca_key , "SHA256" )
665641
666642 good = build_cert_with_san ( "good" , 10 , "good.example.com" , ca_cert , ca_key )
667643 bad = build_cert_with_san ( "bad" , 11 , "bad.evil.com" , ca_cert , ca_key )
@@ -690,12 +666,9 @@ def test_name_constraints_permitted_and_excluded_combined
690666 now = Time . now
691667 ca_key = OpenSSL ::PKey ::RSA . new ( 2048 )
692668 ca_cert = issue_cert ( OpenSSL ::X509 ::Name . parse ( "/CN=CA" ) , ca_key , 1 ,
693- [ [ "basicConstraints" , "CA:TRUE" , true ] , [ "keyUsage" , "cRLSign,keyCertSign" , true ] ] ,
669+ [ [ "basicConstraints" , "CA:TRUE" , true ] , [ "keyUsage" , "cRLSign,keyCertSign" , true ] ,
670+ [ "nameConstraints" , "permitted;DNS:.example.com,excluded;DNS:.bad.example.com" , true ] ] ,
694671 nil , nil , not_before : now , not_after : now + 3600 )
695- # Permit .example.com but exclude .bad.example.com
696- ca_cert . add_extension ( build_name_constraints_ext (
697- permitted_dns : [ ".example.com" ] , excluded_dns : [ ".bad.example.com" ] ) )
698- ca_cert . sign ( ca_key , "SHA256" )
699672
700673 good = build_cert_with_san ( "good" , 10 , "good.example.com" , ca_cert , ca_key )
701674 bad = build_cert_with_san ( "bad" , 11 , "test.bad.example.com" , ca_cert , ca_key )
0 commit comments