View Javadoc
1   /*
2    * GordianKnot: Security Suite
3    * Copyright 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  
18  package io.github.tonywasher.joceanus.gordianknot.impl.jca.agree;
19  
20  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
21  import io.github.tonywasher.joceanus.gordianknot.impl.core.agree.GordianCoreAgreementFactory;
22  import io.github.tonywasher.joceanus.gordianknot.impl.core.exc.GordianCryptoException;
23  import io.github.tonywasher.joceanus.gordianknot.impl.core.spec.agree.GordianCoreAgreementSpec;
24  import io.github.tonywasher.joceanus.gordianknot.impl.jca.agree.JcaAgreement.JcaAgreementBase;
25  import io.github.tonywasher.joceanus.gordianknot.impl.jca.keypair.JcaKeyPair.JcaPrivateKey;
26  import io.github.tonywasher.joceanus.gordianknot.impl.jca.keypair.JcaKeyPair.JcaPublicKey;
27  import org.bouncycastle.jcajce.spec.DHUParameterSpec;
28  
29  import javax.crypto.KeyAgreement;
30  import java.security.InvalidAlgorithmParameterException;
31  import java.security.InvalidKeyException;
32  
33  /**
34   * Jca Unified Agreement.
35   */
36  public class JcaUnifiedEngine
37          extends JcaAgreementBase {
38      /**
39       * Key Agreement.
40       */
41      private KeyAgreement theAgreement;
42  
43      /**
44       * Constructor.
45       *
46       * @param pFactory   the security factory
47       * @param pSpec      the agreementSpec
48       * @param pAgreement the agreement
49       */
50      JcaUnifiedEngine(final GordianCoreAgreementFactory pFactory,
51                       final GordianCoreAgreementSpec pSpec,
52                       final KeyAgreement pAgreement) throws GordianException {
53          /* Initialize underlying class */
54          super(pFactory, pSpec);
55  
56          /* Store the agreement */
57          theAgreement = pAgreement;
58      }
59  
60      @Override
61      public void processClientHello() throws GordianException {
62          /* Protect against exceptions */
63          try {
64              /* Access keys */
65              final JcaPublicKey myClientPublic = (JcaPublicKey) getPublicKey(getClientKeyPair());
66              final JcaPublicKey myClientEphPublic = (JcaPublicKey) getPublicKey(getClientEphemeral());
67              final JcaPrivateKey myPrivate = (JcaPrivateKey) getPrivateKey(getServerKeyPair());
68              final JcaPublicKey myEphPublic = (JcaPublicKey) getPublicKey(getServerEphemeral());
69              final JcaPrivateKey myEphPrivate = (JcaPrivateKey) getPrivateKey(getServerEphemeral());
70              theAgreement = adjustAgreement(theAgreement, getServerKeyPair());
71  
72              /* Derive the secret */
73              final DHUParameterSpec myParams = new DHUParameterSpec(myEphPublic.getPublicKey(),
74                      myEphPrivate.getPrivateKey(), myClientEphPublic.getPublicKey(), getAdditional());
75              theAgreement.init(myPrivate.getPrivateKey(), myParams, getRandom());
76              theAgreement.doPhase(myClientPublic.getPublicKey(), true);
77              storeSecret(theAgreement.generateSecret());
78  
79          } catch (InvalidKeyException
80                   | InvalidAlgorithmParameterException e) {
81              throw new GordianCryptoException(JcaAgreement.ERR_AGREEMENT, e);
82          }
83      }
84  
85      @Override
86      public void processServerHello() throws GordianException {
87          /* Protect against exceptions */
88          try {
89              /* Access keys */
90              final JcaPublicKey myServerPublic = (JcaPublicKey) getPublicKey(getServerKeyPair());
91              final JcaPublicKey myServerEphPublic = (JcaPublicKey) getPublicKey(getServerEphemeral());
92              final JcaPrivateKey myPrivate = (JcaPrivateKey) getPrivateKey(getClientKeyPair());
93              final JcaPublicKey myEphPublic = (JcaPublicKey) getPublicKey(getClientEphemeral());
94              final JcaPrivateKey myEphPrivate = (JcaPrivateKey) getPrivateKey(getClientEphemeral());
95              theAgreement = adjustAgreement(theAgreement, getServerKeyPair());
96  
97              /* Derive the secret */
98              final DHUParameterSpec myParams = new DHUParameterSpec(myEphPublic.getPublicKey(),
99                      myEphPrivate.getPrivateKey(), myServerEphPublic.getPublicKey(), getAdditional());
100             theAgreement.init(myPrivate.getPrivateKey(), myParams);
101             theAgreement.doPhase(myServerPublic.getPublicKey(), true);
102             storeSecret(theAgreement.generateSecret());
103 
104         } catch (InvalidKeyException
105                  | InvalidAlgorithmParameterException e) {
106             throw new GordianCryptoException(JcaAgreement.ERR_AGREEMENT, e);
107         }
108     }
109 }