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