~ubuntu-branches/ubuntu/maverick/openssl/maverick

« back to all changes in this revision

Viewing changes to crypto/objects/obj_dat.c

  • Committer: Bazaar Package Importer
  • Author(s): Kurt Roeckx
  • Date: 2006-05-15 16:00:58 UTC
  • mto: (11.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20060515160058-pg6lnbkkpkwpdj2e
Tags: upstream-0.9.8b
ImportĀ upstreamĀ versionĀ 0.9.8b

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
 
59
59
#include <stdio.h>
60
60
#include <ctype.h>
 
61
#include <limits.h>
61
62
#include "cryptlib.h"
62
63
#include <openssl/lhash.h>
63
64
#include <openssl/asn1.h>
413
414
        /* Work out size of content octets */
414
415
        i=a2d_ASN1_OBJECT(NULL,0,s,-1);
415
416
        if (i <= 0) {
416
 
                /* Clear the error */
417
 
                ERR_clear_error();
 
417
                /* Don't clear the error */
 
418
                /*ERR_clear_error();*/
418
419
                return NULL;
419
420
        }
420
421
        /* Work out total size */
436
437
 
437
438
int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
438
439
{
439
 
        int i,idx=0,n=0,len,nid;
 
440
        int i,n=0,len,nid, first, use_bn;
 
441
        BIGNUM *bl;
440
442
        unsigned long l;
441
443
        unsigned char *p;
442
 
        const char *s;
443
444
        char tbuf[DECIMAL_SIZE(i)+DECIMAL_SIZE(l)+2];
444
445
 
445
 
        if (buf_len <= 0) return(0);
446
 
 
447
446
        if ((a == NULL) || (a->data == NULL)) {
448
447
                buf[0]='\0';
449
448
                return(0);
450
449
        }
451
450
 
452
 
        if (no_name || (nid=OBJ_obj2nid(a)) == NID_undef) {
453
 
                len=a->length;
454
 
                p=a->data;
455
 
 
456
 
                idx=0;
457
 
                l=0;
458
 
                while (idx < a->length) {
459
 
                        l|=(p[idx]&0x7f);
460
 
                        if (!(p[idx] & 0x80)) break;
461
 
                        l<<=7L;
462
 
                        idx++;
463
 
                }
464
 
                idx++;
465
 
                i=(int)(l/40);
466
 
                if (i > 2) i=2;
467
 
                l-=(long)(i*40);
468
 
 
469
 
                BIO_snprintf(tbuf,sizeof tbuf,"%d.%lu",i,l);
470
 
                i=strlen(tbuf);
471
 
                BUF_strlcpy(buf,tbuf,buf_len);
472
 
                buf_len-=i;
473
 
                buf+=i;
474
 
                n+=i;
475
 
 
476
 
                l=0;
477
 
                for (; idx<len; idx++) {
478
 
                        l|=p[idx]&0x7f;
479
 
                        if (!(p[idx] & 0x80)) {
480
 
                                BIO_snprintf(tbuf,sizeof tbuf,".%lu",l);
481
 
                                i=strlen(tbuf);
482
 
                                if (buf_len > 0)
483
 
                                        BUF_strlcpy(buf,tbuf,buf_len);
484
 
                                buf_len-=i;
485
 
                                buf+=i;
486
 
                                n+=i;
487
 
                                l=0;
488
 
                        }
489
 
                        l<<=7L;
490
 
                }
491
 
        } else {
 
451
 
 
452
        if (!no_name && (nid=OBJ_obj2nid(a)) != NID_undef)
 
453
                {
 
454
                const char *s;
492
455
                s=OBJ_nid2ln(nid);
493
456
                if (s == NULL)
494
457
                        s=OBJ_nid2sn(nid);
495
 
                BUF_strlcpy(buf,s,buf_len);
 
458
                if (buf)
 
459
                        BUF_strlcpy(buf,s,buf_len);
496
460
                n=strlen(s);
497
 
        }
498
 
        return(n);
 
461
                return n;
 
462
                }
 
463
 
 
464
 
 
465
        len=a->length;
 
466
        p=a->data;
 
467
 
 
468
        first = 1;
 
469
        bl = NULL;
 
470
 
 
471
        while (len > 0)
 
472
                {
 
473
                l=0;
 
474
                use_bn = 0;
 
475
                for (;;)
 
476
                        {
 
477
                        unsigned char c = *p++;
 
478
                        len--;
 
479
                        if ((len == 0) && (c & 0x80))
 
480
                                goto err;
 
481
                        if (use_bn)
 
482
                                {
 
483
                                if (!BN_add_word(bl, c & 0x7f))
 
484
                                        goto err;
 
485
                                }
 
486
                        else
 
487
                                l |= c  & 0x7f;
 
488
                        if (!(c & 0x80))
 
489
                                break;
 
490
                        if (!use_bn && (l > (ULONG_MAX >> 7L)))
 
491
                                {
 
492
                                if (!bl && !(bl = BN_new()))
 
493
                                        goto err;
 
494
                                if (!BN_set_word(bl, l))
 
495
                                        goto err;
 
496
                                use_bn = 1;
 
497
                                }
 
498
                        if (use_bn)
 
499
                                {
 
500
                                if (!BN_lshift(bl, bl, 7))
 
501
                                        goto err;
 
502
                                }
 
503
                        else
 
504
                                l<<=7L;
 
505
                        }
 
506
 
 
507
                if (first)
 
508
                        {
 
509
                        first = 0;
 
510
                        if (l >= 80)
 
511
                                {
 
512
                                i = 2;
 
513
                                if (use_bn)
 
514
                                        {
 
515
                                        if (!BN_sub_word(bl, 80))
 
516
                                                goto err;
 
517
                                        }
 
518
                                else
 
519
                                        l -= 80;
 
520
                                }
 
521
                        else
 
522
                                {
 
523
                                i=(int)(l/40);
 
524
                                l-=(long)(i*40);
 
525
                                }
 
526
                        if (buf && (buf_len > 0))
 
527
                                {
 
528
                                *buf++ = i + '0';
 
529
                                buf_len--;
 
530
                                }
 
531
                        n++;
 
532
                        }
 
533
 
 
534
                if (use_bn)
 
535
                        {
 
536
                        char *bndec;
 
537
                        bndec = BN_bn2dec(bl);
 
538
                        if (!bndec)
 
539
                                goto err;
 
540
                        i = strlen(bndec);
 
541
                        if (buf)
 
542
                                {
 
543
                                if (buf_len > 0)
 
544
                                        {
 
545
                                        *buf++ = '.';
 
546
                                        buf_len--;
 
547
                                        }
 
548
                                BUF_strlcpy(buf,bndec,buf_len);
 
549
                                if (i > buf_len)
 
550
                                        {
 
551
                                        buf += buf_len;
 
552
                                        buf_len = 0;
 
553
                                        }
 
554
                                else
 
555
                                        {
 
556
                                        buf+=i;
 
557
                                        buf_len-=i;
 
558
                                        }
 
559
                                }
 
560
                        n++;
 
561
                        n += i;
 
562
                        OPENSSL_free(bndec);
 
563
                        }
 
564
                else
 
565
                        {
 
566
                        BIO_snprintf(tbuf,sizeof tbuf,".%lu",l);
 
567
                        i=strlen(tbuf);
 
568
                        if (buf && (buf_len > 0))
 
569
                                {
 
570
                                BUF_strlcpy(buf,tbuf,buf_len);
 
571
                                if (i > buf_len)
 
572
                                        {
 
573
                                        buf += buf_len;
 
574
                                        buf_len = 0;
 
575
                                        }
 
576
                                else
 
577
                                        {
 
578
                                        buf+=i;
 
579
                                        buf_len-=i;
 
580
                                        }
 
581
                                }
 
582
                        n+=i;
 
583
                        l=0;
 
584
                        }
 
585
                }
 
586
 
 
587
        if (bl)
 
588
                BN_free(bl);
 
589
        return n;
 
590
 
 
591
        err:
 
592
        if (bl)
 
593
                BN_free(bl);
 
594
        return -1;
499
595
}
500
596
 
501
597
int OBJ_txt2nid(const char *s)