1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * Copyright © 2017 Amdocs
\r
7 * * ===========================================================================
\r
8 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * * you may not use this file except in compliance with the License.
\r
10 * * You may obtain a copy of the License at
\r
12 * * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * * Unless required by applicable law or agreed to in writing, software
\r
15 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * * See the License for the specific language governing permissions and
\r
18 * * limitations under the License.
\r
19 * * ============LICENSE_END====================================================
\r
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cadi.principal;
\r
26 import java.io.IOException;
\r
27 import java.security.cert.CertificateEncodingException;
\r
28 import java.security.cert.X509Certificate;
\r
29 import java.util.regex.Pattern;
\r
31 import com.att.cadi.GetCred;
\r
33 public class X509Principal extends BearerPrincipal implements GetCred {
\r
34 private static final Pattern pattern = Pattern.compile("[a-zA-Z0-9]*\\@[a-zA-Z0-9.]*");
\r
35 private byte[] content;
\r
36 private X509Certificate cert;
\r
37 private String name;
\r
39 public X509Principal(String identity, X509Certificate cert, byte[] content) {
\r
41 this.content = content;
\r
45 public X509Principal(X509Certificate cert, byte[] content) throws IOException {
\r
46 this.content=content;
\r
48 String subj = cert.getSubjectDN().getName();
\r
49 int cn = subj.indexOf("OU=");
\r
52 int space = subj.indexOf(',',cn);
\r
54 String id = subj.substring(cn, space);
\r
55 if(pattern.matcher(id).matches()) {
\r
61 throw new IOException("X509 does not have Identity as CN");
\r
66 public String getAsHeader() throws IOException {
\r
69 content=cert.getEncoded();
\r
70 } catch (CertificateEncodingException e) {
\r
71 throw new IOException(e);
\r
73 return "X509 " + content;
\r
76 public String toString() {
\r
77 return "X509 Authentication for " + name;
\r
81 public byte[] getCred() {
\r
83 return content==null?(content=cert.getEncoded()):content;
\r
84 } catch (CertificateEncodingException e) {
\r
90 public String getName() {
\r