55import com .venafi .vcert .sdk .VCertException ;
66import lombok .Data ;
77import org .bouncycastle .asn1 .ASN1ObjectIdentifier ;
8- import org .bouncycastle .asn1 .eac .ECDSAPublicKey ;
98import org .bouncycastle .asn1 .x500 .X500NameBuilder ;
109import org .bouncycastle .asn1 .x500 .style .BCStyle ;
1110import org .bouncycastle .jce .PKCS10CertificationRequest ;
2726import java .security .*;
2827import java .security .cert .Certificate ;
2928import java .security .interfaces .RSAPublicKey ;
29+ import java .security .interfaces .ECPublicKey ;
3030import java .security .spec .ECGenParameterSpec ;
3131import java .time .Duration ;
3232import java .util .Collection ;
@@ -82,6 +82,12 @@ public ChainOption chainOption() {
8282 :ChainOption .ChainOptionRootFirst ;
8383 }
8484
85+ public PrivateKey privateKey () {
86+ return (!Objects .isNull (keyPair ))
87+ ?keyPair .getPrivate ()
88+ :null ;
89+ }
90+
8591 public void generatePrivateKey () throws VCertException {
8692 if (keyPair != null ) {
8793 return ;
@@ -110,11 +116,9 @@ public void generateCSR() throws VCertException {
110116 for ( String san : dnsNames ) {
111117 sans .add (new GeneralName (GeneralName .dNSName , san ));
112118 }
113-
114119 for ( InetAddress san : ipAddresses ) {
115120 sans .add (new GeneralName (GeneralName .iPAddress , new DEROctetString (san .getAddress ())));
116121 }
117-
118122 for ( String san : emailAddresses ) {
119123 sans .add (new GeneralName (GeneralName .rfc822Name , san ));
120124 }
@@ -232,8 +236,7 @@ public static class AttributeTypeAndValueSET {
232236 }
233237
234238 public boolean checkCertificate (Certificate certificate ) throws VCertException {
235- // TODO handle enum exception
236- PublicKeyAlgorithm publicKeyAlgorithm = PublicKeyAlgorithm .valueOf (certificate .getPublicKey ().getAlgorithm ());
239+ PublicKeyAlgorithm publicKeyAlgorithm = KeyType .from (certificate .getPublicKey ().getAlgorithm ()).X509Type ();
237240
238241 if (keyPair != null && keyPair .getPublic () != null && keyPair .getPrivate () != null ) {
239242 if ( keyType .X509Type () != publicKeyAlgorithm ) {
@@ -249,11 +252,22 @@ public boolean checkCertificate(Certificate certificate) throws VCertException {
249252 }
250253 break ;
251254 case ECDSA :
252- ECDSAPublicKey certEcdsaPublicKey = (ECDSAPublicKey ) certificate .getPublicKey ();
253- ECDSAPublicKey reqEcdsaPublicKey = (ECDSAPublicKey ) keyPair .getPublic ();
254- // TODO make sure comparison is valid
255- if (certEcdsaPublicKey .getPrimeModulusP ().compareTo (reqEcdsaPublicKey .getPrimeModulusP ()) != 0 ) {
256- throw new VCertException ("unmatched X for eliptic keys" );
255+ ECPublicKey certEcPublicKey = (ECPublicKey ) certificate .getPublicKey ();
256+ ECPublicKey reqEcPublicKey = (ECPublicKey ) keyPair .getPublic ();
257+
258+ // https://stackoverflow.com/questions/24121801/how-to-verify-if-the-private-key-matches-with-the-certificate
259+ java .security .spec .ECParameterSpec certSpec = certEcPublicKey .getParams (), csrSpec = reqEcPublicKey .getParams ();
260+ java .security .spec .EllipticCurve certCurve = certSpec .getCurve (), csrCurve = csrSpec .getCurve ();
261+ java .security .spec .ECField certField = certCurve .getField (), csrField = csrCurve .getField ();
262+ if ( certSpec != csrSpec //
263+ && ( certSpec .getCofactor () != csrSpec .getCofactor () //
264+ || ! certSpec .getOrder ().equals ( csrSpec .getOrder () ) //
265+ || ! certSpec .getGenerator ().equals ( csrSpec .getGenerator () ) //
266+ || certCurve != csrCurve //
267+ && ( ! certCurve .getA ().equals ( csrCurve .getA () ) //
268+ || ! certCurve .getB ().equals ( csrCurve .getB () ) //
269+ || certField .getFieldSize () != csrField .getFieldSize () ) ) ) {
270+ throw new VCertException ("unmatched parameters for elliptic keys" );
257271 }
258272 break ;
259273 default :
@@ -279,11 +293,22 @@ public boolean checkCertificate(Certificate certificate) throws VCertException {
279293 }
280294 break ;
281295 case ECDSA :
282- ECDSAPublicKey certEcdsaPublicKey = (ECDSAPublicKey ) certificate .getPublicKey ();
283- ECDSAPublicKey reqEcdsaPublicKey = (ECDSAPublicKey ) csr .getPublicKey ();
284- // TODO make sure comparison is valid
285- if (certEcdsaPublicKey .getPrimeModulusP ().compareTo (reqEcdsaPublicKey .getPrimeModulusP ()) != 0 ) {
286- throw new VCertException ("unmatched X for eliptic keys" );
296+ ECPublicKey certEcPublicKey = (ECPublicKey ) certificate .getPublicKey ();
297+ ECPublicKey reqEcPublicKey = (ECPublicKey ) csr .getPublicKey ();
298+
299+ // https://stackoverflow.com/questions/24121801/how-to-verify-if-the-private-key-matches-with-the-certificate
300+ java .security .spec .ECParameterSpec certSpec = certEcPublicKey .getParams (), csrSpec = reqEcPublicKey .getParams ();
301+ java .security .spec .EllipticCurve certCurve = certSpec .getCurve (), csrCurve = csrSpec .getCurve ();
302+ java .security .spec .ECField certField = certCurve .getField (), csrField = csrCurve .getField ();
303+ if ( certSpec != csrSpec //
304+ && ( certSpec .getCofactor () != csrSpec .getCofactor () //
305+ || ! certSpec .getOrder ().equals ( csrSpec .getOrder () ) //
306+ || ! certSpec .getGenerator ().equals ( csrSpec .getGenerator () ) //
307+ || certCurve != csrCurve //
308+ && ( ! certCurve .getA ().equals ( csrCurve .getA () ) //
309+ || ! certCurve .getB ().equals ( csrCurve .getB () ) //
310+ || certField .getFieldSize () != csrField .getFieldSize () ) ) ) {
311+ throw new VCertException ("unmatched parameters for elliptic keys" );
287312 }
288313 break ;
289314 }
0 commit comments