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.impl.bc;
18  
19  import io.github.tonywasher.joceanus.gordianknot.api.agree.GordianAgreementSpec;
20  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
21  import io.github.tonywasher.joceanus.gordianknot.api.keypair.GordianKeyPair;
22  import io.github.tonywasher.joceanus.gordianknot.impl.core.agree.GordianCoreAgreementEngine;
23  import io.github.tonywasher.joceanus.gordianknot.impl.core.agree.GordianCoreAgreementFactory;
24  import io.github.tonywasher.joceanus.gordianknot.impl.core.exc.GordianDataException;
25  import io.github.tonywasher.joceanus.gordianknot.impl.core.keypair.GordianPrivateKey;
26  import io.github.tonywasher.joceanus.gordianknot.impl.core.keypair.GordianPublicKey;
27  
28  /**
29   * Base Agreement Engine class.
30   */
31  public abstract class BouncyAgreementBase
32          extends GordianCoreAgreementEngine {
33      /**
34       * Constructor.
35       *
36       * @param pFactory the security factory
37       * @param pSpec    the agreementSpec
38       * @throws GordianException on error
39       */
40      BouncyAgreementBase(final GordianCoreAgreementFactory pFactory,
41                          final GordianAgreementSpec pSpec) throws GordianException {
42          /* Invoke underlying constructor */
43          super(pFactory, pSpec);
44  
45          /* Enable derivation */
46          enableDerivation();
47      }
48  
49      @Override
50      protected GordianPublicKey getPublicKey(final GordianKeyPair pKeyPair) throws GordianException {
51          /* Validate the keyPair */
52          if (!(pKeyPair instanceof BouncyKeyPair)) {
53              /* Reject keyPair */
54              throw new GordianDataException("Invalid KeyPair");
55          }
56  
57          /* Access public key */
58          return super.getPublicKey(pKeyPair);
59      }
60  
61      @Override
62      protected GordianPrivateKey getPrivateKey(final GordianKeyPair pKeyPair) throws GordianException {
63          /* Validate the keyPair */
64          if (!(pKeyPair instanceof BouncyKeyPair)) {
65              /* Reject keyPair */
66              throw new GordianDataException("Invalid KeyPair");
67          }
68  
69          /* Access private key */
70          return super.getPrivateKey(pKeyPair);
71      }
72  }