如何验证java,如何验证Java中的区分名称(DN)?

这篇博客讨论了如何在Java中验证X509名称,确保它只包含单个CN(Common Name)。作者提供了一个使用BouncyCastle库的示例代码,但发现该代码允许多个CN。为解决此问题,作者提出了一种枚举方法,通过遍历X500Name的每个可能元素来检查是否存在多个CN,如果有,则抛出IllegalArgumentException。
摘要由CSDN通过智能技术生成

I am trying to validate DN in java.

So far I have tried to validate it using Bouncy castle libry

private boolean isValidDn(String dn) {

try {

X509Name name = new X509Name(dn);

return true;

} catch (IllegalArgumentException e) {

}

return false;

}

This code is working but the problem is this code allows to have multiple CN.

For example: this code return true for CN=first,CN=second,ou=org,ou=org2,c=US

But I want validation that return true only if there is one cn,ou,o,c etc..

Any help would be appreciated.

解决方案

If you use the following enum, you should be able to iterate every element possible for an X500Name or X509Name.

public enum MyBCStyle {

/**

* country code - StringType(SIZE(2))

*/

C(BCStyle.C),

/**

* organization - StringType(SIZE(1..64))

*/

O(BCStyle.O ),

/**

* organizational unit name - StringType(SIZE(1..64))

*/

OU(BCStyle.OU),

/**

* Title

*/

T(BCStyle.T ),

/**

* common name - StringType(SIZE(1..64))

*/

CN(BCStyle.CN ),

/**

* device serial number name - StringType(SIZE(1..64))

*/

SN(BCStyle.SN ),

/**

* street - StringType(SIZE(1..64))

*/

STREET(BCStyle.STREET ),

/**

* device serial number name - StringType(SIZE(1..64))

*/

SERIALNUMBER(BCStyle.SERIALNUMBER),

/**

* locality name - StringType(SIZE(1..64))

*/

L(BCStyle.L ),

/**

* state, or province name - StringType(SIZE(1..64))

*/

ST(BCStyle.ST ),

/**

* Naming attributes of type X520name

*/

SURNAME(BCStyle.SURNAME ),

GIVENNAME(BCStyle.GIVENNAME ),

INITIALS(BCStyle.INITIALS ),

GENERATION(BCStyle.GENERATION ),

UNIQUE_IDENTIFIER(BCStyle.UNIQUE_IDENTIFIER ),

/**

* businessCategory - DirectoryString(SIZE(1..128)

*/

BUSINESS_CATEGORY(BCStyle.BUSINESS_CATEGORY ),

/**

* postalCode - DirectoryString(SIZE(1..40)

*/

POSTAL_CODE(BCStyle.POSTAL_CODE ),

/**

* dnQualifier - DirectoryString(SIZE(1..64)

*/

DN_QUALIFIER(BCStyle.DN_QUALIFIER ),

/**

* RFC 3039 Pseudonym - DirectoryString(SIZE(1..64)

*/

PSEUDONYM(BCStyle.PSEUDONYM ),

/**

* RFC 3039 DateOfBirth - GeneralizedTime - YYYYMMDD000000Z

*/

DATE_OF_BIRTH(BCStyle.DATE_OF_BIRTH ),

/**

* RFC 3039 PlaceOfBirth - DirectoryString(SIZE(1..128)

*/

PLACE_OF_BIRTH(BCStyle.PLACE_OF_BIRTH ),

/**

* RFC 3039 Gender - PrintableString (SIZE(1)) -- "M", "F", "m" or "f"

*/

GENDER(BCStyle.GENDER ),

/**

* RFC 3039 CountryOfCitizenship - PrintableString (SIZE (2)) -- ISO 3166

* codes only

*/

COUNTRY_OF_CITIZENSHIP(BCStyle.COUNTRY_OF_CITIZENSHIP ),

/**

* RFC 3039 CountryOfResidence - PrintableString (SIZE (2)) -- ISO 3166

* codes only

*/

COUNTRY_OF_RESIDENCE(BCStyle.COUNTRY_OF_RESIDENCE ),

/**

* ISIS-MTT NameAtBirth - DirectoryString(SIZE(1..64)

*/

NAME_AT_BIRTH(BCStyle.NAME_AT_BIRTH ),

/**

* RFC 3039 PostalAddress - SEQUENCE SIZE (1..6) OF

* DirectoryString(SIZE(1..30))

*/

POSTAL_ADDRESS(BCStyle.POSTAL_ADDRESS ),

/**

* RFC 2256 dmdName

*/

DMD_NAME(BCStyle.DMD_NAME ),

/**

* id-at-telephoneNumber

*/

TELEPHONE_NUMBER(BCStyle.TELEPHONE_NUMBER),

/**

* id-at-name

*/

NAME(BCStyle.NAME),

/**

* Email address (RSA PKCS#9 extension) - IA5String.

*

Note: if you're trying to be ultra orthodox, don't use this! It shouldn't be in here.

*/

EmailAddress(BCStyle.EmailAddress),

/**

* more from PKCS#9

*/

UnstructuredName(BCStyle.UnstructuredName),

UnstructuredAddress(BCStyle.UnstructuredAddress),

E(BCStyle.E),

DC(BCStyle.DC),

/**

* LDAP User id.

*/

UID(BCStyle.UID );

private ASN1ObjectIdentifier identifier;

public ASN1ObjectIdentifier getIdentifier() {

return identifier;

}

private MyBCStyle(ASN1ObjectIdentifier asn1ObjectIdentifier) {

this.identifier = asn1ObjectIdentifier;

}

}

This way, you can do

for(MyBCStyle bcStyle : MyBCStyle.values()) {

if(x500name.getRDNs(bcStyle.getIdentifier()).length > 1) {

throw new IllegalArgumentException("Multiple " + bcStyle.name() + " was found.");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值