1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 /* 3 * Copyright (C) 2015 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy 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, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.org.conscrypt.ct; 19 20 import java.io.ByteArrayInputStream; 21 import java.io.InputStream; 22 import com.android.org.conscrypt.Internal; 23 24 /** 25 * DigitallySigned structure, as defined by RFC5246 Section 4.7. 26 * @hide This class is not part of the Android public SDK API 27 */ 28 @Internal 29 public class DigitallySigned { 30 /** 31 * @hide This class is not part of the Android public SDK API 32 */ 33 public enum HashAlgorithm { 34 NONE, 35 MD5, 36 SHA1, 37 SHA224, 38 SHA256, 39 SHA384, 40 SHA512; 41 42 private static HashAlgorithm[] values = values(); 43 public static HashAlgorithm valueOf(int ord) { 44 try { 45 return values[ord]; 46 } catch (IndexOutOfBoundsException e) { 47 throw new IllegalArgumentException("Invalid hash algorithm " + ord, e); 48 } 49 } 50 } 51 52 /** 53 * @hide This class is not part of the Android public SDK API 54 */ 55 public enum SignatureAlgorithm { 56 ANONYMOUS, 57 RSA, 58 DSA, 59 ECDSA; 60 61 private static SignatureAlgorithm[] values = values(); 62 public static SignatureAlgorithm valueOf(int ord) { 63 try { 64 return values[ord]; 65 } catch (IndexOutOfBoundsException e) { 66 throw new IllegalArgumentException("Invalid signature algorithm " + ord, e); 67 } 68 } 69 } 70 71 private final HashAlgorithm hashAlgorithm; 72 private final SignatureAlgorithm signatureAlgorithm; 73 private final byte[] signature; 74 75 public DigitallySigned(HashAlgorithm hashAlgorithm, 76 SignatureAlgorithm signatureAlgorithm, 77 byte[] signature) { 78 this.hashAlgorithm = hashAlgorithm; 79 this.signatureAlgorithm = signatureAlgorithm; 80 this.signature = signature; 81 } 82 83 public DigitallySigned(int hashAlgorithm, 84 int signatureAlgorithm, 85 byte[] signature) { 86 this( 87 HashAlgorithm.valueOf(hashAlgorithm), 88 SignatureAlgorithm.valueOf(signatureAlgorithm), 89 signature 90 ); 91 } 92 93 public HashAlgorithm getHashAlgorithm() { 94 return hashAlgorithm; 95 } 96 public SignatureAlgorithm getSignatureAlgorithm() { 97 return signatureAlgorithm; 98 } 99 public byte[] getSignature() { 100 return signature; 101 } 102 103 /** 104 * Get the name of the hash and signature combination. 105 * The result can be used to as the argument to {@link java.security.Signature#getInstance}. 106 */ 107 public String getAlgorithm() { 108 return String.format("%swith%s", hashAlgorithm, signatureAlgorithm); 109 } 110 111 /** 112 * Decode a TLS encoded DigitallySigned structure. 113 */ 114 public static DigitallySigned decode(InputStream input) 115 throws SerializationException { 116 try { 117 return new DigitallySigned( 118 Serialization.readNumber(input, CTConstants.HASH_ALGORITHM_LENGTH), 119 Serialization.readNumber(input, CTConstants.SIGNATURE_ALGORITHM_LENGTH), 120 Serialization.readVariableBytes(input, CTConstants.SIGNATURE_LENGTH_BYTES) 121 ); 122 } catch (IllegalArgumentException e) { 123 throw new SerializationException(e); 124 } 125 } 126 127 /** 128 * Decode a TLS encoded DigitallySigned structure. 129 */ 130 public static DigitallySigned decode(byte[] input) 131 throws SerializationException { 132 return decode(new ByteArrayInputStream(input)); 133 } 134 } 135 136 137