nss: upgrade to release 3.73
[LibreOffice.git] / xmlsecurity / source / xmlsec / nss / sanextension_nssimpl.cxx
blob5827c5f922725b1c03647000e9f311ed4fc499ab
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 namespace {
37 // Helper functions from nss/lib/certdb/genname.c
38 int GetNamesLength(CERTGeneralName *names)
40 int length = 0;
41 CERTGeneralName *first;
43 first = names;
44 if (names != nullptr) {
45 do {
46 length++;
47 names = CERT_GetNextGeneralName(names);
48 } while (names != first);
50 return length;
55 //Methods from XSanExtension
56 css::uno::Sequence< css::security::CertAltNameEntry > SAL_CALL SanExtensionImpl::getAlternativeNames()
58 if (m_Entries.empty())
60 SECItem item;
62 item.type = siDERCertBuffer;
63 item.data = reinterpret_cast<unsigned char*>(m_Extn.m_xExtnValue.getArray());
64 item.len = m_Extn.m_xExtnValue.getLength();
66 PRArenaPool *arena;
67 CERTGeneralName *nameList;
68 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
70 if (!arena)
71 return css::uno::Sequence<css::security::CertAltNameEntry>();
73 nameList = CERT_DecodeAltNameExtension(arena, &item);
75 CERTGeneralName* current = nameList;
77 int size = GetNamesLength(nameList);
78 m_Entries.resize(size);
79 for(int i = 0; i < size; ++i){
80 switch (current->type) {
81 case certOtherName: {
82 m_Entries[i].Type = ExtAltNameType_OTHER_NAME;
83 css::beans::PropertyValue otherNameProp;
84 otherNameProp.Name = OUString::createFromAscii(CERT_GetOidString(&current->name.OthName.oid));
86 Sequence< sal_Int8 > otherName( current->name.OthName.name.len ) ;
87 for( unsigned int r = 0; r < current->name.OthName.name.len ; r ++ )
88 otherName[r] = *( current->name.OthName.name.data + r ) ;
90 otherNameProp.Value <<= otherName;
92 m_Entries[i].Value <<= otherNameProp;
93 break;
95 case certRFC822Name:
96 m_Entries[i].Type = ExtAltNameType_RFC822_NAME;
97 m_Entries[i].Value <<= OUString(reinterpret_cast<char*>(current->name.other.data), current->name.other.len, RTL_TEXTENCODING_ASCII_US);
98 break;
99 case certDNSName:
100 m_Entries[i].Type = ExtAltNameType_DNS_NAME;
101 m_Entries[i].Value <<= OUString(reinterpret_cast<char*>(current->name.other.data), current->name.other.len, RTL_TEXTENCODING_ASCII_US);
102 break;
103 case certX400Address: {
104 // unsupported
105 m_Entries[i].Type = ExtAltNameType_X400_ADDRESS;
106 break;
108 case certDirectoryName: {
109 // unsupported
110 m_Entries[i].Type = ExtAltNameType_DIRECTORY_NAME;
111 break;
113 case certEDIPartyName: {
114 // unsupported
115 m_Entries[i].Type = ExtAltNameType_EDI_PARTY_NAME;
116 break;
118 case certURI:
119 m_Entries[i].Type = ExtAltNameType_URL;
120 m_Entries[i].Value <<= OUString(reinterpret_cast<char*>(current->name.other.data), current->name.other.len, RTL_TEXTENCODING_ASCII_US);
121 break;
122 case certIPAddress: {
123 m_Entries[i].Type = ExtAltNameType_IP_ADDRESS;
125 Sequence< sal_Int8 > ipAddress( current->name.other.len ) ;
126 for( unsigned int r = 0; r < current->name.other.len ; r ++ )
127 ipAddress[r] = *( current->name.other.data + r ) ;
129 m_Entries[i].Value <<= ipAddress;
130 break;
132 case certRegisterID:
133 m_Entries[i].Type = ExtAltNameType_REGISTERED_ID;
136 OString nssOid(CERT_GetOidString(&current->name.other));
137 OString unoOid = removeOIDFromString(nssOid);
138 m_Entries[i].Value <<= OStringToOUString( unoOid, RTL_TEXTENCODING_ASCII_US );
139 break;
141 current = CERT_GetNextGeneralName(current);
144 PORT_FreeArena(arena, PR_FALSE);
147 return comphelper::containerToSequence<css::security::CertAltNameEntry>(m_Entries);
150 OString SanExtensionImpl::removeOIDFromString( const OString &oidString)
152 OString objID;
153 OString oid("OID.");
154 if (oidString.match(oid))
155 objID = oidString.copy(oid.getLength());
156 else
157 objID = oidString;
158 return objID;
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */