src/java.base/share/classes/sun/security/ssl/HandshakeHash.java
changeset 53734 cb1642ccc732
parent 53563 a4b7ea85d668
equal deleted inserted replaced
53733:b5d45c2fe8a0 53734:cb1642ccc732
    27 
    27 
    28 import java.io.ByteArrayOutputStream;
    28 import java.io.ByteArrayOutputStream;
    29 import java.io.IOException;
    29 import java.io.IOException;
    30 import java.nio.ByteBuffer;
    30 import java.nio.ByteBuffer;
    31 import java.security.MessageDigest;
    31 import java.security.MessageDigest;
       
    32 import java.security.NoSuchAlgorithmException;
    32 import java.util.Arrays;
    33 import java.util.Arrays;
    33 import java.util.LinkedList;
    34 import java.util.LinkedList;
    34 import javax.crypto.SecretKey;
    35 import javax.crypto.SecretKey;
    35 import sun.security.util.MessageDigestSpi2;
    36 import sun.security.util.MessageDigestSpi2;
    36 
    37 
   267         private final TranscriptHash md5;
   268         private final TranscriptHash md5;
   268         private final TranscriptHash sha;
   269         private final TranscriptHash sha;
   269         private final ByteArrayOutputStream baos;
   270         private final ByteArrayOutputStream baos;
   270 
   271 
   271         S30HandshakeHash(CipherSuite cipherSuite) {
   272         S30HandshakeHash(CipherSuite cipherSuite) {
   272             this.mdMD5 = JsseJce.getMessageDigest("MD5");
   273             try {
   273             this.mdSHA = JsseJce.getMessageDigest("SHA");
   274                 this.mdMD5 = MessageDigest.getInstance("MD5");
       
   275                 this.mdSHA = MessageDigest.getInstance("SHA");
       
   276             } catch (NoSuchAlgorithmException nsae) {
       
   277                 throw new RuntimeException(
       
   278                     "Hash algorithm MD5 or SHA is not available", nsae);
       
   279             }
   274 
   280 
   275             boolean hasArchived = false;
   281             boolean hasArchived = false;
   276             if (mdMD5 instanceof Cloneable) {
   282             if (mdMD5 instanceof Cloneable) {
   277                 md5 = new CloneableHash(mdMD5);
   283                 md5 = new CloneableHash(mdMD5);
   278             } else {
   284             } else {
   377                 } catch (CloneNotSupportedException ex) {   // unlikely
   383                 } catch (CloneNotSupportedException ex) {   // unlikely
   378                     throw new RuntimeException(
   384                     throw new RuntimeException(
   379                             "MessageDigest does no support clone operation");
   385                             "MessageDigest does no support clone operation");
   380                 }
   386                 }
   381             } else {
   387             } else {
   382                 md5Clone = JsseJce.getMessageDigest("MD5");
   388                 try {
       
   389                     md5Clone = MessageDigest.getInstance("MD5");
       
   390                 } catch (NoSuchAlgorithmException nsae) {
       
   391                     throw new RuntimeException(
       
   392                         "Hash algorithm MD5 is not available", nsae);
       
   393                 }
   383                 md5Clone.update(md5.archived());
   394                 md5Clone.update(md5.archived());
   384             }
   395             }
   385 
   396 
   386             return md5Clone;
   397             return md5Clone;
   387         }
   398         }
   394                 } catch (CloneNotSupportedException ex) {   // unlikely
   405                 } catch (CloneNotSupportedException ex) {   // unlikely
   395                     throw new RuntimeException(
   406                     throw new RuntimeException(
   396                             "MessageDigest does no support clone operation");
   407                             "MessageDigest does no support clone operation");
   397                 }
   408                 }
   398             } else {
   409             } else {
   399                 shaClone = JsseJce.getMessageDigest("SHA");
   410                 try {
       
   411                     shaClone = MessageDigest.getInstance("SHA");
       
   412                 } catch (NoSuchAlgorithmException nsae) {
       
   413                     throw new RuntimeException(
       
   414                         "Hash algorithm SHA is not available", nsae);
       
   415                 }
   400                 shaClone.update(sha.archived());
   416                 shaClone.update(sha.archived());
   401             }
   417             }
   402 
   418 
   403             return shaClone;
   419             return shaClone;
   404         }
   420         }
   445         private final TranscriptHash md5;
   461         private final TranscriptHash md5;
   446         private final TranscriptHash sha;
   462         private final TranscriptHash sha;
   447         private final ByteArrayOutputStream baos;
   463         private final ByteArrayOutputStream baos;
   448 
   464 
   449         T10HandshakeHash(CipherSuite cipherSuite) {
   465         T10HandshakeHash(CipherSuite cipherSuite) {
   450             MessageDigest mdMD5 = JsseJce.getMessageDigest("MD5");
   466             MessageDigest mdMD5;
   451             MessageDigest mdSHA = JsseJce.getMessageDigest("SHA");
   467             MessageDigest mdSHA;
       
   468             try {
       
   469                 mdMD5 = MessageDigest.getInstance("MD5");
       
   470                 mdSHA = MessageDigest.getInstance("SHA");
       
   471             } catch (NoSuchAlgorithmException nsae) {
       
   472                 throw new RuntimeException(
       
   473                     "Hash algorithm MD5 or SHA is not available", nsae);
       
   474             }
   452 
   475 
   453             boolean hasArchived = false;
   476             boolean hasArchived = false;
   454             if (mdMD5 instanceof Cloneable) {
   477             if (mdMD5 instanceof Cloneable) {
   455                 md5 = new CloneableHash(mdMD5);
   478                 md5 = new CloneableHash(mdMD5);
   456             } else {
   479             } else {
   512     static final class T12HandshakeHash implements TranscriptHash {
   535     static final class T12HandshakeHash implements TranscriptHash {
   513         private final TranscriptHash transcriptHash;
   536         private final TranscriptHash transcriptHash;
   514         private final ByteArrayOutputStream baos;
   537         private final ByteArrayOutputStream baos;
   515 
   538 
   516         T12HandshakeHash(CipherSuite cipherSuite) {
   539         T12HandshakeHash(CipherSuite cipherSuite) {
   517             MessageDigest md =
   540             MessageDigest md;
   518                     JsseJce.getMessageDigest(cipherSuite.hashAlg.name);
   541             try {
       
   542                 md = MessageDigest.getInstance(cipherSuite.hashAlg.name);
       
   543             } catch (NoSuchAlgorithmException nsae) {
       
   544                 throw new RuntimeException(
       
   545                         "Hash algorithm " +
       
   546                         cipherSuite.hashAlg.name + " is not available", nsae);
       
   547             }
       
   548 
   519             if (md instanceof Cloneable) {
   549             if (md instanceof Cloneable) {
   520                 transcriptHash = new CloneableHash(md);
   550                 transcriptHash = new CloneableHash(md);
   521                 this.baos = new ByteArrayOutputStream();
   551                 this.baos = new ByteArrayOutputStream();
   522             } else {
   552             } else {
   523                 transcriptHash = new NonCloneableHash(md);
   553                 transcriptHash = new NonCloneableHash(md);
   550 
   580 
   551     static final class T13HandshakeHash implements TranscriptHash {
   581     static final class T13HandshakeHash implements TranscriptHash {
   552         private final TranscriptHash transcriptHash;
   582         private final TranscriptHash transcriptHash;
   553 
   583 
   554         T13HandshakeHash(CipherSuite cipherSuite) {
   584         T13HandshakeHash(CipherSuite cipherSuite) {
   555             MessageDigest md =
   585             MessageDigest md;
   556                     JsseJce.getMessageDigest(cipherSuite.hashAlg.name);
   586             try {
       
   587                 md = MessageDigest.getInstance(cipherSuite.hashAlg.name);
       
   588             } catch (NoSuchAlgorithmException nsae) {
       
   589                 throw new RuntimeException(
       
   590                         "Hash algorithm " +
       
   591                         cipherSuite.hashAlg.name + " is not available", nsae);
       
   592             }
       
   593 
   557             if (md instanceof Cloneable) {
   594             if (md instanceof Cloneable) {
   558                 transcriptHash = new CloneableHash(md);
   595                 transcriptHash = new CloneableHash(md);
   559             } else {
   596             } else {
   560                 transcriptHash = new NonCloneableHash(md);
   597                 transcriptHash = new NonCloneableHash(md);
   561             }
   598             }