1 /*
   2  * Copyright (c) 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
  23  * questions.
  24  */
  25 
  26 package sun.security.util;
  27 
  28 import java.io.IOException;
  29 import java.security.AlgorithmParametersSpi;
  30 import java.security.spec.AlgorithmParameterSpec;
  31 import java.security.spec.InvalidParameterSpecException;
  32 import javax.crypto.spec.GCMParameterSpec;
  33 import sun.security.util.HexDumpEncoder;
  34 import sun.security.util.*;
  35 
  36 /**
  37  * This class implements the parameter set used with
  38  * GCM encryption, which is defined in RFC 5084 as follows:
  39  *
  40  * <pre>
  41  *    GCMParameters ::= SEQUENCE {
  42  *      aes-iv      OCTET STRING, -- recommended size is 12 octets
  43  *      aes-tLen    AES-GCM-ICVlen DEFAULT 12 }
  44  *
  45  *    AES-GCM-ICVlen ::= INTEGER (12 | 13 | 14 | 15 | 16)
  46  *
  47  * </pre>
  48  *
  49  * @since 13
  50  */
  51 public final class GCMParameters extends AlgorithmParametersSpi {
  52 
  53     // the iv
  54     private byte[] iv;
  55     // the tag length in bytes
  56     private int tLen;
  57 
  58     public GCMParameters() {}
  59 
  60     protected void engineInit(AlgorithmParameterSpec paramSpec)
  61         throws InvalidParameterSpecException {
  62 
  63         if (!(paramSpec instanceof GCMParameterSpec)) {
  64             throw new InvalidParameterSpecException
  65                 ("Inappropriate parameter specification");
  66         }
  67         GCMParameterSpec gps = (GCMParameterSpec) paramSpec;
  68         // need to convert from bits to bytes for ASN.1 encoding
  69         this.tLen = gps.getTLen()/8;
  70         if (this.tLen < 12 || this.tLen > 16 ) {
  71             throw new InvalidParameterSpecException
  72                 ("GCM parameter parsing error: unsupported tag len: " +
  73                  this.tLen);
  74         }
  75         this.iv = gps.getIV();
  76     }
  77 
  78     protected void engineInit(byte[] encoded) throws IOException {
  79         DerValue val = new DerValue(encoded);
  80         // check if IV or params
  81         if (val.tag == DerValue.tag_Sequence) {
  82             byte[] iv = val.data.getOctetString();
  83             int tLen;
  84             if (val.data.available() != 0) {
  85                 tLen = val.data.getInteger();
  86                 if (tLen < 12 || tLen > 16 ) {
  87                     throw new IOException
  88                         ("GCM parameter parsing error: unsupported tag len: " +
  89                          tLen);
  90                 }
  91                 if (val.data.available() != 0) {
  92                     throw new IOException
  93                         ("GCM parameter parsing error: extra data");
  94                 }
  95             } else {
  96                 tLen = 12;
  97             }
  98             this.iv = iv.clone();
  99             this.tLen = tLen;
 100         } else {
 101             throw new IOException("GCM parameter parsing error: no SEQ tag");
 102         }
 103     }
 104 
 105     protected void engineInit(byte[] encoded, String decodingMethod)
 106         throws IOException {
 107         engineInit(encoded);
 108     }
 109 
 110     protected <T extends AlgorithmParameterSpec>
 111             T engineGetParameterSpec(Class<T> paramSpec)
 112         throws InvalidParameterSpecException {
 113 
 114         if (GCMParameterSpec.class.isAssignableFrom(paramSpec)) {
 115             return paramSpec.cast(new GCMParameterSpec(tLen * 8, iv));
 116         } else {
 117             throw new InvalidParameterSpecException
 118                 ("Inappropriate parameter specification");
 119         }
 120     }
 121 
 122     protected byte[] engineGetEncoded() throws IOException {
 123         DerOutputStream out = new DerOutputStream();
 124         DerOutputStream bytes = new DerOutputStream();
 125 
 126         bytes.putOctetString(iv);
 127         // Only put non-default values
 128         if (tLen != 12) {
 129             bytes.putInteger(tLen);
 130         }
 131         out.write(DerValue.tag_Sequence, bytes);
 132         return out.toByteArray();
 133     }
 134 
 135     protected byte[] engineGetEncoded(String encodingMethod)
 136         throws IOException {
 137         return engineGetEncoded();
 138     }
 139 
 140     /*
 141      * Returns a formatted string describing the parameters.
 142      */
 143     protected String engineToString() {
 144         String LINE_SEP = System.lineSeparator();
 145         HexDumpEncoder encoder = new HexDumpEncoder();
 146         StringBuilder sb
 147             = new StringBuilder(LINE_SEP + "    iv:" + LINE_SEP + "["
 148                 + encoder.encodeBuffer(iv) + "]");
 149 
 150         sb.append(LINE_SEP + "tLen(bits):" + LINE_SEP + tLen*8 + LINE_SEP);
 151         return sb.toString();
 152     }
 153 }