< prev index next >

src/java.base/share/classes/java/security/cert/X509Certificate.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 659      *
 660      * This method was added to version 1.8 of the Java Platform Standard
 661      * Edition. In order to maintain backwards compatibility with existing
 662      * service providers, this method is not {@code abstract}
 663      * and it provides a default implementation.
 664      *
 665      * @param key the PublicKey used to carry out the verification.
 666      * @param sigProvider the signature provider.
 667      *
 668      * @exception NoSuchAlgorithmException on unsupported signature
 669      * algorithms.
 670      * @exception InvalidKeyException on incorrect key.
 671      * @exception SignatureException on signature errors.
 672      * @exception CertificateException on encoding errors.
 673      * @exception UnsupportedOperationException if the method is not supported
 674      * @since 1.8
 675      */
 676     public void verify(PublicKey key, Provider sigProvider)
 677         throws CertificateException, NoSuchAlgorithmException,
 678         InvalidKeyException, SignatureException {
 679         String sigName = getSigAlgName();
 680         Signature sig = (sigProvider == null)
 681             ? Signature.getInstance(sigName)
 682             : Signature.getInstance(sigName, sigProvider);
 683 




 684         try {
 685             SignatureUtil.initVerifyWithParam(sig, key,
 686                 SignatureUtil.getParamSpec(sigName, getSigAlgParams()));
 687         } catch (ProviderException e) {
 688             throw new CertificateException(e.getMessage(), e.getCause());
 689         } catch (InvalidAlgorithmParameterException e) {
 690             throw new CertificateException(e);
 691         }
 692 
 693         byte[] tbsCert = getTBSCertificate();
 694         sig.update(tbsCert, 0, tbsCert.length);
 695 
 696         if (sig.verify(getSignature()) == false) {
 697             throw new SignatureException("Signature does not match.");
 698         }
 699     }
 700 }
   1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 659      *
 660      * This method was added to version 1.8 of the Java Platform Standard
 661      * Edition. In order to maintain backwards compatibility with existing
 662      * service providers, this method is not {@code abstract}
 663      * and it provides a default implementation.
 664      *
 665      * @param key the PublicKey used to carry out the verification.
 666      * @param sigProvider the signature provider.
 667      *
 668      * @exception NoSuchAlgorithmException on unsupported signature
 669      * algorithms.
 670      * @exception InvalidKeyException on incorrect key.
 671      * @exception SignatureException on signature errors.
 672      * @exception CertificateException on encoding errors.
 673      * @exception UnsupportedOperationException if the method is not supported
 674      * @since 1.8
 675      */
 676     public void verify(PublicKey key, Provider sigProvider)
 677         throws CertificateException, NoSuchAlgorithmException,
 678         InvalidKeyException, SignatureException {

 679         Signature sig = (sigProvider == null)
 680             ? Signature.getInstance(getSigAlgName())
 681             : Signature.getInstance(getSigAlgName(), sigProvider);
 682 
 683         sig.initVerify(key);
 684 
 685         // set parameters after Signature.initSign/initVerify call,
 686         // so the deferred provider selections occur when key is set
 687         try {
 688             SignatureUtil.specialSetParameter(sig, getSigAlgParams());

 689         } catch (ProviderException e) {
 690             throw new CertificateException(e.getMessage(), e.getCause());
 691         } catch (InvalidAlgorithmParameterException e) {
 692             throw new CertificateException(e);
 693         }
 694 
 695         byte[] tbsCert = getTBSCertificate();
 696         sig.update(tbsCert, 0, tbsCert.length);
 697 
 698         if (sig.verify(getSignature()) == false) {
 699             throw new SignatureException("Signature does not match.");
 700         }
 701     }
 702 }
< prev index next >