View Javadoc
1   /*
2    * GordianKnot: Security Suite
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.gordianknot.api.cert;
18  
19  import org.bouncycastle.asn1.x509.KeyUsage;
20  
21  import java.util.EnumSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  /**
26   * KeyPair Usage.
27   */
28  public class GordianKeyPairUsage {
29      /**
30       * The Usage set.
31       */
32      private final EnumSet<GordianKeyPairUse> theUsageSet;
33  
34      /**
35       * Constructor.
36       *
37       * @param pUse the usage.
38       */
39      public GordianKeyPairUsage(final GordianKeyPairUse... pUse) {
40          theUsageSet = EnumSet.noneOf(GordianKeyPairUse.class);
41          theUsageSet.addAll(List.of(pUse));
42      }
43  
44      /**
45       * Add a usage.
46       *
47       * @param pUse the use to add
48       */
49      public void addUse(final GordianKeyPairUse pUse) {
50          theUsageSet.add(pUse);
51      }
52  
53      /**
54       * Does the keyPair have the specified use?
55       *
56       * @param pUse the use to test for
57       * @return true/false
58       */
59      public boolean hasUse(final GordianKeyPairUse pUse) {
60          return theUsageSet.contains(pUse);
61      }
62  
63      /**
64       * Obtain the usageSet.
65       *
66       * @return the UseSet
67       */
68      public Set<GordianKeyPairUse> getUsageSet() {
69          return EnumSet.copyOf(theUsageSet);
70      }
71  
72      /**
73       * Obtain the keyUsage.
74       *
75       * @return the keyUsage
76       */
77      public KeyUsage getKeyUsage() {
78          int myUsage = 0;
79          for (GordianKeyPairUse myUse : theUsageSet) {
80              myUsage |= myUse.getUsage();
81          }
82          return new KeyUsage(myUsage);
83      }
84  }