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.agree;
18  
19  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
20  import io.github.tonywasher.joceanus.gordianknot.api.cert.GordianCertificate;
21  import io.github.tonywasher.joceanus.gordianknot.api.cert.GordianKeyPairUsage;
22  import io.github.tonywasher.joceanus.gordianknot.api.keypair.GordianKeyPair;
23  import io.github.tonywasher.joceanus.gordianknot.api.keypair.GordianKeyPairSpec;
24  import io.github.tonywasher.joceanus.gordianknot.api.sign.GordianSignatureSpec;
25  import org.bouncycastle.asn1.x500.X500Name;
26  
27  import java.util.List;
28  import java.util.function.Predicate;
29  
30  /**
31   * GordianKnot AgreementFactory API.
32   */
33  public interface GordianAgreementFactory {
34      /**
35       * Create new AgreementParams.
36       *
37       * @param pSpec       the agreementSpec
38       * @param pResultType the result type
39       *                    <dl>
40       *                        <dt>GordianFactoryType</dt><dd>To agree a Factory</dd>
41       *                        <dt>GordianSymCipherSpec</dt><dd>To agree a symCipher pair</dd>
42       *                        <dt>GordianStreamCipherSpec</dt><dd>To agree a streamCipher pair</dd>
43       *                        <dt>GordianKeySetSpec</dt><dd>To agree a KeySet</dd>
44       *                        <dt>Integer</dt><dd>To agree a defined length byte array</dd>
45       *                    </dl>
46       * @return the Params
47       * @throws GordianException on error
48       */
49      GordianAgreementParams newAgreementParams(GordianAgreementSpec pSpec,
50                                                Object pResultType) throws GordianException;
51  
52      /**
53       * CreateAgreement.
54       *
55       * @param pParams the agreementParams
56       * @return the Agreement
57       * @throws GordianException on error
58       */
59      GordianAgreement createAgreement(GordianAgreementParams pParams) throws GordianException;
60  
61      /**
62       * Create/Locate Agreement for incoming message.
63       *
64       * @param pMessage the incoming message
65       * @return the Agreement
66       * @throws GordianException on error
67       */
68      GordianAgreement parseAgreementMessage(byte[] pMessage) throws GordianException;
69  
70      /**
71       * Declare signer certificate.
72       *
73       * @param pSigner the certificate
74       * @throws GordianException on error
75       */
76      void setSigner(GordianCertificate pSigner) throws GordianException;
77  
78      /**
79       * Declare signer certificate and specification.
80       *
81       * @param pSigner   the certificate
82       * @param pSignSpec the signSpec
83       * @throws GordianException on error
84       */
85      void setSigner(GordianCertificate pSigner,
86                     GordianSignatureSpec pSignSpec) throws GordianException;
87  
88      /**
89       * Create new miniCertificate.
90       *
91       * @param pSubject the subject of the certificate
92       * @param pKeyPair the keyPair.
93       * @param pUsage   the usage
94       * @return the certificate
95       * @throws GordianException on error
96       */
97      GordianCertificate newMiniCertificate(X500Name pSubject,
98                                            GordianKeyPair pKeyPair,
99                                            GordianKeyPairUsage pUsage) throws GordianException;
100 
101     /**
102      * Obtain predicate for keyAgreement.
103      *
104      * @return the predicate
105      */
106     Predicate<GordianAgreementSpec> supportedAgreements();
107 
108     /**
109      * Check AgreementSpec and KeyPair combination.
110      *
111      * @param pKeyPair       the keyPair
112      * @param pAgreementSpec the macSpec
113      * @return true/false
114      */
115     default boolean validAgreementSpecForKeyPair(final GordianKeyPair pKeyPair,
116                                                  final GordianAgreementSpec pAgreementSpec) {
117         return validAgreementSpecForKeyPairSpec(pKeyPair.getKeyPairSpec(), pAgreementSpec);
118     }
119 
120     /**
121      * Check AgreementSpec and KeyPairSpec combination.
122      *
123      * @param pKeyPairSpec   the keyPairSpec
124      * @param pAgreementSpec the agreementSpec
125      * @return true/false
126      */
127     boolean validAgreementSpecForKeyPairSpec(GordianKeyPairSpec pKeyPairSpec,
128                                              GordianAgreementSpec pAgreementSpec);
129 
130     /**
131      * Obtain a list of supported agreementSpecs.
132      *
133      * @param pKeyPair the keyPair
134      * @return the list of supported agreementSpecs.
135      */
136     List<GordianAgreementSpec> listAllSupportedAgreements(GordianKeyPair pKeyPair);
137 
138     /**
139      * Obtain a list of supported agreementSpecs.
140      *
141      * @param pKeyPairSpec the keySpec
142      * @return the list of supported agreementSpecs.
143      */
144     List<GordianAgreementSpec> listAllSupportedAgreements(GordianKeyPairSpec pKeyPairSpec);
145 
146     /**
147      * Create default agreementSpec for key.
148      *
149      * @param pKeySpec the keySpec
150      * @return the AgreementSpec
151      */
152     GordianAgreementSpec defaultForKeyPair(GordianKeyPairSpec pKeySpec);
153 }