001/* 002 * Copyright 2017-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2017-2024 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2017-2024 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.util.ssl.cert; 037 038 039 040import com.unboundid.util.NotNull; 041import com.unboundid.util.Nullable; 042import com.unboundid.util.StaticUtils; 043import com.unboundid.util.ThreadSafety; 044import com.unboundid.util.ThreadSafetyLevel; 045 046 047 048/** 049 * This enum defines a set of supported PKCS #8 private key versions. 050 */ 051@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 052public enum PKCS8PrivateKeyVersion 053{ 054 /** 055 * The PKCS #8 v1 private key version. 056 */ 057 V1(0, "v1"), 058 059 060 061 /** 062 * The PKCS #8 v2 private key version. 063 */ 064 V2(1, "v2"); 065 066 067 068 // The integer value for this private key version, as used in the encoded 069 // PKCS #8 private key. 070 private final int intValue; 071 072 // The name for this PKCS #8 private key version. 073 @NotNull private final String name; 074 075 076 077 /** 078 * Creates a new PKCS #8 private key version with the provided information. 079 * 080 * @param intValue The integer value for the private key version. Note that 081 * this is the integer value that is used in the encoded 082 * private key, and not the logical version number that the 083 * encoded value represents (for example, the "v1" private 084 * key version has an integer value of 0 rather than 1). 085 * @param name The name for this private key version. It must not be 086 * {@code null}. 087 */ 088 PKCS8PrivateKeyVersion(final int intValue, @NotNull final String name) 089 { 090 this.intValue = intValue; 091 this.name = name; 092 } 093 094 095 096 /** 097 * Retrieves the integer value for this private key version. Note that this 098 * is the integer value that is used in the encoded private key, and not the 099 * logical version number that the encoded value represents (for example, the 100 * "v1" private key version has an integer value of 0 rather than 1). 101 * 102 * @return The integer value for this private key version. 103 */ 104 int getIntValue() 105 { 106 return intValue; 107 } 108 109 110 111 /** 112 * Retrieves the name for this private key version. 113 * 114 * @return The name for this private key version. 115 */ 116 @NotNull() 117 public String getName() 118 { 119 return name; 120 } 121 122 123 124 /** 125 * Retrieves the private key version for the provided integer value. 126 * 127 * @param intValue The integer value for the private key version to 128 * retrieve. Note that this is the integer value that is 129 * used in the encoded private key, and not the logical 130 * version number that the encoded value represents (for 131 * example, the "v1" private key version has an integer 132 * value of 0 rather than 1). 133 * 134 * @return The private key version for the provided integer value, or 135 * {@code null} if the provided version does not correspond to any 136 * known private key version value. 137 */ 138 @Nullable() 139 static PKCS8PrivateKeyVersion valueOf(final int intValue) 140 { 141 for (final PKCS8PrivateKeyVersion v : values()) 142 { 143 if (v.intValue == intValue) 144 { 145 return v; 146 } 147 } 148 149 return null; 150 } 151 152 153 154 /** 155 * Retrieves the PKCS #8 private key version with the specified name. 156 * 157 * @param name The name of the PKCS #8 private key version to retrieve. It 158 * must not be {@code null}. 159 * 160 * @return The requested PKCS #8 private key version, or {@code null} if no 161 * such version is defined. 162 */ 163 @Nullable() 164 public static PKCS8PrivateKeyVersion forName(@NotNull final String name) 165 { 166 switch (StaticUtils.toLowerCase(name)) 167 { 168 case "1": 169 case "v1": 170 return V1; 171 case "2": 172 case "v2": 173 return V2; 174 default: 175 return null; 176 } 177 } 178}