1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmlsecurity.hxx"
26 
27 #include <xmlsecurity/biginteger.hxx>
28 
29 #include <sal/types.h>
30 //For reasons that escape me, this is what xmlsec does when size_t is not 4
31 #if SAL_TYPES_SIZEOFPOINTER != 4
32 #    define XMLSEC_NO_SIZE_T
33 #endif
34 #include <xmlsec/xmlsec.h>
35 #include <xmlsec/bn.h>
36 #include <com/sun/star/uno/Sequence.hxx>
37 
38 using namespace ::com::sun::star::uno ;
39 using ::rtl::OUString ;
40 
numericStringToBigInteger(OUString numeral)41 Sequence< sal_Int8 > numericStringToBigInteger ( OUString numeral )
42 {
43 	if( numeral.getStr() != NULL )
44 	{
45 		xmlChar* chNumeral ;
46 		const xmlSecByte* bnInteger ;
47 		xmlSecSize length ;
48 		xmlSecBn bn ;
49 
50 		rtl::OString onumeral = rtl::OUStringToOString( numeral , RTL_TEXTENCODING_ASCII_US ) ;
51 
52 		chNumeral = xmlStrndup( ( const xmlChar* )onumeral.getStr(), ( int )onumeral.getLength() ) ;
53 
54 		if( xmlSecBnInitialize( &bn, 0 ) < 0 ) {
55 			xmlFree( chNumeral ) ;
56 			return Sequence< sal_Int8 >();
57 		}
58 
59 		if( xmlSecBnFromDecString( &bn, chNumeral ) < 0 ) {
60 			xmlFree( chNumeral ) ;
61 			xmlSecBnFinalize( &bn ) ;
62 			return Sequence< sal_Int8 >();
63 		}
64 
65 		xmlFree( chNumeral ) ;
66 
67 		length = xmlSecBnGetSize( &bn ) ;
68 		if( length <= 0 ) {
69 			xmlSecBnFinalize( &bn ) ;
70 			return Sequence< sal_Int8 >();
71 		}
72 
73 		bnInteger = xmlSecBnGetData( &bn ) ;
74 		if( bnInteger == NULL ) {
75 			xmlSecBnFinalize( &bn ) ;
76 			return Sequence< sal_Int8 >();
77 		}
78 
79 		Sequence< sal_Int8 > integer( length ) ;
80 		for( unsigned int i = 0 ; i < length ; i ++ )
81 		{
82 			integer[i] = *( bnInteger + i ) ;
83 		}
84 
85 		xmlSecBnFinalize( &bn ) ;
86 		return integer ;
87 	}
88 
89 	return Sequence< sal_Int8 >();
90 }
91 
bigIntegerToNumericString(Sequence<sal_Int8> integer)92 OUString bigIntegerToNumericString ( Sequence< sal_Int8 > integer )
93 {
94 	OUString aRet ;
95 
96 	if( integer.getLength() ) {
97 		xmlSecBn bn ;
98 		xmlChar* chNumeral ;
99 
100 		if( xmlSecBnInitialize( &bn, 0 ) < 0 )
101 			return aRet ;
102 
103 		if( xmlSecBnSetData( &bn, ( const unsigned char* )&integer[0], integer.getLength() ) < 0 ) {
104 			xmlSecBnFinalize( &bn ) ;
105 			return aRet ;
106 		}
107 
108 		chNumeral = xmlSecBnToDecString( &bn ) ;
109 		if( chNumeral == NULL ) {
110 			xmlSecBnFinalize( &bn ) ;
111 			return aRet ;
112 		}
113 
114 		aRet = OUString::createFromAscii( ( const char* )chNumeral ) ;
115 
116 		xmlSecBnFinalize( &bn ) ;
117 		xmlFree( chNumeral ) ;
118 	}
119 
120 	return aRet ;
121 }
122 
123