Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / xmlsecurity / source / xmlsec / nss / sanextension_nssimpl.cxx
blob37602b6e03ce2003f4a0da7f78d8ddd48635dcfb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
21 #include <rtl/ustring.hxx>
22 #include <com/sun/star/security/ExtAltNameType.hpp>
23 #include <com/sun/star/security/CertAltNameEntry.hpp>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <comphelper/sequence.hxx>
26 #include <seccomon.h>
27 #include <cert.h>
28 #include <certt.h>
30 #include "sanextension_nssimpl.hxx"
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::uno ;
34 using namespace ::com::sun::star::security ;
36 using ::com::sun::star::security::XCertificateExtension ;
38 namespace {
39 // Helper functions from nss/lib/certdb/genname.c
40 int GetNamesLength(CERTGeneralName *names)
42 int length = 0;
43 CERTGeneralName *first;
45 first = names;
46 if (names != nullptr) {
47 do {
48 length++;
49 names = CERT_GetNextGeneralName(names);
50 } while (names != first);
52 return length;
57 //Methods from XSanExtension
58 css::uno::Sequence< css::security::CertAltNameEntry > SAL_CALL SanExtensionImpl::getAlternativeNames()
60 if (m_Entries.empty())
62 SECItem item;
64 item.type = siDERCertBuffer;
65 item.data = reinterpret_cast<unsigned char*>(m_Extn.m_xExtnValue.getArray());
66 item.len = m_Extn.m_xExtnValue.getLength();
68 PRArenaPool *arena;
69 CERTGeneralName *nameList;
70 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
72 if (!arena)
73 return css::uno::Sequence<css::security::CertAltNameEntry>();
75 nameList = CERT_DecodeAltNameExtension(arena, &item);
77 CERTGeneralName* current = nameList;
79 int size = GetNamesLength(nameList);
80 m_Entries.resize(size);
81 for(int i = 0; i < size; ++i){
82 switch (current->type) {
83 case certOtherName: {
84 m_Entries[i].Type = ExtAltNameType_OTHER_NAME;
85 css::beans::PropertyValue otherNameProp;
86 otherNameProp.Name = OUString::createFromAscii(CERT_GetOidString(&current->name.OthName.oid));
88 Sequence< sal_Int8 > otherName( current->name.OthName.name.len ) ;
89 for( unsigned int r = 0; r < current->name.OthName.name.len ; r ++ )
90 otherName[r] = *( current->name.OthName.name.data + r ) ;
92 otherNameProp.Value <<= otherName;
94 m_Entries[i].Value <<= otherNameProp;
95 break;
97 case certRFC822Name:
98 m_Entries[i].Type = ExtAltNameType_RFC822_NAME;
99 m_Entries[i].Value <<= OUString(reinterpret_cast<char*>(current->name.other.data), current->name.other.len, RTL_TEXTENCODING_ASCII_US);
100 break;
101 case certDNSName:
102 m_Entries[i].Type = ExtAltNameType_DNS_NAME;
103 m_Entries[i].Value <<= OUString(reinterpret_cast<char*>(current->name.other.data), current->name.other.len, RTL_TEXTENCODING_ASCII_US);
104 break;
105 case certX400Address: {
106 // unsupported
107 m_Entries[i].Type = ExtAltNameType_X400_ADDRESS;
108 break;
110 case certDirectoryName: {
111 // unsupported
112 m_Entries[i].Type = ExtAltNameType_DIRECTORY_NAME;
113 break;
115 case certEDIPartyName: {
116 // unsupported
117 m_Entries[i].Type = ExtAltNameType_EDI_PARTY_NAME;
118 break;
120 case certURI:
121 m_Entries[i].Type = ExtAltNameType_URL;
122 m_Entries[i].Value <<= OUString(reinterpret_cast<char*>(current->name.other.data), current->name.other.len, RTL_TEXTENCODING_ASCII_US);
123 break;
124 case certIPAddress: {
125 m_Entries[i].Type = ExtAltNameType_IP_ADDRESS;
127 Sequence< sal_Int8 > ipAddress( current->name.other.len ) ;
128 for( unsigned int r = 0; r < current->name.other.len ; r ++ )
129 ipAddress[r] = *( current->name.other.data + r ) ;
131 m_Entries[i].Value <<= ipAddress;
132 break;
134 case certRegisterID:
135 m_Entries[i].Type = ExtAltNameType_REGISTERED_ID;
138 OString nssOid(CERT_GetOidString(&current->name.other));
139 OString unoOid = removeOIDFromString(nssOid);
140 m_Entries[i].Value <<= OStringToOUString( unoOid, RTL_TEXTENCODING_ASCII_US );
141 break;
143 current = CERT_GetNextGeneralName(current);
146 PORT_FreeArena(arena, PR_FALSE);
149 return comphelper::containerToSequence<css::security::CertAltNameEntry>(m_Entries);
152 OString SanExtensionImpl::removeOIDFromString( const OString &oidString)
154 OString objID;
155 OString oid("OID.");
156 if (oidString.match(oid))
157 objID = oidString.copy(oid.getLength());
158 else
159 objID = oidString;
160 return objID;
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */