42 #include <openssl/hmac.h>
43 #include <openssl/bio.h>
44 #include <openssl/buffer.h>
45 #include <openssl/err.h>
46 #include <openssl/ssl.h>
55 # include "sys/param.h"
58 #if OPENSSL_VERSION_NUMBER < 0x10100000L
60 HMAC_CTX *ctx = (HMAC_CTX *)OPENSSL_malloc(
sizeof(HMAC_CTX));
61 if (ctx) HMAC_CTX_init(ctx);
67 HMAC_CTX_cleanup(ctx);
77 int parseURL(
char *url,
char *host,
int &port,
char **path) {
83 char *p = strstr(url,
"//");
90 char *p2 = strchr(p,
'/');
96 int l = std::min((
int)(p2 - p), (
int)
sizeof (buf));
101 p = strchr(buf,
':');
103 int l = std::min((
int)(p - buf), (
int)
sizeof (buf));
104 strncpy(host, buf, l);
121 void Tobase64(
const unsigned char *input,
int length,
char *out) {
129 b64 = BIO_new(BIO_f_base64());
131 bmem = BIO_new(BIO_s_mem());
133 BIO_write(b64, input, length);
135 if (BIO_flush(b64) <= 0) {
140 BIO_get_mem_ptr(b64, &bptr);
143 memcpy(out, bptr->data, bptr->length);
144 out[bptr->length] =
'\0';
159 if (c >=
'a' && c <=
'f') {
169 bool Fromhexdigest(
const unsigned char *input,
int length,
unsigned char *out) {
170 for (
int idx=0; idx < length; idx += 2) {
173 if ((upper < 0) || (lower < 0)) {
176 out[idx/2] = (upper << 4) + lower;
185 sprintf(buf,
"%ld", i);
194 char *ptr = strchr((
char *)s, c);
197 return strchr((
char *)s,
'\0');
233 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
241 unsigned char mdbuf[EVP_MAX_MD_SIZE];
255 if (!fn || !secent) {
259 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
261 mac = EVP_MAC_fetch(0,
"sha256", 0);
262 ctx = EVP_MAC_CTX_new(mac);
269 EVP_MAC_init(ctx, (
const unsigned char *) key, strlen(key), 0);
273 EVP_MAC_update(ctx, (
const unsigned char *) fn,
276 EVP_MAC_update(ctx, (
const unsigned char *) &request,
280 EVP_MAC_update(ctx, (
const unsigned char *) secent->
name,
281 strlen(secent->
name) + 1);
284 EVP_MAC_update(ctx, (
const unsigned char *) secent->
vorg,
285 strlen(secent->
vorg) + 1);
288 EVP_MAC_update(ctx, (
const unsigned char *) secent->
host,
289 strlen(secent->
host) + 1);
292 EVP_MAC_update(ctx, (
const unsigned char *) secent->
moninfo,
295 localtime_r(&tim, &tms);
296 strftime(buf,
sizeof (buf),
"%s", &tms);
297 EVP_MAC_update(ctx, (
const unsigned char *) buf,
300 EVP_MAC_final(ctx, mdbuf, &len, EVP_MAX_MD_SIZE);
302 EVP_MAC_CTX_free(ctx);
315 HMAC_Init_ex(ctx, (
const void *) key, strlen(key), EVP_sha256(), 0);
319 HMAC_Update(ctx, (
const unsigned char *) fn,
322 HMAC_Update(ctx, (
const unsigned char *) &request,
326 HMAC_Update(ctx, (
const unsigned char *) secent->
name,
327 strlen(secent->
name) + 1);
330 HMAC_Update(ctx, (
const unsigned char *) secent->
vorg,
331 strlen(secent->
vorg) + 1);
334 HMAC_Update(ctx, (
const unsigned char *) secent->
host,
335 strlen(secent->
host) + 1);
338 HMAC_Update(ctx, (
const unsigned char *) secent->
moninfo,
341 localtime_r(&tim, &tms);
342 strftime(buf,
sizeof (buf),
"%s", &tms);
343 HMAC_Update(ctx, (
const unsigned char *) buf,
346 HMAC_Final(ctx, mdbuf, &len);
359 if (h1 == h2)
return 0;
364 return strcmp(h1, h2);
377 char *r = (
char *) malloc(l + 1);
381 for (i = 0; i < l; i++) {
384 char savec = str[i + 3];
387 r[j] = strtol(str + i + 1, 0, 16);
391 }
else r[j] = str[i];
406 char *r = (
char *) malloc(l*3 + 1);
410 for (i = 0; i < l; i++) {
415 strcpy(r + j,
"%20");
419 strcpy(r + j,
"%5B");
423 strcpy(r + j,
"%5D");
427 strcpy(r + j,
"%3A");
431 strcpy(r + j,
"%2F");
435 strcpy(r + j,
"%0A");
439 strcpy(r + j,
"%0D");
457 char *r = (
char *) malloc(l*6 + 1);
461 for (i = 0; i < l; i++) {
466 strcpy(r + j,
""");
470 strcpy(r + j,
"&");
474 strcpy(r + j,
"<");
478 strcpy(r + j,
">");
482 strcpy(r + j,
"'");
void BIO_set_flags(BIO *bio, int flags)
int parseURL(char *url, char *host, int &port, char **path)
void Tobase64(const unsigned char *input, int length, char *out)
int compareHash(const char *h1, const char *h2)
char * unquote(char *str)
bool Fromhexdigest(const unsigned char *input, int length, unsigned char *out)
static int char_to_int(int c)
static void HMAC_CTX_free(HMAC_CTX *ctx)
static HMAC_CTX * HMAC_CTX_new()
void calcHashes(char *hash, const char *fn, kXR_int16 request, XrdSecEntity *secent, time_t tim, const char *key)
char * escapeXML(const char *str)
char * mystrchrnul(const char *s, int c)
char * quote(const char *str)
char * vorg
Entity's virtual organization(s)
char * name
Entity's name.
char * moninfo
Information for monitoring.
char * host
Entity's host name dnr dependent.