11/*
2- * Copyright (c) 2018, 2021 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2018, 2022 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
4747public class SignatureUtil {
4848
4949 /**
50- * Convert OID.1.2.3.4 or 1.2.3.4 to its matching stdName.
50+ * Convert OID.1.2.3.4 or 1.2.3.4 to its matching stdName, and return
51+ * upper case algorithm name.
5152 *
5253 * @param algName input, could be in any form
53- * @return the matching stdName, or {@code algName} if it is not in the
54- * form of an OID, or the OID value if no match is found.
54+ * @return the matching algorithm name or the OID string in upper case.
5555 */
5656 private static String checkName (String algName ) {
57- if (!algName .contains ("." )) {
58- return algName ;
59- } else {
57+ algName = algName .toUpperCase (Locale .ENGLISH );
58+ if (algName .contains ("." )) {
6059 // convert oid to String
6160 if (algName .startsWith ("OID." )) {
6261 algName = algName .substring (4 );
6362 }
63+
6464 KnownOIDs ko = KnownOIDs .findMatch (algName );
65- return ko != null ? ko .stdName () : algName ;
65+ if (ko != null ) {
66+ return ko .stdName ().toUpperCase (Locale .ENGLISH );
67+ }
6668 }
69+
70+ return algName ;
6771 }
6872
6973 // Utility method of creating an AlgorithmParameters object with
7074 // the specified algorithm name and encoding
75+ //
76+ // Note this method can be called only after converting OID.1.2.3.4 or
77+ // 1.2.3.4 to its matching stdName, which is implemented in the
78+ // checkName(String) method.
7179 private static AlgorithmParameters createAlgorithmParameters (String algName ,
7280 byte [] paramBytes ) throws ProviderException {
7381
7482 try {
75- algName = checkName (algName );
7683 AlgorithmParameters result =
7784 AlgorithmParameters .getInstance (algName );
7885 result .init (paramBytes );
@@ -96,7 +103,7 @@ public static AlgorithmParameterSpec getParamSpec(String sigName,
96103
97104 AlgorithmParameterSpec paramSpec = null ;
98105 if (params != null ) {
99- sigName = checkName (sigName ). toUpperCase ( Locale . ENGLISH ) ;
106+ sigName = checkName (sigName );
100107 // AlgorithmParameters.getAlgorithm() may returns oid if it's
101108 // created during DER decoding. Convert to use the standard name
102109 // before passing it to RSAUtil
@@ -140,7 +147,7 @@ public static AlgorithmParameterSpec getParamSpec(String sigName,
140147 AlgorithmParameterSpec paramSpec = null ;
141148
142149 if (paramBytes != null ) {
143- sigName = checkName (sigName ). toUpperCase ( Locale . ENGLISH ) ;
150+ sigName = checkName (sigName );
144151 if (sigName .contains ("RSA" )) {
145152 AlgorithmParameters params =
146153 createAlgorithmParameters (sigName , paramBytes );
@@ -313,7 +320,7 @@ public static String extractKeyAlgFromDwithE(String signatureAlgorithm) {
313320 public static AlgorithmParameterSpec getDefaultParamSpec (
314321 String sigAlg , Key k ) {
315322 sigAlg = checkName (sigAlg );
316- if (sigAlg .equalsIgnoreCase ("RSASSA-PSS" )) {
323+ if (sigAlg .equals ("RSASSA-PSS" )) {
317324 if (k instanceof RSAKey ) {
318325 AlgorithmParameterSpec spec = ((RSAKey ) k ).getParams ();
319326 if (spec instanceof PSSParameterSpec ) {
@@ -428,7 +435,7 @@ public static AlgorithmId fromSignature(Signature sigEngine, PrivateKey key)
428435 */
429436 public static void checkKeyAndSigAlgMatch (PrivateKey key , String sAlg ) {
430437 String kAlg = key .getAlgorithm ().toUpperCase (Locale .ENGLISH );
431- sAlg = checkName (sAlg ). toUpperCase ( Locale . ENGLISH ) ;
438+ sAlg = checkName (sAlg );
432439 switch (sAlg ) {
433440 case "RSASSA-PSS" -> {
434441 if (!kAlg .equals ("RSASSA-PSS" )
0 commit comments