iOS开发 C语言使用笔记 PKCS12 openssl 改 p12密码

//===============================================
    if(11 == 111){
        PKCS12 *p12 = NULL;
        X509* usrCert = NULL;
        EVP_PKEY* pkey = NULL;
        STACK_OF(X509)* ca = NULL;
        BIO*bio = NULL;
        char pass[128] = "";
        //int i;
        char* p = NULL;
        char buf[1024] = {0};
        
        //           SSLeay_add_all_algorithms();
        //           ERR_load_crypto_strings();
        //           bio = BIO_new_file("Web/1.p12", "r");
        
        // Load root CA 读取证书文件 用密码
        NSString *rootCAFilepath = [[NSBundle mainBundle] pathForResource:@"Web/1" ofType:@"p12"];
        rootCAFilepath = [[NSBundle mainBundle] pathForResource:@"Web/noPwd" ofType:@"p12"];
        
        
        NSString *rootCAContents = [NSString stringWithContentsOfFile:rootCAFilepath encoding:NSUTF8StringEncoding error:nil];
        
        bio = BIO_new_file([rootCAFilepath UTF8String], "r");
        
        //           BIO *rootCABio = BIO_new(BIO_s_mem());
        //           BIO_puts(rootCABio, [rootCAContents cStringUsingEncoding:NSUTF8StringEncoding]);
        
        //P12
        //           p12 = d2i_PKCS12_bio(rootCABio, NULL); //得到p12结构
        p12 = d2i_PKCS12_bio(bio, NULL); //得到p12结构
        BIO_free_all(bio);
        PKCS12_parse(p12, pass, &pkey, &usrCert, &ca); //得到x509结构
        //PKCS12_free(p12);
        
        {
            
            int zt = PKCS12_newpass(p12, "", "");
            
            bio_pkcs12 = BIO_new(BIO_s_mem());
            bytes = i2d_PKCS12_bio(bio_pkcs12, p12);
            
            if (bytes <= 0) {
                NSLog(@"Error writing PKCS12 certificate.");
                error = -1;
            }
            
            len = BIO_get_mem_data(bio_pkcs12, &data);
            NSData *resultxxx = [NSData dataWithBytes:data length:len];
            
            NSLog(@"Error writing PKCS12 certificate.");
            
        }
        if (usrCert)
        {
            
            //              int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass);
            
            
            fprintf(stdout, "***User Certificate***\n");
            //PEM_write_X509_AUX(stdout, usrCert);
            fprintf(stdout, "Subject:");
            p = X509_NAME_oneline(X509_get_subject_name(usrCert), NULL, 0);
            fprintf(stdout, "%s\n", p);
            
            fprintf(stdout, "Issuer:");
            p = X509_NAME_oneline(X509_get_issuer_name(usrCert), NULL, 0);
            fprintf(stdout, "%s\n", p);
            
            fprintf(stdout, "Not Before:");
            //              UTCTIME_print(buf, X509_get_notBefore(usrCert));
            fprintf(stdout, "%s\n", buf);
            
            fprintf(stdout, "Not After:");
            //              UTCTIME_print(buf, X509_get_notAfter(usrCert));
            fprintf(stdout, "%s\n", buf);
            //X509_print_fp(stdout, usrCert);   //add by slz token by openssl
        }
        
        
    }
发布了272 篇原创文章 · 获赞 123 · 访问量 109万+

猜你喜欢

转载自blog.csdn.net/qq_21051503/article/details/104850703
今日推荐