📄 asn1.c
字号:
len += total; free( tmp ); memcpy( buf+len, algorithm_id, algorithm_id_len ); len += algorithm_id_len; rc = ber_encode_OCTET_STRING( FALSE, &tmp, &total, priv_key, priv_key_len ); if (rc != CKR_OK){ st_err_log(77, __FILE__, __LINE__); goto error; } memcpy( buf+len, tmp, total ); len += total; free( tmp ); memcpy( buf+len, attrib, sizeof(attrib)); len += sizeof(attrib); rc = ber_encode_SEQUENCE( FALSE, data, data_len, buf, len ); if (rc != CKR_OK) st_err_log(78, __FILE__, __LINE__);error: free( buf ); return rc;}////CK_RVber_decode_PrivateKeyInfo( CK_BYTE * data, CK_ULONG data_len, CK_BYTE ** algorithm, CK_ULONG * alg_len, CK_BYTE ** priv_key ){ CK_BYTE *buf = NULL; CK_BYTE *alg = NULL; CK_BYTE *ver = NULL; CK_ULONG buf_len, offset, len, field_len; CK_RV rc; if (!data || (data_len == 0)){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } rc = ber_decode_SEQUENCE( data, &buf, &buf_len, &field_len ); if (rc != CKR_OK){ st_err_log(81, __FILE__, __LINE__); return rc; } // version -- we just ignore this // offset = 0; rc = ber_decode_INTEGER( buf+offset, &ver, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); return rc; } offset += field_len; // 'buf' is now pointing to the PrivateKeyAlgorithmIdentifier // rc = ber_decode_SEQUENCE( buf+offset, &alg, &len, &field_len ); if (rc != CKR_OK){ st_err_log(81, __FILE__, __LINE__); return rc; } *algorithm = alg; *alg_len = len; rc = ber_decode_OCTET_STRING( alg + len, priv_key, &buf_len, &field_len ); if (rc != CKR_OK) st_err_log(81, __FILE__, __LINE__); return rc;}// RSAPrivateKey ::= SEQUENCE {// version Version -- always '0' for now// modulus INTEGER// publicExponent INTEGER// privateExponent INTEGER// prime1 INTEGER// prime2 INTEGER// exponent1 INTEGER// exponent2 INTEGER// coefficient INTEGER// }//CK_RVber_encode_RSAPrivateKey( CK_BBOOL length_only, CK_BYTE ** data, CK_ULONG * data_len, CK_ATTRIBUTE * modulus, CK_ATTRIBUTE * publ_exp, CK_ATTRIBUTE * priv_exp, CK_ATTRIBUTE * prime1, CK_ATTRIBUTE * prime2, CK_ATTRIBUTE * exponent1, CK_ATTRIBUTE * exponent2, CK_ATTRIBUTE * coeff ){ CK_BYTE *buf = NULL; CK_BYTE *buf2 = NULL; CK_ULONG len, offset; CK_BYTE version[] = { 0 }; CK_RV rc; offset = 0; rc = 0; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, sizeof(version) ); offset += len; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, modulus->ulValueLen ); offset += len; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, publ_exp->ulValueLen ); offset += len; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, priv_exp->ulValueLen ); offset += len; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, prime1->ulValueLen ); offset += len; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, prime2->ulValueLen ); offset += len; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, exponent1->ulValueLen ); offset += len; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, exponent2->ulValueLen ); offset += len; rc |= ber_encode_INTEGER( TRUE, NULL, &len, NULL, coeff->ulValueLen ); offset += len; if (rc != CKR_OK){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } if (length_only == TRUE) { rc = ber_encode_SEQUENCE( TRUE, NULL, &len, NULL, offset ); if (rc != CKR_OK){ st_err_log(78, __FILE__, __LINE__); return rc; } rc = ber_encode_PrivateKeyInfo( TRUE, NULL, data_len, NULL, ber_AlgIdRSAEncryptionLen, NULL, len ); if (rc != CKR_OK){ st_err_log(82, __FILE__, __LINE__); return rc; } return rc; } buf = (CK_BYTE *)malloc(offset); if (!buf){ st_err_log(1, __FILE__, __LINE__); return CKR_HOST_MEMORY; } offset = 0; rc = 0; rc = ber_encode_INTEGER( FALSE, &buf2, &len, version, sizeof(version) ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, buf2, len ); offset += len; free( buf2 ); rc = ber_encode_INTEGER( FALSE, &buf2, &len, (CK_BYTE *)modulus + sizeof(CK_ATTRIBUTE), modulus->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, buf2, len ); offset += len; free( buf2 ); rc = ber_encode_INTEGER( FALSE, &buf2, &len, (CK_BYTE *)publ_exp + sizeof(CK_ATTRIBUTE), publ_exp->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, buf2, len ); offset += len; free( buf2 ); rc = ber_encode_INTEGER( FALSE, &buf2, &len, (CK_BYTE *)priv_exp + sizeof(CK_ATTRIBUTE), priv_exp->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, buf2, len ); offset += len; free( buf2 ); rc = ber_encode_INTEGER( FALSE, &buf2, &len, (CK_BYTE *)prime1 + sizeof(CK_ATTRIBUTE), prime1->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, buf2, len ); offset += len; free( buf2 ); rc = ber_encode_INTEGER( FALSE, &buf2, &len, (CK_BYTE *)prime2 + sizeof(CK_ATTRIBUTE), prime2->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, buf2, len ); offset += len; free( buf2 ); rc = ber_encode_INTEGER( FALSE, &buf2, &len, (CK_BYTE *)exponent1 + sizeof(CK_ATTRIBUTE), exponent1->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, buf2, len ); offset += len; free( buf2 ); rc = ber_encode_INTEGER( FALSE, &buf2, &len, (CK_BYTE *)exponent2 + sizeof(CK_ATTRIBUTE), exponent2->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, buf2, len ); offset += len; free( buf2 ); rc = ber_encode_INTEGER( FALSE, &buf2, &len, (CK_BYTE *)coeff + sizeof(CK_ATTRIBUTE), coeff->ulValueLen ); if (rc != CKR_OK){ st_err_log(76, __FILE__, __LINE__); goto error; } memcpy( buf+offset, buf2, len ); offset += len; free( buf2 ); rc = ber_encode_SEQUENCE( FALSE, &buf2, &len, buf, offset ); if (rc != CKR_OK){ st_err_log(78, __FILE__, __LINE__); goto error; } rc = ber_encode_PrivateKeyInfo( FALSE, data, data_len, ber_AlgIdRSAEncryption, ber_AlgIdRSAEncryptionLen, buf2, len ); if (rc != CKR_OK) { st_err_log(82, __FILE__, __LINE__); }error: if (buf2) free( buf2 ); if (buf) free( buf ); return rc;}////CK_RVber_decode_RSAPrivateKey( CK_BYTE * data, CK_ULONG data_len, CK_ATTRIBUTE ** modulus, CK_ATTRIBUTE ** publ_exp, CK_ATTRIBUTE ** priv_exp, CK_ATTRIBUTE ** prime1, CK_ATTRIBUTE ** prime2, CK_ATTRIBUTE ** exponent1, CK_ATTRIBUTE ** exponent2, CK_ATTRIBUTE ** coeff ){ CK_ATTRIBUTE *n_attr = NULL; CK_ATTRIBUTE *e_attr = NULL; CK_ATTRIBUTE *d_attr = NULL; CK_ATTRIBUTE *p_attr = NULL; CK_ATTRIBUTE *q_attr = NULL; CK_ATTRIBUTE *e1_attr = NULL; CK_ATTRIBUTE *e2_attr = NULL; CK_ATTRIBUTE *coeff_attr = NULL; CK_BYTE *alg = NULL; CK_BYTE *rsa_priv_key = NULL; CK_BYTE *buf = NULL; CK_BYTE *tmp = NULL; CK_ULONG offset, buf_len, field_len, len; CK_RV rc; rc = ber_decode_PrivateKeyInfo( data, data_len, &alg, &len, &rsa_priv_key ); if (rc != CKR_OK){ st_err_log(83, __FILE__, __LINE__); return rc; } // make sure we're dealing with an RSA key // if (memcmp(alg, ber_rsaEncryption, ber_rsaEncryptionLen) != 0){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; // probably ought to use a different error } rc = ber_decode_SEQUENCE( rsa_priv_key, &buf, &buf_len, &field_len ); if (rc != CKR_OK) return rc; // parse the RSAPrivateKey // offset = 0; // Version // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // modulus // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // public exponent // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // private exponent // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // prime #1 // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // prime #2 // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // exponent #1 // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // exponent #2 // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // coefficient // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; if (offset > buf_len){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } // // it looks okay. build the attributes // offset = 0; // skip the version // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } offset += field_len; // modulus // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_MODULUS, tmp, len, &n_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } // public exponent // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_PUBLIC_EXPONENT, tmp, len, &e_attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto cleanup; } offset += field_len; } // private exponent // rc = ber_decode_INTEGER( buf+offset, &tmp, &len, &field_len ); if (rc != CKR_OK){ st_err_log(79, __FILE__, __LINE__); goto cleanup; } else { rc = build_attribute( CKA_PRIVATE_EXPONENT, tmp, len, &d_attr );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -