1- package com .venafi .vcert .sdk .connectors . tpp ;
1+ package com .venafi .vcert .sdk .connectors ;
22
33import static org .apache .commons .lang3 .StringUtils .isNotBlank ;
44import java .util .Collection ;
1515import com .venafi .vcert .sdk .certificate .CertificateRequest ;
1616import com .venafi .vcert .sdk .certificate .EllipticCurve ;
1717import com .venafi .vcert .sdk .certificate .KeyType ;
18- import com .venafi .vcert .sdk .connectors .Policy ;
1918import com .venafi .vcert .sdk .endpoint .AllowedKeyConfiguration ;
2019import com .venafi .vcert .sdk .utils .Is ;
2120
@@ -36,14 +35,15 @@ public class ZoneConfiguration {
3635 // empty map
3736
3837 private String zoneId ;
38+ private AllowedKeyConfiguration keyConfig ;
3939
4040 /**
4141 * UpdateCertificateRequest updates a certificate request based on the zone configuration
4242 * retrieved from the remote endpoint
4343 *
4444 * @return
4545 */
46- public void updateCertificateRequest (CertificateRequest request ) {
46+ public void applyCertificateRequestDefaultSettingsIfNeeded (CertificateRequest request ) {
4747 CertificateRequest .PKIXName subject = request .subject ();
4848 subject .organization (Entity .of (subject .organization (), organization ).resolve ());
4949 if (Is .blank (subject .organizationalUnit ()) && !Is .blank (organizationalUnit )) {
@@ -55,7 +55,8 @@ public void updateCertificateRequest(CertificateRequest request) {
5555
5656 // apply defaults for settings that weren't specified and then make sure they comply with policy
5757 if (request .keyType () == null ) {
58- request .keyType (KeyType .defaultKeyType ());
58+ request
59+ .keyType (keyConfig != null && keyConfig .keyType () != null ? keyConfig .keyType () : KeyType .defaultKeyType ());
5960 }
6061
6162 switch (request .keyType ()) {
@@ -69,8 +70,10 @@ public void updateCertificateRequest(CertificateRequest request) {
6970 break ;
7071
7172 default :
72- if (request .keyLength () < 2048 ) {
73- request .keyLength (2048 );
73+ if (request .keyLength () < KeyType .defaultRsaLength ()) {
74+ request .keyLength (keyConfig != null && !Is .blank (keyConfig .keySizes ())
75+ && keyConfig .keySizes ().get (0 ) >= KeyType .defaultRsaLength () ? keyConfig .keySizes ().get (0 )
76+ : KeyType .defaultRsaLength ());
7477 }
7578 if (request .signatureAlgorithm () == SignatureAlgorithm .UnknownSignatureAlgorithm ) {
7679 request .signatureAlgorithm (SignatureAlgorithm .SHA256WithRSA );
@@ -126,42 +129,37 @@ static Entity of(List<String> target, String source) {
126129 }
127130
128131 List <String > resolve () {
129- if (Is .blank (target ) && isNotBlank (source )) {
130- return Collections .singletonList (source );
131- } else if (!Is .blank (target ) && isNotBlank (source ) && !Is .equalsFold (target .get (0 ), source )) {
132- return Collections .singletonList (source );
133- }
134- return target ;
132+ return Is .blank (target ) && isNotBlank (source ) ? Collections .singletonList (source ) : target ;
135133 }
136134 }
137135
138136 public boolean validateCertificateRequest (CertificateRequest request ) throws VCertException {
139137 if (!isComponentValid (policy .subjectCNRegexes (),
140- Collections .singletonList (request .subject ().commonName ()))) {
138+ Collections .singletonList (request .subject ().commonName ()), false )) {
141139 throw new VCertException (
142140 "The requested CN does not match any of the allowed CN regular expressions" );
143141 }
144- if (!isComponentValid (policy .subjectORegexes (), request .subject ().organization ())) {
142+ if (!isComponentValid (policy .subjectORegexes (), request .subject ().organization (), false )) {
145143 throw new VCertException (
146144 "The requested Organization does not match any of the allowed Organization regular expressions" );
147145 }
148- if (!isComponentValid (policy .subjectOURegexes (), request .subject ().organizationalUnit ())) {
146+ if (!isComponentValid (policy .subjectOURegexes (), request .subject ().organizationalUnit (), false )) {
149147 throw new VCertException (
150148 "The requested Organizational Unit does not match any of the allowed Organization Unit regular expressions" );
151149 }
152- if (!isComponentValid (policy .subjectSTRegexes (), request .subject ().province ())) {
150+ if (!isComponentValid (policy .subjectSTRegexes (), request .subject ().province (), false )) {
153151 throw new VCertException (
154152 "The requested State/Province does not match any of the allowed State/Province regular expressions" );
155153 }
156- if (!isComponentValid (policy .subjectLRegexes (), request .subject ().locality ())) {
154+ if (!isComponentValid (policy .subjectLRegexes (), request .subject ().locality (), false )) {
157155 throw new VCertException (
158156 "The requested Locality does not match any of the allowed Locality regular expressions" );
159157 }
160- if (!isComponentValid (policy .subjectCRegexes (), request .subject ().country ())) {
158+ if (!isComponentValid (policy .subjectCRegexes (), request .subject ().country (), false )) {
161159 throw new VCertException (
162160 "The requested Country does not match any of the allowed Country regular expressions" );
163161 }
164- if (!isComponentValid (policy .dnsSanRegExs (), request .dnsNames ())) {
162+ if (!isComponentValid (policy .dnsSanRegExs (), request .dnsNames (), true )) {
165163 throw new VCertException (
166164 "The requested Subject Alternative Name does not match any of the allowed Country regular expressions" );
167165 }
@@ -188,26 +186,28 @@ public boolean validateCertificateRequest(CertificateRequest request) throws VCe
188186 return true ;
189187 }
190188
191- private boolean isComponentValid (Collection <String > regexes , Collection <String > components ) {
192- if (regexes .size () == 0 || components . size () == 0 ) {
189+ private boolean isComponentValid (Collection <String > regexes , Collection <String > components , boolean optional ) {
190+ if (regexes .isEmpty () || ( optional && Is . blank ( components )) ) {
193191 return true ;
194192 }
195193
196- for (String regex : regexes ) {
197- Pattern pattern ;
198- try {
199- pattern = Pattern .compile (regex );
200- } catch (PatternSyntaxException e ) {
201- // TODO log error
202- return false ;
203- }
204- for (String component : components ) {
205- Matcher m = pattern .matcher (component );
206- if (m .matches ()) {
207- return true ; // todo: that seems wrong. Check if all policy rules need to be matched, or
208- // any one? (E.g.: Policy says location is [0]:Madrid,[1]:London - does it
209- // need to match either or both?) Also, if we have locations 0:London, 1:
210- // Brussels, 2: Madrid, won't this pass? Should it?
194+ if (components != null ) {
195+ for (String regex : regexes ) {
196+ Pattern pattern ;
197+ try {
198+ pattern = Pattern .compile (regex );
199+ } catch (PatternSyntaxException e ) {
200+ // TODO log error
201+ return false ;
202+ }
203+ for (String component : components ) {
204+ Matcher m = pattern .matcher (component );
205+ if (m .matches ()) {
206+ return true ; // todo: that seems wrong. Check if all policy rules need to be matched, or
207+ // any one? (E.g.: Policy says location is [0]:Madrid,[1]:London - does it
208+ // need to match either or both?) Also, if we have locations 0:London, 1:
209+ // Brussels, 2: Madrid, won't this pass? Should it?
210+ }
211211 }
212212 }
213213 }
0 commit comments