GNUnet  0.10.x
credential_misc.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009-2013, 2016 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
21 
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
32 #include "gnunet_signatures.h"
33 #include "credential.h"
34 #include <inttypes.h>
35 
36 char*
38 {
39  char *cred_str;
40  char *subject_pkey;
41  char *issuer_pkey;
42  char *signature;
43 
44 
48  sizeof (struct GNUNET_CRYPTO_EcdsaSignature),
49  &signature);
50  GNUNET_asprintf (&cred_str,
51  "%s.%s -> %s | %s | %"SCNu64,
52  issuer_pkey,
53  cred->issuer_attribute,
54  subject_pkey,
55  signature,
56  cred->expiration.abs_value_us);
57  GNUNET_free (subject_pkey);
58  GNUNET_free (issuer_pkey);
59  GNUNET_free (signature);
60  return cred_str;
61 }
62 
65 {
66  struct GNUNET_CREDENTIAL_Credential *cred;
67  size_t enclen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
68  if (enclen % 5 > 0)
69  enclen += 5 - enclen % 5;
70  enclen /= 5; /* 260/5 = 52 */
71  char subject_pkey[enclen + 1];
72  char issuer_pkey[enclen + 1];
73  char name[253 + 1];
74  char signature[256]; //TODO max payload size
75 
76  struct GNUNET_CRYPTO_EcdsaSignature *sig;
77  struct GNUNET_TIME_Absolute etime_abs;
78 
79  if (5 != SSCANF (s,
80  "%52s.%253s -> %52s | %s | %"SCNu64,
81  issuer_pkey,
82  name,
83  subject_pkey,
84  signature,
85  &etime_abs.abs_value_us))
86  {
88  _("Unable to parse CRED record string `%s'\n"),
89  s);
90  return NULL;
91  }
92  cred = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_Credential) + strlen (name) + 1);
94  strlen (subject_pkey),
95  &cred->subject_key);
97  strlen (issuer_pkey),
98  &cred->issuer_key);
100  strlen (signature),
101  (char**)&sig));
102  cred->signature = *sig;
103  cred->expiration = etime_abs;
104  GNUNET_free (sig);
105  GNUNET_memcpy (&cred[1],
106  name,
107  strlen (name)+1);
108  cred->issuer_attribute_len = strlen ((char*)&cred[1]);
109  cred->issuer_attribute = (char*)&cred[1];
110  return cred;
111 }
112 
123  struct GNUNET_CRYPTO_EcdsaPublicKey *subject,
124  const char *attribute,
126 {
127  struct CredentialEntry *crd;
128  struct GNUNET_CREDENTIAL_Credential *cred;
129  size_t size;
130 
131  size = sizeof (struct CredentialEntry) + strlen (attribute) + 1;
132  crd = GNUNET_malloc (size);
133  cred = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_Credential) + strlen (attribute) + 1);
134  crd->purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_EcdsaSignature));
135 
136  crd->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL);
138  &crd->issuer_key);
139  crd->subject_key = *subject;
140  crd->expiration = GNUNET_htonll (expiration->abs_value_us);
141  crd->issuer_attribute_len = htonl (strlen (attribute)+1);
142  GNUNET_memcpy ((char*)&crd[1],
143  attribute,
144  strlen (attribute)+1);
145  if (GNUNET_OK !=
146  GNUNET_CRYPTO_ecdsa_sign (issuer,
147  &crd->purpose,
148  &crd->signature))
149  {
150  GNUNET_break (0);
151  GNUNET_free (crd);
152  GNUNET_free (cred);
153  return NULL;
154  }
155  cred->signature = crd->signature;
156  cred->expiration = *expiration;
158  &cred->issuer_key);
159 
160  cred->subject_key = *subject;
161  GNUNET_memcpy (&cred[1],
162  attribute,
163  strlen (attribute)+1);
164  cred->issuer_attribute = (char*)&cred[1];
165  GNUNET_free (crd);
166  return cred;
167 }
168 
169 
struct GNUNET_CRYPTO_EcdsaPublicKey issuer_pkey
Issuer key.
struct GNUNET_CREDENTIAL_Credential * GNUNET_CREDENTIAL_credential_from_string(const char *s)
static char * expiration
Credential TTL.
struct GNUNET_CRYPTO_EcdsaPublicKey subject_key
Public key of the subject this credential was issued to.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
IPC messages between CREDENTIAL API and CREDENTIAL service.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
Private ECC key encoded for transmission.
struct GNUNET_CRYPTO_EcdsaPublicKey issuer_key
The issuer of the credential.
struct GNUNET_CRYPTO_EcdsaPublicKey subject_pkey
Subject key.
uint64_t abs_value_us
The actual value.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
int GNUNET_CRYPTO_ecdsa_sign(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EcdsaSignature *sig)
ECDSA Sign a given block.
Definition: crypto_ecc.c:931
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define GNUNET_memcpy(dst, src, n)
#define GNUNET_SIGNATURE_PURPOSE_CREDENTIAL
Signature for a GNUnet credential.
an ECC signature using ECDSA
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature of this credential.
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **output)
Decode from Base64.
Definition: strings.c:2067
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:2004
char * GNUNET_CRYPTO_ecdsa_public_key_to_string(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Convert a public key to a string.
Definition: crypto_ecc.c:334
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
static unsigned int size
Size of the "table".
Definition: peer.c:67
void GNUNET_CRYPTO_ecdsa_key_get_public(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:241
char * GNUNET_CREDENTIAL_credential_to_string(const struct GNUNET_CREDENTIAL_Credential *cred)
const char * name
struct GNUNET_TIME_Absolute expiration
Expiration of this credential.
const char * issuer_attribute
The attribute.
int GNUNET_CRYPTO_ecdsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Convert a string representing a public key to a public key.
Definition: crypto_ecc.c:468
#define SSCANF
Definition: plibc.h:691
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
#define GNUNET_log(kind,...)
struct GNUNET_CREDENTIAL_Credential * GNUNET_CREDENTIAL_credential_issue(const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer, struct GNUNET_CRYPTO_EcdsaPublicKey *subject, const char *attribute, struct GNUNET_TIME_Absolute *expiration)
Issue an attribute to a subject.
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint32_t issuer_attribute_len
Length of the attribute.