iOS如何通过编程方式创建证书请求

3

我希望您能帮助我在iOS和OSX上以编程方式创建证书请求(我将发送到服务器),而不使用openSSL。我已经成功创建了RSA密钥对,但是在执行其他操作时失败了。我有一段完全符合我的需求的代码,但它是用Java编写的,想知道是否有人可以帮我将其翻译成Objective C。

以下是Java代码:

        test.generateKeys(); // generate RSA key pair

        PrivateKey privateKey = test.keys.getPrivate();
        PublicKey publicKey = test.keys.getPublic();

        SecureRandom sr = new SecureRandom();
        String token = "123456"; // dummy token
        String uuid = "4670ff33-d9f7-4026-957d-25c00e4dec54"; // dummy uuid

        // with Bouncy Castle
        ContentSigner signGen = new JcaContentSignerBuilder("SHA1withRSA").setSecureRandom(sr).build(privateKey);
        X500Principal subject = new X500Principal("O=" + token + ", CN=" + uuid);
        PKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(subject, publicKey);
        PKCS10CertificationRequest request = builder.build(signGen);

        String bc = Hex.encodeHexString(request.getEncoded());
        System.out.println(PEMtoString(request));

我在密码学方面不是很擅长,而苹果正在开发的加密层文档非常缺乏,所以我有点迷失了。我找到了许多类似的示例,但总会有一些遗漏。提前感谢。


强烈建议您使用openssl。如果不这样做,请准备好数周的努力。请参见https://dev59.com/cm7Xa4cB1Zd3GeqPmir5 - Peter Cetinski
我会使用OpenSSL,但据我了解,苹果正在劝阻开发者在新的加密层上使用OpenSSL,因此我不想编写未来可能被拒绝的程序。 - AntonijoDev
1个回答

5

如果有人遇到同样的问题,这里提供了使用苹果通用加密层(不使用OpenSSL)的解决方案。

https://github.com/ateska/ios-csr

只需简单引用,无需数周编码。

   SCCSR *sccsr = [[SCCSR alloc] init];
    sccsr.commonName = @"some name";
    sccsr.organizationName = @"some organisation";
//    // aditional data you can set
//    sccsr.countryName = @"";
//    sccsr.organizationalUnitName = @"";
//    sccsr.subjectDER = nil;
//    //
//    
    NSData *certificateRequest = [sccsr build:pPublicKey privateKey:privateKey];

    NSString *str = [certificateRequest base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

    NSString *strCertificateRequest = @"-----BEGIN CERTIFICATE REQUEST-----\n";
    strCertificateRequest = [strCertificateRequest stringByAppendingString:str];
    strCertificateRequest = [strCertificateRequest stringByAppendingString:@"\n-----END CERTIFICATE REQUEST-----\n"];

SCCSR.h -> 从提供的链接下载


如何为服务器创建证书以确保有效性? - ΩlostA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接