4 * Reading/writing bignum values
6 * This is free software; see Copyright file in the source
7 * distribution for precise wording.
9 * Copyright (c) 2003 America Online, Inc. All rights reserved.
19 #include <libxml/tree.h>
21 #include <xmlsec/xmlsec.h>
22 #include <xmlsec/buffer.h>
23 #include <xmlsec/base64.h>
24 #include <xmlsec/errors.h>
26 #include <xmlsec/nss/crypto.h>
27 #include <xmlsec/nss/bignum.h>
30 * xmlSecNssNodeGetBigNumValue:
31 * @arena: the arena from which to allocate memory
32 * @cur: the poitner to an XML node.
33 * @a: a SECItem object to hold the BigNum value
35 * Converts the node content from CryptoBinary format
36 * (http://www.w3.org/TR/xmldsig-core/#sec-CryptoBinary)
37 * to a SECItem. If no SECItem object provided then a new
38 * one is created (caller is responsible for freeing it).
40 * Returns: a pointer to SECItem produced from CryptoBinary string
41 * or NULL if an error occurs.
44 xmlSecNssNodeGetBigNumValue(PRArenaPool *arena, const xmlNodePtr cur,
51 xmlSecAssert2(arena != NULL, NULL);
52 xmlSecAssert2(cur != NULL, NULL);
54 ret = xmlSecBufferInitialize(&buf, 128);
56 xmlSecError(XMLSEC_ERRORS_HERE,
58 "xmlSecBufferInitialize",
59 XMLSEC_ERRORS_R_XMLSEC_FAILED,
60 XMLSEC_ERRORS_NO_MESSAGE);
64 ret = xmlSecBufferBase64NodeContentRead(&buf, cur);
66 xmlSecError(XMLSEC_ERRORS_HERE,
68 "xmlSecBufferBase64NodeContentRead",
69 XMLSEC_ERRORS_R_XMLSEC_FAILED,
70 XMLSEC_ERRORS_NO_MESSAGE);
71 xmlSecBufferFinalize(&buf);
75 len = xmlSecBufferGetSize(&buf);
78 rv = SECITEM_AllocItem(arena, NULL, len);
81 xmlSecAssert2(rv->data == NULL, NULL);
83 rv->data = PORT_ArenaZAlloc(arena, len);
86 PORT_Memcpy(rv->data, xmlSecBufferGetData(&buf), len);
88 xmlSecBufferFinalize(&buf);
93 * xmlSecNssNodeSetBigNumValue:
94 * @cur: the pointer to an XML node.
95 * @a: a SECItem containing the BigNum value.
96 * @addLineBreaks: if the flag is equal to 1 then
97 * linebreaks will be added before and after
100 * Converts SECItem to CryptoBinary string
101 * (http://www.w3.org/TR/xmldsig-core/#sec-CryptoBinary)
102 * and sets it as the content of the given node. If the
103 * addLineBreaks is set then line breaks are added
104 * before and after the CryptoBinary string.
106 * Returns: 0 on success or -1 otherwise.
109 xmlSecNssNodeSetBigNumValue(xmlNodePtr cur, const SECItem *a, int addLineBreaks) {
113 xmlSecAssert2(a != NULL, -1);
114 xmlSecAssert2(cur != NULL, -1);
116 ret = xmlSecBufferInitialize(&buf, a->len + 1);
118 xmlSecError(XMLSEC_ERRORS_HERE,
120 "xmlSecBufferInitialize",
121 XMLSEC_ERRORS_R_XMLSEC_FAILED,
122 "size=%d", a->len + 1);
126 PORT_Memcpy(xmlSecBufferGetData(&buf), a->data, a->len);
128 ret = xmlSecBufferSetSize(&buf, a->len);
130 xmlSecError(XMLSEC_ERRORS_HERE,
132 "xmlSecBufferSetSize",
133 XMLSEC_ERRORS_R_XMLSEC_FAILED,
135 xmlSecBufferFinalize(&buf);
140 xmlNodeSetContent(cur, xmlSecStringCR);
142 xmlNodeSetContent(cur, xmlSecStringEmpty);
145 ret = xmlSecBufferBase64NodeContentWrite(&buf, cur, xmlSecBase64GetDefaultLineSize());
147 xmlSecError(XMLSEC_ERRORS_HERE,
149 "xmlSecBufferBase64NodeContentWrite",
150 XMLSEC_ERRORS_R_XMLSEC_FAILED,
151 XMLSEC_ERRORS_NO_MESSAGE);
152 xmlSecBufferFinalize(&buf);
157 xmlNodeAddContent(cur, xmlSecStringCR);
160 xmlSecBufferFinalize(&buf);