- more changes: sign & verify working and tested
[oweals/gnunet.git] / src / namestore / namestore_common.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file namestore/namestore_common.c
23  * @brief API to access the NAMESTORE service
24  * @author Martin Schanzenbach
25  * @author Matthias Wachs
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "namestore.h"
35 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
36
37 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
38
39
40 /**
41  * Internal format of a record in the serialized form.
42  */
43 struct NetworkRecord
44 {
45
46   /**
47    * Expiration time for the DNS record.
48    */
49   struct GNUNET_TIME_AbsoluteNBO expiration;
50
51   /**
52    * Number of bytes in 'data', network byte order.
53    */
54   uint32_t data_size;
55
56   /**
57    * Type of the GNS/DNS record, network byte order.
58    */
59   uint32_t record_type;
60
61   /**
62    * Flags for the record, network byte order.
63    */
64   uint32_t flags;
65   
66 };
67
68 /**
69  * Calculate how many bytes we will need to serialize the given
70  * records.
71  *
72  * @param rd_count number of records in the rd array
73  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
74  *
75  * @return the required size to serialize
76  *
77  */
78 size_t
79 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
80                                    const struct GNUNET_NAMESTORE_RecordData *rd)
81 {
82   unsigned int i;
83   size_t ret;
84
85   ret = sizeof (struct NetworkRecord) * rd_count;
86   for (i=0;i<rd_count;i++)
87   {
88     GNUNET_assert (ret + rd[i].data_size >= ret);
89     ret += rd[i].data_size;
90   }
91   return ret;  
92 }
93
94
95 /**
96  * Serialize the given records to the given destination buffer.
97  *
98  * @param rd_cound number of records in the rd array
99  * @param rd array of GNUNET_NAMESTORE_RecordData with rd_count elements
100  * @param dest_size size of the destination array
101  * @param dest where to write the result
102  *
103  * @return the size of serialized records
104  */
105 ssize_t
106 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
107                                     const struct GNUNET_NAMESTORE_RecordData *rd,
108                                     size_t dest_size,
109                                     char *dest)
110 {
111   struct NetworkRecord rec;
112   unsigned int i;
113   size_t off;
114   
115   off = 0;
116   for (i=0;i<rd_count;i++)
117   {
118     rec.expiration = GNUNET_TIME_absolute_hton (rd[i].expiration);
119     rec.data_size = htonl ((uint32_t) rd[i].data_size);
120     rec.record_type = htonl (rd[i].record_type);
121     rec.flags = htonl (rd[i].flags);
122     if (off + sizeof (rec) > dest_size)
123       return -1;
124     memcpy (&dest[off], &rec, sizeof (rec));
125     off += sizeof (rec);
126     if (off + rd[i].data_size > dest_size)
127       return -1;
128     memcpy (&dest[off], rd[i].data, rd[i].data_size);
129     off += rd[i].data_size;
130   }
131   return off;
132 }
133
134
135 /**
136  * Deserialize the given records to the given destination.
137  *
138  * @param len size of the serialized record data
139  * @param src the serialized record data
140  * @param rd_cound number of records in the rd array
141  * @param dest where to put the data
142  *
143  * @return GNUNET_OK on success, GNUNET_SYSERR on error
144  */
145 int
146 GNUNET_NAMESTORE_records_deserialize (size_t len,
147                                       const char *src,
148                                       unsigned int rd_count,
149                                       struct GNUNET_NAMESTORE_RecordData *dest)
150 {
151   struct NetworkRecord rec;
152   unsigned int i;
153   size_t off;
154   
155   off = 0;
156   for (i=0;i<rd_count;i++)
157   {
158     if (off + sizeof (rec) > len)
159       return GNUNET_SYSERR;
160     memcpy (&rec, &src[off], sizeof (rec));
161     dest[i].expiration = GNUNET_TIME_absolute_ntoh (rec.expiration);
162     dest[i].data_size = ntohl ((uint32_t) rec.data_size);
163     dest[i].record_type = ntohl (rec.record_type);
164     dest[i].flags = ntohl (rec.flags);
165     off += sizeof (rec);
166
167     if (off + sizeof (dest[i].data_size) > len)
168       return GNUNET_SYSERR;
169     dest[i].data = &src[off];
170     off += dest[i].data_size;
171   }
172   return GNUNET_OK; 
173 }
174
175 struct GNUNET_CRYPTO_RsaSignature *
176 GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key, const char *name, struct GNUNET_NAMESTORE_RecordData *rd, unsigned int rd_count)
177 {
178   struct GNUNET_CRYPTO_RsaSignature *sig = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignature));
179   struct GNUNET_CRYPTO_RsaSignaturePurpose *sig_purpose;
180   size_t rd_ser_len;
181   size_t name_len;
182   char * name_tmp;
183   char * rd_tmp;
184   int res;
185
186   if (name == NULL)
187   {
188     GNUNET_break (0);
189     GNUNET_free (sig);
190     return NULL;
191   }
192   name_len = strlen (name) + 1;
193
194   rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
195   char rd_ser[rd_ser_len];
196   GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
197
198   sig_purpose = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + rd_ser_len + name_len);
199
200   sig_purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)+ rd_ser_len + name_len);
201   sig_purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
202   name_tmp = (char *) &sig_purpose[1];
203   rd_tmp = &name_tmp[name_len];
204   memcpy (name_tmp, name, name_len);
205   memcpy (rd_tmp, rd_ser, rd_ser_len);
206
207   res = GNUNET_CRYPTO_rsa_sign (key, sig_purpose, sig);
208
209   GNUNET_free (sig_purpose);
210
211   if (GNUNET_OK != res)
212   {
213     GNUNET_break (0);
214     GNUNET_free (sig);
215     return NULL;
216   }
217   return sig;
218 }
219
220 /* end of namestore_api.c */