1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.ohos.hapsigntool.hap.sign; 17 18 import com.ohos.hapsigntool.hap.entity.Pair; 19 20 import java.security.spec.AlgorithmParameterSpec; 21 import java.security.spec.MGF1ParameterSpec; 22 import java.security.spec.PSSParameterSpec; 23 24 /** 25 * Signature algorithm 26 * 27 * @since 2021-12-13 28 */ 29 public enum SignatureAlgorithm { 30 RSA_PSS_WITH_SHA256( 31 0x101, 32 "RSA", 33 ContentDigestAlgorithm.SHA256, 34 Pair.create( 35 "SHA256withRSAANDMGF1", 36 new PSSParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, 256 / 8, 1))), 37 RSA_PSS_WITH_SHA384( 38 0x102, 39 "RSA", 40 ContentDigestAlgorithm.SHA384, 41 Pair.create( 42 "SHA384withRSAANDMGF1", 43 new PSSParameterSpec("SHA-384", "MGF1", MGF1ParameterSpec.SHA384, 384 / 8, 1))), 44 RSA_PSS_WITH_SHA512( 45 0x103, 46 "RSA", 47 ContentDigestAlgorithm.SHA512, 48 Pair.create( 49 "SHA512withRSAANDMGF1", 50 new PSSParameterSpec("SHA-512", "MGF1", MGF1ParameterSpec.SHA512, 512 / 8, 1))), 51 RSA_PKCS1_V1_5_WITH_SHA256(0x104, "RSA", ContentDigestAlgorithm.SHA256, Pair.create("SHA256withRSA", null)), 52 RSA_PKCS1_V1_5_WITH_SHA384(0x105, "RSA", ContentDigestAlgorithm.SHA256, Pair.create("SHA384withRSA", null)), 53 RSA_PKCS1_V1_5_WITH_SHA512(0x106, "RSA", ContentDigestAlgorithm.SHA512, Pair.create("SHA512withRSA", null)), 54 ECDSA_WITH_SHA256(0x201, "EC", ContentDigestAlgorithm.SHA256, Pair.create("SHA256withECDSA", null)), 55 ECDSA_WITH_SHA384(0x202, "EC", ContentDigestAlgorithm.SHA384, Pair.create("SHA384withECDSA", null)), 56 ECDSA_WITH_SHA512(0x203, "EC", ContentDigestAlgorithm.SHA512, Pair.create("SHA512withECDSA", null)), 57 DSA_WITH_SHA256(0x301, "DSA", ContentDigestAlgorithm.SHA256, Pair.create("SHA256withDSA", null)), 58 DSA_WITH_SHA384(0x302, "DSA", ContentDigestAlgorithm.SHA384, Pair.create("SHA384withDSA", null)), 59 DSA_WITH_SHA512(0x303, "DSA", ContentDigestAlgorithm.SHA512, Pair.create("SHA512withDSA", null)); 60 61 private int id; 62 63 private String keyAlgorithm; 64 65 private ContentDigestAlgorithm contentDigestAlgorithm; 66 67 private Pair<String, ? extends AlgorithmParameterSpec> signatureAlgAndParams; 68 SignatureAlgorithm( int id, String keyAlgorithm, ContentDigestAlgorithm contentDigestAlgorithm, Pair<String, ? extends AlgorithmParameterSpec> signatureAlgAndParams)69 SignatureAlgorithm( 70 int id, 71 String keyAlgorithm, 72 ContentDigestAlgorithm contentDigestAlgorithm, 73 Pair<String, ? extends AlgorithmParameterSpec> signatureAlgAndParams) { 74 this.id = id; 75 this.keyAlgorithm = keyAlgorithm; 76 this.contentDigestAlgorithm = contentDigestAlgorithm; 77 this.signatureAlgAndParams = signatureAlgAndParams; 78 } 79 getId()80 public int getId() { 81 return id; 82 } 83 getKeyAlgorithm()84 public String getKeyAlgorithm() { 85 return keyAlgorithm; 86 } 87 getContentDigestAlgorithm()88 public ContentDigestAlgorithm getContentDigestAlgorithm() { 89 return contentDigestAlgorithm; 90 } 91 getSignatureAlgAndParams()92 public Pair<String, ? extends AlgorithmParameterSpec> getSignatureAlgAndParams() { 93 return signatureAlgAndParams; 94 } 95 96 /** 97 * Find value of SignatureAlgorithm according to ID 98 * 99 * @param id ID of SignatureAlgorithm object 100 * @return SignatureAlgorithm object 101 */ findById(int id)102 public static SignatureAlgorithm findById(int id) { 103 SignatureAlgorithm ret = null; 104 for (SignatureAlgorithm alg : SignatureAlgorithm.values()) { 105 if (id == alg.getId()) { 106 return alg; 107 } 108 } 109 return ret; 110 } 111 } 112