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);
77int 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);
121void 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') {
169bool 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);
372 char *r = (
char *) malloc(l + 1);
376 for (i = 0; i < l; i++) {
379 char savec = str[i + 3];
382 r[j] = strtol(str + i + 1, 0, 16);
386 }
else r[j] = str[i];
401 char *r = (
char *) malloc(l*3 + 1);
405 for (i = 0; i < l; i++) {
410 strcpy(r + j,
"%20");
414 strcpy(r + j,
"%5B");
418 strcpy(r + j,
"%5D");
422 strcpy(r + j,
"%3A");
430 strcpy(r + j,
"%23");
434 strcpy(r + j,
"%0A");
438 strcpy(r + j,
"%0D");
442 strcpy(r + j,
"%3D");
460 char *r = (
char *) malloc(l*6 + 1);
464 for (i = 0; i < l; i++) {
469 strcpy(r + j,
""");
473 strcpy(r + j,
"&");
477 strcpy(r + j,
"<");
481 strcpy(r + j,
">");
485 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)
static HMAC_CTX * HMAC_CTX_new()
bool Fromhexdigest(const unsigned char *input, int length, unsigned char *out)
char * unquote(char *str)
char * quote(const char *str)
char * escapeXML(const char *str)
static int char_to_int(int c)
char * mystrchrnul(const char *s, int c)
static void HMAC_CTX_free(HMAC_CTX *ctx)
void calcHashes(char *hash, const char *fn, kXR_int16 request, XrdSecEntity *secent, time_t tim, const char *key)
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.