70 #ifndef OPENSSL_NO_DES
73 #ifndef OPENSSL_NO_ENGINE
81 static int load_iv(
char **fromp,
unsigned char *to,
int num);
82 static int check_pem(
const char *
nm,
const char *name);
87 #ifdef OPENSSL_NO_FP_API
104 prompt=
"Enter PEM pass phrase:";
112 memset(buf,0,(
unsigned int)num);
118 fprintf(stderr,
"phrase is too short, needs to be at least %d chars\n",
MIN_LENGTH);
147 static const unsigned char map[17]=
"0123456789ABCDEF";
157 for (i=0; i<
len; i++)
159 buf[j+i*2] =map[(str[i]>>4)&0x0f];
160 buf[j+i*2+1]=map[(str[i] )&0x0f];
166 #ifndef OPENSSL_NO_FP_API
185 static int check_pem(
const char *
nm,
const char *name)
188 if (!strcmp(nm,name))
return 1;
230 #ifndef OPENSSL_NO_ENGINE
262 #ifndef OPENSSL_NO_CMS
277 char *nm=NULL,*header=NULL;
278 unsigned char *
data=NULL;
290 if(check_pem(nm, name))
break;
313 #ifndef OPENSSL_NO_FP_API
338 int dsize=0,i,j,ret=0;
339 unsigned char *
p,*
data=NULL;
340 const char *objstr=NULL;
355 if ((dsize=i2d(x,NULL)) < 0)
376 if (callback == NULL)
385 #ifdef CHARSET_EBCDIC
389 kstr=(
unsigned char *)buf;
447 if (cipher->
cipher == NULL)
return(1);
448 if (callback == NULL)
457 #ifdef CHARSET_EBCDIC
463 (
unsigned char *)buf,klen,1,key,NULL);
487 char **header_pp = &header;
490 if ((header == NULL) || (*header ==
'\0') || (*header ==
'\n'))
492 if (strncmp(header,
"Proc-Type: ",11) != 0)
495 if (*header !=
'4')
return(0); header++;
496 if (*header !=
',')
return(0); header++;
497 if (strncmp(header,
"ENCRYPTED",9) != 0)
499 for (; (*header !=
'\n') && (*header !=
'\0'); header++)
504 if (strncmp(header,
"DEK-Info: ",10) != 0)
512 #ifndef CHARSET_EBCDIC
513 if (!( ((c >=
'A') && (c <=
'Z')) || (c ==
'-') ||
514 ((c >=
'0') && (c <=
'9'))))
517 if (!( isupper(c) || (c ==
'-') ||
533 if (!load_iv(header_pp,&(cipher->
iv[0]),enc->
iv_len))
539 static int load_iv(
char **fromp,
unsigned char *to,
int num)
545 for (i=0; i<
num; i++) to[i]=0;
547 for (i=0; i<
num; i++)
549 if ((*from >=
'0') && (*from <=
'9'))
551 else if ((*from >=
'A') && (*from <=
'F'))
553 else if ((*from >=
'a') && (*from <=
'f'))
561 to[i/2]|=v<<(long)((!(i&1))*4);
568 #ifndef OPENSSL_NO_FP_API
591 unsigned char *buf = NULL;
598 if ( (
BIO_write(bp,
"-----BEGIN ",11) != 11) ||
623 if ((outl) && (
BIO_write(bp,(
char *)buf,outl) != outl))
630 if ((outl > 0) && (
BIO_write(bp,(
char *)buf,outl) != outl))
goto err;
634 if ( (
BIO_write(bp,
"-----END ",9) != 9) ||
648 #ifndef OPENSSL_NO_FP_API
671 int end=0,i,
k,bl=0,hl=0,nohead=0;
680 if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL))
700 while ((i >= 0) && (buf[i] <=
' ')) i--;
701 buf[++i]=
'\n'; buf[++i]=
'\0';
703 if (strncmp(buf,
"-----BEGIN ",11) == 0)
705 i=strlen(&(buf[11]));
707 if (strncmp(&(buf[11+i-6]),
"-----\n",6) != 0)
714 memcpy(nameB->
data,&(buf[11]),i-6);
715 nameB->
data[i-6]=
'\0';
722 headerB->
data[0]=
'\0';
728 while ((i >= 0) && (buf[i] <=
' ')) i--;
729 buf[++i]=
'\n'; buf[++i]=
'\0';
731 if (buf[0] ==
'\n')
break;
734 if (strncmp(buf,
"-----END ",9) == 0)
739 memcpy(&(headerB->
data[hl]),buf,i);
740 headerB->
data[hl+i]=
'\0';
755 while ((i >= 0) && (buf[i] <=
' ')) i--;
756 buf[++i]=
'\n'; buf[++i]=
'\0';
759 if (strncmp(buf,
"-----END ",9) == 0)
767 memcpy(&(dataB->
data[bl]),buf,i);
768 dataB->
data[bl+i]=
'\0';
776 while ((i >= 0) && (buf[i] <=
' ')) i--;
777 buf[++i]=
'\n'; buf[++i]=
'\0';
790 i=strlen(nameB->
data);
791 if ( (strncmp(buf,
"-----END ",9) != 0) ||
792 (strncmp(nameB->
data,&(buf[9]),i) != 0) ||
793 (strncmp(&(buf[9+i]),
"-----\n",6) != 0))
801 (
unsigned char *)dataB->
data,&bl,
802 (
unsigned char *)dataB->
data,bl);
816 if (bl == 0)
goto err;
818 *header=headerB->
data;
819 *data=(
unsigned char *)dataB->
data;
839 int pem_len = strlen(pem_str);
840 int suffix_len = strlen(suffix);
842 if (suffix_len + 1 >= pem_len)
844 p = pem_str + pem_len - suffix_len;
845 if (strcmp(p, suffix))